如何在不打开文件(C#.NET / AutoCAD)的情况下获取.DWG文件中特定图层上的所有实体?

时间:2016-12-09 14:38:37

标签: c# .net autocad

我正在编写一个C#.NET程序,它通过AutoCAD .NET API与AutoCAD交互。程序循环遍历目录中的DWG文件,并检查" testLayer"上的每个文本实体。图层是否匹配" testText"。我通过打开每个文件并使用Selectionfilter来获取" testLayer"中的所有实体来实现这一点。层

        Application.DocumentManager.Open(curfile.FullName, false);
        ....
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;

        using (Transaction acTrans = doc.TransactionManager.StartTransaction())
        {
            ObjectIdCollection ents = new ObjectIdCollection();

            // Set up filter and filter on layer name
            TypedValue[] tvs = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, "testLayer")};
            SelectionFilter sf = new SelectionFilter(tvs);
            PromptSelectionResult psr = ed.SelectAll(sf);

            if (psr.Status == PromptStatus.OK)
            {
                // Get the object ids for all of the entities for the filtered layer
                ents = new ObjectIdCollection(psr.Value.GetObjectIds());

                foreach (ObjectId objid in ents)
                {
                    DBText dbText = acTrans.GetObject(objid, OpenMode.ForRead) as DBText;
                    if (dbText.TextString.Contains("testText")
                    {
                         return dbText.TextString;
                    }
                }
                return "";
            }
            else
            {
                return "";
            }
        }           
    }

但是现在我正在将我的程序转换为侧载基础数据库,因为程序打开和关闭每个.DWG文件需要很长时间。问题是现在我正在使用

db.ReadDwgFile(currentDWG, FileOpenMode.OpenForReadAndAllShare, true, string.Empty);

在没有实际打开文件的情况下读取文件,因此我无法使用

Editor ed = Application.DocumentManager.MdiActiveDocument.Editor

ed.SelectAll(sf)我之前使用过的选择过滤器策略,因为该文档实际上并未打开。那么我怎样才能得到名为" testLayer"的每一层上的所有文本实体。没有实际打开DWG文件?

1 个答案:

答案 0 :(得分:2)

在'side database'中,为了模仿SelectAll,你必须遍历所有布局中的所有实体并检查实体层。

编辑:在“侧数据库”中,要模仿SelectAll,您必须遍历所有布局中的所有实体,并检查实体类型和图层。

    private IEnumerable<ObjectId> GetTextEntitiesOnLayer(Database db, string layerName)
    {
        using (var tr = db.TransactionManager.StartOpenCloseTransaction())
        {
            var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
            foreach (ObjectId btrId in blockTable)
            {
                var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                var textClass = RXObject.GetClass(typeof(DBText));
                if (btr.IsLayout)
                {
                    foreach (ObjectId id in btr)
                    {
                        if (id.ObjectClass == textClass)
                        {
                            var text = (DBText)tr.GetObject(id, OpenMode.ForRead);
                            if (text.Layer.Equals(layerName, System.StringComparison.CurrentCultureIgnoreCase))
                            {
                                yield return id;
                            }
                        }
                    }
                }
            }
        }
    }