【发布时间】:2021-06-16 01:49:47
【问题描述】:
目前正在尝试构建一个递归遍历 BlockTable 的命令,为每个 BlockTableRecord 组件创建新的绘图。为此,我需要锁定每个新文档以正确编辑它,我正在尝试通过 Document.LockDocument() 函数来执行此操作。但是,由于此命令使用递归辅助函数,因此它会引发“DocumentLock 是一种在给定上下文中无效的类型”错误,我认为这是由缺少“CommandFlags.Session”标志的函数引起的。有没有办法将此标志附加到辅助函数?我已经在下面包含了我的函数代码,谢谢。
public List<string> BTRRecursor(BlockTableRecord bTR, string filepath)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
var filepaths = new List<string>();
foreach (ObjectId internalObj in bTR)
{
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTableRecord internalBTR = trans.GetObject(internalObj, OpenMode.ForRead) as BlockTableRecord;
if (internalBTR.Name.Contains("_MR_"))
{
string strTemplatePath = "acad.dwt";
Document newDoc = Application.DocumentManager.Add(strTemplatePath);
using (DocumentLock lock = newDoc.LockDocument()) {
BlockTable nDBT = trans.GetObject(newDoc.Database.BlockTableId, OpenMode.ForWrite) as BlockTable;
nDBT.Add(internalBTR);
//toDWG
//Create new file
//Open its BTR for write
//Set its BTR to internalBTR
filepaths.Append("DWG Filepath");
}
}
else if (internalBTR.Name.Contains("_NMR_"))
{
BTRRecursor(internalBTR, Directory.CreateDirectory(filepath + @"\" + internalBTR.Name).FullName);
}
}
finally
{
trans.Dispose();
}
}
return filepaths;
}
【问题讨论】:
-
你不需要任何递归,一个数据库的块表包含了这个数据库的所有块表记录,一个块表记录不能包含任何其他块表记录,它只包含实体。因此,在您的代码中,internalBTR 将始终为空。
-
那么有没有办法访问给定块中所有嵌套块的枚举?谢谢你的回复!
标签: c# .net autocad autocad-plugin