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

Delete specific Block

4 REPLIES 4
Reply
Message 1 of 5
raghulan
655 Views, 4 Replies

Delete specific Block

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
4 REPLIES 4
Message 2 of 5
mzakiralam
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
Message 3 of 5
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 5
raghulan
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
Message 5 of 5
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost