简体   繁体   中英

Using C# to Join AutoCAD Entities into a Block Programmatically

I'm trying to write a method that prompts the user to select all the entities they want to combine into a block and then joins them together into a block and returns the block reference. Right now it looks like this.

        /// <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;

        }

My problem is that I have no idea how or if it is possible to take a list of entities and join them into a blockreference.

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

In summary:

  1. use Editor.Getselection so the user can select the entities
  2. create a blockTableRecord (BTR) on the BlockTable (from Database.BlockTableId)
  3. append all entities to the newly created BTR, here you may need to create new entities or move ownership (see BlockTableRecord.AssumeOwnershipOf method)
  4. create a new blockreference that points to the BTR
  5. open the Model Space (or Paper Space) and append the block reference to it
  6. optional: erase all original entities from the model space (avoid duplicated), if you haven't changed ownership

The post mentioned can help, but it creates new entities (and doesn't move from model to the block definition (step #3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM