.NET

Reply
Valued Contributor
98 Posts
1 Kudo
Registered: ‎01-11-2012
Post 1 of 5

Delete specific Block

300 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
232 Posts
30 Kudos
Registered: ‎11-09-2012
Post 2 of 5

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*
2,140 Posts
253 Kudos
Registered: ‎04-29-2006
Post 3 of 5

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
98 Posts
1 Kudo
Registered: ‎01-11-2012
Post 4 of 5

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*
2,140 Posts
253 Kudos
Registered: ‎04-29-2006
Post 5 of 5

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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!