Created
December 7, 2022 18:24
-
-
Save AmenJlili/ff5a770e320797777b4bf85afa691c57 to your computer and use it in GitHub Desktop.
Print Assembly Components as DXF.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enum SheetMetalOptions_e | |
ExportFlatPatternGeometry = 1 | |
IncludeHiddenEdges = 2 | |
ExportBendLines = 4 | |
IncludeSketches = 8 | |
MergeCoplanarFaces = 16 | |
ExportLibraryFeatures = 32 | |
ExportFormingTools = 64 | |
ExportBoundingBox = 2048 | |
End Enum | |
Dim swApp As SldWorks.SldWorks | |
Dim swAssemblyDoc As AssemblyDoc | |
Dim swcomponent As Component2 | |
Dim vcomponents As Variant | |
Dim processedFiles As New Collection | |
Dim component | |
Sub main() | |
Set swApp = Application.SldWorks | |
Set swAssemblyDoc = swApp.ActiveDoc | |
'make check if the active document | |
vcomponents = swAssemblyDoc.GetComponents(False) | |
For Each component In vcomponents | |
Set swcomponent = component | |
Dim swmodel As ModelDoc2 | |
Set swmodel = swcomponent.GetModelDoc2 | |
If Not swmodel Is Nothing Then | |
If ExistsInCollection(processedFiles, swmodel.GetTitle()) = False Then | |
PrintDXF swmodel | |
processedFiles.Add swmodel.GetTitle(), swmodel.GetTitle() | |
End If | |
End If | |
Next | |
End Sub | |
Function PrintDXF(ByRef swmodel As ModelDoc2) As String | |
If swmodel.GetType() = SwConst.swDocumentTypes_e.swDocPART Then | |
Dim swPart As PartDoc | |
Set swPart = swmodel | |
Dim modelPath As String | |
modelPath = swPart.GetPathName | |
Dim OUT_PATH As String | |
OUT_PATH = Left(modelPath, Len(modelPath) - 6) | |
OUT_PATH = OUT_PATH + "dxf" | |
swmodel.Visible = True | |
If False = swPart.ExportToDWG2(OUT_PATH, modelPath, swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Empty, False, False, SheetMetalOptions_e.ExportFlatPatternGeometry + SheetMetalOptions_e.ExportBendLines, Empty) Then | |
err.Raise vbError, "", "Failed to export flat pattern" | |
End If | |
swmodel.Visible = False | |
End If | |
Debug.Print swmodel.GetPathName() | |
End Function | |
Public Function ExistsInCollection(col As Collection, key As Variant) As Boolean | |
On Error GoTo err | |
ExistsInCollection = True | |
IsObject (col.Item(key)) | |
Exit Function | |
err: | |
ExistsInCollection = False | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment