Hello all,
I have a Macro that I found online for saving drawings as PDF files. There are two items that this does that I would like to change but I do not know if these are code problems or SolidWorks settings issues. The code will be attached below and was written by Blue Byte Systems Inc. I have reached out to them with the same questions but have not received a reply as of yet.
Now to my questions. I added appearance colors to my parts and have this color carried through to my drawings. When this Macro it will take my Sheet Metal Flat Pattern to a black line drawing for that view only. I do not have this happen if I used the CutePDF writer that is installed on my computer.
This Macro also creates Bookmarks in the PDF drawing. These are not needed. If someone could help me solve this I would appreciate it.
Code is below;
Dim swApp As Object
' 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 [Blue Byte Systems Inc.](https://bluebyte.biz).
Option Explicit
' --------------------------------------------------------------------------
' Main subroutine to save the active drawing as a PDF file
' --------------------------------------------------------------------------
Sub main()
' Declare and initialize necessary SolidWorks objects
Dim swApp As SldWorks.SldWorks ' SolidWorks application object
Dim swModel As SldWorks.ModelDoc2 ' Active document object (drawing)
Dim swModelDocExt As SldWorks.ModelDocExtension ' Document extension object for saving operations
Dim swExportData As SldWorks.ExportPdfData ' PDF export data object
Dim boolstatus As Boolean ' Boolean status to capture operation results
Dim filename As String ' String to hold the file path of the drawing
Dim lErrors As Long ' Error variable for save operation
Dim lWarnings As Long ' Warning variable for save operation
' Initialize SolidWorks application and get the active document
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' Check if there is an active document open
If swModel Is Nothing Then
MsgBox "No current document. Please open a drawing and try again.", vbCritical, "No Active Document"
End
End If
' Check if the active document is a drawing
If swModel.GetType <> swDocDRAWING Then
MsgBox "This macro only works on drawings. Please open a drawing and try again.", vbCritical, "Invalid Document Type"
End
End If
' Get the document extension object for saving operations
Set swModelDocExt = swModel.Extension
' Get the export PDF data object to specify PDF export options
Set swExportData = swApp.GetExportFileData(swExportPdfData)
' Get the file path of the active drawing
filename = swModel.GetPathName
' Check if the drawing has been saved previously (it must have a valid file path)
If filename = "" Then
MsgBox "Please save the file first and try again.", vbCritical, "File Not Saved"
End
End If
' Generate the PDF file path by changing the file extension to .PDF
filename = Strings.Left(filename, Len(filename) - 6) & "PDF"
' Set the export option to include all sheets in the PDF file
boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)
' Save the drawing as a PDF file using the specified filename and options
boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)
' Check if the save operation was successful and display a message box
If boolstatus Then
MsgBox "Save as PDF successful!" & vbNewLine & filename, vbInformation, "Save Successful"
Else
MsgBox "Save as PDF failed. Error code: " & lErrors, vbExclamation, "Save Failed"
End If
End Sub
If you (and everyone who uses your macro) never wants bookmarks, even when saving manually from SW, just add that line right after Set swApp = Application.SldWorks
If you want to tweak the setting only during the macro run, then you will want to store the current setting at the start, change it while the macro does its thing, and then restore it at the end
Dim currentSetting as Integer
currentSetting = swApp.GetUserPreferenceToggle(swPdfIncludeBookmarks)
...
...
...
...
swApp.SetUserPreferenceToggle swPdfIncludeBookmarks, currentSetting
The macro isnât doing anything more than performing a Save As. Have you tried doing a Save As PDF manually from inside SW to see if the problem occurs there as well?
M12241 00104C.PDF (84.8 KB) M12241 00104Ca.PDF (84.8 KB) M12241 00104C.SLDDRW (255.8 KB) M12241 00104C.SLDPRT (192.5 KB)
Jim,
I had not tried that until you asked. I get the same results either way. I have attached a PDF file created both ways showing the same result. The PDF that ends in a was done as a save as the other one with the Macro. I have also attached a drawing and a part file to this. I was not able to attach the assembly file. I expect views to be empty if you try to open the drawing. I am applying colors at the part level. I do not know why I am only having the color issue with Flat Pattern views. I do not know what setting may need to be changed.
I can replicate the issue. Interestingly, it may have nothing to do with the fact that itâs a flat pattern. Rather, it has to do with the fact that your drawing includes views of the assembly model and of a part model from the assembly. The problem goes away if you eliminate the part model and have an assembly configuration that shows the flat pattern. Unfortunately, you then donât get the bend lines. Iâm out of time for today, but Iâll look a little more closely tomorrow.
Mr. Perman,
I will look into this. How did you get the table at the bottom of the help page you linked me to. Is that part of future version help files. Currently running 2020 here due to lack of computer upgrades. Second work station we had could not handle anything newer.
Jim,
I appreciate the time you have spent on this for me. I just tested your observation with a part that has no assembly file associated with it, it was jus a sheet metal part with a single bend and I had the same result. The flat pattern view changed from red in the SolidWorks Drawing to black in the saved PDF file.
Bug/conflict with the setting you are using âUse model color for HLR/HLV in drawingsâ. Seems to ignore the sheet metal specific color option in the above pic but does honor it on export to PDF.
We always use the same color for model edges (black), otherwise you would have to be very selective in what colors you use in your model so that it contrasts with the sheet background color.
Alex,
I had been doing a print to PDF using Cute PDF writer. I found the Macro and used it a couple times before I realized it had the issues I am trying to get resolved.
I appreciate the assistance from those that offered. I sent this to my VAR to see what they had to say. The response from them was to change the Display Style for that view to Draft Quality. I did that and Save As works like I want it to.
Do yourself a favor and purchase SharpTask. It has prebuilt macros for various SolidWorks tasks including saving files to PDF. It works way better than Task Scheduler.