MessageBox.Show("DestroyNotify event was fired after closing a document.") MessageBox.Show("FileOpenPreNotify event was fired while opening a document.") static public void FileOpenPreNotify(string FileName, dsDocumentOpenOption_e Option) Postconditions: Message boxes pop up for all events, Set a break point in the project where the project connects Start DraftSight, create, save, and close a drawing named Change the path and name of the image file to insert. Copy and paste this example into the C# IDE.
This example shows how to fire application and document events. SwEx.AddIn framework significantly simplifies managing of document events via its document management mechanism.įor example the following code would handle rebuild operations (pre and post) for all types of documents currently loaded or newly created or opened in the SOLIDWORKS session.Fire Application and Document Events Example (C#) To handle events of documents it required to catch the document loading events of SldWorks interface first and subscribe to target events within its handler. It is required to explicitly handle this case in your application. SOLIDWORKS doesn’t group document events under the common IModelDoc2 interface, and in most cases provides similar or the same event for part, assembly and drawing.
swMbOk )) Return 0 End Function End Module Handling events in SwEx.AddIn Framework swMbInformation ), CInt ( swMessageBoxBtn_e. SendMsgToUser2 ( $ " opened", CInt ( swMessageBoxIcon_e. FileOpenNotify2, AddressOf OnFileOpenNotify2 End Sub Function OnFileOpenNotify2 ( ByVal fileName As String ) As Integer m_App. FileOpenNotify2, AddressOf OnFileOpenNotify2 Console. GetTypeFromProgID ( "SldWorks.Application" )), SldWorks ) m_App. Imports Imports Module Module1 Dim m_App As SldWorks Sub Main () m_App = Tr圜ast ( Activator. Select the required event and implement the code within the generated event handler function. Once the object is declared with WithEvents keyword, its events can be selected from the drop-down in the VBA Editor. This keyword can only be used in class modules and not available in modules. To handle events in VBA it is required to declare the object which exposes the events using the WithEvents keyword. Document opened message box Handling events in VBA Macros
We will display a simple message box with the full path to the opened document once the event is triggered. In this blog article I will show how to handle file open event in VBA, C# and VB.NET There are much more events exposed by SOLIDWORKS, this includes but not limited to feature creation, document save, objects selection etc.īelow is a list of SOLIDWORKS interfaces which expose events and the corresponding links to the API help documentation:
For example, you might want to update custom property when new document is created or write a log message on file is closing. run certain code) when some event happens. When developing applications or macros using SOLIDWORKS API it might be required to trigger certain functionality (i.e. Notification is a way of SOLIDWORKS to tell its listeners that something has happened.