Advertisement
nyptus

Save All Part Configurations - SOLIDWORKS

Dec 22nd, 2024 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 2.41 KB | Spirit | 0 0
  1. Dim swApp As SldWorks.SldWorks
  2. Dim swModel As SldWorks.ModelDoc2
  3. Dim boolstatus As Boolean
  4. Dim IErrors As Long
  5. Dim IWarnings As Long
  6. Dim folderFullPath As String
  7. Dim ConfNameArr As Variant
  8. Dim ConfigName As String
  9.  
  10. Sub main()
  11.  
  12. Set swApp = Application.SldWorks
  13. Set swModel = swApp.ActiveDoc
  14.  
  15. ' check is .SLDPRT is opened
  16. If swModel Is Nothing Then
  17.     MsgBox "[ERROR] No active files found!"
  18.     Exit Sub
  19. End If
  20.  
  21. ' check is active document is .SLDPRT file
  22. If swModel.GetType <> swDocPART Then
  23.     MsgBox "[ERROR] Open .SLDPRT first!"
  24.     Exit Sub
  25. End If
  26.  
  27. ' zooms to fit the model in the graphics area.
  28. swModel.ViewZoomtofit2
  29.  
  30. ' saves the model.
  31. boolstatus = swModel.Save3(swSaveAsOptions_Silent, IErrors, IWarnings)
  32.  
  33. FilePath = swModel.GetPathName ' Get the full path of the part/assembly file
  34. FileTitle = swModel.GetTitle   ' Get the full part name
  35.  
  36. PathSize = Strings.Len(FilePath)    ' Get the length of the full path
  37. TitleSize = Strings.Len(FileTitle)  ' Get the length of the part name
  38. FolderSize = Strings.Len(FilePath) - Strings.Len(FileTitle) ' Get the length of the folder
  39.  
  40. RootFolderName = Strings.Left(FilePath, FolderSize)     ' Truncate to folder name
  41.  
  42. MfFolderPath = RootFolderName & "3mf\"
  43. StlFolderPath = RootFolderName & "stl\"
  44. StepFolderPath = RootFolderName & "step\"
  45.  
  46. 'checking existing of 3mf, step, stl folders
  47. If Dir(MfFolderPath) = "" Then
  48.     'MsgBox "[ERROR] Folders '3mf', 'step', 'stl' not found!"
  49.    MfFolderPath = RootFolderName
  50.     StlFolderPath = RootFolderName
  51.     StepFolderPath = RootFolderName
  52.     'Exit Sub
  53. End If
  54.  
  55. FileName = Strings.Left(FileTitle, Len(FileTitle) - 7)
  56.  
  57. ConfNameArr = swModel.GetConfigurationNames
  58. For i = 0 To UBound(ConfNameArr)
  59.     ConfigName = ConfNameArr(i)
  60.     swModel.ShowConfiguration2 (ConfigName)
  61.     MfFilePath = MfFolderPath & FileName & "_" & ConfigName & ".3mf"
  62.     StlFilePath = StlFolderPath & FileName & "_" & ConfigName & ".stl"
  63.     StepFilePath = StepFolderPath & FileName & "_" & ConfigName & ".step"
  64.     boolstatus = swModel.SaveAs3(MfFilePath, 0, 2)
  65.     boolstatus = swModel.SaveAs3(StlFilePath, 0, 2)
  66.     boolstatus = swModel.SaveAs3(StepFilePath, 0, 2)
  67. Next i
  68.  
  69. MsgBox ("3MF, STEP and STL saved!")
  70.  
  71. 'close the document
  72. 'swApp.CloseDoc FileTitle
  73.  
  74. ' errors
  75. Debug.Print ("Errors as defined in swFileSaveError_e: " & IErrors)
  76.  
  77. ' warnings
  78. Debug.Print ("Warnings as defined in swFileSaveWarning_e: " & IWarnings)
  79.  
  80. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement