Preventing an event from executing using a pre delegate - SOLIDWORKS API [Resolved]

There are a few delegates in the SOLIDWORKS API that execute prior to an event. A lot of them are marked with the word Pre in the name of the delegate.

The delegates returns an integer. This leads to believe that returns 0, -1 or some sort of special value can set the event as handled (and consequently prevent SOLIDWORKS from carrying on the actual command).

I have tried to return 0 or -1 on the UndoPreNotify event. My goal here to prevent the user from undoing the previous command.

Returning those integer values has not yield the expected result so I’m wondering if I’m missing something here. I do think allowing us developer to have control over the event does present a security risk for SOLIDWORKS and hence why this might be disabled.

I have opened a ticket with SOLIDWORKS support. Would love to hear your views?

There are no solid rules for that (usually S_FALSE, e.g. 1) equals to fail and will not proceed further, but it only works for some events, not for the others. You can also try to throw COMException. The most consistent way for me was handling the CommandOpenPreNotify and returning S_FALSE from there, which in most cases blocked the command. Check this macro.

1 Like

Nice. Thanks for the suggestions!

I wonder if DSldWorksEvents_CommandOpenPreNotifyEventHandler fires before the UndoPreNotify does. Looking at this, I’m assuming only certain commands are handled by this (but not all).

This is what I got from API support:

Dear Amen,

Thank you for contacting Dassault Systèmes SolidWorks Corp. API Support.

You can use CommandOpenPreNotifyEventHandler for this purpose. Returning -1 from this event should help in preventing execution of the command. To disable undo command, check if command clicked by user is “swCommands_e.swCommands_Undo”. Please refer below help documentation more details:
DSldWorksEvents_CommandOpenPreNotifyEventHandler Delegate (SolidWorks.Interop.sldworks) - 2021 - SOLIDWORKS API Help

I hope this helps.

Regards,
Dassault Systèmes SolidWorks Corp. API Support

1 Like