Jan 23, 2012

One Button PDFs in SolidWorks

Welcome to the beginning of SolidAPI - a blog dedicated to the SolidWorks API.  What better place to start than one of my favorite macros - one button PDF publishing.  I've had the chance to present this material several times at SolidWorks World.  But if you're coming this year and were hoping to re-live the experience - this post will have to suffice.  I'm presenting on the SolidWorks Enterprise PDM API instead.  So here's your chance to brush up on macro basics using VSTA (Visual Studio Tools for Applications) and Visual Basic.

Step #1 – Record it!


Start by opening any SolidWorks file.  Drawings are great, but models work fine too.  Start recording the macro by going to Tools, Macro, Record.
Select File, Save As.  In the Save As dialog, change the save as type to Adobe Acrobat PDF (*.pdf) and save.  That’s all we need to get started. 

Stop and save the macro by selecting Tools, Macro, Stop.  Change the save as type to SW VSTA VB Macro (*.vbproj) and save the macro as AutoPDF.vbproj.
Edit the newly saved macro by selecting Tools, Macro, Edit.  Browse to AutoPDF.vbproj (found under the newly created folders \AutoPDF\SwMacro\).  The important part of the macro is shown here.  Anything else in your recorded macro is not needed.

Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
 
Partial Class SolidWorksMacro
 
    Public Sub main()
        Dim swDoc As ModelDoc2 = Nothing
        Dim longstatus As Integer = 0
        swDoc = CType(swApp.ActiveDoc, ModelDoc2)
        longstatus = swDoc.SaveAs3("C:\...\Drawing.PDF", 0, 0)
    End Sub
 
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
 
End Class

Description


There are really only two lines of important code in Sub main() that do all the work. 

    swDoc = CType(swApp.ActiveDoc, ModelDoc2)

This line connects the variable named swDoc to the SolidWorks application’s active document using swApp.ActiveDoc.
Next, a Save As operation is calle from the active document interface that we’ve captured in the variable swDoc.  The main argument of interest is the first.  It must be a string containing the full path to the desired PDF file.

Code Changes


To make this macro worthwhile, we need to get a PDF path that makes sense.  The macro above would just keep overwriting the same Drawing.pdf file with a new PDF each time the macro was run.  Make the following changes to convert the active document’s current path to PDF.
    Public Sub main()
        Dim swDoc As ModelDoc2 = Nothing
        Dim longstatus As Integer = 0
        swDoc = CType(swApp.ActiveDoc, ModelDoc2)
        'get the current model path
        Dim modelPath As String = swDoc.GetPathName
        Dim pdfName As String
        'use IO.Path class to change the extension
        pdfName = IO.Path.ChangeExtension(modelPath, "pdf")
        'save the PDF
        longstatus = swDoc.SaveAs3(pdfName, 0, 0)
    End Sub

I dig the IO.Path class in .NET!  It makes it easy to manipulate file paths in all kinds of ways.  Changing a file’s extension is as easy as that.  Now the SaveAs3 method uses the newly modified path.

Make a Button in SolidWorks


Build your new macro (create the dlls) by selecting Build, Build AutoPDF.  Once that’s done, close the macro and jump back to SolidWorks.  To add a button to your toolbars or the Command Manager, select Tools, Customize.  Select the Commands tab and choose Macro from the list of command groups.  Drag and drop a new macro button onto any toolbar. 
 


Fill out the form by adding a tool tip and a prompt.  Then browse to the bin folder in your macro directory to find AutoPDF.dll.  Click OK and you’re done!  Now your PDFs are only one button away.

2 comments:

  1. Mike, I bought your book S-Works 2015 using macros. So far I have found it to be enlightening. But with this example my PC will not create a .DLL file whats up?

    ReplyDelete
  2. Mike, I bought your book S-Works 2015 using macros. So far I have found it to be enlightening. But with this example my PC will not create a .DLL file whats up?

    ReplyDelete

Note: Only a member of this blog may post a comment.