pdf & dxf export adding sheet name to filename for multi-sheet

Library for macros
Martin Abbott Design
Posts: 3
Joined: Tue Jul 02, 2024 10:46 am
Answers: 0
x 1

pdf & dxf export adding sheet name to filename for multi-sheet

Unread post by Martin Abbott Design »

Hello,

For years I've been using a macro to export a SW drawing file as in pdf and dxf format in one go.

I use it daily and it's saved me so much time over the years.

I'd love to add an extra level of function to it, whereby any multi-sheet drawings are exported as individual dxf files with the sheet name as a suffix of each file name rather than the default numerical prefix.

I've tried editing the macro to add this, but my knowledge is negligible and have got nowhere.

I'll paste the existing code below, and if anyone can help me achieve what I'm after I'll be extremely grateful.

Thanks,
Martin



'------------------------------------------------------------------

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sPathName As String
Dim nErrors As Long
Dim nWarnings As Long
Dim nRetval As Long
Dim bShowMap As Boolean
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swExportData As SldWorks.ExportPdfData
Dim filename As String
Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long
Dim bRet As Boolean

Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Strip off SolidWorks drawing file extension (.slddrw)
' and add DXF file extension (.dxf)
sPathName = swModel.GetPathName
sPathName = Left(sPathName, Len(sPathName) - 6)
sPathName = sPathName + "dxf"
' Show current settings


' Turn off showing of map
bShowMap = swApp.GetUserPreferenceToggle(swDXFDontShowMap)
Set swModelDocExt = swModel.Extension
Set swExportData = swApp.GetExportFileData(swExportPDFData)
filename = swModel.GetPathName
filename = Strings.Left(filename, Len(filename) - 6) & "PDF"
boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)
boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)
If boolstatus Then
MsgBox "A .PDF and a .DXF have been saved." & vbNewLine
Else
MsgBox "I didn't like that!" & lErrors '
End If
swApp.SetUserPreferenceToggle swDXFDontShowMap, False

bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
If bRet = False Then
nRetval = swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk)
End If
' Restore old setting
swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap
End Sub

'----------------------------------------------
by AlexB » Wed Jul 03, 2024 10:21 am
Martin Abbott Design wrote: Wed Jul 03, 2024 5:52 am Hi AlexB, thanks for the link,

Funnily enough I had been looking at that very page and trying to tweak the above code to pull through the vSheetName parts into my existing macro. It was after many fruitless attempts that actually led me here to get some help from someone who knows what they're doing, as this is apparently beyond my abilities!

Martin
Hi Martin,

See if this does what you're looking for. I added a few lines to your code:

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sPathName As String
Dim nErrors As Long
Dim nWarnings As Long
Dim nRetval As Long
Dim bShowMap As Boolean
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swExportData As SldWorks.ExportPdfData
Dim filename As String
Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long
Dim bRet As Boolean

Dim i As Integer
Dim vSheetNames As Variant
Dim swDraw As DrawingDoc
Dim bSheetExportOption As Integer

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    ' Strip off SolidWorks drawing file extension (.slddrw)
    ' and add DXF file extension (.dxf)
    sPathName = swModel.GetPathName
    sPathName = Left(sPathName, Len(sPathName) - 7)
    ' Show current settings
    
    
    ' Turn off showing of map
    bShowMap = swApp.GetUserPreferenceToggle(swDXFDontShowMap)
    bSheetExportOption = swApp.GetUserPreferenceIntegerValue(swDxfMultiSheetOption)
    
    Set swModelDocExt = swModel.Extension
    Set swExportData = swApp.GetExportFileData(swExportPdfData)
    
    filename = swModel.GetPathName
    filename = Strings.Left(filename, Len(filename) - 6) & "PDF"
    
    boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)
    boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)
    
    swApp.SetUserPreferenceToggle swDXFDontShowMap, True
    swApp.SetUserPreferenceIntegerValue swDxfMultiSheetOption, swDxfActiveSheetOnly
    
    'bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
    
    Set swDraw = swModel
    vSheetNames = swDraw.GetSheetNames
    For i = 0 To UBound(vSheetNames)
        bRet = swDraw.ActivateSheet(vSheetNames(i))
        bRet = swModel.SaveAs4(sPathName & "_" & vSheetNames(i) & ".dxf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
    Next i
    
    If boolstatus And bRet Then
        MsgBox "A .PDF and a .DXF have been saved." & vbNewLine
    Else
        MsgBox "I didn't like that!" & lErrors '
    End If
    
    If bRet = False Then
        nRetval = swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk)
    End If
    ' Restore old setting
    swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap
    swApp.SetUserPreferenceIntegerValue swDxfMultiSheetOption, bSheetExportOption
End Sub
Go to full post
User avatar
AlexB
Posts: 493
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 27
x 265
x 440

Re: pdf & dxf export adding sheet name to filename for multi-sheet

Unread post by AlexB »

