nyptus icon

Save All Part Configurations - SOLIDWORKS

nyptus | PRO | 12/22/24 05:48:37 PM UTC (Edited) | 0 ⭐ | 9232 👁️ | Never ⏰ | [macro, Solidworks]
VBScript |

2.41 KB

|

Spirit

|

0 👍

/

0 👎

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim IErrors As Long
Dim IWarnings As Long
Dim folderFullPath As String
Dim ConfNameArr As Variant
Dim ConfigName As String
 
Sub main()
 
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
 
' check is .SLDPRT is opened
If swModel Is Nothing Then
    MsgBox "[ERROR] No active files found!"
    Exit Sub
End If
 
' check is active document is .SLDPRT file
If swModel.GetType <> swDocPART Then
    MsgBox "[ERROR] Open .SLDPRT first!"
    Exit Sub
End If
 
' zooms to fit the model in the graphics area.
swModel.ViewZoomtofit2
 
' saves the model.
boolstatus = swModel.Save3(swSaveAsOptions_Silent, IErrors, IWarnings)
 
FilePath = swModel.GetPathName ' Get the full path of the part/assembly file
FileTitle = swModel.GetTitle   ' Get the full part name
 
PathSize = Strings.Len(FilePath)    ' Get the length of the full path
TitleSize = Strings.Len(FileTitle)  ' Get the length of the part name
FolderSize = Strings.Len(FilePath) - Strings.Len(FileTitle) ' Get the length of the folder
 
RootFolderName = Strings.Left(FilePath, FolderSize)     ' Truncate to folder name
 
MfFolderPath = RootFolderName & "3mf\"
StlFolderPath = RootFolderName & "stl\"
StepFolderPath = RootFolderName & "step\"
 
'checking existing of 3mf, step, stl folders
If Dir(MfFolderPath) = "" Then
    'MsgBox "[ERROR] Folders '3mf', 'step', 'stl' not found!"
    MfFolderPath = RootFolderName
    StlFolderPath = RootFolderName
    StepFolderPath = RootFolderName
    'Exit Sub
End If
 
FileName = Strings.Left(FileTitle, Len(FileTitle) - 7)
 
ConfNameArr = swModel.GetConfigurationNames
For i = 0 To UBound(ConfNameArr)
    ConfigName = ConfNameArr(i)
    swModel.ShowConfiguration2 (ConfigName)
    MfFilePath = MfFolderPath & FileName & "_" & ConfigName & ".3mf"
    StlFilePath = StlFolderPath & FileName & "_" & ConfigName & ".stl"
    StepFilePath = StepFolderPath & FileName & "_" & ConfigName & ".step"
    boolstatus = swModel.SaveAs3(MfFilePath, 0, 2)
    boolstatus = swModel.SaveAs3(StlFilePath, 0, 2)
    boolstatus = swModel.SaveAs3(StepFilePath, 0, 2)
Next i
 
MsgBox ("3MF, STEP and STL saved!")
 
'close the document
'swApp.CloseDoc FileTitle
 
' errors
Debug.Print ("Errors as defined in swFileSaveError_e: " & IErrors)
 
' warnings
Debug.Print ("Warnings as defined in swFileSaveWarning_e: " & IWarnings)
 
End Sub

Comments