I’m writing a .net plugin where i need to check if two components can be connected to each other. For al the components in the world, I capture the ISimInterface Connected and Disconnected events. Based on this i can find which interfaces and which resources are getting connected (disconnected).
I perform my check’s, but sometimes these components can’t get connected to each other (based on data from a custom meta-model), so the worlds needs to go back to the way it was, just before connecting these resources to each other.
The simplest way to do this, is by pressing CTRL+Z in the world view. But is this also possible to be done Programmatically?
I looked in using IUndoHelper. Using this, you can do Save/RestoreState, but i can’t save the state before the connection is made, because i use the events coming from the world.
If you just need to undo some action that user could with Ctrl + Z you can simply call IUndoService.Undo()
There is also a command with id “Undo” that does the same.
Note that undo is quite limited in VC and only works for some actions done from the UI. If you manipulate the world directly from the API it doesn’t create anything undoable and actually will just mess with any stored undoable actions because the state is not what they would expect it to be.
Also for your specific use case there is probably a better way.
It is possible to prevent / discard interface connection from happening in the first place by registering a handler to ISimInterface.Connecting event and then setting the InterfaceConnectPreEventArgs.Handled to true if you want prevent the connection.
Both options seem to work, but the resource stay’s located next to each other.
So it looks like they are connected, when they are not.
Is there a way to solve this?
When you press CTRL+Z the component springs back to it last place, but this doesn’t happen with the IUndoService.Undo
The difference is probably in how the connection operation is done. Did you do it from the API or use the PnP tool from the UI?
The PnP tool probably creates specific undoable operations which include and store the original positions of the the components and thus those can be restored. If done some other way the information is not stored anywhere.