.NET

Reply
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 1 of 14 (257 Views)

Erase last object

257 Views, 13 Replies
03-20-2012 12:27 PM
Hey everyone, In AutoCAD, i can type "erase" and then "last" into the commandline and it will automatically select and erase the last object i created. How is this accomplished in C#? I'm generating a polyline to automatically calculate areas, but i would like that polyline to vanish once the area has been measured. Thanks Vince
Distinguished Mentor
gasty1001
Posts: 577
Registered: ‎04-11-2010
Message 2 of 14 (246 Views)

Re: Erase last object

03-20-2012 01:20 PM in reply to: vince1327

Hi,

 

There is a SelectLast option:

PromptSelectionResult acSSPrompt;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
acSSPrompt = ed.SelectLast();

 

Gaston Nunez

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 3 of 14 (241 Views)

Re: Erase last object

03-20-2012 01:42 PM in reply to: gasty1001

Ok that helps, what would i use to delete what i selected with the editor?

 

Thanks

Vince

Distinguished Mentor
gasty1001
Posts: 577
Registered: ‎04-11-2010
Message 4 of 14 (235 Views)

Re: Erase last object

03-20-2012 02:22 PM in reply to: vince1327

Hi,

 

You should start a transaction, get the last object (as entity)  erase it (entity.erase), and commit the transaction and that's it. Like this (not tested, and translated from VB)

 

[CommandMethod("ELAST")]
public void EraseObj()
{
	Document acDoc = Application.DocumentManager.MdiActiveDocument;
	Database acCurDb = acDoc.Database;
	Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

	using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) {
		PromptSelectionResult acSSPrompt;
		acSSPrompt = ed.SelectLast;
		Entity ent;
		SelectionSet ss = acSSPrompt.Value;

		if (ss.Count > 0) {
			ent = acTrans.GetObject(ss.Item(0).ObjectId, OpenMode.ForWrite);
			ent.Erase();
		}
		acTrans.Commit();
	}
}

 

Gaston Nunez

 

Valued Mentor
jeff
Posts: 335
Registered: ‎05-12-2009
Message 5 of 14 (224 Views)

Re: Erase last object

03-20-2012 04:48 PM in reply to: gasty1001

I have not used it and of course 'not supported' but maybe you can search for

 

Autodesk.AutoCAD.Internal.Utils.EntLast();

 

and see if people have had success

You can also find your answers @ TheSwamp
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 6 of 14 (203 Views)

Re: Erase last object

03-21-2012 05:49 AM in reply to: gasty1001

Thanks for this sample, i'm having an issue with the line:

 

 ent = acTrans.GetObject(ss.(0).ObjectId, OpenMode.ForWrite);

.

Visual Studio 2010 and AutoCAD 2012 are telling me that there is no definition for ss.item and thus i cannot compile. As i missing  a reference?

 

Thanks

Vince

 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 7 of 14 (202 Views)

Re: Erase last object

03-21-2012 05:49 AM in reply to: jeff

Thanks jeff, i'll have a look into this as well.

Cheers

Vince

Distinguished Mentor
gasty1001
Posts: 577
Registered: ‎04-11-2010
Message 8 of 14 (196 Views)

Re: Erase last object

03-21-2012 06:12 AM in reply to: vince1327

Hi,

 

SelectionSet  it's part of the EditorInput, and I see you are "using" it, so I have no idea why VS is telling you that. Any way the correct sintax is : ss(index).ObjectId without a dot after ss.

 

Gaston Nunez

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 9 of 14 (194 Views)

Re: Erase last object

03-21-2012 06:20 AM in reply to: gasty1001

Ok, so i've got this now:

 

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
PromptSelectionResult acSSPrompt;
acSSPrompt = ed.SelectLast();
Entity ent;
SelectionSet ss = acSSPrompt.Value;

if (ss.Count > 0)
{
ent = acTrans.GetObject(ss(0).ObjectId, OpenMode.ForWrite);
ent.Erase();
}
acTrans.Commit();
}
}
}
}

 

 

But the error I have is 'ss' is a 'variable' but is used like a 'method'

Distinguished Mentor
gasty1001
Posts: 577
Registered: ‎04-11-2010
Message 10 of 14 (192 Views)

Re: Erase last object

03-21-2012 06:25 AM in reply to: vince1327

Hi,

 

My mistake (i'm a vb guy), just change ss(0) by ss[0].

 

Gaston Nunez

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!