gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:02:52 34 4
gpt4 key购买 nike

我正在编写一个 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) 用于我之前使用的选择过滤策略,因为文档实际上并未打开。那么,如何在不实际打开 DWG 文件的情况下获取名为“testLayer”的每个图层上的所有文本实体呢?

最佳答案

在“侧数据库”中,要模仿 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;
}
}
}
}
}
}
}

关于c# - 如何在不打开文件 (C#.NET/AutoCAD) 的情况下获取 .DWG 文件中特定图层上的所有实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41062809/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com