Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save As PDF Macro with Custom Properties

Status
Not open for further replies.

Jterry85

Mechanical
Sep 28, 2010
9
Hello,

I'm looking for a Save As PDF Macro that's a little different from the ones I've found.

Here's what I'm looking for:

- Ability to save multiple open drawings as PDF files
- Save in original folder as drawing files
- Automatically name pdf file the name of drawing file AND name it the current revision (ex: saving "Connecting_Bar.slddrw" which happens to be REV2 in the custom part properties to "Connecting_Bar-Rev2.PDF"

I'm using solidworks 08

Below is the macro code I currently use:



Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2

Sub main()

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String

FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"

Part.SaveAs2 NewFilePath, 0, True, fale


End Sub



Anyone have a macro like this?



 
Replies continue below

Recommended for you

Here is a macro that I modified to add the "grab rev" functionality. I also modified it to create a dwg of each sheet. That has been commented out of the code below. It is probably a good starting point for your macro. This code originally came from fcsuper at
Be sure to post your finished code because the ability to saveas pdf "all open drawings" is something that I have wanted to do... but haven't had the time.

Code:
' -----------------------------------------------------------------------------
' Save Drawing as PDF and DXF
' SaveDwgAsPDF.swp - Original functional code by Lee Bell on 02/10/02.
'                    Modified for SolidWorks 2006 by JB on 3/31/06.
'                    Modified by Matthew Lorono ([URL unfurl="true"]http://sw.fcsuper.com)[/URL] on 4/24/06.
'                    Copyright 2006, 2007
'                    Modified by Dustin Biber on 11/11/2008
'                    Based on publically available information found at:
'                      [URL unfurl="true"]http://www.eng-tips.com/viewthread.cfm?qid=151087[/URL]
' Contact:  fcsuper@aol.com (Matthew Lorono)
' This macro is provided as is.  No claims, support, refund, safety net, or
' warranties are expressed or implied.  By using this macro and/or its code in
' any way whatsoever, the user and any entities which the user represents,
' agree to hold the authors free of any and all liability.  Free distribution
' and use of this code in other free works is welcome.  If any portion of
' this code is used in other works, credit to the authors must be placed in
' that work within a user viewable location (e.g., macro header).  All other
' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
' without the expressed written consent by the authors.  Use at your own risk!
' ------------------------------------------------------------------------------
' Description:
' Saves the active drawing or current view of a model or assembly as a PDF and DXF to a specified directory.
' ------------------------------------------------------------------------------
' Version - By Matthew Lorono, Copyright 2006
'           1.00         * Created Macro from various sources at the above
'                          website.
'                        * Added error handling for no docs loaded.
'                        * Modified error handling for failed save.
'                        * Added/modified comments.
'                        * Cleaned up user interface.
'           1.10         * Add nondrawing support
'                        * Add error handling for missing directory
'           1.20         * Add error handling for missing document path
'                        * Add user input/confirmation of save folder
'           1.21         * Expand header to prohibit money based distribution
'                          (such as for-profit or fee based) of this macro.
'           1.22         * Add save status to lower left status bar pane; some
'                          clean up; add detail to description.
'           1.23         * Added DXF functionality.
' ------------------------------------------------------------------------------
Option Explicit

Dim SwApp As SldWorks.SldWorks
Dim Model As SldWorks.ModelDoc2
Dim swModelDocExt       As SldWorks.ModelDocExtension
Dim swExportPDFData As SldWorks.ExportPdfData
Dim MyPathPDF, MyPathDWG, ModName, NewNamePDF, NewNameDWG As String
Dim Rev As String
Dim dPathName As String
'Dim MyPathConfPDF, MyPathConfDWG As String
Dim fso As Object
Dim MBpdf, MBdwg As Boolean
Dim Errs As Long
Dim Warnings As Long
Dim swFrame As SldWorks.Frame
Dim Sheet1, FlatPatternSheet As String
Dim strSheetName(4)     As String
Dim varSheetName        As Variant
Dim i                   As Long



Sub main()

  Set SwApp = Application.SldWorks
  SwApp.Visible = True
  Set Model = SwApp.ActiveDoc
  Set swFrame = SwApp.Frame
  Set swModelDocExt = Model.Extension
  Set swExportPDFData = SwApp.GetExportFileData(1)
      ' Names of the sheets
    'strSheetName(0) = "Sheet1"
    'strSheetName(1) = "Sheet2"
    ' strSheetName(2) = "Sheet3"
    ' strSheetName(3) = "Sheet4"
    
'varSheetName = strSheetName

' Error handler for no document loaded
  If Model Is Nothing Then MsgBox "No document loaded!", vbCritical: End
  
' Use one of the three following options for PDF save location
' Comment out the options with are not used.

' Option 1: Use the current directory
'  MyPath = CurDir

' Option 2: Specify the directory you want to use
  'MyPathPDF = "D:\_DesignFiles\SolidWorks\Temp"
  'MyPathDWG = "D:\_DesignFiles\SolidWorks\Temp"
  
'MyPathPDF = "N:\Engineering\Public\Drawings"
'MyPathDWG = "N:\Engineering\Public\CAM"
   
' Option 3: Use the drawing folder
  MyPathPDF = Left(Model.GetPathName, InStrRev(Model.GetPathName, "\") - 1)
  'MyPathDWG = Left(Model.GetPathName, InStrRev(Model.GetPathName, "\") - 1)
  
' Call correct sub
  If Model.GetType <> 3 Then Call notdrawing
  Call ifdrawing

End Sub

Sub notdrawing()

' Get documnet save path
  dPathName = Model.GetPathName()

' Error handler if no save path
  If ("" = dPathName) Then MsgBox ("This document has not been saved yet"), vbCritical: End

' Set PDF file name
  ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, ".") - 1)

  Call alldoc

End Sub

Sub ifdrawing()

' Set PDF file name
  Rev = Model.CustomInfo2("", "Revision")
  Sheet1 = Model.ActivateSheet("Sheet1")
  ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, " Sheet") - 3) + "_Rev" + Rev
  
  Call alldoc
  
