US$0.00
0

SOLIDWORKS Macro to Export Named Sketches to DXF Files

Export every renamed sketch in a SOLIDWORKS part to its own 1:1 DXF file with one click.
SOLIDWORKS Macro to Export Named Sketches to DXF Files
Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

Description

  • Exports each renamed sketch in the active part to a separate DXF file at 1:1 scale.
  • Sketches with default names (Sketch1, Sketch2, …) are skipped, so you control what gets exported by renaming sketches.
  • Bodies are hidden during the export and restored afterwards.

System Requirements

  • SOLIDWORKS Version: SOLIDWORKS 2014 or newer
  • Operating System: Windows 10 or later

Pre-Requisites

  • The active document must be a saved part.
  • A default drawing template must be set in Tools > Options > Default Templates.
  • Rename the sketches you want to export (for example “Laser Profile”).

Results

  • One DXF per renamed sketch, saved next to the part as PartName SketchName.DXF.
  • A message shows how many sketches were exported.

Steps to Set Up the Macro

  1. Open SOLIDWORKS and the document described in the pre-requisites.
  1. Create the macro
  • Go to Tools > Macro > New, give the .swp file a name and save it. The VBA editor opens.
  • Replace everything in the module with the code below.
  1. Run the macro
  • Press F5 in the VBA editor, or use Tools > Macro > Run.
  • For daily use, assign it to a toolbar button or keyboard shortcut via Tools > Customize.

VBA Macro Code

' Disclaimer:
' The code provided should be used at your own risk.
' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code.
' For more information, visit https://bluebyte.biz
' Reviewed and updated by Blue Byte Systems Inc.

Option Explicit

' Exports every renamed sketch in the active part to its own 1:1 DXF file.
' Sketches that still have a default name ("Sketch1", "Sketch2", ...) are skipped.
' DXF files are saved next to the part as "<PartName> <SketchName>.DXF".

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim swDraw As SldWorks.DrawingDoc
    Dim swSheet As SldWorks.Sheet
    Dim swDrawModel As SldWorks.ModelDoc2
    Dim vBodies As Variant
    Dim vFeats As Variant
    Dim vItem As Variant
    Dim swFeat As SldWorks.Feature
    Dim templatePath As String
    Dim partPath As String
    Dim outFolder As String
    Dim baseName As String
    Dim outFile As String
    Dim errors As Long
    Dim warnings As Long
    Dim exported As Long

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    ' Validate the active document
    If swModel Is Nothing Then
        MsgBox "Open a part before running this macro.", vbExclamation
        Exit Sub
    End If
    If swModel.GetType <> swDocPART Then
        MsgBox "The active document must be a part.", vbExclamation
        Exit Sub
    End If
    partPath = swModel.GetPathName
    If partPath = "" Then
        MsgBox "Save the part first. The DXF files are written to the part's folder.", vbExclamation
        Exit Sub
    End If

    templatePath = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)
    If templatePath = "" Then
        MsgBox "No default drawing template is set in Tools > Options > Default Templates.", vbExclamation
        Exit Sub
    End If

    Set swPart = swModel
    outFolder = Left(partPath, InStrRev(partPath, "\"))
    baseName = GetBaseName(partPath)

    ' Hide all solid and surface bodies so only the sketch appears in the view
    vBodies = swPart.GetBodies2(swAllBodies, False)
    If Not IsEmpty(vBodies) Then
        For Each vItem In vBodies
            vItem.HideBody True
        Next
    End If

    ' Hide every sketch first
    vFeats = swModel.FeatureManager.GetFeatures(False)
    If IsEmpty(vFeats) Then GoTo Cleanup
    For Each vItem In vFeats
        Set swFeat = vItem
        If swFeat.GetTypeName2 = "ProfileFeature" Then
            swFeat.Select2 False, -1
            swModel.BlankSketch
        End If
    Next

    ' Show and export each renamed sketch, one at a time
    For Each vItem In vFeats
        Set swFeat = vItem
        If swFeat.GetTypeName2 = "ProfileFeature" And InStr(1, swFeat.Name, "Sketch", vbTextCompare) <> 1 Then
            swFeat.Select2 False, -1
            swModel.UnblankSketch
            swFeat.Select2 False, -1
            swModel.Extension.RunCommand swCommands_NormalTo, ""

            Set swDraw = swApp.NewDocument(templatePath, swDwgPaperBsize, 0.2794, 0.4318)
            If Not swDraw Is Nothing Then
                swDraw.CreateDrawViewFromModelView3 partPath, "Current Model View", 0, 0, 0
                Set swSheet = swDraw.GetCurrentSheet
                swSheet.SetScale 1, 1, True, False

                Set swDrawModel = swDraw
                outFile = outFolder & baseName & " " & swFeat.Name & ".DXF"
                swDrawModel.Extension.SaveAs outFile, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, errors, warnings
                swApp.CloseDoc swDrawModel.GetTitle
                If errors = 0 Then exported = exported + 1
            End If

            swFeat.Select2 False, -1
            swModel.BlankSketch
        End If
    Next

Cleanup:
    ' Restore body visibility
    If Not IsEmpty(vBodies) Then
        For Each vItem In vBodies
            vItem.HideBody False
        Next
    End If
    swModel.ClearSelection2 True

    MsgBox exported & " sketch(es) exported to " & outFolder, vbInformation
End Sub

' Returns the file name without folder and without the last extension
Private Function GetBaseName(ByVal fullPath As String) As String
    Dim fileName As String
    fileName = Mid(fullPath, InStrRev(fullPath, "\") + 1)
    If InStrRev(fileName, ".") > 0 Then fileName = Left(fileName, InStrRev(fileName, ".") - 1)
    GetBaseName = fileName
End Function

Code Review Notes

We reviewed this macro before publishing it here. Changes from the original version:

  • Added checks for a missing, unsaved or non-part document and a missing drawing template.
  • Parts with no bodies no longer crash the macro.
  • File names with extra dots are now handled correctly.
  • Body visibility is restored when the export finishes.

Need to customize the macro?

Contact us and we will adapt this macro to your workflow, or build a complete SOLIDWORKS or PDM automation for your team.

Need this adapted to your workflow?

We build custom SOLIDWORKS macros, add-ins and PDM automation.

Picture of Amen Jlili

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM.

Leave a Comment

0
0
Your Cart
Your cart is emptyReturn to Shop