Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Detecting Entity Deletion

1 REPLY 1
Reply
Message 1 of 2
TNorbith
124 Views, 1 Reply

Detecting Entity Deletion

How would I register an event handler that would detect the deletion of a sketch entity?  I have a SketchLine with some associated text and I want to delete the text when the line is deleted.

 

Thanks

1 REPLY 1
Message 2 of 2
TNorbith
in reply to: TNorbith

After thinking about this for a while I decided to try listening for ui.ActiveSelectionChanged events.  I thought I would be able to detect when a labeled SketchLine was selected and then look for it after the selection changes to being nothing.  If I couldn't find the SketchLine then I could assume it was deleted.  I stored the entityToken of the SketchLine and then looked for it after the selection was cleared which can happen either by hitting ESC key, clicking on an empty part of the screen, or deleting the selected entity.

 

The problem I have is that when the SketchLine is deleted the findEntityByToken() function still returns a valid object in the ActiveSelectionChanged callback.  Upon a second entrance into the callback the findEntityByToken() correctly fails to find the deleted SketchLine. 

 

As a kludge I executed the SelectCommand again from with the callback if the selection was empty and the last thing selected was a labeled SketchLine and this worked.  I was able to detect that the labeled SketchLine was deleted and then I was able to delete the corresponding label text.

 

So I'm not sure if occasionally executing the SelectCommand again within my callback will cause problems or if there is a way to update the deleted items within the callback so findEntityByToken() correctly cannot find the deleted item.

 

EDIT:

The above didn't really work too well.  Retriggering the SelectCommand caused normal commands to abort.

 

The solution I came up with that seems to work well is the following:

 

I will call the sketch object that consists of a line with associated text a "LabeledLine" object (my name, not a Fusion thing)

 

  1. When creating a LabeledLine, add attributes to the line and the textbox so they know they are associated with each other.  I did this by giving the SketchLine the entityToken of the SketchText object and giving the SketchText the entityToken of the SketchLine.  That way when the SketchLine is selected you can lookup the associated SketchText and vice-versa.
  2. Listen to ui.commandStarting and ui.activeSelectionChanged events.  In the activeSelectionChanged callback detect if a "LabeledLine" is selected by looking for the attributes assigned in Step 1.  If either the line or the text is selected then consider the "LabeledLine" selected.
  3. In the ui.commandStarting callback, check if a "LabeledLine" is selected and if the current command is "Delete".  If so then stop the native Delete command by setting the args.isCanceled property to True and execute a custom delete command to delete both the line and the textbox.  I used a global variable to keep track of the "LabeledLine" that is selected and needs to be passed to the custom delete command.

This solution is working great for me at this point.

 

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

Post to forums  

Autodesk Design & Make Report