End Sub

Sub alldoc()

' See PDF and DXF file name with extention .pdf or .dwg
  NewNamePDF = ModName & ".pdf"
  
' Get path and user confirmation
  'MyPathConfPDF = InputBox("By Matthew Lorono" & Chr(13) & Chr(13) & "No notification will occur for " & Chr(13) & "successful PDF creation." & Chr(13) & Chr(13) & "Save " & NewNamePDF & " to:", "Confirm PDF Save Path", MyPathPDF)
  'MyPathConfPDF = MyPathPDF & "\" & NewNamePDF
    'If MyPathConfPDF = "" Then MsgBox "Save As PDF cancelled by user.", vbInformation: End
    
' Get path and user confirmation
  'MyPathConfDWG = InputBox("By Matthew Lorono" & Chr(13) & Chr(13) & "No notification will occur for " & Chr(13) & "successful DXF creation." & Chr(13) & Chr(13) & "Save " & NewNameDXF & " to:", "Confirm DXF Save Path", MyPathDXF)
  'MyPathConfDWG = MyPathDWG & "\" & NewNameDWG
    'If MyPathConfDWG = "" Then MsgBox "Save As DWG cancelled by user.", vbInformation: End
  
' Determine if directory exists
  Set fso = CreateObject("Scripting.FileSystemObject")
   If (Not fso.FolderExists(MyPathPDF)) Then MsgBox (MyPathPDF + " does not exist!"), vbCritical: End
   'If (Not fso.FolderExists(MyPathDWG)) Then MsgBox (MyPathDWG + " does not exist!"), vbCritical: End

