Warning: Do Not run this macro with large assembly as it may take very long time to convert whole assy as iges many times
Hello Friends,
I am working on a macro to recursively convert all parts in an assembly & its sub assemblies as iges and to save the same in a specified folder. Below is my macro
Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
Dim swModel As ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If Not swModel Is Nothing Then
If swModel.GetType = swDocASSEMBLY Then
Call Traverse(swModel)
swApp.SendMsgToUser "IGS Creation is Complete"
Else
Call SaveAsIges(swModel)
End If
Else
swApp.SendMsgToUser "Open Any Model First"
End If
End Sub
Private Sub Traverse(swModel As ModelDoc2)
Dim swAssy As AssemblyDoc
Dim swSubModel As ModelDoc2
Dim vComps As Variant
Dim i As Integer
Set swAssy = swModel
vComps = swAssy.GetComponents(True)
For i = 0 To UBound(vComps)
Set swSubModel = vComps(i).GetModelDoc2
If Not swSubModel Is Nothing Then
If swSubModel.GetType = swDocASSEMBLY Then
Call Traverse(swSubModel)
Else
Call SaveAsIges(swSubModel)
End If
End If
Next
End Sub
Private Sub SaveAsIges(swModel As ModelDoc2)
Dim TargetFolder As String
TargetFolder = "C:\Users\sanjeevdesign\Desktop\New Folder\"
Dim DocName As String, FolderName As String
Dim swPart As PartDoc
Dim longstatus As Long, longwarnings As Long
Call GetDocName(swModel, DocName, FolderName)
Set swPart = swApp.OpenDoc6(FolderName + DocName + ".sldprt", swDocPART, swOpenDocOptions_Silent, "", longstatus, longwarnings)
longstatus = swPart.SaveAs3(TargetFolder + DocName + ".IGS", 0, swSaveAsOptions_Silent)
swApp.CloseDoc FolderName + DocName + ".sldprt"
End Sub
Private Sub GetDocName(ByVal swModel As ModelDoc2, DocName As String, FolderName As String)
Dim PathNoExtension As String
Dim TestChar As String
Dim i As Integer
PathNoExtension = Strings.Left(swModel.GetPathName, Strings.Len(swModel.GetPathName) - 7)
For i = 1 To Strings.Len(PathNoExtension)
DocName = Strings.Right(PathNoExtension, i)
TestChar = Strings.Left(DocName, 1)
If TestChar = "\" Then
DocName = Strings.Right(PathNoExtension, i - 1)
Exit For
End If
Next i
FolderName = Strings.Left(PathNoExtension, Strings.Len(PathNoExtension) - Strings.Len(DocName))
End Sub
The problem is that it traverse all the child parts but save the main assy as iges in the name of all child parts. But the same macro works in case of DXF/PDF with suitable changes. Is there anything which I am missing here.
Same macro is also attached
SolidworksApi macros