Hello Guys,
I coudnt find this online.
I have a handle for a specific block.
I want to delete using the Handle.
Here is what I tried but its not working..please advice.
DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); using (Transaction tn = Global.variables.db.TransactionManager.StartTransaction()) { try { ObjectId obj = convert_handle_to_objectid(handle); BlockReference Block = (BlockReference)(tn.GetObject(obj, OpenMode.ForWrite)); Block.Erase(); tn.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { tn.Abort(); MessageBox.Show(ex.StackTrace); } } doclock.Dispose();
Nothing happens when I run this.
Hi,
I don't know anything about the convert_handle_to_objectid method but what about using:
ObjectId id; if (db.TryGetObjectId(handle, out id)) { // open and erase the DBObject here (no need to cast into a BlockReference) } else { // unable to convert the handle into an ObjectId }
Thanks Mate,
Here is what I tried, nothing happens.
DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); using (Transaction tn = Global.variables.db.TransactionManager.StartTransaction()) { try { ObjectId id; if (db.TryGetObjectId(handle, out id)) { DBObject bowner = tn.GetObject(obj, OpenMode.ForWrite, false) as DBObject; bowner.Erase(true); } else { // unable to convert the handle into an ObjectId } tn.Commit(); Global.variables.ed.Regen(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { tn.Abort(); MessageBox.Show(ex.StackTrace); } } doclock.Dispose();
I am trying to delete a block.
I tried few other as well still the same issue.
Also I have the same issue with moving blocks. where I am able to move other object but not blocks.
Any Help would be great.
Try this way:
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; try { using (doc.LockDocument()) using (Transaction tr = db.TransactionManager.StartTransaction()) { ObjectId id; if (db.TryGetObjectId(handle, out id)) { DBObject obj = tr.GetObject(id, OpenMode.ForRead); if (obj is BlockReference) { try { obj.UpgradeOpen(); obj.Erase(); } catch { MessageBox.Show("Unable to erase the BlockReference"); throw; } } else { MessageBox.Show("The object is not a BlockReference, it's a " + obj.GetType().Name); } } else { MessageBox.Show("Unable to convert Handle to ObjectId"); } tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } catch (System.Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); }