6 Replies Latest reply on Nov 4, 2018 5:17 PM by Barry Cavanaugh

    PDM via VBA - add file to vault as new version on existing file

    Karel Varel

      Hello everyone,

      I've created Solidworks macro - it's purpose is to export drawing to PDF and BOM to CSV file into PDM. I'm unsing PDM api function fileadd2 and everything works ok IF I'm creating new PDF in PDM. But when I want to create updated version of PDF and save it over checked in file, macro will hang on SaveAs3 to PDF step. When I then kill Solidworks, restart it and want to manually save drawing as PDF over that file, Solidworks keeps me telling that file is read only (but I see that it's checked in for me).

      I tried to edit macro, that it creates new pdf in temp directory and then copy it to PDM via fileadd2, but I get error saying that I can't add file to PDM because file with same filename is already there. What is VBA command to update file in PDM with new version of file?

      Thanks in advance.

        • Re: PDM via VBA - add file to vault as new version on existing file
          Jim Sculley

          SaveAs3 has been obsolete since at least SOLIDWORKS 2010.  You should be using:


          2018 SOLIDWORKS API Help - SaveAs Method (IModelDocExtension)


          Do that and then if you still have the same problem,  reply back.

            • Re: PDM via VBA - add file to vault as new version on existing file
              Karel Varel

              Hello Jim, thanks for advice, but I don't think that this is the problem. I just tried following:


              1. Saved drawing manually as PDF into PDM. It created only local copy, and I had to rightmouse click it and add it to the vault. Then I checked in this PDF file. So far so good.


              2. I checked out this file again and in solidworks I tried save as PDF option - into the same vault directory with same filename (but I didn't clicked or anyhow selected previously saved PDF). In this case Solidworks says that file is read-only.


              3. Now i tried to Save As PDF file in Solidworks again, but i explicitly selected PDF created in step 1 (doubleclicked on it instead of select save button). In this case everything is ok and PDF is rewrited with new version.


              Now - I know that this is something with file ID, that must remain same within all versions of saved PDFs. But I don't know exact procedure how to replicate step 3 using VBA (get id of file and save new PDF with the same ID). Can you advice me with example code?

            • Re: PDM via VBA - add file to vault as new version on existing file
              Karel Varel

              I think I've figured it out.


              The initial problem was that I could save new PDF to vault, but then I couldn't overwrite it with same saveas function even if the original PDF file was checked out (Solidworks macro falled into infinite loop on saveas PDF).

              So what I'm doing now is that I generate new PDF to local disk (into windows/temp folder), then I'm using SetAttr 0 to checked out file in vault (set it to writable) and then vba.filecopy new PDF file from temp over old PDF file in vault.


              Using this way macro works perfectly and new PDF is written as new version of file in vault.

              • Re: PDM via VBA - add file to vault as new version on existing file
                Barry Cavanaugh

                Thanks for explaining your solution, Jim. Is it correct to say that you are using both the PDM API and the Solidworks API?


                In my line of work with have multiple vaults because one vault holds unclassified data and the other holds classified data...

                We have scenarios where the same file exists in both vaults but at different revisions...occasionally we need to bring in an updated revision from the unclassified side over to the classified vault - the new revision could be several revisions higher than what is currently in the classified vault.


                It seems to me you are accomplishing this type of thing and you are using both the PDM and SW APIs to do it...do you think this can be done solely with the PDM API? I have tried but there seems to be no provision in the PDM API to automate the addition of an existing file into the vault (for instance even if you check out the existing file first, the addfile2 method does not provide any parameters that allow you to add a file that already exists)


                thanks for your thoughts on this...