Home > Macro > Workgroup PDM Macro 4 – Export PDFs of Drawings in a Project

Workgroup PDM Macro 4 – Export PDFs of Drawings in a Project

September 23rd, 2010

Again in this series of Workgroup PDM Macros, this one builds upon the previous. It’s actually nearly identical, but instead of going “doc.Save”, we go “doc.SaveAsPDF”.

The Macro

This macro saves/exports the PDF of all Drawings in a Project in the PDM Vault onto a local computer.

' Title of Macro: Save/Export PDFs from all Drawings in a Workgroup PDM Project
' Further info: http://solidworks.burkesys.com/category/macro/
' Author: Stephen Burke | www.sburke.eu
' Version: SolidWorks 2010
' Add in "SolidWorks Workgroup PDM 1.0 Type Library" under: Tools - References
' Date: 23/09/2010

Option Explicit
Dim swApp As Object
Sub main()
    Dim connection As PDMWConnection
    Set connection = CreateObject("PDMWorks.PDMWConnection")
    connection.Login "pdmwadmin", "pdmwadmin", "localhost"
    'Enter your PDM Login details above, "username", "password", "pdmserver"

    Dim alldocs As PDMWDocuments
    Dim doc As PDMWDocument
    Dim project
    Dim item
    Dim msg
    Dim filename
    Set alldocs = connection.Documents
 
    Dim options As PDMWorks.PDMWSearchOptions
    Dim criteria As PDMWorks.PDMWSearchCriteria
    Dim results As PDMWorks.PDMWSearchResults
    Dim result As PDMWorks.PDMWSearchResult
    Dim i
    Dim cnt
 
    Set options = connection.GetSearchOptionsObject
    options.SearchOnlyChildrenOf = "Project1"
    ' ^ Type in the name of the Project you wish to save the files out of.

    options.SearchCriteria.AddCriteria pdmwOr, pdmwDocumentName, "All", pdmwContains, "slddrw"
    ' ^ set the search query: look for "All" (everything/*) in slddrw.

    Set results = connection.Search(options)
    cnt = results.Count
 
    For i = 0 To cnt - 1
        Set result = results(i)
        'msg = MsgBox(result.Name, vbCritical)
        Set doc = alldocs(result.Name)
        doc.SaveAsPDF "C:\Temp\"
 
        'filename = "C:\Temp\" & Replace(result.Name, ".SLDDRW", " ") & result.Revision & ".pdf"
        'If Dir(filename) <> "" Then Kill filename
        'Name "C:\Temp\" & result.Name & ".pdf" As filename
        ''Uncomment the above 3 lines. This will output the file "drawing 01.pdf", where 01 is the current revision of the drawing.
    Next i
 
    connection.Logout
End Sub

Make sure to scroll to the right in the code area above ^. Link to Macro Download

Note: The default filename which will be created is “filename.SLDDRW.pdf”. I read on the SolidWorks Forums of a person requesting an output file name: “filename 01.pdf” where 01 is the current revision. This functionality is in the above code. Uncomment the 3 lines underneath “doc.SaveAsPDF” (do so by removing the single quotation mark ‘ ). This is quite nice, because if this macro was being run by the Task Scheduler every night, it would be evident from looking at C:\Temp (or outbox folder specified) that  a new revision is available.

Readme

New in SolidWorks 2008 was the option to automatically have PDM create a PDF for every drawing upon check-in. This macro saves/exports this PDF, and is much quicker than if we were to re-print a PDF. If however, the option to create PDFs on check-in is not set in the Vault Admin Tool, this Macro will not work. See image below for where this option is within the VaultAdmin Tool.

The setting to create PDFs must be set in the Vault Admin Tool.

If this setting was not turned on, and you just enabled it, this macro still won’t work. The PDF only gets created upon check-in of drawings after this above setting is enabled. To check to see if a PDF is available for drawings in the Vault, right-click on a Drawing and see if the option “View Document as PDF” is available (see image below). If this method works, then the macro should too. If a drawing does not have a PDF, you may get the error “An unknown filesystem error occurred. (50)”. In this case, set the above option, checkout and checkin the drawing to have the PDF created automatically.

Right-click on a drawing and go "View Document as PDF" to check if the PDF shows.

Cautionary Warning

While macros are very powerful and can perform a lot of tasks in a short space of time, you need to be very careful. Manually, the PDFs exported, need to be checked by hand, individually, to make sure the contents are correct and accurate. A disaster can occur if someone runs the above macro and sends them off to manufacture, but never checks to see if the exported PDFs are correct and accurate. Check and check again. Unfortunately issues for whatever reason can occur, and its not enough just to say “it should have worked, its not my fault, I just ran the macro and emailed the pdfs”. Manual checking of the actions carried out by the macro must be done.

