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
Solved! Go to Solution.
Solved by SENL1362. Go to Solution.
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; }
Thanks for the reply.
This solution works like dream!!Absolutely perfect for my problem!!
Simple and efficient solution!!
Regards
Aaditya Aggarwal
Can't find what you're looking for? Ask the community or share your knowledge.