Hide Table of Contents

SolidWorks Macro Template

A SolidWorks macro template is a SolidWorks macro that lets you specify parameter values when you schedule it as a custom task in SolidWorks Task Scheduler. A SolidWorks macro template is a text file that typically contains Visual Basic code, SolidWorks API calls, and token names. When you schedule a SolidWorks macro template to run as a custom task in SolidWorks Task Scheduler, you specify the parameter values to substitute for the token names.

Token names can be either text strings or numerics. They are formatted as follows:

Type of Parameter

Format of Token Name

String

$$$<token_name>$$$

Number

###<token_name>###

A SolidWorks macro template must have a .swb filename extension.

To create a macro template:

  1. Open a new text file using Windows Notepad, Microsoft WordPad, or other text editor.

  2. Copy and paste a SolidWorks macro into the text file.

See SolidWorks API and Add-Ins Help Topics for information about SolidWorks macro files.

  1. Type token names in place of the parameters whose values you want to specify.

    For example:

    Original code:

    swApp.SetCurrentWorkingDirectory "c:\temp"

    Modified code:

    swApp.SetCurrentWorkingDirectory $$$TASK_WORKING_DIR$$$

  1. Save the text file with a filename extension .swb.

Create a backup copy of the SolidWorks macro template file before scheduling and running it, or store the SolidWorks macro template file in a location other than the task working directory. Click View, Options, Task Options to see the task working directory. When a task using a SolidWorks macro template runs, the system creates a copy of the macro template and replaces the token names with the parameter values you specified when you scheduled the task. The system saves the file in the task working directory under the same filename as the original SolidWorks macro template. If the original SolidWorks macro template is stored in the task working directory, the system overwrites it with the new file.

The following code is an example of a SolidWorks macro template. It opens a SolidWorks drawing document and saves it as a DXF file. When you schedule the macro template as a custom task in SolidWorks Task Scheduler, you specify the parameters to substitute for the token names: $$$TASK_WORKING_DIR$$$, $$$TASK_FILE_TYPE$$$, $$$TASK_FILEPATH_NAME$$$, and $$$TASK_FILE_NAME$$$.

Dim swApp As Object
Dim swModel As Object
Dim ret As Boolean
Dim dummyRet As Long
Dim longstatus As Long
Dim errorFilePath As String
Dim nErrors As Long
Dim nWarnings as Long

Sub main()

errorFilePath = $$$TASK_WORKING_DIR$$$  +  "\" + "swTaskScheduler.error"
ErrorOut "Start logging any errors...", errorFilePath

Set swApp = CreateObject("SldWorks.Application")
swApp.SetCurrentWorkingDirectory $$$TASK_WORKING_DIR$$$

' Determine type of document
longstatus = 0

' $$$TASK_FILE_TYPE$$$ is case-sensitive! You must enter SW_DRAWING_TYPE or SW_DRAWING_TYPE_OLD
If ($$$TASK_FILE_TYPE$$$ = "SW_DRAWING_TYPE" OR $$$TASK_FILE_TYPE$$$ = "SW_DRAWING_TYPE_OLD")  Then
Set swModel = swApp.OpenDoc6 ($$$TASK_FILE_PATHNAME$$$, 3, 1, "", dummyRet, longstatus)
ErrorOut "File is a drawing document.", errorFilePath
Else
ErrorOut "File is not a drawing document.", errorFilePath
End If

ret = True
' If document is read-only, locked, or view-only, then log an error
If (longstatus And 2) Then
ErrorOut "File is read-only.", errorFilePath
ret = False
ElseIf (longstatus And 4) Then
ErrorOut "File is locked by another user.", errorFilePath
ret = False
ElseIf (longstatus And 512) Then
ErrorOut "File is view-only.", errorFilePath
ret = False
End If

' Close the document if read-only, locked, or view-only
If (ret = False) Then
Set swModel = Nothing
swApp.CloseDoc $$$TASK_FILE_NAME$$$
End If
Set swModel = swApp.ActivateDoc ($$$TASK_FILE_NAME$$$)
If Not swModel Is Nothing Then
' If document is a drawing, then...
If ($$$TASK_FILE_TYPE$$$ = "SW_DRAWING_TYPE" OR $$$TASK_FILE_TYPE$$$ = "SW_DRAWING_TYPE_OLD")  Then
' Strip off SolidWorks drawing file extension (.slddrw)
' and add DXF file extension (.dxf)
sPathName = swModel.GetPathName
sPathName = Left(sPathName, Len(sPathName) - 6)
sPathName = sPathName + "dxf"
' Save as file as DXF
bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
End If
Else
ErrorOut "Document was not or could not be opened.", errorFilePath
End If

' Exit the SolidWorks software
swApp.ExitApp
Set swApp = Nothing
End Sub

Function ErrorOut(errorString As String, errorFilePath As String)
Open errorFilePath For Append As #5
Print #5, errorString
Close #5
End Function



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   SolidWorks Macro Template
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: SOLIDWORKS 2011 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.