Instructions
- Preconditions
- Assembly is open.
- Result
- All part components are saved out to specified location with specified file extension.
Files
Save out all part components to specified location
Code
Module: mMain
'Saves out all part components to specified folder with specified extension.
'Preconditions: assembly is open.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Option Explicit
Sub main()
Dim strFileExtension As String
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim i As Integer
Dim vComps As Variant
Dim strNewCompPath As String
Dim strOutputPath As String
Dim lngErr As Long, lngWarn As Long
Dim fso As New FileSystemObject
strOutputPath = InputBox("Enter a directory (e.g., C:\Engineering).")
If strOutputPath = Empty Then Exit Sub
If Right(strOutputPath, 1) <> "\" Then strOutputPath = strOutputPath & "\"
If fso.FolderExists(strOutputPath) = False Then
swApp.SendMsgToUser strOutputPath & " doesn't exist."
Exit Sub
End If
strFileExtension = InputBox("Enter desired file extension. Examples:" & vbCrLf & "x_t" & vbCrLf & "sldprt" & vbCrLf & "jpg")
If strFileExtension = Empty Then Exit Sub
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then Exit Sub
If swModel.GetType <> swDocASSEMBLY Then Exit Sub
Set swAssy = swModel
vComps = swAssy.GetComponents(False)
If IsEmpty(vComps) Then Exit Sub
For i = 0 To UBound(vComps)
Set swComp = vComps(i)
Set swCompModel = swComp.GetModelDoc2
If swCompModel Is Nothing Then
swApp.SendMsgToUser "Cannot process " & swComp.Name2 & vbCrLf & _
"Component might be suppressed or lightweight."
Else
If swCompModel.GetType = swDocPART Then
strNewCompPath = strOutputPath & GetName(swComp.GetPathName, strFileExtension)
If fso.FileExists(strNewCompPath) = False Then
swCompModel.Extension.SaveAs strNewCompPath, swSaveAsCurrentVersion, _
swSaveAsOptions_Silent, Nothing, lngErr, lngWarn
If lngErr <> 0 Then
swApp.SendMsgToUser "Saving " & strNewCompPath & " failed with error code " & lngErr
End If
End If
End If
End If
Next i
End Sub
Private Function GetName(strFilePath As String, strFileExtension As String) As String
strFilePath = Right(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
GetName = Left(strFilePath, Len(strFilePath) - 6) & strFileExtension
End Function