SolidWorks Tips and Things

Subscribe | Daily Tips | Unsubscribe
SolidWorks Tips Home | 3D iDesign, Inc Home
#1 | #2 | #3 | #4 | #5 | #6 | #7 | #8 | #9 | #10 | #11 | #12 | #13 | #14| #15| #16|
#1 | #2 | #3 | #4 | #5 | #6 |
subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link
subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link

Using Notifications (Events) in SolidWorks

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

  1. Start a new macro by selecting Tools, Macro, New or clicking the new macro button on the Macro toolbar.  Save the macro to "C:\Macros" (create a new folder if necessary) as notifications.swp.  This begins our macro with the following code.

 

Dim swApp As Object

Sub main()

Set swApp = Application.SldWorks

End Sub

Classes

To enable notifications you must insert a class module into the macro.  Notifications cannot be used in a standard code module.

  1. Add a new class module to your macro by selecting Insert, Class Module from the VBA interface (macro editor).  You should see the following in the VBA Project Explorer with a new class module.



 

 

 

 

 

 

 

 

 

 

 

 

 

 

WithEvents Declaration

Declaring 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.

 

  1. Rename the class module Notification_Class and add the following declarations to enable notifications for the SolidWorks application and SolidWorks parts.


Option Explicit

Dim WithEvents swApp As SldWorks.SldWorks
Dim WithEvents MyPart As SldWorks.PartDoc 


  1. Add the following procedure for attaching to the SolidWorks object through the class.
Public Sub MonitorSolidWorks()
    Set swApp = Application.SldWorks
End Sub

  1. Switch back to the original code module named notifications1.  Remove the swApp declaration and modify the code in the main procedure as follows.

Public MyClass As New Notification_Class

Sub Main()
MyClass.MonitorSolidWorks
End Sub

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.

  1. Switch back to the Notification_Class class module.

Creating the Notification Functions

Since 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. 

 

 

  1. Select swApp from the object list.  From the procedures list, select ActiveDocChangeNotify.  A new private fuction will be created that will run every time the active document in SolidWorks changes.  This includes when a new file is opened or when switching between open documents.
     
  2. Add the following code to the swApp_ActiveDocChangeNotify function.

 

Private Function swApp_ActiveDocChangeNotify() As Long

    If swApp.ActiveDoc.GetType = swDocPART Then
        Set MyPart = swApp.ActiveDoc
    Else
        Set MyPart = Nothing
    End If

End Function

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.

  1. Create a function that runs when a part is first saved by selecting MyPart from the object list and FileSaveAsNotify2 from the procedure list.  Add the following code to the newly created MyPart_FileSaveAsNotify2 function.

Private Function MyPart_FileSaveAsNotify2(ByVal FileName  As String) As Long

    Dim result As Long
    result = MsgBox("Click OK to save " & FileName & ".", vbOKCancel + vbQuestion)

    If result = vbCancel Then
        MyPart_FileSaveAsNotify2 = 1
    End If

End Function 

FileSaveAsNotify2

MyPart_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.

  1. Test your new macro by selecting Run, Run Sub/UserForm or by clicking the Play button on the Macro toolbar.  Switch back to SolidWorks, create a new part and click Save.  You should see your message box display.

Starting Macros with SolidWorks

For 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.

  1. Create a new shortcut on your Desktop by right-clicking and selecting New, Shortcut.


 

 

 

  1. Browse to the SolidWorks executable, typically found in "C:\Program Files\SolidWorks\SLDWORKS.exe", and click OK.

















  1. Click Next and name the shortcut "SolidWorks with Notifications".  Then click Finish.
  2. Right-click on the newly created shortcut and select Properties.  In the Target box, add the following text.  This example assumes the standard SolidWorks executable location and that you have saved the macro to the directory mentioned in step 1.

"C:\Program Files\SolidWorks\SLDWORKS.exe" /m "C:\Macros\notifications.swp"

Conclusion

Start 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.

  Download Example


Mike Spens
mikespens@solidworkstips.com

For more from this author look into:

 

 

 

 

 

SolidWorks Daily Text Tips

Chat here it stays so you can get updates each time you visit back.


Get your own Chat Box! Go Large!

Click to see how 3D iDesign, Inc can help your company get to market faster. Click Here to view the vast services Engineering needs.
Product Design
2D to 3D
Art to Part
Reverse Engineering
Customer SolidWorks and PDMWorks Programming
Stress Analysis
CNC Programming

Current SolidWorks Service Pack
The current SolidWorks 2009 Service Pack is 1.0
The current SolidWorks 2008 Service Pack is 5.0
The current SolidWorks 2007 Service Pack is SP 5.0
The current SolidWorks 2007 x64 Service Pack is SP5.0
The current SolidWorks 2006 Service Pack is SP5.1
The current SolidWorks 2006 x64 Service Pack is SP5.1
The current PDMWorks 2006 Service Pack is SP5.1
The current SolidWorks 2005 Service Pack is SP5.0
The current PDMWorks 2005 Service Pack is SP5.0

Click here to login and download from SolidWorks Web site

Quick Links SolidWorks
3D iDesign, Inc Engineering Services
Video Cards Testing

Please support this site and donate any amount you would like if this site helped you out.

 

For more books visit here

New book from the author of SolidWorks Tips and Things API Tips Section Mike Spens pick one up today

About Us | Contact Us | ©2007 3D iDesign, Inc ©

SolidWorks and SolidWorks Applications are registered trademarks of SolidWorks Corporation. All other names may be trademarks of their respective owners. SolidWorks Tips and Things is not affiliated with or sanctioned by the SolidWorks Corporation