.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JLS3

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
jlsantiago
283 Views, 2 Replies

JLS3

How to get the ObjectId of an existing block in c#

2 REPLIES 2
Message 2 of 3
Hallex
in reply to: jlsantiago

Do you want to get ObjectId of BlockReferece (graphical instance of Block Definition)

 or of Block Definition itself?

There is a two different objects btw

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 3
Hallex
in reply to: jlsantiago

Hetre is quick code hope this will  make a sence

       [CommandMethod("testblock")]
        public static void TestBlocks()
        {
            string blockName = "dyn";
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
             Transaction tr = db.TransactionManager.StartTransaction();

             using (tr)
             {
                 try
                 {
                     BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                     if (!bt.Has(blockName))
                     {
                         ed.WriteMessage("\nBlock \"{0}\" does not exist", blockName);
                         return;
                     }
                     
                     ObjectId blkID = bt[blockName];
                     ed.WriteMessage("\nBlock \"{0}\" does exist, ObjectId is: {1}", blockName, blkID);
                     
                     BlockTableRecord btr = tr.GetObject(bt[blockName], OpenMode.ForRead) as BlockTableRecord;


                     ObjectIdCollection blkRefIds = btr.GetBlockReferenceIds(true, false);

                     if (blkRefIds.Count == 0)
                     {
                         ed.WriteMessage("\nNo block instances \"{0}\" found", blockName);
                         return;
                     }
                     foreach (ObjectId brefId in blkRefIds)
                     {
                         BlockReference blkRef = (BlockReference)tr.GetObject(brefId, OpenMode.ForRead, false) as BlockReference;      
                         ed.WriteMessage("\nBlock Reference \"{0}\" with ObjectID {1} found  at position: {2}", blkRef.Name, brefId,blkRef.Position);

                     }
                     tr.Commit();//optional in this context
                 }
                 catch (Autodesk.AutoCAD.Runtime.Exception ex)
                 {
                     ed.WriteMessage(("AutoCAD Exception: " + ex.Message));

                 }
                 catch (System.Exception ex)
                 {
                     ed.WriteMessage(("Sytem Exception: " + ex.Message));

                 }
                 finally
                 {
                     // optional
                 }
             }
			}

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost