8 Replies Latest reply on May 31, 2018 7:13 PM by Artem Taturevych

    Get first component name in assembly

    Aaron Heathcote

      I am trying to get name of the first component/assembly in an assembly. I am new to writing in vb but slowly picking it up.

      Its important that the name I get is the first part or assembly rather than the name of the whole assembly

       

      Here is my current file structure that would be typical for my work. As you can see what I am trying to get is 222222-1110

      This number may be different for every job.

      File Structure.png

      and I have achieved this using this code:

       

      Imports SolidWorks.Interop.sldworks
      Imports SolidWorks.Interop.swconst
      Imports System.Runtime.InteropServices
      Imports System
      Imports System.Diagnostics
      
      
      
      
      Partial Class SolidWorksMacro
      
      
          Public Sub main()
      
      
              Dim swModel As ModelDoc2                                'ModelDoc2 allows access to SOLIDWORKS parts, assemblies and drawing documents 
              Dim swModelDocExt As ModelDocExtension                  'I think this looks at the document type
      
      
              swModel = swApp.ActiveDoc                               'sets the current document to the active document
              swModelDocExt = swModel.Extension                       'get the current document extension e.g. .sldasm
      
      
              Dim swFeature As Feature
              Dim featureName As String
              swFeature = swModel.FirstFeature
              featureName = swFeature.Name 'Retrieves "Comments"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Favorites"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "History"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Selection Sets"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Sensors"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Tables"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Live Section PLanes"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Design Binder"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Annotations"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Lights, Camera and Scenes
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Equations"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Front Plane"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Top Plane"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Right Plane"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "Origin"
              swFeature = swFeature.GetNextFeature
              featureName = swFeature.Name 'Retrieves "222222-1110-1" THIS IS WHAT I WANTED :)
      
      
              MsgBox(featureName)
      
      
      
      
          End Sub
      
      
          ''' <summary>
          ''' The SldWorks swApp variable is pre-assigned for you.
          ''' </summary>
          Public swApp As SldWorks
      
      
      
      
      End Class
      

       

      Now as I said I'm new, so i was wondering if this is the best way to do this? seems long winded. I've been on the api help for about 4 hours now and this is the only way i could get the result I wanted.

       

      Once I have worked this out I would then like to get the name of the folder that first part is in but will come to it when I suss out the first problem.

       

      Any help I would be truly grateful.

       

      PS, ignore all the comments in the code, I'm new to api so I comment to help me remember what everything is doing

        • Re: Get first component name in assembly
          Artem Taturevych

          Hi Aaron,

           

          Take a look a this example: #CODE|Get First Component in a Tree

           

          Thanks,
          Artem

            • Re: Get first component name in assembly
              Aaron Heathcote

              Amen Allah Jlili

               

              To both of you, thanks for taking the time to reply to me.

              I noticed you used different methods to achieve the same result, id be interested to know which one is best and why?

               

              Also I ran both of your codes on my assembly and they both returned a value of "222222-1120". This was the second assembly in the feature tree rather than the first. I moved this assembly to the bottom of my feature tree like the image below and it still returned "222222-1120" rather than "222222-1110"

              File Structure2.png

               

              What would cause this to happen?

                • Re: Get first component name in assembly
                  Artem Taturevych

                  Hi Aaron,

                   

                  This macro returns the first component in historical tree. To get the first component in visual tree please use this example: #CODE|Get First Component in a Visual Tree

                   

                  Please note if the first component is in the folder you need to change the example to traverse the folder.

                   

                  Thanks,
                  Artem

                    • Re: Get first component name in assembly
                      Aaron Heathcote

                      Thank you very much Artem, really appreciate your help.

                      So i must have put in 222222-1120 in the assembly first I imagine and that is why the first code gave that return.

                       

                      Once again thanks for your time and help

                      • Re: Get first component name in assembly
                        Mike Childs

                        Artem,

                         

                        Could you update the link? I'm also trying to find a way to find the first component in an assembly but when I click on the link you provided I get a message saying service unavailable.

                          • Re: Get first component name in assembly
                            Artem Taturevych

                            here is the macros

                             

                            Get first component in historical tree

                             

                            Imports SolidWorks.Interop.sldworks

                            Imports SolidWorks.Interop.swconst

                            Imports System.Runtime.InteropServices

                            Imports System

                             

                            Partial Class SolidWorksMacro

                             

                                Public Sub main()

                             

                                    Dim model As IModelDoc2 = swApp.ActiveDoc

                             

                                    If model IsNot Nothing Then

                                        Dim rootComp As IComponent2

                                        rootComp = model.ConfigurationManager.ActiveConfiguration.GetRootComponent3(False)

                                        Dim childComps As Object() = CType(rootComp.GetChildren(), Object())

                                        If childcomps IsNot Nothing Then

                                            If childComps.Length > 0 Then

                                                Dim firstChildComp As IComponent2 = childComps(0)

                                                MsgBox(firstChildComp.Name2)

                                            End If

                                        End If

                                    End If

                             

                                End Sub

                             

                                Public swApp As SldWorks

                             

                            End Class

                            And here is Get First Component In Visual Tree

                             

                            Imports SolidWorks.Interop.sldworks

                            Imports SolidWorks.Interop.swconst

                            Imports System.Runtime.InteropServices

                            Imports System

                             

                            Partial Class SolidWorksMacro

                             

                                Public Sub main()

                             

                                    Dim model As IModelDoc2 = swApp.ActiveDoc

                             

                                    If model IsNot Nothing Then

                                        Dim treeItem As ITreeControlItem

                                        treeItem = model.FeatureManager.GetFeatureTreeRootItem2(swFeatMgrPane_e.swFeatMgrPaneBottom)

                                        treeItem = treeItem.GetFirstChild()

                             

                                        While treeItem IsNot Nothing

                                            If treeItem.ObjectType = swTreeControlItemType_e.swFeatureManagerItem_Component Then

                                                Dim firstChildComp As IComponent2 = treeItem.Object

                                                MsgBox(firstChildComp.Name2)

                                                Exit While

                                            End If

                                            treeItem = treeItem.GetNext()

                                        End While

                                    End If

                             

                                End Sub

                             

                                Public swApp As SldWorks

                             

                            End Class

                    • Re: Get first component name in assembly
                      Amen Allah Jlili

                      You can modify this!

                      Public Sub main()
                              Dim modeldoc As ModelDoc2
                              Dim assembly As AssemblyDoc
                              modeldoc = swApp.ActiveDoc
                              assembly = modeldoc
                              Dim comps As Object()
                              comps = assembly.GetComponents(True)
                              Dim i As Integer
                              Dim comp As Component2
                              Dim modeldocfromcomp As ModelDoc2
                              For i = LBound(comps) To UBound(comps)
                                  comp = comps(i)
                                  modeldocfromcomp = comp.GetModelDoc2
                                  If modeldocfromcomp.GetType = swDocumentTypes_e.swDocASSEMBLY Then
                                      MsgBox(comp.Name2)
                                      Exit For
                                  End If
                              Next i
                          End Sub