9 Replies Latest reply on Nov 14, 2018 6:33 AM by Lasse Nielsen

    PDM API file reference

    Lasse Nielsen

      Hi forum,

       

      I made a macro, that during the process moves a zip-file to the vault, adds data to some variables, and finally checks the zip file into the vault. The zip files contains an assembly saved as shelled part. What I would like to do is create a reference between the zip file and the assembly like the way the PDM convert does with drawings and PDFs (see image)

       

      Can any one tell what API I need to use to accomplish this?

       

      PDM reference.PNG

        • Re: PDM API file reference
          Mario Zahren

          Hi Lasse,

          which programming language did you use?

           

          Here a snippet in C#:

              private void AddCustomReference(IEdmFile8 yourZipFile, IEdmFile8 yourReferenceFile, IEdmVault13 yourVault, IEdmFolder5 yourReferenceFileParentFolder)
              {
                  IEdmEnumeratorCustomReference6 addCustRefs;
                  try
                  {
                      addCustRefs = (IEdmEnumeratorCustomReference6)yourZipFile;
                      addCustRefs.AddReference2(yourReferenceFile.ID, yourReferenceFileParentFolder.ID, 0);
                      yourZipFile.UnlockFile(0, "filereference created", 8);
                  }
                  catch (COMException exp)
                  {
                      vault.GetErrorString(exp.ErrorCode, out string errorName, out string errorDesc);
                      vault.MsgBox(0, errorDesc, EdmMBoxType.EdmMbt_OKOnly, errorName);
                  }
              }
            • Re: PDM API file reference
              Lasse Nielsen

              The macro is written in VBA.

              There is probably some bad written code, since this is my first functional macro that interacts with the PDM API. And while on the subject of PDM API. Can I activate intellisense for PDM API in the SolidWorks macro tool?

               

               

              Option Explicit

               

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swCustPropMgr As SldWorks.CustomPropertyManager

               

              Dim pdmVault As Object

               

              Dim strFullPath As String

              Dim strFolderPath As String

               

              Dim strFileName As String

               

              Dim strFileType As String

              Dim intFileType As Integer

               

              Dim i As Integer

               

              Dim wsh As Variant

              Dim strCommand As String

               

              Dim strDelete As String

              Dim strBug As String

               

              Const strTemp As String = "C:\NGI-temp\"

              Const strNAV As String = "\\ngi-nav.local.ngi.dk\Sync\Varer_MAS\"

               

              Dim strSerialNo As String

              Dim strDescription As String

              Dim strRev As String

              Dim strResolved As String

               

              Dim strSerialNoParen As String

               

              Dim PartName As String

              Dim StepName As String

              Dim ZipName As String

               

               

               

              Sub Main()

               

                  'Initialize macro

                  Set swApp = Application.SldWorks

               

                  strFullPath = "<Filepath>"

                  strFolderPath = "<Path>"

                

                      'Append \ to path if missing

                  If Right(strFolderPath, 1) <> "\" Then

                      strFolderPath = strFolderPath & "\"

                  End If

                

                

                  'Limit the macro to SLDASM og SLDPRT files

                  strFileType = LCase(Right(strFullPath, 6))

                

                  Select Case strFileType

                      Case "sldprt"

                          intFileType = 1

                      Case "sldasm"

                          intFileType = 2

                      Case Else

                          End

                  End Select

                

                

                  'Grabs filename only

                  i = InStrRev(strFullPath, "\")

                  strFileName = Right(strFullPath, Len(strFullPath) - i)

                  strFileName = Left(strFileName, Len(strFileName) - 7)

                

                    

                  'Create vault object

                  Set pdmVault = CreateObject("ConisioLib.EdmVault")

                  pdmVault.loginauto "NGI", 0

                

                

                  'Oben model

                  Set swModel = swApp.OpenDoc6(strFullPath, intFileType, 1, "Default", Empty, Empty)

                

                

                  'Get Info from CustomProperties

                  Set swCustPropMgr = swModel.Extension.CustomPropertyManager("Default")

                  swCustPropMgr.Get2 "Serie_NR", strSerialNo, strResolved

                  swCustPropMgr.Get2 "Description", strDescription, strResolved

                  swCustPropMgr.Get2 "Revision", strRev, strResolved

                

                

                  'Creates wrapped serial number for file naming

                  If strSerialNo <> "" Then

                      strSerialNoParen = " (" & strSerialNo & ")"

                  End If

                

                  'Generate names with file extension

                  PartName = strFileName & strSerialNoParen & ".sldprt"

                  StepName = strFileName & strSerialNoParen & ".STEP"

                  ZipName = strFileName & strSerialNoParen & ".ZIP"

                

                

                  'Grab user settings and set it to external faces only

                  Dim UserSetting As Integer

                  UserSetting = swApp.GetUserPreferenceIntegerValue(swSaveAssemblyAsPartOptions)

                  swApp.SetUserPreferenceIntegerValue swSaveAssemblyAsPartOptions, 3

                

                

                  'If assembly

                  If strFileType = "sldasm" Then

                      swModel.SaveAs strTemp & PartName

                      swApp.CloseDoc strFileName & ".sldasm"

                      swApp.OpenDoc strTemp & PartName, 1

                      Set swModel = swApp.ActivateDoc(PartName)

                      swModel.SaveAs strTemp & StepName

                      swApp.CloseDoc PartName

                      Kill strTemp & PartName

                  End If

                

                

                  'If part

                  If strFileType = "sldprt" Then

                      swModel.SaveAs strTemp & StepName

                      swApp.CloseDoc strFileName & ".sldprt"

                  End If

                

                  strBug = Dir(strTemp & StepName)

                

                  If strBug = "" Then

                      MsgBox "An error occured during export. Proberbly due to a bad spline. No ZIP or STEP files was created & vbCrLf & Proceed manually."

                      Exit Sub

                      End

                  End If

                

                

                  'Initalize ZIP shell

                  Set wsh = VBA.CreateObject("WScript.Shell")

                

                

                  'ZIP step file for Vault

                  strCommand = Chr(34) & "C:\NGI\tools\Settings 7-zip\7z.exe" & Chr(34) & _

                                      " a -tzip " & _

                                      Chr(34) & strTemp & ZipName & Chr(34) & _

                                      " " & Chr(34) & strTemp & StepName & Chr(34)

                

                  wsh.Run strCommand, WindowStyle:=0, WaitOnReturn:=1

                

                

                  'ZIP step file for NAV

                  strCommand = Chr(34) & "C:\NGI\tools\Settings 7-zip\7z.exe" & Chr(34) & _

                                      " a -tzip " & _

                                      Chr(34) & strNAV & ZipName & Chr(34) & _

                                      " " & Chr(34) & strTemp & StepName & Chr(34)

                                    

                  wsh.Run strCommand, WindowStyle:=0, WaitOnReturn:=1

               

               

                  'Deleting STEP files from temp-folder

                  strDelete = Dir(strTemp & StepName)

                

                  If strDelete <> "" Then

                    Kill strTemp & StepName

                  End If

               

                    

                  'Delete  pre-existing ZIP-file from vault

                  Dim folder2 As Object

                  Set folder2 = pdmVault.GetFolderFromPath(strFolderPath)

                

                  Dim fileDeleteStep As Object

                  Set fileDeleteStep = pdmVault.GetFileFromPath(strFolderPath & ZipName, folder2)

                

                  If Not fileDeleteStep Is Nothing Then

                      folder2.DeleteFile 0, fileDeleteStep.ID, True

                  End If

                

                

                  'Add new ZIP-file to vault

                  Dim folder As Object

                  Set folder = pdmVault.GetFolderFromPath(strFolderPath)

                

                  folder.AddFile 0, strTemp & ZipName, "", 64

                

                  Dim file As Object

                  Set file = pdmVault.GetFileFromPath(strFolderPath & ZipName, folder)

                  

                

                  'Check out if needed

                  If False = file.IsLocked Then

                      file.LockFile folder.ID, 0

                  End If

                

                

                  'Update datacard

                  Dim pEnumVar As Object

                  Set pEnumVar = file.GetEnumeratorVariable("")

                

                  pEnumVar.SetVar "Serie_NR", "", strSerialNo, False

                  pEnumVar.SetVar "Revision", "", strRev, False

                  pEnumVar.SetVar "Description", "", strDescription, False

                  pEnumVar.Flush

                

                

                  'Check in

                  If True = file.IsLocked Then

                      file.UnlockFile 0, "Checked in by Macro"

                  End If

                

              End Sub

            • Re: PDM API file reference
              Lasse Nielsen

              So I came a little closer to achieving my goal. But then again. Not at all.

               

              I found that enabling Tools > References > PDMWorks Enterprise 2013 Type Library in the SolidWorks build-in VBA editor enables InteliSense for PDM API e.g. stuff like IEdmVault and IEdmFile etc which to me is HUGE!

               

              Now using the intended namespace for PDM API (with PDMWorks Enterprise 2013 Type Library enabled) the macro code should look somewhat like this

               

              Dim ZipFile As IEdmFile5

              Dim pEnumCustRef As IEdmEnumeratorCustomReference7

              Set ZipFile = Vault.GetFileFromPath(strFolderPath & ZipName, ParentFolder)

              Set pEnumCustRef = ZipFile

              pEnumCustRef.AddReference3 ParentFile.ID, ParentFolder.ID, 1, False

               

              Which works great if the macro is executed from SolidWorks directly.

               

              Now I would like to use my code here:

              Capture.PNG

               

              But as far as i can tell the PDM Task-addin does not understand the PDMWorks Enterprise 2013 Type Library and thus the above piece of code does NOT work from Task-Addin.

               

              I have created a little test to see if I was mistaken and found that the code below  will check out LQN 013.SLDASM from my sandbox vault when executed from SolidWorks AND from the Task-Addin

               

              Option Explicit

               

              Dim swApp As SldWorks.SldWorks

               

              Dim Vault As Object

               

              Dim FilePath As String

              Dim Path As String

               

              Dim pdmParentFile As Object

              Dim pdmFolder As Object

               

              Sub main()

                  Set swApp = Application.SldWorks

                  

                  Set Vault = CreateObject("ConisioLib.EdmVault")

                  Vault.LoginAuto "LQN-Sandbox", 0

                

                  FilePath = "C:\LQN-Sandbox\LQN 013.SLDASM"

                  Path = "C:\LQN-Sandbox\"

               

                  Set pdmFolder = Vault.GetFolderFromPath(Path)

                  Set pdmParentFile = Vault.GetFileFromPath(FilePath, pdmFolder)

                 

                  pdmParentFile.LockFile pdmFolder.ID, 0

                 

              End Sub

               

               

               

              While the code below will ONLY work when executed from Solidworks and NOT from the task-addin

               

               

              Option Explicit

               

              Dim swApp As SldWorks.SldWorks

               

              Dim Vault As Object

               

              Dim FilePath As String

              Dim Path As String

               

              Dim pdmParentFile As IEdmFile5

              Dim pdmFolder As IEdmFolder5

               

              Sub main()

                  Set swApp = Application.SldWorks

                  

                  Set Vault = CreateObject("ConisioLib.EdmVault")

                  Vault.LoginAuto "LQN-Sandbox", 0

                

                  FilePath = "C:\LQN-Sandbox\LQN 013.SLDASM"

                  Path = "C:\LQN-Sandbox\"

               

                  Set pdmFolder = Vault.GetFolderFromPath(Path)

                  Set pdmParentFile = Vault.GetFileFromPath(FilePath, pdmFolder)

                 

                  pdmParentFile.LockFile pdmFolder.ID, 0

                 

              End Sub

               

              Now declaring pEnumCustRef as object does not work like the others does. Why does it have to be so confusing?

               

              I really hope someone can shed some light on this or point me to somewhere where I can read about the possibilities and limitations of Task-Addin

                • Re: PDM API file reference
                  Mario Zahren

                  I ran into the same issue while trying writing variable values to the datacard of the new created file during task is runing.

                  My problem was(is) that the file, that was created by task, was not added to the vault. So i had no chance to write any value to the datacard.

                   

                  Maybe there is a similar problem here?

                   

                  Sorry for my english. it´s not the best but i try to do my very best.

                • Re: PDM API file reference
                  Lasse Nielsen

                  To make it a little shorter and easier to manage I have extended the test script to included the reference part

                   

                  Option Explicit

                   

                  Dim swApp As SldWorks.SldWorks

                   

                  Dim Vault As Object

                   

                  Dim FilePath As String

                  Dim Path As String

                   

                  Dim ZipFile As Object

                  Dim pdmFolder As Object

                  Dim pEnumCustRef As Object

                   

                  Sub main()

                      Set swApp = Application.SldWorks

                      

                      Set Vault = CreateObject("ConisioLib.EdmVault")

                      Vault.LoginAuto "LQN-Sandbox", 0

                    

                      FilePath = "C:\LQN-Sandbox\LQN 013.ZIP"

                      Path = "C:\LQN-Sandbox\"

                   

                      Set pdmFolder = Vault.GetFolderFromPath(Path)

                      Set ZipFile = Vault.GetFileFromPath(FilePath, pdmFolder)

                     

                      ZipFile.LockFile pdmFolder.ID, 0

                     

                      Set pEnumCustRef = ZipFile

                     

                      pEnumCustRef.AddReference3 55, pdmFolder.ID, 1, False

                     

                      ZipFile.UnlockFile pdmFolder.ID, 0

                  End Sub

                   

                   

                  - 55 is the ID-number of the file that is to be referenced.

                  - When run i get "Run time error 438: Object doesn't support this property or method

                  - All other parts of the macro works as intended, e.g file check out and vault login

                  - If pEnumCustRef is declared as IEdmEnumeratorCustomReference7 and run from SolidWorks everything works as intended