.NET

Reply
Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 1 of 5 (269 Views)

Delete specific Block

269 Views, 4 Replies
08-25-2013 11:26 PM

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.

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 2 of 5 (263 Views)

Re: Delete specific Block

08-26-2013 12:22 AM in reply to: raghulan
You can try following link where deletion of a block is discussed:

http://forums.augi.com/showthread.php?135845-Delete-block
*Expert Elite*
_gile
Posts: 2,093
Registered: ‎04-29-2006
Message 3 of 5 (252 Views)

Re : Delete specific Block

08-26-2013 02:17 AM in reply to: raghulan

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
}

 

 

Gilles Chanteau
Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 4 of 5 (218 Views)

Re : Delete specific Block

08-26-2013 06:25 PM in reply to: _gile

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.

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
*Expert Elite*
_gile
Posts: 2,093
Registered: ‎04-29-2006
Message 5 of 5 (208 Views)

Re : Delete specific Block

08-26-2013 11:59 PM in reply to: raghulan

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);
            }

 

 

 

Gilles Chanteau
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.