Archive

Archive for September, 2010

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 , , ,

Workgroup PDM Macro 3 – Save out all files & linked files in a Project

September 23rd, 2010

In the previous Workgroup PDM Macro, there was a limitation when saving files in a Project, in that linked files were not saved. That was because “PDMWConnection::DocumentList” listed document names for the specified project, and has no option to include referenced/linked files.

The Macro

This macro saves all files including linked files in a particular Project in the PDM Vault, to the local computer.

' Title of Macro: Save all files & linked files from a Project in Workgroup PDM
' 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
    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 = "Project2"
    ' ^ Type in the name of the Project you wish to save the files out of.

    options.SearchCriteria.AddCriteria pdmwOr, pdmwDocumentName, "All", pdmwContains, "sldprt"
    options.SearchCriteria.AddCriteria pdmwOr, pdmwDocumentName, "All", pdmwContains, "slddrw"
    options.SearchCriteria.AddCriteria pdmwOr, pdmwDocumentName, "All", pdmwContains, "sldasm"
    ' ^ set the search query: look for "All" (everything/*) in sldprt,sldasm,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.Save "C:\Temp\"
    Next i
 
    connection.Logout
End Sub

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

If you are having issues or errors make sure you look at the previous two posts with details on using the PDM API #1, #2.

This macro searches for all files in a particular project. It may not be the most efficient macro, but it works, and provides an excellent building block from which to make more intelligent and useful macros. It opens a lot more possibilities of been able to search the Workgroup PDM Vault in a scripted manner and carry out various operations such as saving out files. Again, the SolidWorks Task Scheduler can be set to run this macro periodically. Comments are welcome.

Macro , , ,

Workgroup PDM Macro 2 – Save out all files in a Project

September 23rd, 2010

So the previous post showed how a single file can be saved from Workgroup PDM onto your local computer using a macro. This macro builds upon that. If you are starting out with macros, I suggest you go back and get that working first.

The Macro

' Title of Macro: Save all files out from a Project in Workgroup PDM
' 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
    ' ^ remove the single quote from above if you want to debug.

    Set alldocs = connection.Documents
    project = connection.DocumentList("Project1")
    ' ^ Type in the name of the Project you wish to save the files out of.

    For Each item In project
        'msg = MsgBox(item, vbCritical)
        ' ^ remove the single quote from above if you want to debug.
        Set doc = alldocs(item)
        doc.Save "C:\Temp\"
    Next item
 
    connection.Logout
End Sub

Readme

This macro uses connection.DocumentList(“Projectname”) to create an array of files contained in a particular project in the PDM Vault. To debug this macro further, uncomment the lines “Dim msg” and “msg = MsgBox(item, vbCritical)”, and when you run the macro you will get a pop-up dialog telling you the names of each of the files found in the PDM Project.

There is one cavet. If you have links / references in the Project you are saving, those linked files will not be saved. E.g. in the image below, the Car-and-Engine-Drawing and the Slide3 files will not be saved out. I will post a following macro which will be able to save linked files.

Linked files will not be saved.

I’m not going to provide a download of this Macro. I recommend that you create a new Macro in SolidWorks, and type (or copy and paste) the above code into the new empty macro. Also, in case you have not read the previous macro, make sure to add in “SolidWorks Workgroup PDM 1.0 Type Library” under Tools -> References in Microsoft Visual Basic. That way you can use “IntelliSense” (code prompt and completion) to make sure that the pdm parameters are referenced correctly. In the image below, after typing the . (period/full stop), a drop-down list of available parameters should show up. If the drop-down list doesn’t show up, something is wrong. Check Tools -> References and go from there.

IntelliSense Code completion helps make sure you are referencing the PDM API correctly

Macro , , ,

Workgroup PDM Macro 1 – Save out a single file onto your computer

September 23rd, 2010

Skip straight to the Macro >

Introduction

Macros in SolidWorks are extremely powerful and useful. They can be however difficult to maintain, especially when upgrading to new versions of SW and requires a good knowledge of SW API and VB/C++. There are also many macros available for SolidWorks, (lenny’s SW Macros, 3D ContentCentral Macros, Loronos SW Macro Downloads, SolidWorks Forums – login required, and SolidWorks API Examples – broken, even when logged in) which are particularly helpful to study and use as a foundation to build your own macro. Unfortunately there are very few macros available for Workgroup PDM and some of which seem to get lost, especially links on the SolidWorks website (the sample pdm api project on the main solidworks help website is broken – FAIL). Also most of the active work regarding managing SW data is going towards SolidWorks Enterprise PDM which sits on a SQL server as opposed to a file based DB with Workgroup PDM.

