.NET

Reply
Contributor
jlsantiago
Posts: 14
Registered: ‎03-29-2011
Message 1 of 3 (185 Views)
Accepted Solution

JLS3

185 Views, 2 Replies
03-30-2011 07:23 AM

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

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'~

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 3 (134 Views)

Re: JLS3

04-01-2011 12:06 AM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 3 (127 Views)

Re: JLS3

04-01-2011 12:41 AM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.