DXF macro export sheet by sheet

I don't know anything about macros and I'd like to know if it's possible to make a DXF export macro that exports all the sheets of a drawing in separate files keeping the names of the sheets.

After searching on the forum I found this topic with the macro attached, which is what I'm looking for in principle, only it does PDFs and not DXFs:

https://www.lynkoa.com/forum/solidworks/export-diff%C3%A9renci%C3%A9-en-pdf-et-dxf-des-onglets-de-mise-en-plan-par-une-macro

Could a good soul of the forum modify this macro for me to replace the PDF with the DXF.
Will the macro keep the SolidWorks "save as" options that I use when I do it manually, such as using a projection file?

 

Thank you


pdf_page_par_page.swp

Hello

I replaced ".pdf" with ".dxf" in the macro

Try it should work


dxf_page_par_page.swp

Thank you
But it doesn't work properly, 

My drawing has a view with dimensions for the fab ("Sheet 1" tab) and a unfolded sheet metal for laser cutting (tab "Rep A")

The PDF macro does:

a file: "name of the fichier_Feuille 1.PDF"

a "name of the fichier_Rep A.PDF" file

The DXF macro does:

a file:" 00_nom of fichier_Feuille1.dxf"

a file: "00_nom of the A.dxf fichier_Rep"

a file: "01_nom of fichier_Feuille1.dxf"

a file:"01_nom of the A.dxf fichier_Rep"

Hello

I'm not sure I understand the previous message.

Is this the PDF macro done and the DXF macro should do that we need to understand?

I didn't open the macro but if there was simply a change of file extension on the whole code, it logically only saves in another format.

 

That's right

The "pdf_page_par_page.swp" macro creates 1 file per drawing sheet and includes in the PDF file name the name of the drawing file + the name of the sheet

For example, if the drawing file is called "TOTO" and has 2 sheets, "PLAN"  and "Rep A", the PDF macro does:

1 "TOTO_PLAN" file with only the "PLAN" sheet

1 "TOTO_Rep A" file with only the "Rep A" sheet

That's what I'd like to do with DXF.

With the "dxf_page_par_page.swp" macro there are 4 files

"00_TOTO_PLAN" with only the "PLAN" sheet

"00_TOTO_Rep a" with only the "Rep A" sheet

"01_TOTO_PLAN" with only the "PLAN" sheet

"01_TOTO_Rep a" with only the "Rep A" sheet

Hello

With the macro used you need to modify the export options of the dxf/dwg formats in the solidworks options:

Kind regards

Hello

The result is the same, I still have 4 files at the lie of 2

Unlike PDFs

Hello

For my part, I am set to "Export all sheets in a file" and I have no problem.

Only if the setting is "Export all sheets in separate files" the export generates 4 files.

What I have also noticed is that you have to force the change of settings twice for it to be taken into account correctly (probably SW2020 bug).

SW had not taken into account the "Export all sheets to a file" setting

I do have 2 files with the right syntax but it's the same sheet in the 2, the one that is "active" on the screen.

Looking at the macro, there are several times "swExpPdfData" and "SldWorks.ExportPdfData"

and a line  outFile = outFile & IIf(INCLUDE_DRAWING_NAME, drawName & "_", "") & sheetName & ".dxf" or it's ".pdf" in the PDF macro that works

Is it the  ..... ExpPdfData that should be replaced by the equivalent for DXFs?

Oops, I'm looking in more detail, I didn't open the files.

Re

Normally the code below meets the need

Option Explicit
Dim swApp       As SldWorks.SldWorks
Dim swModel     As SldWorks.ModelDoc2
Dim swDraw      As SldWorks.DrawingDoc
Dim sPathname   As String
Dim vSheetName  As Variant
Dim nErrors     As Long
Dim nWarnings   As Long
Dim i           As Long
Dim bRet        As Boolean
Dim lParam      As Long

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    
    lParam = swApp.GetUserPreferenceIntegerValue(swDxfMultiSheetOption)
    
    'Changement paramétrage export dxf si différent de feuille active
    If lParam <> 0 Then
        bRet = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, swDxfMultisheet_e.swDxfActiveSheetOnly)
    End If
    
    sPathname = swModel.GetPathName
    sPathname = Left(sPathname, Len(sPathname) - 7)
    vSheetName = swDraw.GetSheetNames
    For i = 0 To UBound(vSheetName)
        bRet = swDraw.ActivateSheet(vSheetName(i))
        bRet = swModel.SaveAs4(sPathname & "_" & vSheetName(i) & ".dxf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
    Next i
    ' Retour à la Feuille 1
    bRet = swDraw.ActivateSheet(vSheetName(0))
    ' Remise en place du paramétrage initial
    bRet = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, lParam)
End Sub

 

3 Likes

Thank you
It works perfectly ;-)

Hello
Sorry to dust off the subject, but I tried this macro on SW2025 and it doesn't work.
This is exactly what I am looking to do as well, I do drawing of parts with multiple welded bodies.
Thank you very much
Manu

Hello @Emmanuel_SERVEL and welcome.

To be able to help you, we would need more information:

You say that the macro does not work under Sw2025, what and the error message?
Is your version of Solidworks a Local or 3Dexperience version?
Have you thought about loading Solidworks References into your VBA editor?
image

Note:
The " SaveAs4 " function is considered deprecated in the Solidworks API Help.
strangely it is replaced by " SaveAs3 "
and their statements are slightly different:
value = instance. SaveAs4(Name, Version, Options, Errors, Warnings)
and
value = instance. SaveAs3(Name, Version, Options, ExportData, AdvancedSaveAsOptions, Errors, Warnings)

That said, an " obsolete " function is not necessarily unusable...

But here we are attacking a subject that depends on your level in VBA programming.
In your case I'm thinking more of the VBA References to add...

Kind regards.

2 Likes

Hello
Thank you for your feedback, when I went to check the references, the SW boxes were already all checked. I just added Microsoft Scripting Runtime, ran the macro again to try and it seems to work perfectly. I don't know if that was the problem, but in any case I'm satisfied.
Thank you very much
Manu

1 Like

Hello

It's not strangely replaced by SaveAs3, it's just not on the same methods, SaveAs4 is part of the ModelDoc2 and SaveAs3 methods of ModelDocExtension.
In response @Emmanuel_SERVEL , the Microsoft Scripting Runtime reference is normally not needed for the snippet of code I gave. The bug could come from the Left function which sometimes is not recognized, you have to use VBA as a replacement. Left.

1 Like

Thank you @Cyril_f ... Enrichment of my personal culture: :grinning:
(and there's not even sarcasm)

ModelDoc2 :
Represents the main document in SolidWorks (part, assembly, or drawing)

  • Allows access to basic operations on the document: open, save, rebuild, get document type, access configurations, manage views, etc
    So => ModelDoc2:p all the basic operations on the SolidWorks* document.

ModelDocExtension :

Is an extension of ModelDoc2, accessed through the property .Extension of a ModelDoc2* object.

  • Was created to add additional methods and properties when ModelDoc2 reached its capacity limit in terms of the number of methods.

  • Provides access to advanced or less common features, such as: custom property management, mass property calculations, bill of materials (BOM) table management, conversion operations, advanced record management (e.g., SaveAs2)
    So: ModelDocExtension :p advanced or specialized operations that are not available directly in ModelDoc2, accessible through the property .Extension of a ModelDoc2 object

In summary, ModelDocExtension complements ModelDoc2 and provides access to additional functionality needed for macros or advanced automations in SolidWorks

.

3 Likes