首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不打开文件(C#.NET/AutoCAD)的情况下在.DWG文件中的特定层上获得所有实体?

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

Stack Overflow用户
提问于 2016-12-09 14:38:36
回答 1查看 3.9K关注 0票数 2

我正在编写一个C#.NET程序,它通过AutoCAD .NET API与AutoCAD进行交互。该程序循环遍历目录中的DWG文件,并检查"testLayer“层上的每个文本实体,以查看它是否与"testText”匹配。我通过打开每个文件并创建一个选择筛选器来获取"testLayer“层上的所有实体。

代码语言:javascript
运行
复制
        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);

读取文件而不实际打开它们,这样我就不能使用

代码语言:javascript
运行
复制
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor

以及我之前使用的选择筛选策略的ed.SelectAll(sf),因为文档实际上并不是打开的。那么,在不实际打开DWG文件的情况下,如何才能在每个名为"testLayer“的层上获取所有文本实体呢?

EN

Stack Overflow用户

回答已采纳

发布于 2016-12-09 17:00:07

在“侧数据库”中,要模仿SelectAll,必须遍历所有布局中的所有实体,并检查实体层。

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

代码语言:javascript
运行
复制
    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;
                            }
                        }
                    }
                }
            }
        }
    }
票数 5
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41062809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档