下列程式碼為 SOLIDWORKS 巨集範本的範例。 它會開啟 SOLIDWORKS 工程圖文件並另存為 DXF 檔案。 當您排程巨集範本,做為 SOLIDWORKS 工作排程器中的自訂工作時,您會指定參數來替代記號名稱:
$$$TASK_
WORKING_DIR$$$、
$$$TASK_FILE_TYPE$$$、
$$$TASK_FILEPATH_NAME$$$ 及
$$$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