There's already a pretty good example on the api help site:
https://help.solidworks.com/2020/englis ... ple_VB.htm
Martin Abbott Design
Posts: 3
Joined: Tue Jul 02, 2024 10:46 am
Answers: 0
x 1

Re: pdf & dxf export adding sheet name to filename for multi-sheet

Unread post by Martin Abbott Design »

Hi AlexB, thanks for the link,

Funnily enough I had been looking at that very page and trying to tweak the above code to pull through the vSheetName parts into my existing macro. It was after many fruitless attempts that actually led me here to get some help from someone who knows what they're doing, as this is apparently beyond my abilities!

Martin
User avatar
AlexB
Posts: 493
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 27
x 265
x 440

Re: pdf & dxf export adding sheet name to filename for multi-sheet

Unread post by AlexB »

Martin Abbott Design wrote: Wed Jul 03, 2024 5:52 am Hi AlexB, thanks for the link,

Funnily enough I had been looking at that very page and trying to tweak the above code to pull through the vSheetName parts into my existing macro. It was after many fruitless attempts that actually led me here to get some help from someone who knows what they're doing, as this is apparently beyond my abilities!

Martin
Hi Martin,

See if this does what you're looking for. I added a few lines to your code:

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sPathName As String
Dim nErrors As Long
Dim nWarnings As Long
Dim nRetval As Long
Dim bShowMap As Boolean
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swExportData As SldWorks.ExportPdfData
Dim filename As String
Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long
Dim bRet As Boolean

Dim i As Integer
Dim vSheetNames As Variant
Dim swDraw As DrawingDoc
Dim bSheetExportOption As Integer

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    ' Strip off SolidWorks drawing file extension (.slddrw)
    ' and add DXF file extension (.dxf)
    sPathName = swModel.GetPathName
    sPathName = Left(sPathName, Len(sPathName) - 7)
    ' Show current settings
    
    
    ' Turn off showing of map
    bShowMap = swApp.GetUserPreferenceToggle(swDXFDontShowMap)
    bSheetExportOption = swApp.GetUserPreferenceIntegerValue(swDxfMultiSheetOption)
    
    Set swModelDocExt = swModel.Extension
    Set swExportData = swApp.GetExportFileData(swExportPdfData)
    
    filename = swModel.GetPathName
    filename = Strings.Left(filename, Len(filename) - 6) & "PDF"
    
    boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)
    boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)
    
    swApp.SetUserPreferenceToggle swDXFDontShowMap, True
    swApp.SetUserPreferenceIntegerValue swDxfMultiSheetOption, swDxfActiveSheetOnly
    
    'bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
    
    Set swDraw = swModel
    vSheetNames = swDraw.GetSheetNames
    For i = 0 To UBound(vSheetNames)
        bRet = swDraw.ActivateSheet(vSheetNames(i))
        bRet = swModel.SaveAs4(sPathName & "_" & vSheetNames(i) & ".dxf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
    Next i
    
    If boolstatus And bRet Then
        MsgBox "A .PDF and a .DXF have been saved." & vbNewLine
    Else
        MsgBox "I didn't like that!" & lErrors '
    End If
    
    If bRet = False Then
        nRetval = swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk)
    End If
    ' Restore old setting
    swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap
    swApp.SetUserPreferenceIntegerValue swDxfMultiSheetOption, bSheetExportOption
End Sub
User avatar
DanPihlaja
Posts: 839
Joined: Thu Mar 11, 2021 9:33 am
Answers: 25
Location: Traverse City, MI
x 804
x 973

Re: pdf & dxf export adding sheet name to filename for multi-sheet

Unread post by DanPihlaja »

I don't know if this setting will affect a macro's output or not...

But what do you have for this setting?
image.png
On mine, that setting saves out the files as
00_filename
01_filename
02_filename
etc...

and it doesn't use sheet name.

So maybe it isn't related.

UU
-Dan Pihlaja
Solidworks 2022 SP4

2 Corinthians 13:14
User avatar
AlexB
Posts: 493
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 27
x 265
x 440

Re: pdf & dxf export adding sheet name to filename for multi-sheet

Unread post by AlexB »

DanPihlaja wrote: Wed Jul 03, 2024 1:35 pm I don't know if this setting will affect a macro's output or not...

But what do you have for this setting?
image.png

On mine, that setting saves out the files as
00_filename
01_filename
02_filename
etc...

and it doesn't use sheet name.

So maybe it isn't related.

UU
It does need to be selected to "save the active sheet only" and I've got that being set in the macro above in order to guarantee it outputs just one sheet at a time. When saving via the API, the filename provided to the save method will be what it's saved as. In this case, "fileName_sheetName". This does require looping through and activating each view before saving though which takes a bit of time, especially for larger drawings.
Martin Abbott Design
Posts: 3
Joined: Tue Jul 02, 2024 10:46 am
Answers: 0
x 1

Re: pdf & dxf export adding sheet name to filename for multi-sheet

Unread post by Martin Abbott Design »

AlexB, that's absolutely perfect!

Thank you so much!

🙏
Post Reply