【问题标题】:IS there a way to add CommandFlags to a helper function?有没有办法将 CommandFlags 添加到辅助函数中?
【发布时间】: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


【解决方案1】:

我在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;
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2018-11-30
    • 2023-01-10
    • 2020-12-31
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多