Share this Tip on Facebook

24

Jan

2007

SolidWorks Macro - One Button PDF Publishing Print

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.

 

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.

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

HUNDREDS MORE VIDEOS AND TIPS IN THE MEMBERS AREA.  SIGN UP HERE.

 

 

For everything you need to know about Automating SolidWorks Using Macros.  Click here to check out the book.

 

Comments 

 
0 #1 Devdas Malekar 2010-06-24 07:06
Can we do this in catia???? any help in this matter will be highly appreciated....
please reply at .
Thanks
Quote
 
 
-1 #2 Neil Custard 2010-07-09 16:31
I did find this page with the same question and it looks like a good answer on how to get started. http://www.coe.org/Collaboration/DiscussionForum/ActiveDiscussions/tabid/210/aff/10/aft/91922/afv/topic/Default.aspx560.

Quoting Devdas Malekar:
Can we do this in catia???? any help in this matter will be highly appreciated....
please reply at .
Thanks
Quote
 
 
0 #3 Jordi 2010-12-03 01:56
How could I save all my drawings to a specific folder and keep their name?

Thanks
Quote
 
 
0 #4 Jeff 2010-12-06 09:50
I am trying to use this script to create a pdf file - whenit runs the file changes to a [read only] file - any idea why - if I close the file and reopen it - the read only is gone - what is making it look like a read only file when it compares itself to the pdmworks vault/
Quote
 
 
0 #5 Alain 2011-07-12 11:15
Jordi :
I had the same problem... This is the way I solved it...

Path--> Solidworks saves the path as String
(ex: C:\folder\part.slddrw)

Name--> Dir (Path) which removes everything except the part name and the extention (ex:part.slddrw)

Name--> Left(Name, Instr(Name,".")-1) removes everything which is left from "." (ex: part)

There you have your name "isolated". Make the saveas in the folder and that's it.

sub main()

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

Name = Dir(Path)
Name = Left(Name, InStr(Name, ".") - 1)
longstatus = Part.SaveAs3("c:\Your folder\PDF DRAWING\" + Name + ".PDF", 0, 0)
MsgBox ("Operation Completed!")
end sub

Hope it helps!
Quote
 

Add comment


Security code
Refresh

Related Items