Reputation: 487
It's very specific to AutoCAD
SDK, developing an add-in for AutoCAD. I have loaded some DWG files in to a drawing and created some objects of the classes loaded. When these objects are created I add an Xdata containing some information on BlockTableRecord. Now, I need to remove BlockTableRecords for the objects that have been deleted from the drawing. I am getting a call back from AutoCAD when document is saved. In this call back, I am trying to traverse through all Block Table Records to find the records with our information. And the records that contain information that they were created using my add-in, I am trying to see the number of objects created from this record item. If there exists none, I will delete the block table record or at least update our footprint.
Here I am unable to get the count of objects created from one specific table record. Here's what I have done so far.
private static void CommandExecutionCallBack(object sender, CommandEventArgs e)
{
string commando = e.GlobalCommandName.ToLower();
if (commando.Contains("save"))
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var documentManager = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Editor ed = doc.Editor;
Database db = doc.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction myT = tm.StartTransaction())
{
BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
SymbolTableEnumerator enumerator = bt.GetEnumerator();
while (enumerator.MoveNext())
{
BlockTableRecord btr = tm.GetObject(enumerator.Current, OpenMode.ForRead, false) as BlockTableRecord;
ResultBuffer xdata = btr.GetXDataForApplication(REGAPP_NAME);
if (null != xdata)
{
//Here I am trying to fetch the objects that exist against the selected btr.
AcadApplication acApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as AcadApplication;
AcadDocument thisDrawing = acApp.ActiveDocument;
thisDrawing.SendCommand("(command MAPSELECTCLASSIFIED " + btr.Name + ")" + Environment.NewLine);
AcadSelectionSet selectionSet = thisDrawing.PickfirstSelectionSet;
//I am always getting selectionSet.Count as zero.
MessageBox.Show(btr.Name + " -> SelectionSet Count: " + selectionSet.Count.ToString());
}
else
{
//Will ignore this condition. The current btr is not created by our add-in
}
}
}
}
}
Any help in this regard would be appreciated.
I have taken some help from AutoCAD forum, https://forums.autodesk.com/t5/net/how-to-find-and-export-object-classess-from-drawing/m-p/5814774#M45948
Regards, Umar
Upvotes: 0
Views: 532
Reputation: 2493
Delating unreferenced BlockTableRecord (or any other SymbolTableRecord) is commonly called: Purge. The Database class provide a Purge() method which can be used to get the "purgeable" objects.
private static ObjectIdCollection GetPurgeableBlocks(Database db)
{
var ids = new ObjectIdCollection();
using (var tr = new OpenCloseTransaction())
{
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId id in bt)
{
var btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
if (btr.GetXDataForApplication(REGAPP_NAME) != null)
{
ids.Add(id);
}
}
tr.Commit();
}
db.Purge(ids);
return ids;
}
Upvotes: 0