nyptus

Save As Macro - SOLIDWORKS

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