×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Save as PDF
3

Save as PDF

Save as PDF

(OP)
Is there anyway to link the save as pdf option in the file --> Save As Pulldown to a button or something to make it a little faster to do?

Thanks
Chris Tellers

RE: Save as PDF

Make sure save as pdf is checked in Tools, Add ins.

¿)

RE: Save as PDF

2
I think he means something like -> create macro SaveAsPDF -> rightclick toolbar -> commands -> macro -> new macro button.

Stefan Hamminga
EngIT Solutions
CSWP/Mechanical designer/AI student

RE: Save as PDF

(OP)
Stefan

Not follwoing you.  Could you give me a little more detail?

Thanks
Chris Tellers

RE: Save as PDF

You would want to create a macro that saves the current document to a PDF. You could start with the macro in this faq:

http://www.eng-tips.com/faqs.cfm?fid=530

Modify it to save as pdf instead (see SW API help).

Then you would assign a button to this macro (to run it straight from the toolbar) with the procedure I described.

Add a macro button to your toolbar:

1. know where you saved the macro
2. right click on the toolbar
3. choose 'customize'
4. choose the 'commands' tab
5. choose the 'macro' category
6. drag the most right button (the 'new macro button') to the desired location on the toolbar
7. a box pops up, fill in the required fields

That should do the trick

Stefan Hamminga
EngIT Solutions
CSWP/Mechanical designer/AI student

RE: Save as PDF

(OP)
Stefan

I am having no luck modifying the VBA to get it to work for the pdf.

Chris

RE: Save as PDF

I checked out the VBA in the FAQ.  I've made the following changes for PDF and updating to SW2K6:

1. Change "+" to "&"
2. Eliminate the portion that strips ".sldprt" from modname, since the extension is not returned by GetTitle for a drawing file
3. Add a backslash between MyPath and NewName in the SaveAs line
4. Update to SaveAs4 from SaveAs2
5. Use "Sldworks.Application" instead of CreateObject

CODE

Dim SwApp As SldWorks.SldWorks
Dim Model As SldWorks.ModelDoc2
Dim MyPath, ModName, NewName As String
Dim MB As Boolean
Dim Errs As Long
Dim Warnings As Long

' ********************************************************
' Save As Parasolid.swb - Recorded on 02/10/02 by Lee Bell
' Modified for SW2K6 by JB on 3/31/06
' ********************************************************


Sub main()
 
  Set SwApp = Application.SldWorks
  ' This ensures that there are files loaded in SWX

  Set Model = SwApp.ActiveDoc
  If Model Is Nothing Then
      MB = MsgBox("This routine requires files to be loaded before it is ran" & Chr(13) & _
                  "Please load a file and run the routine again", vbCritical)
      Exit Sub
    End
  End If

' Use the current directory
  MyPath = CurDir
