AnsweredAssumed Answered

Amend Macro to Get Config Name as Drawing Export Filename

Question asked by Oli Sparrow on May 13, 2020
Latest reply on May 13, 2020 by Alex Burnett

Hi all,

I picked up the attached macro for exporting a PDF and IGES of the current drawing sheet in a multisheet document.

By default it names the exports as the filename followed by the sheet number.

I want it to name both the PDF & IGES after either the active model configuration or the sheet name (I already have a working macro that renames all sheets by the active model config, so both results would be the same)

 

I've had a look at the method in API help but just can't get it to work. Any ideas? So I'm assuming where I have "TempFileName = swModel.GetPathName" I instead need "TempFileName = **the active config/sheet name** "

Also, is there a way to make this do the same for every sheet in the drawing without having to run the macro on every sheet individually?

 

Thanks in advance!

Oli

 

Option Explicit

'---DECLARATIONS---'

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swRefModel As SldWorks.ModelDoc2
Dim swSheet As SldWorks.Sheet
Dim swDraw As SldWorks.DrawingDoc
Dim swExportPDFData As SldWorks.ExportPdfData
Dim swView As SldWorks.View
Dim swRefModelbyView As SldWorks.ModelDoc2

Dim IsDrawingFlag As Boolean
Dim TempFileName As String

Dim boolstatus As Long
Dim nErrors As Long
Dim nWarnings As Long

Dim CurrentSheetName As String
Dim swSheetNames As Variant
Dim CurrentSheetNum As Long
Dim i As Integer
Dim SavePDFName As String
Dim SaveIGESName As String

 

'---SUBROUTINE---'

Sub main()

Set swApp = Application.SldWorks

'Get active document
Set swModel = swApp.ActiveDoc

'Display message and terminate if nothing is open
If swModel Is Nothing Then
MsgBox "OPEN SOLIDWORKS DRAWING TO RUN EXPORTER", vbOKOnly
End
End If

'Check file type
Select Case swModel.GetType()
Case swDocPART
IsDrawingFlag = False
Case swDocASSEMBLY
IsDrawingFlag = False
Case swDocDRAWING
IsDrawingFlag = True
End Select

If IsDrawingFlag = False Then
MsgBox "THIS MACRO CAN ONLY BE RUN FROM THE SOLIDWORKS DRAWING", vbOKOnly
End
End If

 

'Get File Path
TempFileName = swModel.GetPathName
'Remove extension,
TempFileName = Left(TempFileName, InStrRev(TempFileName, ".") - 1)

'Get Current Sheet Num
Set swDraw = swModel
Set swSheet = swDraw.GetCurrentSheet
CurrentSheetName = swSheet.GetName
swSheetNames = swDraw.GetSheetNames

For i = 0 To UBound(swSheetNames)
If CurrentSheetName = swSheetNames(i) Then
CurrentSheetNum = i + 1
End If
Next i


'Add sheet num and extension to file name
SavePDFName = TempFileName & "-" & CurrentSheetNum
SavePDFName = SavePDFName & ".PDF"

'Save current sheet as pdf
Set swExportPDFData = swApp.GetExportFileData(1)
swExportPDFData.SetSheets swExportData_ExportCurrentSheet, ""
swExportPDFData.ViewPdfAfterSaving = False

boolstatus = swModel.Extension.SaveAs(SavePDFName, 0, 0, swExportPDFData, nErrors, nWarnings)

'Open part
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
Set swRefModelbyView = swView.ReferencedDocument
Set swRefModel = swApp.ActivateDoc(swRefModelbyView.GetPathName)

Debug.Print swRefModelbyView.GetPathName

'Save as iges
Set swModel = swRefModel
SaveIGESName = TempFileName & "-" & CurrentSheetNum
SaveIGESName = SaveIGESName & ".igs"

Debug.Print SaveIGESName
swModel.SaveAs SaveIGESName

'close part/asm
swApp.CloseDoc swModel.GetTitle

Set swRefModelbyView = Nothing
Set swRefModel = Nothing
Set swModel = Nothing
End Sub

Outcomes