question

David Besson avatar image
1 Like"
David Besson asked David Besson edited

How to execute a custom code when an object is manually deleted in the 3D view

I would have liked to execute some code when an object is deleted in the 3D view.

  • while the simulation is stopped
  • not in onModelReset

For example: delete a row in a global table when a Processor is deleted.

Any idea how to do it?

Thanks!

FlexSim 17.2.3
delete
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image
3 Likes"
Phil BoBo answered David Besson commented

1. You can add an OnDestroy attribute to the object's eventfunctions in order to have a callback when the object is destroyed.

You can return 1 in OnDestroy to prevent the object from being destroyed. For example, if you wanted to add a message box for the user to confirm that they want to delete the object.

Also, when you override an event function on an object, you should almost always call inheritcode() so that it executes the class object's version of that function. Otherwise, the default behavior of that function will not happen.

2. You can also use nodejoin() to couple two nodes together so that when one is destroyed, the other is also destroyed.

Attached is a sample model that shows both of these methods.

The queue has an OnDestroy attribute that fires custom code when it is destroyed.

The processors each have a node within their stored attribute that is linked to a global table row so that when you destroy the processor, it deletes the table row.

The script windows show how I linked the processors to the global table and how you can programmatically add the OnDestroy node to the queue.

I don't understand what you mean by the conditions:

  • while the simulation is stopped
  • not in onModelReset

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

David Besson avatar image David Besson commented ·

Thank you!

This is exactly what I was looking for.

By "not in onModelReset", I meant that it was not necessary to reset the model to execute this code.

By "while the simulation is stopped", I meant that it was not necessary to have the simulation running to execute this code

0 Likes 0 ·
Steven Hamoen avatar image
2 Likes"
Steven Hamoen answered David Besson commented

@David Besson Because you have given very little information I don't know why you want to do this? It sounds very strange to remove objects in the middle of a run? Can't you make a gui where you can select an object to delete? That way you have complete control.

To answer the original question I think there are several approached possible:

1. If the object is deleted by code you can do it in that code obviously so I guess you want this when somebody manually deletes an object.

2. If you create your own module object you have a destructor to this.

3. you can use the onrun start trigger. So if you stopped the model, removed an object and start the continued run, the run start trigger is fired.

4. Last option you could have a piece of code that runs continuously (like a plc loop) by writing an ondraw trigger, that checks if an object does not exist anymore. If it not exists you can do some stuff. Advantage of this method is also if you close your windows for a fast run it is not executed. But it is not a very nice solution and if possible avoid it. An important rule is to never write logic on the ondraw that is needed for running the model.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

David Besson avatar image David Besson commented ·

Thanks also for your answer!

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.