Jump to content

Insert annotated block into AutoCAD drawing using .NET


Kevin Malone

Recommended Posts

Hello,

 

I have basic piece of code to insert block into drawing. It's just a method that accepts block name, insertion point, rotation, layer, list of attribute tag-value pairs (if attributes exist). And it works fine. However, along the way I decided to modify my blocks to be annotative. 

 

Now I'm getting some strange behavior. After insertion, only attribute value is visible. If I explode a block, block elements appear as it should and in proper size. If I insert block without attribute nothing happens, so my first thought is that there is something wrong with annotation scale (annotation mismatch between ModelSpace and blocks annotation scale)  and maybe block units, so it's too small to be visible, but I cant select anything in place where block should be placed. When I change annotation scale to any other, and then switch back to original one block appears.

I tried manually, and it works fine, so it shouldn't be something with blocks definition. 

ANNOALLVISIBLE is set to 1. Also tried REGEN after insert. 

 

I simplified my code and created simple dwg (attached) with one block just to test it, and I get same behavior. 

 

This is my simplified code:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(InsertAnnotatedBlockTest.ACadCommand))]

namespace InsertAnnotatedBlockTest
{
    public class ACadCommand
    {
        [CommandMethod("InsertAnnBlock")]
        public static void RunACadCommand()
        {
            PromptPointOptions ppo = new PromptPointOptions("\nPick insertion point: ");
            PromptPointResult ppr = Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(ppo);

            if (ppr.Status == PromptStatus.OK)
            {
                Point3d insertion_point = ppr.Value;

                InsertAttributeBlock("TestAnnotBlock", insertion_point);
            }   
        }

        public static void InsertAttributeBlock(string block_name, Point3d insertion_point)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                BlockTable acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; ;
                BlockTableRecord acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                ObjectId blkRecId = acBlkTbl[block_name];
                using (BlockReference acBlkRef = new BlockReference(insertion_point, blkRecId))
                {
                    BlockTableRecord blkTblRec = acBlkTbl[block_name].GetObject(OpenMode.ForRead) as BlockTableRecord;

                    //Found this piece of code on 
                    //https://through-the-interface.typepad.com/through_the_interface/2007/05/using_a_jig_fro_1.html
                    //and also in VB form at
                    //https://www.theswamp.org/index.php?topic=31859.0
                  	if (blkTblRec.Annotative == AnnotativeStates.True)
                    {
                        ObjectContextManager ocm = db.ObjectContextManager;
                        ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
                        ObjectContexts.AddContext(blkTblRec, occ.CurrentContext);
                    }

                    acBlkTblRec.AppendEntity(acBlkRef);
                        tr.AddNewlyCreatedDBObject(acBlkRef, true);
                }
                tr.Commit();
            }
        }

    }
}

 

BlockAnnotate.dwg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...