Macro , , ,

  1. March 1st, 2011 at 15:56 | #1

    Stephen,

    Thank you so much for this info! This has given me a great starting place for developing my own Workgroup PDM export macro. I’m adding the functionality to duplicate the vault project structure as folders during the export.

    Keep up the good work. I appreciate it.

    Steve

  2. Anders Lund
    May 6th, 2011 at 09:59 | #2

    Hi,
    This is just the macro I have been looking for. But I do not get it to work. I have put in my username, password and server and name of projekt.
    But on first line after Sub main() I get message :
    User defined type not defined.
    Can you help me, I have no experience in Maro programing.
    Anders

  3. May 6th, 2011 at 14:00 | #3

    @Anders Lund
    Hi Anders,

    There could be a few reasons for the error “User defined type not defined”. It would be best to go through the first pdmworks macro tutorial I created at: http://solidworks.burkesys.com/2010/09/workgroup-pdm-macro-save-single-file/
    On the page there is a “Troubleshooting & FAQ’s” section. Most likely you will have to add in the “SolidWorks Workgroup PDM 1.0 Type Library”. If this still does not work, you would be best to copy and paste into a new blank macro.

  4. ROLAND MAHECHA
    January 3rd, 2012 at 15:12 | #4

    @Steve Takata
    hi,
    did you get to create that folder structure to work from this macro? I really need something like that, would greatly appreciate it. I also want to see if I can make pop up windows to ask what project to work out of. do you if this is available too?

  5. ROLAND MAHECHA
    January 3rd, 2012 at 17:40 | #5

    @sburke
    where is the “SolidWorks Workgroup PDM 1.0 Type Library” added at? Do I need admin access to the vault?

  6. January 3rd, 2012 at 18:10 | #6

    @ROLAND MAHECHA
    For the “SolidWorks Workgroup PDM 1.0 Type Library”:
    When you go Tools -> Macro -> New inside of SolidWorks, the Microsoft Visual Basic editor opens. Under the Tools menu, choose “References”. In the list, scroll down, and tick on the SolidWorks Workgroup PDM 1.0 Type Library.

    For your initial query, people have already adapted and modified this simple macro above, to recreate the whole folder structure and save the PDFs inside a project folder. For details on this see the SolidWorks Forum at: https://forum.solidworks.com/message/264677

  7. February 22nd, 2012 at 10:42 | #7

    Hi there Stephen,

    Could this macro be adapted to save multiple file types to another location? I’m currently in the process of justifying Workgroup PDM at our company. The PDM Viewer our VAR recommended simply doesn’t work (the ‘eDrawings’ type view will only show the part in its ‘active configuration’ at the time it was saved). This has led me to investigate other ideas in order to get people without vault access visibility of our models and associated drawings we create.

    The idea is to have a button on the SW interface which will save the current Part/Assembly/Drawing to its Vault location, but will also save an eDrawing, PDF and .dwg/dxf. to another server. Hitting this button would provide some tick boxes for the file types to be saved and would offer a default file location (but also offers the opportunity to ‘Browse’ to save to another location).

    Do you know of anything available like that? I wish I knew how to program macros myself.

    Yours sincerely,

    Richard

  8. February 22nd, 2012 at 22:39 | #8

    Hi Richard,

    This macro could not be easily adapted for what you require. There is no “doc.SaveAsPDF” equivalent for eDrawings to quickly parse the PDM Vault and extract edrawings of SW Files with their configurations.

    You’d have to create a separate Macro to save the current document (whether be it a slddrw, sldprt, sldasm) to a specified folder.
    You could add this as a Macro button which users would click, after checking-into PDM.
    There are many examples of these Macros which save a PDF, DXF etc. of the current document.
    See:
    http://www.3dcontentcentral.com/macros/181/files/macro.aspx?id=179370
    https://forum.solidworks.com/message/138017

    It would be too complicated to have one macro button, to do the saving and checking into PDM. Leave the check-in into PDM as normal, and just get a separate Macro to Save As the current document in the various formats to a output folder on a Network etc.

    Best of luck,
    Steve

  9. Steve
    June 29th, 2012 at 20:40 | #9

    I’ve got the macro working well, but have 1 issue. The search returns the latest revision of all child documents. On occasion, an older revision is used for particular assemblies. Is there a way to modify the search to return the revision referenced by the project?

  10. July 5th, 2012 at 00:18 | #10

    @Steve: A more accurate description of the search in the above macro would be: “The search returns the latest revision of all documents with slddrw in the filename”.

    If you want to return “referenced” files (at a particular revision), then you need to look at the “links” section of the api. You would also need to specify a top-level assembly for which you want to obtain the referenced parts and sub-assemblies and drawings. See: http://help.solidworks.com/2012/English/api/pdmworksapi/PDMWorks.Interop.pdmworks~PDMWorks.Interop.pdmworks.IPDMWLinks.html and http://help.solidworks.com/2012/English/api/pdmworksapi/PDMWorks.Interop.pdmworks~PDMWorks.Interop.pdmworks.IPDMWLink.html

  11. Matt
    August 6th, 2014 at 18:41 | #11

    After finding this macro very useful and adapting it for other uses (namely exporting all the pdf’s in the vault to a network folder for non-SW users to view), I changed some lines in the original to streamline the program. In my vault it takes a long time to acquire all the documents so i took that line out and instead of:
    Set doc = alldocs(result.name) used Set doc = result.Document.

    I also changed the For loop to a For Each loop (For Each result in results…Next result), eliminating the need for the i and cnt variables and the line “Set result = results(i)”.

    Thanks for the great work.

  1. No trackbacks yet.