How to name a pdf file by the solidworks part configuration name

Hello

I created a macro that allows me to save a step file with the name of the configuration of the part to save. It works very well but I want to do the same thing with a plan saved in pdf but it doesn't work.

Could someone help me?

Thank you

PS: Here is the macro to save in step format
File Save As STEP.swp (41 KB)

Hello @lucas.bc.41

Happy to see you on the forum and to welcome you.
I hope that we can help you solve some problems, but also that we can benefit from your knowledge and experience.

Given the holidays, you may have to wait a little longer to have the complete answer to your request :slightly_smiling_face:

Kind regards

1 Like

Hello
While waiting for the macro experts to show up, have you searched the database (search bar at the top)? There are dozens of topics regarding your needs. That might answer your question.

1 Like

Above all, there is a lack of information: how is the drawing done? One sheet per configuration?
A single drawing and you want to change the configuration of the drawing model configuration by configuration and export the MEP with the name of the configuration?
For a MEP it's not as simple as exporting a 3D model.
You will therefore have to explain your wish more clearly, or propose a file for testing.

1 Like

I made a single drawing that changes according to the selected configuration, I think it's complicated to create a loop to save a pdf file for each of the configurations so first I just want to save the active configuration. After if anyone knows
how to make a loop would be great!

To export a drawing to pdf: (Save SOLIDWORKS Drawing to PDF Macro to automate the process)

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim strFilename As String
Dim status As Boolean
Dim errors As Long, warnings As Long

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

'Save
status = swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, errors, warnings)

'Export to PDF if it is a drawing
If (swModel.GetType = swDocDRAWING) Then

'Ici la variable strFilename à modifier pour récupérer le nom de la configuration active de la MEP
strFilename = swModel.GetPathName
strFilename = Left(strFilename, Len(strFilename) - 6) & "pdf"
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs strFilename, 0, 0, swExportPDFData, 0, 0

End If

End Sub

To retrieve the active configuration of an MEP:
https://help.solidworks.com/2016/English/api/sldworksapi/Get_Configurations_Referenced_in_View_Example_VB.htm

Then it remains to loop on each config and loop on each view of the MEP and to change the configuration reference at each view for the new desired config.

5 Likes

@sbadenis

You're too strong! Simply :star_struck:

Kind regards

Thank you sbadenis!

I combined the two macros you gave me but I can't get the desired result. My macro debugs the line "Set swDraw = swModel" and tells me that Swdraw = Nothing.
I don't understand why? Here's my modified macro.

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim strFilename As String
Dim status As Boolean
Dim errors As Long, warnings As Long
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim bRet As Boolean
Dim swconfig As String

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

Debug.Print "File = " & swModel.GetPathName

' First drawing view is actually the first sheet,
' so skip getting model name and configuration from
' the sheet
Set swView = swDraw.GetFirstView
' Get first drawing view in first sheet
Set swView = swView.GetNextView
Do While Not swView Is Nothing
Debug.Print " View = " + swView.Name
Debug.Print " Model = " + swView.GetReferencedModelName
Debug.Print " Config = " + swView.ReferencedConfiguration

' Récupérez le nom de la configuration active de la vue en cours de traitement
swconfig = swView.ReferencedConfiguration

Set swView = swView.GetNextView
Loop
'Save
status = swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, errors, warnings)

'Export to PDF if it is a drawing
If (swModel.GetType = swDocDRAWING) Then
• Generate the name of the PDF file using the name of the active configuration
strFilename = swModel.GetPathName
strFilename = Left(strFilename, Len(strFilename) - 6) & swconfig & ".pdf"
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs strFilename, 0, 0, swExportPDFData, 0, 0
End If

End Sub

If anyone knows where the problem is, I'd be delighted!

PS: I've been starting in Macro since last week, I don't have all the basics yet

Hello

Stupid question but you never know, is there really a file open? And is it really a drawing?

2 Likes

Hello

No problem with the line indicated if a drawing document is open, as indicated by Cyril.f.
To make the macro functional, I made only one modification:
In terms of saving in PDF format, you must remove the spaces in the file name extension: ".pdf" instead of ".pdf"...
Solidworks uses this extension to recognize the expected format, here pdf.

Line 43:
strFilename = Left(strFilename, Len(strFilename) - 6) & swconfig & ".pdf"

And beware of the quotation marks in the text editor of the myCAD forum, they are interpreted in a variable way depending on what they frame...

Kind regards.

1 Like

Indeed, everything is fine! I had a room and an open plan maybe for that...
All I have to do is make the loop, but I don't know if it's possible because the scale has to be adjusted according to the size of the room

Otherwise I found this macro that creates each drawing from an MEP:

This can help you move forward but for scaling it's more complicated:

If you manage to combine all this into a macro, it means that you will have made good progress in your progress.
On the other hand, it starts to this very complicated.

2 Likes

Oh yes, it's starting to look great! Thank you for all this, I'll see what I can do