10 Replies Latest reply on Nov 28, 2017 2:48 PM by Josh Brady

    How to not add child assembly's component to parent assembly

    Julien Losier

      Hello there,


      I have an issue since using feature to create my BOM. In fact, when i go through my assembly with my recursive method to add those sweets components to my BOM. However, when i come across a sub-assembly I'm calling my  recursive method again but this create an issue. From that point i'm adding every NextFeature in my sub-Assembly even though it is not the right parent. Then when it went all the way down,  I come back to my "head assembly" last added and it will be adding AGAIN the same features to it.


      I tried to getParents() from my feature or getParent() from my component to be sure to add the component to right assembly but it's either ones is always Null.

      I added as attachment something similar of what i m using.

      Of course i know it is logical because next step in assembly call recursively and when done we are going back where we were. but i don t know how to fix this if you guys have an idea.


      Thank you.

        • Re: How to not add child assembly's component to parent assembly
          Josh Brady

          You probably want GetOwnerFeature rather than getParents or getParent.

            • Re: How to not add child assembly's component to parent assembly
              Julien Losier

              Thanks josh.


              I tried but like the other method



              does the GetNextFeature() makes them unavailable?

                • Re: How to not add child assembly's component to parent assembly
                  Josh Brady

                  Hmm, what is your goal?  Do you wish to re-create the SolidWorks feature tree, showing only components?  In that case, you should use the ITreeControlItem.  Below is a copy/paste of VBA macro from the SolidWorks help, with a couple of small changes to show only components rather than all features:


                  ' Preconditions:
                  ' 1. Open a part or assembly.
                  ' 2. Open the Immediate window.
                  ' Postconditions:
                  ' 1. Traverses the FeatureManager design
                  '    tree.
                  ' 2. Examine the Immediate window.
                  Option Explicit
                  Dim traverseLevel As Long
                  Sub main()
                      Dim swApp As SldWorks.SldWorks
                      Dim myModel As SldWorks.ModelDoc2
                      Dim featureMgr As SldWorks.FeatureManager
                      Dim rootNode As SldWorks.TreeControlItem
                      Set swApp = Application.SldWorks
                      Set myModel = swApp.ActiveDoc
                      Set featureMgr = myModel.FeatureManager
                      Set rootNode = featureMgr.GetFeatureTreeRootItem()
                      If Not rootNode Is Nothing Then
                          traverseLevel = 0
                          traverse_node rootNode
                      End If
                  End Sub
                  Private Sub traverse_node(node As SldWorks.TreeControlItem)
                      Dim childNode As SldWorks.TreeControlItem
                      Dim featureNode As SldWorks.Feature
                      Dim componentNode As SldWorks.Component2
                      Dim nodeObjectType As Long
                      Dim nodeObject As Object
                      Dim restOfString As String
                      Dim indent As String
                      Dim i As Long
                      Dim displayNodeInfo As Boolean
                      Dim compName As String
                      Dim suppr As Long, supprString As String
                      Dim vis As Long, visString As String
                      Dim fixed As Boolean, fixedString As String
                      Dim componentDoc As Object, docString As String
                      Dim refConfigName As String
                      displayNodeInfo = False
                      nodeObjectType = node.ObjectType
                      Set nodeObject = node.Object
                      Select Case nodeObjectType
                      Case SwConst.swTreeControlItemType_e.swFeatureManagerItem_Feature:
                          displayNodeInfo = False
                          If Not nodeObject Is Nothing Then
                              Set featureNode = nodeObject
                              restOfString = "[FEATURE: " & featureNode.Name & "]"
                              restOfString = "[FEATURE: object null?]"
                          End If
                      Case SwConst.swTreeControlItemType_e.swFeatureManagerItem_Component:
                          displayNodeInfo = True
                          If Not nodeObject Is Nothing Then
                              Set componentNode = nodeObject
                              compName = componentNode.Name2
                              If (compName = "") Then
                                  compName = "?"
                              End If
                              suppr = componentNode.GetSuppression()
                              Select Case (suppr)
                              Case SwConst.swComponentSuppressionState_e.swComponentFullyResolved
                                  supprString = "Resolved"
                              Case SwConst.swComponentSuppressionState_e.swComponentLightweight
                                  supprString = "Lightweight"
                              Case SwConst.swComponentSuppressionState_e.swComponentSuppressed
                                  supprString = "Suppressed"
                              End Select
                              vis = componentNode.Visible
                              Select Case (vis)
                              Case SwConst.swComponentVisibilityState_e.swComponentHidden
                                  visString = "Hidden"
                              Case SwConst.swComponentVisibilityState_e.swComponentVisible
                                  visString = "Visible"
                              End Select
                              fixed = componentNode.IsFixed
                              If fixed = 0 Then
                                  fixedString = "Floating"
                                  fixedString = "Fixed"
                              End If
                              Set componentDoc = componentNode.GetModelDoc
                              If componentDoc Is Nothing Then
                                  docString = "Not loaded"
                                  docString = "Loaded"
                              End If
                              refConfigName = componentNode.ReferencedConfiguration
                              If (refConfigName = "") Then
                                  refConfigName = "?"
                              End If
                              restOfString = "[COMPONENT: " & compName & " " & docString & " " & supprString & " " & visString & " " & refConfigName & "]"
                              restOfString = "[COMPONENT: object null?]"
                          End If
                      Case Else:
                          displayNodeInfo = False
                          If Not nodeObject Is Nothing Then
                              restOfString = "[object type not handled]"
                              restOfString = "[object null?]"
                          End If
                      End Select
                      For i = 1 To traverseLevel
                          indent = indent & "  "
                      Next i
                      If (displayNodeInfo) Then
                          Debug.Print indent & node.Text & " : " & restOfString
                      End If
                      traverseLevel = traverseLevel + 1
                      Set childNode = node.GetFirstChild()
                      While Not childNode Is Nothing
                          traverse_node childNode
                          Set childNode = childNode.GetNext
                      traverseLevel = traverseLevel - 1
                  End Sub