SolidWorks Workgroup PDM is a product that comes with SolidWorks Professional and SolidWorks Premium. It allows for revision control of SolidWorks files, and allows multiple people to work on the same SolidWorks project. All SolidWorks files (and other files such as DWG, doc, jpg) gets stored in a main Vault on a central server. There is no direct access to files in the PDM Vault. Access must be through either SolidWorks or SolidWorks Explorer with a PDM Add-In. Files in the vault cannot be accessed directly via Windows Network Share/SMB. As a result interaction with data in Workgroup PDM must be done manually. However here is where SolidWorks Macros and the PDM API comes in.

This macro is the first of a few that I am going to write on Workgroup PDM. If your beginning to write macros, I’d recommend starting with this Macro which is a good foundation.

The Macro


This macro allows a user to copy/save a single specified file from the PDM Vault onto their computer.
While this is quite a small task, it is an excellent beginning to learn the SolidWorks PDM API.

Download Zip file of the Macro >

' Title of Macro: Save a single file out from the Workgroup PDM Vault
' Further info: http://solidworks.burkesys.com/category/macro/
' Author: Stephen Burke | www.sburke.eu
' Version: SolidWorks 2010
' Instructions: Replace "123456.SLDDRW", with the file required.
' 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
    Set alldocs = connection.Documents
 
    Set doc = alldocs("123456.SLDDRW")
    ' ^ Type in the name of the file you wish to save out.
    doc.Save "C:\Temp\"
    ' ^ Type in a folder on your computer to save into.

    connection.Logout
End Sub
Readme / Usage
  1. Open SolidWorks
  2. Tools -> Macro -> Edit (browse to the saved Zip file)
  3. (in Microsoft Visual basic) Tools -> References -> (add-in) “SolidWorks Workgroup PDM 1.0 Type Library”
  4. (in Microsoft Visual basic) Click the Save icon and close down the Visual Basic Editor
  5. (in SolidWorks) Tools -> Macro -> Run
  6. Check C:\Temp for the file

Troubleshooting & FAQ’s

Problem: You receive the following error: “Compile error: Can’t find project or library”
Solution: Go to Tools -> References and add in “SolidWorks Workgroup PDM 1.0 Type Library”. If that does not work, then go to SolidWorks, Tools -> Macro -> New. Choose a new name for the macro and where to save it. This will open up a new Macro. Go to Tools -> References and add in “SolidWorks Workgroup PDM 1.0 Type Library”. Then copy and paste the code from above.

Problem: You receive the following error: “Method ‘Item’ of object ‘IPDMWDocuments’ failed”.
Solution: The file you require was misspelt in the macro, or doesn’t exist in the Vault. Check the line ‘Set doc = alldocs(“123456.SLDDRW”)’

Note: It is not required to be logged into Workgroup PDM within SolidWorks, nor is it required to have “Workgroup PDM” turned on in Tools -> Add-Ins. It is required to have “Workgroup PDM” visible in Tools -> Add-Ins as this indicates that the PDM libraries are installed on your computer.

Note: It is possible to run this Macro from within SolidWorks (Tools -> Macro -> Run) and also from the SolidWorks Task Scheduler with a custom task.

Note: This is the macro in its simplest form. It would be possible to show an input dialog to allow a user select and choose which file to save out.

Note: While this macro was developed with SolidWorks 2010, it is possible to use this macro with all versions of SolidWorks. It would be best to copy and paste the code into a new macro in this case.

Note: If you re-run the macro, it will overwrite and replace any existing file.

SolidWorks Workgroup PDM API Help (pdmworksapi.chm)

This help file contains all the api references which can be used in Macros. This file is essential if you want to make full use of the workgroup PDM in Macros. Getting this file was a little difficult. SolidWorks’s help brought me to here, which wasn’t fully correct, but led me to this. A google before this led me to a 2008 version of this chm file, which fortunately had much more information and examples than the 2010 version. I’m going to provide a mirror of the files here, due to the large amount of broken links that occurs. Local Mirror of pdmworksapi-sw2008.chm and pdmworksapi-sw2010.chm. (You might have to go properties of the chm files after downloading and go “unblock”.) In the pdmworksapi-sw2008.chm, fortunately the Example contained therein had most of the macro in this post.

Stay tuned for more macros based on this simple example.

Macro , , , ,