3

我正在尝试编写一个方法,提示用户选择他们想要组合到一个块中的所有实体,然后将它们连接到一个块中并返回块引用。现在它看起来像这样。

        /// <summary>
        /// Returns all entities in an AutoCAD drawing in a list
        /// </summary>
        public static List<Entity> GetEntitiesInDrawing()
        {
            List<Entity> entitiesToReturn = new List<Entity>(); //Blocks that will be returned
            Transaction tr = _database.TransactionManager.StartTransaction();
            DocumentLock docLock = _activeDocument.LockDocument();

            using (tr)
            using (docLock)
            {
                BlockTableRecord blockTableRecord = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(_database), OpenMode.ForRead);
                foreach (ObjectId id in blockTableRecord)
                {
                    try
                    {
                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                        entitiesToReturn.Add(ent);
                    }
                    catch (InvalidCastException)
                    {
                        continue;
                    }
                }
            }
            return entitiesToReturn;
        }
        /// <summary>
        /// Prompts the user for a number of entities and then joins them into a block
        /// </summary>
        public static BlockReference JoinEntities()
        {
            BlockReference blkToReturn = null;
            List<Entity> entitiesToJoin = PromptUserForEntities();
            foreach (Entity ent in entitiesToJoin)
            {
                // ToDo: Join entities into blkToReturn
            }
            return blkToReturn;

        }

我的问题是我不知道如何或是否可以获取实体列表并将它们加入块引用中。

4

2 回答 2

4

Kean 在他的博客中介绍了这一点:使用 .NET 创建 AutoCAD 块

于 2015-05-25T21:51:47.287 回答
3

总之:

  1. 使用 Editor.Getselection 以便用户可以选择实体
  2. 在 BlockTable 上创建一个 blockTableRecord (BTR)(来自 Database.BlockTableId)
  3. 将所有实体附加到新创建的 BTR,在这里您可能需要创建新实体或移动所有权(参见 BlockTableRecord.AssumeOwnershipOf 方法)
  4. 创建一个指向 BTR 的新块引用
  5. 打开模型空间(或图纸空间)并将块引用附加到它
  6. 可选:如果您没有更改所有权,则从模型空间中删除所有原始实体(避免重复)

提到的帖子可以提供帮助,但它会创建新实体(并且不会从模型移动到块定义(步骤 #3)

于 2015-05-26T14:07:44.073 回答