I want to improve the code. It would nice to let the code do several dxf one for each configuration of the piece wiyh the name of the file and the configuration. The code is the following one:
Enum SheetMetalOptions_e
None = 0
Geometry = 1
HiddenEdges = 2
BendLines = 4
Sketches = 8
CoplanarFaces = 16
LibraryFeatures = 32
FormingTools = 64
BoundingBox = 2048
End Enum
Sub Main()
' Connect to SolidWorks
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
' Connect to the active model
Dim swModel As ModelDoc2
Set swModel = swApp.ActiveDoc
' Validate a model is open
If swModel Is Nothing Then
swApp.SendMsgToUser2 "Open a part to run this macro", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
' Validate the open model is a part document
If swModel.GetType <> swDocumentTypes_e.swDocPART Then
swApp.SendMsgToUser2 "This macro only runs on part documents", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
Dim swPart As PartDoc
Set swPart = swModel
' Get the file path
Dim filePath As String
filePath = swModel.GetPathName 'WARNING: this will be an empty string if the part document has not been saved
' Validate the file has been saved
If filePath = "" Then
swApp.SendMsgToUser2 "Save the part document before running this macro", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
' Get the configurations
Dim swConfigMgr As ConfigurationManager
Set swConfigMgr = swModel.ConfigurationManager
Dim configNames As Variant
configNames = swConfigMgr.GetConfigurationNames
' Define sheet metal information to export
Dim sheetMetalOptions As SheetMetalOptions_e
sheetMetalOptions = Geometry Or HiddenEdges Or BendLines
' Loop through each configuration and export to DXF
Dim i As Integer
For i = LBound(configNames) To UBound(configNames)
Dim configName As String
configName = configNames(i)
swConfigMgr.ActiveConfiguration = configName
' Build the new file path
Dim pathNoExtension As String
Dim newFilePath As String
pathNoExtension = Left(filePath, Len(filePath) - 6) 'WARNING: this assumes the file extension is 6 characters (sldprt)
newFilePath = pathNoExtension & "_" & configName & ".dxf"
' Export the DXF
Dim success As Boolean
success = swPart.ExportToDWG2(newFilePath, filePath, swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Nothing, False, False, 0, Nothing)
' Report success or failure to the user
If success Then
swApp.SendMsgToUser2 "The DXF for configuration " & configName & " was exported successfully", swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk
Else
swApp.SendMsgToUser2 "Failed to export the DXF for configuration " & configName, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
End If
Next i
End Sub