.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Delete block from Autocad Drawing.

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
7774 Views, 2 Replies

Delete block from Autocad Drawing.

Hello everyone,

 

  i need to delete a block from my autocad drawing.

  Is there any way i can search for a block in the drawing(by its name for example-'Terminal1') and delete it from the drawing??

  I am able to check if a certain block name (ex-terminal1) is present in the current drawing or not,but i do not know how to remove that portion from the   drawing.If anyone can give a solution it will be of great help.

 

Regards

 

Aaditya

2 REPLIES 2
Message 2 of 3
SENL1362
in reply to: Anonymous

 

This might help you to get started. Don't ever erase a Block when there are references to prevent drawing corruption.

 

        [CommandMethod("TestEraseBlk")]
        public void TestEraseBlk()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            var blkName = "Terminal1";

            try
            {
                var blkId = GetBlkId(db, blkName);
                if (blkId.IsNull)
                    throw new System.Exception(string.Format("\n Block not found: {0}", blkName));

                if (!EraseBlkRefs(blkId))
                    throw new System.Exception(string.Format("\n Failed to Erase BlockReferences for: {0}", blkName));

                if (!EraseBlk(blkId))
                    throw new System.Exception(string.Format("\n Failed to Erase Block: {0}", blkName));

                ed.WriteMessage("\n Block Erased: {0}", blkName);
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }


        public static ObjectId GetBlkId(Database db, string blkName)
        {

            ObjectId blkId = ObjectId.Null;

            if (db == null)
                return ObjectId.Null;

            if (string.IsNullOrWhiteSpace(blkName))
                return ObjectId.Null;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (bt.Has(blkName))
                    blkId = bt[blkName];
                tr.Commit();
            }
            return blkId;
        }


        public static bool EraseBlkRefs(ObjectId blkId)
        {
            bool blkRefsErased = false;

            if (blkId.IsNull)
                return false;

            Database db = blkId.Database;
            if (db == null)
                return false;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
                var blkRefs = blk.GetBlockReferenceIds(true, true);
                if (blkRefs != null && blkRefs.Count > 0)
                {
                    foreach (ObjectId blkRefId in blkRefs)
                    {
                        BlockReference blkRef = (BlockReference)tr.GetObject(blkRefId, OpenMode.ForWrite);
                        blkRef.Erase();
                    }
                    blkRefsErased = true;
                }
                tr.Commit();
            }
            return blkRefsErased;
        }



        public static bool EraseBlk(ObjectId blkId)
        {
            bool blkIsErased = false;

            if (blkId.IsNull)
                return false;

            Database db = blkId.Database;
            if (db == null)
                return false;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
                var blkRefs = blk.GetBlockReferenceIds(true, true);
                if (blkRefs == null || blkRefs.Count == 0)
                {
                    blk.UpgradeOpen();
                    blk.Erase();
                    blkIsErased = true;
                }
                tr.Commit();
            }
            return blkIsErased;
        }

 

Message 3 of 3
Anonymous
in reply to: SENL1362

Thanks for the reply.

This solution works like dream!!Absolutely perfect for my problem!! Smiley Happy

Simple and efficient solution!!

Regards

 

Aaditya Aggarwal

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report