SolidWorks Tips and Things

Subscribe | Daily Tips | Unsubscribe
SolidWorks Tips Home | 3D iDesign, Inc Home
#1 | #2 | #3 | #4 | #5 | #6 | #7 | #8 | #9 | #10 | #11 | #12 | #13 | #14| #15| #16|
#1 | #2 | #3 | #4 | #5 | #6 |
subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link
subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link

One Button PDF Publishing

API Tip #6    01/25/2007

A fellow SolidWorks user asked me if there was an easy way to publish PDFs faster than doing a Save As operation and specifying the file name and folder.  He wanted PDFs published to the same folder the drawing was opened from using the same name as the drawing.  I thought this would make a good tip since it makes use of saving SolidWorks files in a different format.  It also makes use of some simple string manipulation and using the file path of the active document.  This code can also be easily manipulated to save out any other format such as IGES, DXF or eDrawing.

Initial Code

As usual, this macro is a simple one to build by starting with a recording.

  1. Open a drawing in SolidWorks and start recording a macro by selecting Tools, Macro, Record.
  2. Create a PDF by selecting File, Save As.  From the file type list, select Adobe Portable Document Format (*.pdf).  Use the current directory and default file name and click Save.
  3. Stop the macro recording by selecting Tools, Macro, Stop and save the macro into your SolidWorks installation directory in a folder named "macros".  Name the file SavePDF.swp.  On my computer that is "C:\Program Files\SolidWorks\macros\SavePDF.swp".  Create the macros folder if it does not exist yet.
  4. Edit the new macro by selecting Tools, Macro, Edit and browse to the newly created macro.

Your code should look something like the following.

There are really only three important lines of code in the procedure main().  First, connect to SolidWorks using Application.SldWorks.  Second, connect to the active document with swApp.ActiveDoc.  Finally, save the active document by calling Part.SaveAs2.  For those who are new to object oriented programming, understand that swApp and Part are simply variable names that represent the SolidWorks application object and the ModelDoc2 object respectively.  They have been declared generally by the macro recorder as Objects.

Code Modifications

If you run the recorded macro with another drawing open, it will simply overwrite the original PDF with a PDF of the active document.  However, the name of the PDF will be the same as the drawing you recorded from initially.  Our path and file name are currently hard-coded into the macro.  This might be OK if you intent to copy and rename the new PDF every time you publish one.  But it wouldn't likely pass the test of being even somewhat useful.

  1. Edit the code to match the following.

 

The first modification is to remove the unnecessary variables at the top of the macro and change the swApp and Part variables to use early binding*. 

A couple unnecessary lines of code were also eliminated from the body of the procedure.

The example uses four different variables to manipulate the file path in order to remove the default SolidWorks extension of six characters in length and append the PDF extension.  You could certainly reduce the number of variables and lines of code if you combine the string operations.  I have left them separated in this example for clarity.

The call to Part.GetPathName will get the full path and file name of the ModelDoc2 object.  Since our goal is to have the PDF published to the same directory, we simply modify the extension and we are ready for the SaveAs2 operation.

One last line of code has been added to inform the user that the PDF has been created.  If you are looking to streamline the process, this line of code could be removed.

Believe it or not, that is it. 

  1. Save your changes to the modified macro and close the macro editor.

Create a Button in SolidWorks

Adding a custom button to a standard SolidWorks toolbar is a great way to run a commonly used macro.  You can even add your own bitmap image as an icon on the button you create. 

  1. Open a document in SolidWorks and select Tools, Customize.  Select the Commands tab.  Select Macros from the categories list.
  2. Drag and drop the user defined macro icon onto any toolbar to launch the Customize Macro Button tool.

 

9. Fill out the Customize dialog by browsing to a bitmap for a custom icon, filling out any desired tooltip and prompt, browsing to a macro file and filling in a keyboard shortcut.

10. If you want to modify the settings for the button after creating it, select Tools, Customize and then right-click on the custom macro button.  The Customize Macro Button dialog will appear and you can then make the desired modifications.

That is it!  Now every time you want to publish a PDF, simply click your new macro button.

Mike Spens
mikespens@solidworkstips.com

 

 

 

 

SolidWorks Daily Text Tips

Chat here it stays so you can get updates each time you visit back.


Get your own Chat Box! Go Large!

Click to see how 3D iDesign, Inc can help your company get to market faster. Click Here to view the vast services Engineering needs.
Product Design
2D to 3D
Art to Part
Reverse Engineering
Customer SolidWorks and PDMWorks Programming
Stress Analysis
CNC Programming

Current SolidWorks Service Pack
The current SolidWorks 2009 Service Pack is 1.0
The current SolidWorks 2008 Service Pack is 5.0
The current SolidWorks 2007 Service Pack is SP 5.0
The current SolidWorks 2007 x64 Service Pack is SP5.0
The current SolidWorks 2006 Service Pack is SP5.1
The current SolidWorks 2006 x64 Service Pack is SP5.1
The current PDMWorks 2006 Service Pack is SP5.1
The current SolidWorks 2005 Service Pack is SP5.0
The current PDMWorks 2005 Service Pack is SP5.0

Click here to login and download from SolidWorks Web site

Quick Links SolidWorks
3D iDesign, Inc Engineering Services
Video Cards Testing

Please support this site and donate any amount you would like if this site helped you out.

 

For more books visit here

New book from the author of SolidWorks Tips and Things API Tips Section Mike Spens pick one up today

About Us | Contact Us | ©2007 3D iDesign, Inc ©

SolidWorks and SolidWorks Applications are registered trademarks of SolidWorks Corporation. All other names may be trademarks of their respective owners. SolidWorks Tips and Things is not affiliated with or sanctioned by the SolidWorks Corporation