|
|
API Tip #6 01/25/2007A 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 CodeAs usual, this macro is a simple one to build by starting with a recording. 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 ModificationsIf 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.
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.
Create a Button in SolidWorksAdding 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.
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
|
Related Items |





Comments
please reply at mechbull11
Thanks
Quoting Devdas Malekar:
Thanks
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!