首页 > 解决方案 > 需要获取从 AutoCAD Civil3D 中的类创建的对象的计数

问题描述

它非常特定于AutoCADSDK,为 AutoCAD 开发插件。我已将一些 DWG 文件加载到绘图中并创建了加载的类的一些对象。创建这些对象后,我添加了一个 Xdata,其中包含有关 BlockTableRecord 的一些信息。现在,我需要为已从绘图中删除的对象删除 BlockTableRecords。保存文档时,我收到了来自 AutoCAD 的回电。在这个回调中,我试图遍历所有块表记录以找到包含我们信息的记录。并且包含使用我的加载项创建的信息的记录,我试图查看从此记录项创建的对象的数量。如果不存在,我将删除块表记录或至少更新我们的足迹。

在这里,我无法获取从一个特定表记录创建的对象的数量。这是我到目前为止所做的。

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
                }
            }
        }
    }
}

在这方面的任何帮助将不胜感激。

我从 AutoCAD 论坛获得了一些帮助,https://forums.autodesk.com/t5/net/how-to-find-and-export-object-classess-from-drawing/mp/5814774#M45948

问候, 奥马尔

标签: c#autocadautocad-plugin

解决方案


取消引用的 BlockTableRecord(或任何其他 SymbolTableRecord)通常称为:Purge。Database 类提供了一个Purge()方法,可用于获取“可清除”对象。

    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;
    }

推荐阅读