Community
How to get the ObjectId of an existing block in c#
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
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
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'~