2 Replies Latest reply on Mar 8, 2018 11:27 AM by Eric Schwieterman

    Get top level names in assembly tree to create mates api

    Eric Schwieterman

      All of my files have a coordinate system set up on the left and right side of modular items.  I would like to write a macro to go down the tree of an assembly (top to bottom) selecting coordinate2 of component 1 and coordinate1 of component 2, then add a coincident mate.  Then coordinate2 of component 2 and coodinate1 of component 3, then add a coincident mate.  And so on all the way down the tree. 

       

      I have a variable for :

      firstselection

      secondselection

      pathnoextension  (assembly name)  I already have this one figured out

       

               boolstatus = Part.Extension.SelectByID2("Coordinate System2@" & firstselection & "@" & PathNoExtension, "COORDSYS", 0, 0, 0, False, 1, Nothing, 0)

              boolstatus = Part.Extension.SelectByID2("Coordinate System1@" & secondselection & "@" & PathNoExtension, "COORDSYS", 0, 0, 0, True, 1, Nothing, 0)

              Dim swMate As Mate2

              Set swMate = Part.AddMate5(20, -1, False, 0.124990244006958, 0.001, 0.001, 0.001, 0.001, 0, 0, 0, False, False, 0, longstatus)

       

       

      How do I traverse down the tree to get the name of the 1st component in the tree then get the name of the 2nd component?  So I can execute the mate as shown above.  And on down the list 2nd component, 3rd component - mate.  3rd component, 4th component - mate.  etc.

       

      Any help is appreciated.  Thanks - Eric

        • Re: Get top level names in assembly tree to create mates api
          Ivana Kolin
          Option Explicit
          Sub Main()
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swAssy As SldWorks.AssemblyDoc
          Dim swFeatMgr As SldWorks.FeatureManager
          Dim swFeat As SldWorks.Feature
          Dim previousFeature As SldWorks.Feature
          Dim swMate As Mate2
          Dim boolstatus As Boolean
          Dim longstatus As Long
          Const PathNoExtension As String = ......
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swAssy = swModel
          Set swFeatMgr = swModel.FeatureManager
          Set swFeat = swModel.firstFeature
          
          
          Do While Not swFeat Is Nothing
          
          
              If swFeat.GetTypeName2 = "Reference" Then
                  If previousFeature Is Nothing Then
                      Set previousFeature = swFeat
                  Else
                      boolstatus = swModel.Extension.SelectByID2("Coordinate System2@" & previousFeature.Name & "@" & PathNoExtension, "COORDSYS", 0, 0, 0, False, 1, Nothing, 0)
                      boolstatus = swModel.Extension.SelectByID2("Coordinate System1@" & swFeat.Name & "@" & PathNoExtension, "COORDSYS", 0, 0, 0, True, 1, Nothing, 0)
                      
                      Set swMate = swModel.AddMate5(20, -1, False, 0.124990244006958, 0.001, 0.001, 0.001, 0.001, 0, 0, 0, False, False, 0, longstatus)
                      Set previousFeature = swFeat
                  End If
              End If
                  
              Set swFeat = swFeat.GetNextFeature
          Loop
          
          
          End Sub