有没有办法将 CommandFlags 添加到辅助函数中?

时间:2021-06-14 15:59:18

标签: c# .net autocad autocad-plugin

目前正在尝试构建一个命令,该命令以递归方式遍历 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;
        }

1 个答案:

答案 0 :(得分:0)

我针对您在 Autodesk forum 中的帖子发布了一个示例。这是另一种更实用的风格。

    static IEnumerable<BlockTableRecord> GetNestedBlockTableRecords(BlockTableRecord source, Transaction tr)
    {
        foreach (var btr in source.Cast<ObjectId>()
            .Where(id => id.ObjectClass.DxfName == "INSERT")
            .Select(id => (BlockReference)tr.GetObject(id, OpenMode.ForRead))
            .Select(br => (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead)))
        {
            yield return btr;
            foreach (var item in GetNestedBlockTableRecords(btr, tr))
            {
                yield return item;
            }
        }
    }