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:
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:
The file name and location of the macro depends on the Model Generation method being used.
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
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.
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
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
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.
The name of the sub-routine within the DriveWorks module depends on the model generation method being used.
The following sub-routine names can be used for On Demand model generation.
Sub OnDemandFinalizing() ‘ Code here End Sub
The sub-routine must be named Main.
Sub Main() ‘ Code here End Sub
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 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.
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 |
---|