Home Search

DriveWorks Pro 21
How To: Create A Macro To Run On A SOLIDWORKS File (KB13103006)

Send Feedback

Introduction

DriveWorks has the ability to automatically run SOLIDWORKS macros on components or drawings that are being created by DriveWorks.

The type of SOLIDWORKS document that can have a macro run depends on the model generation method being used:

  • On Demand Generation - Macro is only run for the top-level model, not for any sub-components or drawings.
  • Queued Generation (Manual and Automatic) - Macros can be run on any or all models and drawings.

For more information on the different model generation methods see Info: Model Generation Behavior.

Aspects of the Macro are significant when DriveWorks runs them, these include:

  1. The Macro Name and Location.
  2. The Module Name.
  3. The Sub-routine.

Macro Name and Location

The file name and location of the macro depends on the Model Generation method being used.

On Demand Generation

The name of the macro must be Model.swp. This will be run on the top level assembly during On Demand generation.

The macro must be located in a sub-folder of the Group Content Folder called \Macros.

For example:

If the Group content folder is set to be

C:\DriveWorks\My Project\

The macros should be stored in

C:\DriveWorks\My Project\Macros

Queued Generation

  • To run a SOLIDWORKS macro on all models or drawings.

    The name given to the macro depends on the SOLIDWORKS document type it is being run on, and when it is required to be run.

    1. Model.swp - will be run on all models before any individual macros have been executed
    2. ModelAfterMacro.swp - will be run on all models after any individual macros have been executed
    3. Drawing.swp - will be run on all drawings before any individual macros have been executed
    4. DrawingAfterMacro.swp - will be run on all drawings after any individual macros have been executed

    The macro must be located in a sub-folder of the Group Content Folder called \Macros.

    For example:

    If the Group content folder is set to be

    C:\DriveWorks\My Project\

    The macros should be stored in

    C:\DriveWorks\My Project\Macros

  • To run a SOLIDWORKS macro on a single model or drawing.

    The macro must be located in the same location as the master model or drawing.

    The name of the macro must be the same as the master model it is to be run on, as drawings can have the same name as a model a macro to be run on a drawing must have Draw appended to the file name.

    For example the SOLIDWORKS documents:

    MyAssembly.sldasm will have a macro named MyAssembly.swp

    MyPart.sldprt will have a macro named MyPart.swp

    MyAssembly.slddrw will have a macro named MyAssemblyDraw.swp

Module Name

Regardless of the Generation method being used the macro must contain a module called DriveWorks.

Once the DriveWorks module has been created the sub-routine, which includes the code required to run, can be created.

Sub-routine

The name of the sub-routine within the DriveWorks module depends on the model generation method being used.

On Demand Generation

The following sub-routine names can be used for On Demand model generation.

  • OnDemandFinalizing - Will run during the finalizing of the model.
  • OnDemandFinalized - Will run once the model has been finalized.
  • OnDemandPreview - Will run each time the model is previewed during On Demand generation.
Example On Demand sub-routine naming
Sub OnDemandFinalizing()
    ‘ Code here
End Sub

Queued Generation

The sub-routine must be named Main.

Example Queued sub-routine naming
Sub Main()
    ‘ Code here
End Sub

Example

This example requires a custom property named SaveLocation in the SOLIDWORKS model. This custom property can be captured and the model will be saved to the location, with the file name and extension driven into the custom property value.

For the macro to run successfully the Custom Property value must obey the following criteria:
  • The file path must be a fully qualified file path. For example C:\DriveWorks\Images\...
  • The file path must exist. The example given below will not create the file path if it does not exist.
  • The user must have permissions to create files in the location specified.
  • The file extension must be included.
  • The file name cannot contain any illegal characters.

For example if the custom property SaveLocation results in the value C:\DriveWorks\Images\Project1005.jpg the model will be saved to C:\DriveWorks\Images as a .jpg image file.

Example Macro (Applies to Queued or On Demand Generation)
Sub Main()

    ' Get SOLIDWORKS
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks

    ' Get the currently open document
    Dim model As ModelDoc2
    Set model = swApp.IActiveDoc2

    ' Get the "general", i.e. non-configuration specific, custom property manager
    Dim cpm As CustomPropertyManager
    Set cpm = model.Extension.CustomPropertyManager("")

    ' Get the custom property called SaveLocation
    Dim saveLocation As String
    saveLocation = cpm.Get("SaveLocation")

    ' Save the file to the new location
    Dim version As swSaveAsVersion_e
    Dim options As swSaveAsOptions_e
    Dim errors As Long
    Dim warnings As Long
    Dim result As Boolean
    result = model.Extension.SaveAs(saveLocation, version, options, Nothing, errors, warnings)
    
End Sub


Sub OnDemandFinalized()

    ' Get SOLIDWORKS
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks

    ' Get the currently open document
    Dim model As ModelDoc2
    Set model = swApp.IActiveDoc2

    ' Get the "general", i.e. non-configuration specific, custom property manager
    Dim cpm As CustomPropertyManager
    Set cpm = model.Extension.CustomPropertyManager("")

    ' Get the custom property called SaveLocation
    Dim saveLocation As String
    saveLocation = cpm.Get("SaveLocation")

    ' Save the file to the new location
    Dim version As swSaveAsVersion_e
    Dim options As swSaveAsOptions_e
    Dim errors As Long
    Dim warnings As Long
    Dim result As Boolean
    result = model.Extension.SaveAs(saveLocation, version, options, Nothing, errors, warnings)
    
End Sub



Knowledge Base Article Ref:KB13103006