DXF Sheet Metal Export Macro Problem

Hello everyone, I wrote a macro to export the unfolded sheet metal parts, everything works well when it is engaged from a part.

I want to make it work even when the active document is a plan (I'm getting there for export step).

The object is, in the case where the starting point is a plan, to open/activate the part of the same name and then to export it via ExportToDWG2.

But I get a "property or method not handled by this object" error in this context, ideas?

(however, after opening the part, MsgBox ("The active document is: " & swApp.ActiveDoc.GetTitle) tells me that the part is active)

---------------------------------CONTEXT CHECK AND LAUNCH-------------------------

Sub dxfexport()

If Not swDOC Is Nothing Then
If swDOC.GetType = swDocDRAWING Then
If fso. FileExists(PART_PATH) Then
Call openpart
Call dxf
Call closepart

    Else: MsgBox ("Pièce Introuvable")
    End If
ElseIf swDOC.GetType = swDocPART Then
    'openpart
    dxf
End If

Else
MsgBox "Please open a document", vbExclamation + vbOKOnly
End If

End Sub

--------------------------------- OPENING OF THE ROOM----------------------------
Sub openpart()
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

Do
If fso. FileExists(PART_PATH) Then
Set Part = swApp.OpenDoc6(PART_PATH, 1, 0, "", longstatus, longwarnings)
swApp.ActivateDoc3 PART_PATH, False, swRebuildOnActivation_e.swDontRebuildActiveDoc, longstatus
'swApp.ActivateDoc2 PART_PATH, False, longstatus
Set Part = swApp.ActiveDoc
Else: MsgBox ("part not found")

End If
Loop Until Part.GetType = swDocPART

Part.EditRebuild3
Part.ViewZoomtofit2
MsgBox ("The active document is: " & swApp.ActiveDoc.GetTitle)
End Sub

-------------------------------------------EXPORT------------------------------------
Sub dxf()

Dim modelPath As String
Dim longstatus As Long

modelPath = swDOC.GetPathName



Dim OUT_PATH As String
OUT_PATH = EXPORT_PATH & FILE_NAME & Indice + ".DXF"

Dim options As Long
options = 1 Or 2 Or 4 Or 8 Or 16 Or 32

'Codes d'options d'export:
'ExportFlatPatternGeometry = 1
'IncludeHiddenEdges = 2
'ExportBendLines = 4
'IncludeSketches = 8
'MergeCoplanarFaces = 16
'ExportLibraryFeatures = 32
'ExportFormingTools = 64
'ExportBoundingBox = 2048

If modelPath = "" Then
Err.Raise vbError, "", "Enregistrez votre pièce avant export"

Else

If False = swDOC.ExportToDWG2(OUT_PATH, PART_PATH, swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Empty, False, False, options, Empty) Then
    Err.Raise vbError, "", "Erreur d'export DXF"
End If

End If

End Sub

Hello @Clément_Barillot_1 ,

The error is likely due to the ExportToDWG2 method, which is specific to the IPartDoc class, not to ImodelDoc2.
Therefore, the active part document must be assigned to an swPart variable of type SldWorks.PartDoc.

 Dim swPart As SldWorks.PartDoc
        Set swPart = swApp.ActiveDoc
        If False = swPart.ExportToDWG2(OUT_PATH, PART_PATH, swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Empty, False, False, options, Empty) Then
            Err.Raise vbError, "", "Erreur d'export DXF"
        End If
1 Like

THANK YOU! I've been struggling for several days, I even used chatGPT to help me... You are better than " him " for the moment :wink:

Thanks again

Hello
For this to work, you will also have to move the declarations to (before in global) the main function which is "dxfexport()" not "openpart()",
Add the reference, declaration and assignment of the "fso",
Delete the "do" loop that if the fso does not find the file it will result in an infinite loop,
(that's how it worked for me)

In reality it was a mistake on my part in openpart:

Set Part = swApp.ActiveDoc
Instead of
Set swDOC = swApp.ActiveDoc

But dxf() exports on the basis of swDOC...

So inevitably... The principle was good but the variable, not...

Mr. Blt's intervention allowed me to realize this, quite simply...

As for the declarations, I still have duplicates but they are of the Public type & not Dim, when initializing Main()

dxfexport() is not my main function, the full macro exports in DXF, STEP + PDF, whether you are in a room or its plan.

I'll see about the inifinie loop, I haven't had any problem so far but I'll note...

Hello
Let's say that there is less risk there since the file necessarily exists.
On the other hand, I wonder what is the point of this control of the existence of the file since if I understood correctly, either we have the 3D directly open or we open it from the 2D, so it necessarily exists.
Edit: on the other hand, checking that the exported file doesn't already exist would be much more useful (and generating the associated behavior)

Hello
I suspected that other functions exist, but I can only deal with what you have posted :wink:

Hello Cyril
Indeed the 3d surely exists, but not necessarily with the same name, or even more in the specified directory which will start the loop with an infinite message

Uh, no, if it is obtained from a 2D view it can only exist in the defined location unless it is a virtual file of an asm but not the case here to my knowledge.
If I understood correctly, it's SW open with the file in visual, so it must exist.

Which is not the case because in the post he specified the same name as the drawing

1 Like

Accurate, misread

1 Like