Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 893
Nick Gilbert Avatar asked Nov 22 '25 05:11

Nick Gilbert


1 Answers

Kean covered this in his blog: Creating an AutoCAD block using .NET

like image 88
Miiir Avatar answered Nov 24 '25 22:11

Miiir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!