Code Library
SolidWorks APIVBASolidWorks 2016+ · built-in VBA editorMIT licenseIntermediate

Batch-export sheet-metal flat patterns to DXF

A folder of brackets and enclosures needs to become a folder of DXFs for the laser or the waterjet — and half of them, being non-sheet-metal fittings mixed into the same folder, shouldn't export at all. This macro walks a folder of parts, checks each one for a flat-pattern feature before touching it, exports sheet-metal parts' flat patterns to DXF, and logs everything it skipped instead of guessing.

Before you run it

  • A folder of sheet-metal parts (not assemblies) with a flat-pattern feature
  • SolidWorks type-library references ticked in the VBA editor

The code

GitHub
Option Explicit

' Batch-export the flat pattern of every sheet-metal part in a folder to DXF.
' Edit FOLDER, then run main(). Non-sheet-metal parts are skipped and logged.

Const FOLDER As String = "C:\SheetMetal\"   ' must end with a backslash

Sub main()
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks

    Dim fileName As String
    fileName = Dir$(FOLDER & "*.sldprt")
    Dim exported As Long, skipped As Long, failed As Long

    Do While fileName <> ""
        Dim errs As Long, warns As Long
        Dim swModel As SldWorks.ModelDoc2
        Set swModel = swApp.OpenDoc6(FOLDER & fileName, swDocPART, _
            swOpenDocOptions_Silent, "", errs, warns)

        If Not swModel Is Nothing Then
            Dim swPart As SldWorks.PartDoc
            Set swPart = swModel

            ' A part with no sheet-metal feature has no flat pattern to export
            If Not swModel.FeatureByName("Flat-Pattern") Is Nothing Then
                Dim dxfPath As String
                dxfPath = FOLDER & Left$(fileName, Len(fileName) - 7) & ".dxf"

                If swPart.ExportFlatPatternView(dxfPath, 0) Then
                    exported = exported + 1
                Else
                    failed = failed + 1
                    Debug.Print "EXPORT FAILED: " & fileName
                End If
            Else
                skipped = skipped + 1
                Debug.Print "NOT SHEET METAL (no Flat-Pattern feature): " & fileName
            End If
            swApp.CloseDoc swModel.GetTitle
        Else
            failed = failed + 1
            Debug.Print "COULD NOT OPEN: " & fileName
        End If

        fileName = Dir$()
    Loop

    swApp.SendMsgToUser exported & " DXF(s) exported, " & skipped & _
        " skipped (not sheet metal), " & failed & " failed." & vbNewLine & _
        "Details are in the Immediate window (Ctrl+G)."
End Sub

What you get

What you get

C:\SheetMetal\
├── Bracket_100.SLDPRT
├── Bracket_100.dxf        (new)
├── MountPlate.SLDPRT
├── MountPlate.dxf         (new)
├── Fitting_22.SLDPRT      <- not sheet metal, skipped
└── ...

2 DXF(s) exported, 1 skipped (not sheet metal), 0 failed.
Details are in the Immediate window (Ctrl+G).

How it works

  • Dir$(FOLDER & "*.sldprt") walks the folder exactly like the batch-PDF macro — same zero-dependency pattern, different target format.
  • FeatureByName("Flat-Pattern") is the cheap way to ask "is this actually sheet metal?" without inspecting feature types one by one — a non-sheet-metal part returns Nothing and gets skipped instead of force-exported into garbage.
  • ExportFlatPatternView is the dedicated API for this job: it flattens and exports in one call, instead of manually switching to a flat-pattern configuration and doing a generic SaveAs.
  • Three counters — exported, skipped, failed — because "not sheet metal" and "broke during export" are different problems on a big batch and deserve different follow-up.

Gotchas & honest limits

  • Assumes the flat-pattern feature is still named "Flat-Pattern" (SolidWorks's English-install default). If someone renamed it, the check falls through to "skipped" rather than crashing, but that file needs a manual look.
  • ExportFlatPatternView's second argument controls bend-line/annotation layers via swExportFlatPatternViewOptions_e; this macro passes 0 for the plain default — check your API reference if you want bend lines split onto their own DXF layer.
  • The flat pattern exports as last computed — a part with an unresolved or broken flat pattern (a bend that won't unfold) can export empty or wrong. Spot-check before trusting a big batch.
  • DXF layer names and units follow Tools → Options → Export → DXF/DWG, same as a manual Save As — set that once for your shop before batch-running.

Goes deeper

Want this adapted to your shop — or built into a real tool?

Samples are the free 80%. The last 20% is the part I do for a living.

Get in touch
Home
Blog
Tools
Code
Email
LinkedIn
Résumé