Save assembly as eDrawing with all configurations
I’m trying to write a VBA macro to save the active assembly as an eDrawing with all its configurations.
The SolidWorks UI allows to select which configuration to include in the eDrawing when using File → Publish to eDrawings. I taught that the API would allow the same behavior, according to this, but when I tried with the sample code below only the active configurations is exported.
I don’t know if the problem is related to the fact that I’m using the Extension.SaveAs3
method to save… is there another one to mimic the publish command?
' Export the active model as eDrawing
Const OUT_FOLDER As String = "C:\_Export"
Const OUT_NAME As String = "C:\_Export\test.easm"
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As ModelDoc2
Set swModel = swApp.ActiveDoc
' Exit if model is not an assembly
If Not swModel.GetType() = swDocumentTypes_e.swDocASSEMBLY Then
GoTo finally_
End If
' Get model configurations
Dim vConfs As Variant
If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then
vConfs = swModel.GetSheetNames()
Else
vConfs = swModel.GetConfigurationNames()
End If
' Create output directory
CreateDirectories OUT_FOLDER
' * Export file
Dim exportResult As Boolean
Dim errs As Long
Dim warns As Long
Dim AdvOptData As AdvancedSaveAsOptions
Set AdvOptData = swModel.Extension.GetAdvancedSaveAsOptions((1 + 2 + 4))
' Set the Publish to eDrawing options
' https://help.solidworks.com/2018/english/api/swconst/FilePublishToEDrawings.htm
SetExportOption vConfs
' Set the advanced save as options
' https://help.solidworks.com/2022/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IAdvancedSaveAsOptions~ConfigurationsToSave.html?verRedirect=1
AdvOptData.ConfigurationsToSave = vConfs
exportResult = swModel.Extension.SaveAs3(OUT_NAME, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, AdvOptData, errs, warns)
finally_:
End Sub
Sub SetExportOption(vConfig As Variant)
Dim configNames As String
configNames = ""
For i = 0 To UBound(vConfig)
configNames = configNames & CStr(vConfig(i))
If i <> UBound(vConfig) Then
configNames = configNames & vbNewLine
End If
Next
Dim value as String
swApp.SetUserPreferenceStringListValue swUserPreferenceStringListValue_e.swEmodelSelectionList, configNames
End Sub
Sub CreateDirectories(path As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(path) Then
Exit Sub
End If
CreateDirectories FSO.GetParentFolderName(path)
FSO.CreateFolder path
End Sub