- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everyone,
I'm a french dude so sorry if my English isn't well enough.
I'm new with AutoCAD (2012) and I'm trying to create a commandmethod which allow to select and see what's in the block selected.
I already looked some forums but they used the database instead of the promptselection.
- http://forums.autodesk.com/t5/NET/simple-example-of-iterating-a-collection-eg-blocks/td-p/2553278
This is my code:
[CommandMethod("SelectBlock")]
public void SelectBlock()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
Transaction tr = db.TransactionManager.StartTransaction();
try
{
PromptSelectionOptions opts = new PromptSelectionOptions();
opts.MessageForAdding = "Select block references:";
PromptSelectionResult res = ed.GetSelection(opts);
if (res.Status != PromptStatus.OK){return;}
SelectionSet selSet = res.Value;
ObjectId[] ids = selSet.GetObjectIds();
foreach (ObjectId blkId in ids)
{
if (blkId != null)
{
// Open the Block table record Model space for read
BlockTableRecord acBlkTblRec = tr.GetObject(blkId, OpenMode.ForRead) as BlockTableRecord;
//acBlkTblRec is still null
// Step through the Block table record
if (acBlkTblRec != null)
{
foreach (ObjectId asObjId in acBlkTblRec)
{
Entity e = (Entity)tr.GetObject(asObjId, OpenMode.ForRead);
ed.WriteMessage(e.ToString());
}
}
}
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(("Exception: " + ex.Message));
}
finally
{
tr.Dispose();
}
}
}
}Everything is fine until the bold line. In fact, I find the block selected (it contains 4 blocks) and go inside the first foreach, but when I instantiate my BlockTableRecord, acBlkTblRec stay "null" and the second foreach is ignored. Do you have any idea about what happended ?
I use AutoCad 2012 and Visual Studio 2010.
Thank you in advance for your precious help ![]()
Max
Solved! Go to Solution.