產生 SOLIDWORKS 巨集範本

SOLIDWORKS 巨集範本是 SOLIDWORKS 的巨集,可以讓您在安排巨集作為 SOLIDWORKS 工作排程器的自訂工作時,指定參數值。 SOLIDWORKS 巨集範本通常是包含 Visual Basic 碼、SOLIDWORKS API 呼叫與記號名稱的文字檔案。 當您排定 SOLIDWORKS 巨集範本執行為 SOLIDWORKS 工作排程器的自訂工作時,您指定參數值以替代記號名稱。

記號名稱可以是文字字串或數字。 其格式如下:

參數類型 記號名稱格式
字串 $$$<token_name>$$$
數字 ###<token_name>###

SOLIDWORKS 巨集範本必須有 .swb 的副檔名。

產生巨集範本︰

  1. 使用 Windows 記事本、Microsoft WordPad 或其他文字編輯器來開啟新的文字檔案。
  2. 複製並貼上 SOLIDWORKS 巨集至文字檔案中。
    關於 SOLIDWORKS 巨集檔案的詳細資訊請參考 SOLIDWORKS API 及附加程式說明主題。
  3. 在您要指定的值的參數處輸入記號名稱。 例如:
    原始碼: swApp.SetCurrentWorkingDirectory "c:\temp"
    修改碼: swApp.SetCurrentWorkingDirectory $$$TASK_WORKING_DIR$$$
  4. .swb 副名儲存文字檔案。
    在排定 SOLIDWORKS 巨集範本檔案的工作並執行之前,產生其備份副本,或將 SOLIDWORKS 巨集範本檔案儲存在工作執行目錄以外的位置。 按一下 檢視 > 選項 > 工作選項 以查看工作執行的目錄。 當使用 SOLIDWORKS 巨集範本的工作在執行時,系統會產生巨集範本的副本,並且使用您排定工作時指定的參數值代替記號名稱。 系統會在工作執行目錄中以與原始 SOLIDWORKS 巨集範本相同的檔案名稱來儲存檔案。 如果原始的 SOLIDWORKS 巨集範本已儲存在工作執行目錄中,系統會用新檔案來覆寫它。
    下列程式碼為 SOLIDWORKS 巨集範本的範例。它會開啟資料夾中各個 SOLIDWORKS 工程圖文件,並將其另存為 DXF 檔案。當您排程巨集範本,做為 SOLIDWORKS 工作排程器中的自訂工作時,您會指定參數來替代記號名稱:$$$TASK_SOURCE_DIR$$$$$$TASK_DESTINATION_DIR$$$
    Dim swApp As Object
    Dim swModel As Object
    Dim ret As Boolean
    Dim openError As Long
    Dim openWarn As Long
    Dim errorFilePath As String
    Dim nErrors As Long
    Dim nWarnings As Long
    Dim destinationPath As String
    Dim sourcePath As String
    Dim modelTitle As String
    
    Sub main()
    
        sourcePath = $$$TASK_SOURCE_DIR$$$
        destinationPath = $$$TASK_DESTINATION_DIR$$$
         
        errorFilePath = destinationPath + "\" + "swTaskScheduler.error"
        ErrorOut "Start logging any errors...", errorFilePath
        
        Set swApp = CreateObject("SldWorks.Application")
        swApp.SetCurrentWorkingDirectory destinationPath
        
        ' Determine type of document
        
        openWarn = 0
        
        Dim fileName As String
        fileName = Dir(sourcePath + "\*.SLDDRW")
        
        Do While fileName <> ""
            modelTitle = ""
            Set swModel = swApp.OpenDoc6(sourcePath + "\" + fileName, 3, 1, "", openError, openWarn)
            
            
            If (openWarn And 2) Then
                ErrorOut "File is read-only.", errorFilePath
                ret = False
            ElseIf (openWarn And 4) Then
                ErrorOut "File is locked by another user.", errorFilePath
                ret = False
            ElseIf (openWarn And 512) Then
                ErrorOut "File is view-only.", errorFilePath
                ret = False
            Else
                ret = True
            End If
            
            If Not swModel Is Nothing Then
                modelTitle = swModel.GetTitle()
                If ret = True Then
                        ' Strip off SOLIDWORKS drawing file extension (.slddrw)
                        ' and add DXF file extension (.dxf)
                        Dim sPathName As String
                        sPathName = swModel.GetPathName
                        sPathName = Left(sPathName, Len(sPathName) - 6)
                        sPathName = sPathName + "dxf"
                        sPathName = Replace(sPathName, sourcePath, destinationPath)
                        ' Save as file as DXF
                        bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
                End If
                
                Set swModel = Nothing
                swApp.CloseDoc modelTitle
            End If
    
            
            fileName = Dir
        Loop
              
        ' 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