|
|
API Tip #5 05/30/2005 (updated 5/1/06) Notifications or Events give you a powerful method for triggering code to run when something happens in SolidWorks. This is the ideal method, for example, if you would like to run a procedure that checks to see if certain file properties have been filled out when a user saves a part. Unlike typical macros which run after the user clicks a button or manually runs the macro, macros that use notifications run continuously. They are prompted to run procedures when specific actions occur in SolidWorks. This example will process notifications from SolidWorks to return when a part model is active and a notification from the part when it is saved.Initial Code
Dim swApp As Object
ClassesTo enable notifications you must insert a class module into the macro. Notifications cannot be used in a standard code module.
WithEvents DeclarationDeclaring a variable using the WithEvents keyword tells the code that this is an object that will be triggered by SolidWorks. Several objects in SolidWorks support notifications. Each of which has its own list of notifications that are specific to the object type. This example uses notifications from the SolidWorks application and from parts.
You must create an object of the Class type (the name of the class you created) to use one of its methods. Using the New keyword creates an instance of the object the first time one of its procedures is called. This eliminates the need to use the Set command after declaring the object.
Creating the Notification FunctionsSince the swApp and MyPart objects were declared using the WithEvents keyword, their notifications or events will be available through the object pull down list in the VBA interface.
The new code will check if the active document is a part. If it is, it will set the MyPart object to the active part document. Otherwise, it will set it to nothing, or no object.
Private Function MyPart_FileSaveAsNotify2(ByVal FileName As String) As Long Dim result As Long If result = vbCancel Then End Function FileSaveAsNotify2MyPart_FileSaveAsNotify2 will run after the user has filled out the Save As dialog and clicked OK, but prior to the file actually being saved. This is a pre-notification. This allows you to catch the Save As action to make any necessary checks. It also passes the variable FileName. This corresponds to the file name the user typed into the Save As dialog. In the example, if the user clicks Cancel on the message box, the Function is set to a non-zero value, causing the Save As operation to be canceled.
Starting Macros with SolidWorksFor notifications to work properly, they usually need to run during your entire SolidWorks session. In fact, if you manually start a macro that uses notifications, it will continue to run until you close your SolidWorks session. To start a macro with a SolidWorks session you must create a shortcut to the SolidWorks executable, adding the "/m" command line argument. Follow the steps below to launch a macro with your SolidWorks session.
"C:\Program Files\SolidWorks\SLDWORKS.exe" /m "C:\Macros\notifications.swp" ConclusionStart SolidWorks by using the new shortcut. Create a new part and save it. You should see your message box display. Notifications are the building blocks to automatically running any procedures without requiring the user to remember to. They can be a great way to build in automated check or output routines. Here are some common uses and ideas for your own utilities: prompt the user to add properties or materials when they create or save models, automated part numbering, standards checking, notify a user that additional information must be updated, check for under or over defined sketches, etc.
Mike Spens HUNDREDS MORE VIDEOS AND TIPS IN THE MEMBERS AREA. SIGN UP HERE.
|
|
|




Comments
Thanks