10 Replies Latest reply on Mar 10, 2016 10:08 AM by Joe Pickens

    EPDM API issues pulling parts for SolidWorks

    Joe Pickens

      So I wrote a macro that searches the vault and finds the files and then i use the file locations to insert the files into solidworks. the macro used to work fine but i made a change to do a batch add into the solidworks assembly and now it acts like the API cant reach the file locations and also when i switch it back the macro still acts like it cant find the file locations.

       

      This is the macro I wrote

       

      Sub Open_New_Asm_and_Srt_Search()

       

      'For Solidworks

      Dim SWApp           As SldWorks.SldWorks

      Dim SWModel         As SldWorks.modelDoc2

      Dim SWAsm           As SldWorks.assemblyDoc

      Dim SWAddComp       As SldWorks.modelDoc2

      Dim SWComp          As Component2

      Dim SWAsmEvents     As Class1

       

      'For EPDM

      Dim VaultApp        As New EdmVault5

      Dim EPDMSearch      As IEdmSearch7

      Dim EPDMSearchR     As IEdmSearchResult5

      Dim EPDMFile        As IEdmFile10

       

      'General

      'Dim SWCompName      As String

      Dim TLAsmTemp       As String

      Dim SWAsmTitle      As String

      Dim SWAsmName       As String

      Dim SWCompName      As String

      Dim FName           As String

      Dim CompName        As String

      Dim CompPath(200)      As String

      Dim FPath           As String

      Dim FType           As String

      Dim FDate           As String

      Dim VaultID         As String

      Dim UserID          As String

      Dim Passowrd        As String

      Dim OpSeq           As String

      Dim count           As Integer

      Dim Row             As Integer

      Dim i               As Integer

      Dim FSize           As Long

      Dim Errors          As Long

      Dim Warnings        As Long

      Dim PrtAsm          As Long

      Dim Str             As Variant

      Dim CompsForm       As Variant

      Dim Comps           As Variant

      Dim AddComps        As Variant

      Dim boolstat        As Boolean

       

      Dim CompCoordSysNames(1) As String

      Dim CompXforms(15)              As String

      Dim VCompCoordSysNames As Variant

      Dim VCompXforms          As Variant

       

      'Open new session of SolidWorks

          Set SWApp = CreateObject("SldWorks.Application")

       

      'Define assembly template location

          TLAsmTemp = "C:\Users\pickensj\Documents\Templets\JOE'S MO ASSY.asmdot"

       

      'Open new Solidworks Assembly with specified Template

          Set SWModel = SWApp.NewDocument(TLAsmTemp, 0, 0, 0)

       

      'Show Solidworks assembly

          SWModel.Visible = True

       

      'Set up events

          Set SWAsm = SWModel

       

      'Get the title of the assembly document

          SWAsmTitle = SWModel.GetTitle

                 

      'Split the title into two strings using the period(.) as the delimiter

          Str = Split(SWAsmTitle, ".")

       

      'Set assemby name for use in mating in module5

          SWAsmName = Str(0)

       

      'Login to EPDM Vault

          VaultApp.LoginAuto "MIG1", 0

                     

      'Do a loop search till there is a blank value in column A

          While Sheets("JDE BOM").Range("A6").Offset(i, 0).Value <> ""

       

          'Use Values that only have the number 1 in column E

              If Sheets("JDE BOM").Range("E6").Offset(i, 0).Value = 1 Then

             

              'If the statement is true use the value in column "A" as the name of the file

                  CompName = Sheets("JDE BOM").Range("A6").Offset(i, 0).Value

                  OpSeq = Sheets("JDE BOM").Range("C6").Offset(i, 0).Value

                         

              'Create a search in the EPDM Vault

                  Set EPDMSearch = VaultApp.CreateSearch()

                  EPDMSearch.FileName = "%" & CompName & "%"

                  EPDMSearch.FindFiles = True

                  EPDMSearch.FindFolders = False

                  EPDMSearch.FindHistoricStates = False

                  EPDMSearch.Recursive = True

             

              'Get the first file in the EPDM Vault

                  Set EPDMSearchR = EPDMSearch.GetFirstResult

       

              If EPDMSearchR Is Nothing Then

                  GoTo End_of_Sub

              End If

              'Get the File info from EPDM Vault File

                  FPath = EPDMSearchR.Path

                  FName = EPDMSearchR.Name

                  FDate = EPDMSearchR.FileDate

       

              'Loop through all files in the vault untill...

                  While Right(FName, 6) = "SLDPRT" And Right(FName, 6) = "sldprt" And _

                      Right(FName, 6) = "SLDASM" And Right(FName, 6) = "sldasm"

       

                  'If there is no matching file in the vault end sub

                      On Error GoTo End_of_Sub

                  'Goto next file

                      Set EPDMSearchR = EPDMSearch.GetNextResult

                      FName = EPDMSearchR.Name

                     

                  Wend

              'End of loop

       

              'Define file info variables

                  CompPath(count) = EPDMSearchR.Path

                 

              'Set variable incrament instance

                  count = count + 1

       

              End If

       

      End_of_Sub:

       

          'Set variable incrament instance

              i = i + 1

       

          Wend

       

      ' Add a rotational diagonal unit matrix (zero rotation) to the transformation matrix

          CompXforms(0) = 1#

          CompXforms(1) = 0#

          CompXforms(2) = 0#

          CompXforms(3) = 0#

          CompXforms(4) = 1#

          CompXforms(5) = 0#

          CompXforms(6) = 0#

          CompXforms(7) = 0#

          CompXforms(8) = 1#

       

      ' Add a translation vector to the transformation matrix

          CompXforms(9) = 0#

          CompXforms(10) = 0#

          CompXforms(11) = 0#

       

      ' Add a scaling factor to the transform

          CompXforms(12) = 1#

       

      ' The last three elements of the transformation matrix are unused

       

      ' Register the component's coordinate system name

          CompCoordSysNames(0) = "Coordinate System1"

       

        ' Add the component to the assembly.

        VCompXforms = CompXforms

        'vcompXforms = Nothing  ' also achieves zero rotation and translation of component

        VCompCoordSysNames = CompCoordSysNames

       

      Comps = CompPath

       

      'Acts like file path is invalid/ works when i set path instance to normal folder and doesn't when pointed to files in the vault

      AddComps = SWAsm.AddComponents3(Comps, CompXforms, VCompCoordSysNames)

       

      End Sub

        • Re: EPDM API issues pulling parts for SolidWorks
          Joe Pickens

          I have been playing with the macro and now for reasons unknown I can open files individually and add them but i cant get the AddComponents3 to work if the files are in the EPDM Vault

            • Re: EPDM API issues pulling parts for SolidWorks
              Lee CS Young

              I don't see anywhere in the code where you're getting latest version, so the files may not be in your local cache.

                • Re: EPDM API issues pulling parts for SolidWorks
                  Joe Pickens

                  In my search i have the EPDMSearch.FindHistoricStates = False other than that i dont know how to only call the latest version and the macro is working if i use use this code to add my components but dosent work for the batch and im dealing with adding so many components that the batch add makes the most sense to use

                   

                  If Right(FLocation, 6) = "sldprt" Or Right(FLocation, 6) = "SLDPRT" Then

                      PrtAsm = 1 'swDocPART

                  Else

                      PrtAsm = 2 'swDocASSEMBLY

                  End If

                   

                  Set SWAddComp = SWApp.OpenDoc6(FLocation, PrtAsm, 1, "", Errors, Warnings)

                   

                  Set SWComp = SWAsm.AddComponent5(FLocation, 0, "", False, "", 0, 0, 0)

                    • Re: EPDM API issues pulling parts for SolidWorks
                      Artem Taturevych

                      Try this method: 2014 SOLIDWORKS API Help - GetFileCopy Method (IEdmFile5)

                      Or 2014 SOLIDWORKS API Help - IEdmBatchGet Interface to do this in a batch mode for several files (will be more efficient). I did similar development and it did worked using the above methods.

                        • Re: EPDM API issues pulling parts for SolidWorks
                          Joe Pickens

                          To try to make this short i have essentually added this to my macro. the number of files in the EPDMBatchF.FileCount is equal to the number of files i am trying to pull and add to my assembly and my macro still dosent work

                           

                          also I am running this macro from Excel if that matters

                           

                           

                          Set EPDMBatchF = VaultApp.CreateUtility(EdmUtil_BatchGet)

                          Call EPDMBatchF.AddSelection(VaultApp, EPDMSelObj())

                          Call EPDMBatchF.CreateTree(0, EdmGetCmdFlags.Egcf_IncludeAutoCacheFiles)

                          Call EPDMBatchF.GetFiles(0, Nothing)

                          NumberofFiles = EPDMBatchF.FileCount

                           

                          AddComps = SWAsm.AddComponents3(Comps, CompXforms, VCompCoordSysNames)

                           

                          End Sub

                          I dont know how to add files so if you think adding my macro file would help you help me tell me how to add a file

                          • Re: EPDM API issues pulling parts for SolidWorks
                            Joe Pickens

                            I have been working on the macro more and came down to this conclusion braking the macro down to using the AddComponents3 is i use an array larger than (0) in the compNames thus adding more than one component at a time. the AddComponents3 doesn't work.

                             

                             

                            Dim swApp As SldWorks.SldWorks

                            Dim assemb As SldWorks.assemblyDoc

                            Dim compNames(0) As String

                            Dim compXforms(15) As Double

                            Dim compCoordSysNames(0) As String

                            Dim vcompNames As Variant

                            Dim vcompXforms As Variant

                            Dim vcompCoordSysNames As Variant

                            Dim vcomponents As Variant

                            Option Explicit

                             

                            Sub main()

                             

                            Set swApp = CreateObject("SldWorks.Application")

                            Set assemb = swApp.ActiveDoc

                             

                            ' For each component to be added, create a separate transform

                             

                            If (Not assemb Is Nothing) Then

                             

                                compNames(0) = "File1"

                                'compNames(1) = "File2"

                             

                            ' Define the transformation matrix. See the IMathTransform API documentation.

                             

                            ' Add a rotational diagonal unit matrix (zero rotation) to the transformation matrix

                                compXforms(0) = 1#

                                compXforms(1) = 0#

                                compXforms(2) = 0#

                                compXforms(3) = 0#

                                compXforms(4) = 1#

                                compXforms(5) = 0#

                                compXforms(6) = 0#

                                compXforms(7) = 0#

                                compXforms(8) = 1#

                             

                            ' Add a translation vector to the transformation matrix

                                compXforms(9) = 0#

                                compXforms(10) = 0#

                                compXforms(11) = 0#

                             

                            ' Add a scaling factor to the transform

                                compXforms(12) = 1#

                            ' The last three elements of the transformation matrix are unused

                             

                            ' Register the component's coordinate system name

                                compCoordSysNames(0) = "Coordinate System1"

                             

                              ' Add the component to the assembly.

                              vcompNames = compNames

                              vcompXforms = compXforms

                              'vcompXforms = Nothing  ' also achieves zero rotation and translation of component

                              vcompCoordSysNames = compCoordSysNames

                                 vcomponents = assemb.AddComponents3((vcompNames), (vcompXforms), (vcompCoordSysNames))

                            End If

                            End Sub

                              • Re: EPDM API issues pulling parts for SolidWorks
                                Artem Taturevych

                                Your compXforms should be of size [comps count]X16. So you need to add the transforms for each components. You only specified for one. That explains why it works for single component because 16x1=16. Just repeat the array as many time as you have components to insert.

                                  • Re: EPDM API issues pulling parts for SolidWorks
                                    Joe Pickens

                                    is there a way for me to increase the array size as the macro runs or am i forced to define the array size from the start.

                                      • Re: EPDM API issues pulling parts for SolidWorks
                                        Artem Taturevych

                                        Use ReDim or ReDim Preserve keywords

                                         

                                        ReDim compXforms((Ubound(compNames) + 1) * 16 - 1)

                                          • Re: EPDM API issues pulling parts for SolidWorks
                                            Joe Pickens

                                            Thanks for all your help Artem my macro works now  adding the IEdmBatchGet to set the files in the local cache so they can be resched and setting the arrays to the exact size by using ReDim Preserve to use AddComponents3 properly was the issue

                                             

                                            Here is my finished macro it still needs some cleanup but it seem to be working grate

                                             

                                            Sub Open_New_Asm_and_Srt_Search()

                                             

                                            'For Solidworks

                                            Dim SWApp           As SldWorks.SldWorks

                                            Dim SWModel         As SldWorks.modelDoc2

                                            Dim SWAsm           As SldWorks.assemblyDoc

                                            Dim SWAddComp       As SldWorks.modelDoc2

                                            Dim SWComp          As Component2

                                            Dim SWAsmEvents     As Class1

                                             

                                            'For EPDM

                                            Dim VaultApp        As New EdmVault5

                                            Dim Vault           As IEdmVault13

                                            Dim EPDMSearch      As IEdmSearch7

                                            Dim EPDMSearchR     As IEdmSearchResult5

                                            Dim EPDMFile        As IEdmFile5

                                            Dim EPDMBatchF      As IEdmBatchGet

                                            Dim EPDMObj         As IEdmObject5

                                            Dim EPDMSelObj()    As EdmSelItem

                                            Dim EPDMFolder      As IEdmFolder5

                                             

                                            'General

                                            'Dim SWCompName      As String

                                            Dim TLAsmTemp       As String

                                            Dim SWAsmTitle      As String

                                            Dim SWAsmName       As String

                                            Dim SWCompName      As String

                                            Dim FName           As String

                                            Dim CompName        As String

                                            Dim CompPath()      As String

                                            Dim FPath           As String

                                            Dim FType           As String

                                            Dim FDate           As String

                                            Dim VaultID         As String

                                            Dim UserID          As String

                                            Dim Passowrd        As String

                                            Dim OpSeq           As String

                                            Dim VaultName       As String

                                            Dim count           As Integer

                                            Dim Row             As Integer

                                            Dim i               As Integer

                                            Dim FSize           As Long

                                            Dim Errors          As Long

                                            Dim Warnings        As Long

                                            Dim PrtAsm          As Long

                                            Dim Str             As Variant

                                            Dim CompsForm       As Variant

                                            Dim Comps           As Variant

                                            Dim AddComps        As Variant

                                            Dim boolstat        As Boolean

                                            Dim CompCoordSysNames() As String

                                            Dim VCompCoordSysNames As Variant

                                            Dim VCompXforms          As Variant

                                             

                                            'Open new session of SolidWorks

                                                Set SWApp = CreateObject("SldWorks.Application")

                                             

                                            'Define assembly template location

                                                TLAsmTemp = "C:\Users\pickensj\Documents\Templets\JOE'S MO ASSY.asmdot"

                                             

                                            'Open new Solidworks Assembly with specified Template

                                                Set SWModel = SWApp.NewDocument(TLAsmTemp, 0, 0, 0)

                                             

                                            'Show Solidworks assembly

                                                SWModel.Visible = True

                                               

                                            'Set up events

                                                Set SWAsm = SWModel

                                            '    Set SWAsmEvents = New Class1

                                            '    Set SWAsmEvents.SWAsm = SWApp.ActiveDoc

                                               

                                            'Get the title of the assembly document

                                                SWAsmTitle = SWModel.GetTitle

                                                       

                                            'Split the title into two strings using the period(.) as the delimiter

                                                Str = Split(SWAsmTitle, ".")

                                             

                                            'Set assemby name for use in mating in module5

                                                SWAsmName = Str(0)

                                             

                                            'Login to EPDM Vault

                                                VaultApp.LoginAuto "MIG1", 0

                                             

                                            'Do a loop search till there is a blank value in column A

                                                While Sheets("Sheet1").Range("A6").Offset(i, 0).Value <> ""

                                             

                                                'Use Values that only have the number 1 in column E

                                                    If Sheets("Sheet1").Range("E6").Offset(i, 0).Value = 1 Then

                                             

                                                    'If the statement is true use the value in column "A" as the name of the file

                                                        CompName = Sheets("Sheet1").Range("A6").Offset(i, 0).Value

                                                        OpSeq = Sheets("Sheet1").Range("C6").Offset(i, 0).Value

                                                               

                                                    'Create a search in the EPDM Vault

                                                        Set EPDMSearch = VaultApp.CreateSearch()

                                                        EPDMSearch.FindFiles = True

                                                        EPDMSearch.FindFolders = False

                                                        EPDMSearch.FindHistoricStates = False

                                                        EPDMSearch.Recursive = True

                                                        EPDMSearch.FileName = "%" & CompName & "%"

                                                          

                                                    'Get the first file in the EPDM Vault

                                                        Set EPDMSearchR = EPDMSearch.GetFirstResult

                                             

                                                    If EPDMSearchR Is Nothing Then

                                                        GoTo End_of_Sub

                                                    End If

                                                    'Get the File info from EPDM Vault File

                                                        FPath = EPDMSearchR.Path

                                                        FName = EPDMSearchR.Name

                                                        FDate = EPDMSearchR.FileDate

                                             

                                                    'Loop through all files in the vault untill...

                                                        While Right(FName, 6) = "SLDPRT" And Right(FName, 6) = "sldprt" And _

                                                            Right(FName, 6) = "SLDASM" And Right(FName, 6) = "sldasm"

                                             

                                                        'If there is no matching file in the vault end sub

                                                            On Error GoTo End_of_Sub

                                                        'Goto next file

                                                            Set EPDMSearchR = EPDMSearch.GetNextResult

                                                            FName = EPDMSearchR.Name

                                                                        

                                                        Wend

                                                    'End of loop

                                                        ReDim Preserve EPDMSelObj(count)

                                                        ReDim Preserve CompPath(count)

                                                        ReDim Preserve CompCoordSysNames(count)

                                             

                                                    'Define file info variables

                                                        CompPath(count) = EPDMSearchR.Path

                                                        EPDMSelObj(count).mlDocID = EPDMSearchR.ID

                                                        EPDMSelObj(count).mlProjID = EPDMSearchR.ParentFolderID

                                                                   

                                                    'Set variable incrament instance

                                                       

                                                        count = count + 1

                                             

                                                    End If

                                             

                                            End_of_Sub:

                                             

                                                'Set variable incrament instance

                                                    i = i + 1

                                             

                                                Wend

                                             

                                            ' Register the component's coordinate system name

                                                CompCoordSysNames(0) = "Coordinate System1"

                                             

                                              ' Add the component to the assembly.

                                             

                                              Set VCompXforms = Nothing  ' also achieves zero rotation and translation of component

                                              VCompCoordSysNames = CompCoordSysNames

                                               

                                                Comps = CompPath

                                             

                                            Set EPDMBatchF = VaultApp.CreateUtility(EdmUtil_BatchGet)

                                            Call EPDMBatchF.AddSelection(VaultApp, EPDMSelObj())

                                            Call EPDMBatchF.CreateTree(0, EdmGetCmdFlags.Egcf_IncludeAutoCacheFiles)

                                            Call EPDMBatchF.GetFiles(0, Nothing)

                                            Row = EPDMBatchF.FileCount

                                             

                                            AddComps = SWAsm.AddComponents3(Comps, compXforms, VCompCoordSysNames)

                                             

                                            End Sub