' PDF Creation
  swFrame.SetStatusBarText "Saving files"
  MBpdf = swExportPDFData.SetSheets(swExportData_ExportAllSheets, varSheetName)
  MBpdf = swModelDocExt.SaveAs(MyPathPDF & "\" & NewNamePDF, 0, 0, swExportPDFData, Errs, Warnings)
  'MBpdf = Model.SaveAs4(MyPathConfPDF & "\" & NewNamePDF, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)
  
  
' DWG Creation
'varSheetName = Model.GetSheetNames

    'For i = 0 To UBound(varSheetName)

        'MBdwg = Model.ActivateSheet(varSheetName(i))
        'NewNameDWG = ModName & "_" & varSheetName(i) & ".dwg"
        'MBdwg = Model.SaveAs4(MyPathDWG & "\" & NewNameDWG, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)

        'Debug.Assert MBdwg

    'Next i

    ' Switch back to first sheet

    'MBdwg = Model.ActivateSheet(varSheetName(0))


' Warnings to user on Error
'  MsgBox "Errors: " & Errs & vbCrLf & "Warnings: " & Warnings
  If Warnings <> 0 Then
     swFrame.SetStatusBarText "Warnings during save"
     MsgBox "There were warnings.  File creation may have failed.  Verify" & Chr(13) & "results and check possible causes.", vbExclamation
     Else
  End If

  If MBpdf = False Then
     swFrame.SetStatusBarText "Save failure"
     MsgBox "PDF creation has failed!  Check Add-ins (if S/W 2005 or older)," & Chr(13) & "available disk space or other possible causes.", vbCritical
     Else
  End If
  
  'If MBdwg = False Then
     'swFrame.SetStatusBarText "Save failure"
     'MsgBox "DWG creation has failed!" & Chr(13) & "Check available disk space or other possible causes.", vbCritical
     'Else
  'End If

Call last

End Sub

Sub last()

  swFrame.SetStatusBarText "Done"
  
' Clear immediate values
  Set Model = Nothing
  Set MyPathPDF = Nothing
  'Set MyPathDWG = Nothing
  Set swFrame = Nothing
  Set fso = Nothing
  
End
End Sub

This macro needs an icon associated to it, so you can start it in the .main module.

-Dustin
Professional Engineer
Pretty good with SolidWorks
 
 http://files.engineering.com/getfile.aspx?folder=ad1789d4-4307-40de-8985-68b7f2d45f8a&file=SaveDwgAsPDF.bmp
I should have prefaced that I know very little about programming!

But I assume what I have to come up with goes something like:

1. Dim Rev As String
2. Setting "Rev" variable to equal the Revision custom property
3. Including the "Rev" variable in the FilePath = Part.GetPathName line of code?

Whats the specific line of code to grab the revision custom property from a part file?
 
In the code above:

Code:
Rev = Model.CustomInfo2("", "Revision")

grabs the revision value and assigns it to the Rev variable. The variable is then used later to define the filename.

Use the code provided as a starting point... don't re-invent the wheel... Also walk through the code to try to discern the logic within it.

-Dustin
Professional Engineer
Pretty good with SolidWorks
 
But from Adobe and AutoDesk... surely that is acceptable.

-Dustin
Professional Engineer
Pretty good with SolidWorks
 
I appreciate all the replies but I wasn't kidding in saying I don't know how to program. So the likelihood of me "adding" the grab rev function is nonexistent :)

ShaggyPE: Your code mainly does what I'm looking for but I get an error in the line:

swFrame.SetStatusBarText "Done"

 
There may be a reference issue due to simply copying and pasting the text into the code body. See the attached file for the actual .swb file. Also, did you create a button and start it in the xxxx.main module?

-Dustin
Professional Engineer
Pretty good with SolidWorks
 
 http://files.engineering.com/getfile.aspx?folder=4286b1b5-2309-4311-a5f4-5060a04446f8&file=SaveAsPDF_Rev.swp
Also, what version of SolidWorks are you using? If I recall correctly, this code was generated using SW2007. Anything later should work fine, something older might error out.

-Dustin
Professional Engineer
Pretty good with SolidWorks
 
I did not associate the macro with any icons.

I'm using 2008
 
I did not associate the macro with any icons.

That is definitely at least part of the problem.

1. Put the macro file someplace safe
2. Download one of the .bmp files attached in this thread... put it in the same place
3. With a file open in SolidWorks, go to tools> customize> commands> macro> and drag the "new macro button" to where you want the button. I put mine next to the save disk button.
4. Select "Choose Image" and Browse for your .bmp file
5. Browse for your macro file (.swp or .swb)
6. Hit the drop down in method and select the one that has .main at the end.

Now hit the button.

One issue you may have is the save location. The macro is currently hard coded to save in a particular location. There are instructions within the code to change that to the current working directory.

-Dustin
Professional Engineer
Pretty good with SolidWorks
 
You're killing me Matt... lol... I'm a hack, what can I say... I don't know the proper lingo.

Jterry85,
I just confirmed that the current code above actually saves the pdf in the same folder as the original drawing, there is no need to update the code for that functionality.

-Dustin
Professional Engineer
Pretty good with SolidWorks
 
Holy crap I actually got it to work!

Thanks all!

Now ONE little thing I'd like to change if possible.... The file name saves as "revx", is it possible to save it as "RevX"??
 
Ohh actually it doesn't quite work...it pulls the revision from the drawing custom property, and not the part or assembly custom property.

Is it possible to make this change?
 
That adds a new level of complexity to the macro. It will have to dig into one of the views to open the part or assembly model and then grab the revision from that... now we are a bit over my head...


-Dustin
Professional Engineer
Pretty good with SolidWorks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor