Get Bodies from component groups

I want to collect bodies from each component group individually to create wrap geometry, But this is collecting all the bodies in the workpart. Is there anyway to change it only the component from group.

            'Collect all Component group detail and Template coordinate system details
            Dim GroupList As List(Of ComponentGroup) = New List(Of ComponentGroup)
            Dim BodyList As List(Of Body) = New List(Of Body)
            Dim PartsList As List(Of Part) = New List(Of Part)
            For Each Mygroup As ComponentGroup In workPart.ComponentGroups
                If TypeOf Mygroup Is ComponentGroup Then
                End If
            For Each MyG As ComponentGroup In GroupList
                For Each Mycomp As Component In MyG.GetComponents
                For Each MyP As Part In PartsList
                    For Each MyB As Body In AskAllBodies(MyP).ToArray
                lw.WriteLine("Count: " & BodyList.Count.ToString)
                'Create Wrap gemotry with bodyList and move to next group
                Dim nullFeatures_WrapGeometry As WrapGeometry = Nothing
                Dim WrapBuild As Features.WrapGeometryBuilder
                WrapBuild = workPart.Features.CreateWrapGeometryBuilder(nullFeatures_WrapGeometry)
                WrapBuild.CloseGaps = Features.WrapGeometryBuilder.CloseGapType.NoOffset
                WrapBuild.AddOffset.RightHandSide = "0"
                WrapBuild.SplitOffset.RightHandSide = "0"
                WrapBuild.IsInterpart = True
                WrapBuild.DistTol = 0.01
                WrapBuild.Associative = False
                Dim AddBodies As Boolean = Nothing
                AddBodies = WrapBuild.Geometry.Add(BodyList.ToArray)
                Dim NxObj As NXObject
                NxObj = WrapBuild.Commit()
                Dim MyBrep As Brep = Utilities.NXObjectManager.Get(NxObj.Tag)
                MyBrep.SetName("Group_" & MyG.Name)
                Dim HideObj() As DisplayableObject = Nothing
                HideObj = MyBrep.GetBodies()
    Function AskAllBodies(ByVal thePart As Part) As List(Of Body)
        Dim thebodies As New List(Of Body)
            Dim BodyTag As Tag = Tag.Null
                ufs.Obj.CycleObjsInPart(thePart.Tag, UFConstants.UF_solid_type, BodyTag)
                If BodyTag = Tag.Null Then
                    Exit Do
                End If
                Dim theType As Integer, theSubtype As Integer
                ufs.Obj.AskTypeAndSubtype(BodyTag, theType, theSubtype)
                If theSubtype = UFConstants.UF_solid_body_subtype Then
                End If
            Loop While True
        Catch ex As NXException
            lw.WriteLine(ex.ErrorCode & ex.Message)
        End Try
        Return thebodies
    End Function

I did something similar with this code

        Dim J As integer
        For Each myFeature As Features.Feature In theSession.Parts.Work.Features.GetFeatures()
            If myFeature.FeatureType= "FSET" Then
  		theUfSession.Modl.AskAllMembersOfSet(myFeature.tag, List_groups, i)
                for J = 0 to i - 1
            End If

I've not dealt with the "wrap" functions as yet, I assume that you will need the occurrence bodies for this to work correctly. To get the occurrence bodies of a specific component group, 2 different strategies come to mind:

  • Continue using the AskAllBodies function (it returns occurrence bodies of the components and any prototype bodies owned by the assembly); for each body returned, query the .OwningComponent property to see if the component belongs to the component group.
  • Attack the problem from the other direction. Get the components from the desired component group and query the reference set currently used in the assembly. In the component part file, gather up the bodies in the reference set (these will be prototype bodies) then use the .FindOccurrence method to find the corresponding occurrence body in the assembly.

I tend to favor the first option, a couple of easy checks will tell you if the body belongs to a component in the group. The second option would require more code and would probably require the components to be fully loaded.

In either case, be careful will deformed or promoted components (perhaps also wave-linked bodies) as these may need some special handling.