' Or specify the directory you want to use
' Comment out the next line if you want to use the current directory
  'MyPath = "C:\Where\Ever\Directory\You\Want\"

  ModName = Model.GetTitle
  NewName = ModName & ".pdf"
  MsgBox "Saving " & NewName & " To" & Chr(13) & MyPath

  MB = Model.SaveAs4(MyPath & "\" & NewName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Errs, Warnings)
  If Not MB Then MsgBox "failed!"
  Set Model = Nothing
  'SwApp.CloseDoc ModName
End Sub

RE: Save as PDF

(OP)
How can I get the file to save in the same directory as the original file.  What would the code be?

Thanks
Chris Tellers

RE: Save as PDF

(OP)
Disregard previos post.  Looks like it is already in there.

Handleman

I cannot seem to get this Macro to work

Thanks
Chris Tellers

RE: Save as PDF

This macro isn't functioning for me quite right. Specifically, it generates the PDF but doesn't seem to be saving to the directory the file originated in. Additionally it always says "Failed!" at the end even though it succeeded.

Any hints?

RE: Save as PDF

What version of SW are you running?
What kind of error messages are you getting?
Also, it doesn't currently save the file in the same directory as the drawing.  It saves in the SolidWorks current directory, which is basically the directory that you last opened from or saved to.  If you want to ensure that the PDF goes in the same directory, change the line

  MyPath = CurDir

to

MyPath = Left(Model.GetPathName, InStrRev(Model.GetPathName, "\") - 1)

RE: Save as PDF

Yes, I am now getting the "failed!" message as well, even though the file seems to have saved fine.  I didn't get that message when I modified the code originally.  Weird.  As long as it's working you can get rid of that line of code.

RE: Save as PDF

No errors, just get the "failed!" message despite the fact that it is performing fine otherwise. I'm using 2006.

RE: Save as PDF

(OP)
Got it to work, with the failed message.

The only probelm is it does not come up with the dialog box to prompt you on which prints you want to print.

SolidWorks 2006 - SP 3.1

Thanks
Chris Tellers

RE: Save as PDF

It doesn't look like saving an individual sheet of a multi-sheet drawing is an option with the SaveAs4 API.  The macro at cadimensions.com referenced by smcadman appears to be using the old (2k4) Bluebeam add-in for PDF, as it changes registry key

"HKEY_CURRENT_USER\Software\Bluebeam Software\Pushbutton PDF\SolidWorksLt\WhatToPlot"

to toggle between printing single sheets and printing all sheets.  This key does not exist on my machine.  The macro at rhapsodydesignsolutions.com does not appear to give the option of separate files for sheets.

Sorry!

RE: Save as PDF

(OP)
That is OK.

Thanks for all your help.

Chris Tellers

RE: Save as PDF

This macro spontaneously stopped working for me--or possibly gradually as I wasn't here over the weekend. Nevertheless, it worked fine as of handleman's last post and isn't working for me today. I still get the message saying where it is saving but then it never generates the file.

I tried restarting Solid Works as well as disabling and re-enabling the PDF add-in to no effect. It isn't imperative I have this function, but anything that might save me 5 seconds for every single time we export it might save hours on a long enough time line.

RE: Save as PDF

Sorry, I forgot to mention that all my code is confidential and will self-destruct within 36-48 hours, never to work again.

OK, maybe not.  I do feel like that myself sometimes, though.  Even/especially with stuff I write for my own use.  Something screwy always comes up.

Check the value of Errs and Warnings immediately after the save, as in:

MB = SaveAs4.....
MsgBox "Errors: " & Errs & vbcrlf & "Warnings: " & Warnings

If either of these values is not zero then check the API help for their meaning.  

You mentioned disabling/reenabling the PDF add-in.  Did you have that prior to 2k6?  I actually never used the PDF save option until I got 2k6, which was a fresh install on a new box.  I don't have anything listed in my add-ins.  I wonder if there's some sort of conflict between what the old add-in is trying to do and what the now built-in SW functionality is trying to do?

RE: Save as PDF

D'oh, I missed that somehow I had commented out the SaveAs line! I'm not sure when it happened but oops all the same. Either way, maybe this will lead me to learning a new programming language to augment my laziness--er, workload.

Thanks again, handleman--can't give you another star this week though.

RE: Save as PDF

No doubt the people who knew some VBScript before this thread don't need this next info, nor do people who have multiple page drawings for single parts, but I was getting annoyed with the " - Sheet1" being saved at the end so I adapted the macro some more for my own use. Anyways I modified a line to:

CODE

ModName = Left(Model.GetTitle, InStrRev(Model.GetTitle, "Sheet") - 3)

in order to truncate that annoying little bit when passing it on to whomever is machining the part (or at least saving me the trouble of renaming it).

Figured someone else might find it helpful--I also suspect there is a setting in Solidworks that would directly remove that from being added to the filename. I wasn't sure, however, so I took the "easy" way out by doing a dirty job of removing the unwanted characters manually.

RE: Save as PDF

BiPolarMoment,
 That last bit was exactly what I was looking for!!!
Now I had one other thing I would like to accomplish. We add the Revision letter to the pdf file name: "PartnumberRev.pdf". So in our part properties we have a custom property that is "Revision". When pdf'ing out the drawing, I would like to pull that info from the part file and add it to the "getfile" info so it would look like the example I showed above. Any suggestions or ideas???

Thank You

RE: Save as PDF

This code gets the model referenced by the first view found and loads the value of the custom property "Part_Number" into string variable sPartNo.  It's out of a macro unrelated to this one, but you should be able to hammer out what's going on and rename/declare variables as needed.

IDontCare is a "dummy" variable (you can declare it as a Variant) that OpenDoc6 loads with some information that I can't remember what it is and never cared, hence the name.

CODE

    Set swView = swDrawing.GetFirstView
    out = 0
    While swView.GetReferencedModelName = ""
        Set swView = swView.GetNextView
        out = out + 1
        If out = 10 Then Exit Sub
        If swView Is Nothing Then Exit Sub
    Wend


    sRefModelName = swView.GetReferencedModelName       'get name of referenced part
    If swView.IsModelLoaded = False Then
        swView.LoadModel                'load the model if it ain't
    End If
    Set TargetPart = swApp.OpenDoc6(sRefModelName, swDocASSEMBLY, swOpenDocOptions_Silent, "", IDontCare, IDontCare) 'activate the part
    sPartNum = TargetPart.CustomInfo2("", "Part_Number")

RE: Save as PDF

Crap.  I just noticed some poor programming.  Replace the line

If swView.IsModelLoaded = False Then

with

If Not swView.IsModelLoaded Then


It works for me as-is, but the second way is preferred.  At least I'm pretty sure it is.  Maybe someone who knows proper programming can weigh in.

RE: Save as PDF

I get a error: swview=empty

RE: Save as PDF

Sorry, I probably left too much "as an exercise".  You will need to add:

Dim swDocType As Long
Dim sRefModelName As String
Dim swDrawing As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim TempModelName As String
Dim IDontCare As Long
Dim out as Integer
Dim TargetPart as SldWorks.ModelDoc2
Dim sPartNum as String

Then, add a couple of lines to the last bit of code. I've reposted it below with the additional lines.  The additions are the leading IF statement and the line
Set swView = Model


CODE

If swDocType = swDocDRAWING Then
    Set swDrawing = Model
    Set swView = swDrawing.GetFirstView
    out = 0
    While swView.GetReferencedModelName = ""
        Set swView = swView.GetNextView
        out = out + 1
        If out = 10 Then Exit Sub
        If swView Is Nothing Then Exit Sub
    Wend


    sRefModelName = swView.GetReferencedModelName       'get name of referenced part
    If Not swView.IsModelLoaded Then
        swView.LoadModel                'load the model if it ain't
    End If
    Set TargetPart = swApp.OpenDoc6(sRefModelName, swDocASSEMBLY, swOpenDocOptions_Silent, "", IDontCare, IDontCare) 'activate the part
    sPartNum = TargetPart.CustomInfo2("", "Part_Number")
End If

RE: Save as PDF

For me the posted code works great as a macro.  I want to use the code in a VB app (not a VBA macro).  SaveAs4 then returns an error 256 - invalid file extension.  Any ideas on why the code works in a macro environment but not in a VB app?

Here is my test VB 6 code:

Private swApp As SldWorks.SldWorks
Private swMdl As SldWorks.ModelDoc2


Private Sub Command1_Click()
    Dim sPath As String
    Dim sName1 As String, sName2 As String, sName3 As String
    Dim sFile As String, sPDF As String, swExt As String
    
    sPath = "D:\Test1\Solidworks\"
    sName1 = "E00234-0000-D"
    sName2 = "E00235-0000-D"
    sName3 = "E00236-0000-D"
    swExt = ".SLDDRW"
    
    Set swApp = New SldWorks.SldWorks
    
    swApp.Visible = True
    
    For i = 0 To 2
        Select Case i
            Case 0
                sFile = sPath & sName1 & swExt
                sPDF = sPath & sName1 & ".pdf"
            Case 1
                sFile = sPath & sName2 & swExt
                sPDF = sPath & sName2 & ".pdf"
            Case 2
                sFile = sPath & sName3 & swExt
                sPDF = sPath & sName3 & ".pdf"
        End Select
        
        Dim oe As Long, ow As Long
        
        Set swMdl = swApp.OpenDoc6(sFile, 3, 32 Or 1, "", oe, ow)
        
        Dim bRes As Boolean, se As Long, sw As Long
        
        bRes = swMdl.SaveAs4(sPDF, 0, 0, se, sw)
        
        MsgBox "File = " & sFile & vbCr & "PDF = " & sPDF & vbCr & _
                    "Open Error = " & oe & vbCr & "Open Warning = " & ow & vbCr & _
                    "Result = " & bRes & vbCr & "Save Error = " & se & vbCr & _
                    "Save Warning = " & sw
        
    Next
    
    swApp.ExitApp
End Sub


variable se is returning 256 and bRes is returning False when I test in VB 6, but works great in macro.

Thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources