Document.ImpliedSelectionChanged event handler problem

Document.ImpliedSelectionChanged event handler problem

adam.krug
Advocate Advocate
2,074 Views
2 Replies
Message 1 of 3

Document.ImpliedSelectionChanged event handler problem

adam.krug
Advocate
Advocate

I'm trying to subscribe to the Document.ImpliedSelectionChanged event so that I can show some information about selected objects on my UI palette. The problem is that this works only once, until user presses Escape or invokes any command. After that the event handler is not even hit.

 

Is what I'm trying to achieve possible? If yes, what am I doing wrong?

 

The current code (just a proof of concept):

//method linked with a command to enable selection listening
public void SelInfoListenOn()
{
	var doc = Application.DocumentManager.MdiActiveDocument;
	doc.ImpliedSelectionChanged += Doc_SelChanged;
}

//my handler
private void Doc_SelChanged(object sender, EventArgs e)
{
	var logPath = @"C:\Users\User\Desktop\selchangelog.txt";
	string info = "Selection changed:\nInfo:";
	PromptSelectionResult psr = (sender as Document).Editor.SelectImplied();
	foreach (SelectedObject selObj in psr.Value)
	{
		info += $"\n - {selObj.GetType().ToString()}";
	}

	System.IO.File.AppendAllText(logPath, info + "\n");
}
0 Likes
Accepted solutions (1)
2,075 Views
2 Replies
Replies (2)
Message 2 of 3

Norman_Yuan
Mentor
Mentor
Accepted solution

I think the problem lies in your handler method Doc_SelChanged(), where you need to test PromptSelectionResult's state, and only execute the following code ("foreach...") when the status in PromptStatus.OK. Just think about it: the ImpliedSelectionChanged event fires both when user selected something, or something previously selected become unselected.

 

When the implied selection becomes unselected, the "foreach..." in your code would raise error, because of psr.Value would be null, I guess. Sometimes, AutoCAD seems simply swallow the exception in event handler without stopping. In this case, the silent exception simply unhooked the event handler, I guess.

 

So, in your code, you need:

private void Doc_SelChanged(object sender, EventArgs e)
{
	var logPath = @"C:\Users\User\Desktop\selchangelog.txt";
	string info = "Selection changed:\nInfo:";
	PromptSelectionResult psr = (sender as Document).Editor.SelectImplied();
if (psr.Status == PromptStatus.OK)
{ foreach (SelectedObject selObj in psr.Value) { info += $"\n - {selObj.GetType().ToString()}"; } System.IO.File.AppendAllText(logPath, info + "\n");
} }

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

adam.krug
Advocate
Advocate

Thanks @Norman_Yuan , it works as a charm now 🙂

0 Likes