Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bodycollection - more than one body?

Status
Not open for further replies.

Frank2288

Mechanical
Nov 20, 2015
38
0
0
IT
I'm writing in VB a tool that needs to perform a series of modeling operations starting from a CAD template part that is given as input (it is manually modelled by an operator before the use of the automatic tool).

For a series of reasons i need to be sure that the part that I am using as template is characterized by only one body; the operator knows this requirement and makes sure to have only one body when he/she finishes the modeling operations).

Just to be sure I added a check in the first operations performed by the VB tool to assure that the bodycollection of the part returns 1 element.

Check:
Code:
        Dim body_collection As NXOpen.BodyCollection = workPart.Bodies

        If body_collection.ToArray.Length > 1 Then
            Dim title As String = "WARNING"
            Dim multiple_bodies_text As String = "The provided template is composed by multiple solid bodies"
            System.Windows.Forms.MessageBox.Show(multiple_bodies_text, title, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)

            'Error 5
        End If

The problem is that I often found more than one body in the part, although the user designed the part as a single body and by opening the part (manually) the 3D model that has been generated seems like a single body. Summing up, I don't know why but I found these "ghosts bodies" very often and it is a problem.

1) Does anybody knows why these ghost bodies are created? Which modeling operation could be responsible for this?
2) I tried to cycle through all the bodies in the body collection of a part and highlight each single body, and I found out that the only body that is actually highlighted is always the first one (nothing is highlighted when considering the "ghost bodies"). So it could be ok for me to just remove these other bodies. I tried this solution but it does not work: anybody knows why?


Code:
Imports Snap, NXOpen
Imports Snap.UI.Block, Snap.Create
Imports ConstDef
Imports System
Imports System.Collections.Generic


'test to remove multiple bodies

Public Module MultipleBodies

    Private theSession As NXOpen.Session
    Private theUfSession As UF.UFSession
    Private Matlab As Object

    Public Sub Main()
        '        System.Diagnostics.Debugger.Launch()
        theSession = NXOpen.Session.GetSession
        theUfSession = NXOpen.UF.UFSession.GetUFSession

        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display

        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim solidBodies As New List(Of Body)

        For Each theBody As Body In workPart.Bodies
            solidBodies.Add(theBody)
        Next

        lw.WriteFullline(solidBodies.ToArray.Length.ToString)
        lw.WriteFullline(workPart.Bodies.ToArray.Length.ToString)

        solidBodies.RemoveRange(1, (solidBodies.ToArray.Length - 1))

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "prova")
        theSession.UpdateManager.DoUpdate(markId1)

        lw.WriteFullline("Number of bodies inside SolidBodies:")
        lw.WriteFullline(solidBodies.ToArray.Length.ToString)



        lw.WriteFullline("Number of bodies in workpart.bodies.toarray.length.tostring")
        lw.WriteFullline(workPart.Bodies.ToArray.Length.ToString)

    End Sub

End Module
 
Replies continue below

Recommended for you

Sheet bodies also show up in the .Bodies collection.

The "delete body" command will remove the body from the display, but it will remain in the .Bodies collection.

Some commands create special geometry that is sometimes hidden from the user. This "system created" geometry is usually put on a special layer (such as layer 0 or 257); you might be able to check the layer of the body and exclude those not on the usual layers of 1-256.

www.nxjournaling.com
 
Mh, that's a very good tip. Sadly i never worked with layers.

You said that the delete body command (i guess removeRange works in the same way) only removes from display. Which commands should i use to remove the entities from the body collection? I'm fairly certain that only the first body is the one that I need. Could I use some other command to remove the others without using the layers?

Thanks a lot, this is very interesting.




 
Hi Frank -

I can help you...Almost sure you will be a happy user....
It is exactly what Cowski mentions regarding the sytems layers and the system bodies...
NX provides layers for users between 1 & 256..But NX uses some other layers in the background, which is not visible for the users.
Also not the system objects is visible on these layers (<1 & >256..
An example could be if you create a simple tab with a flange in SheetMetal app. Try create a FlatPattern feature...
Now the NX part contains 3 bodies...But you can only see and handle the original body...your body you created with the tab and the flange..
But NX needs two extra bodies in the background , to be able to create the FlatPattern feature...

I have edited your code - everything works like a dream - from my point of view....
Give this a try....
Code:
Imports Snap, NXOpen
Imports Snap.UI.Block, Snap.Create
Imports ConstDef
Imports System
Imports System.Collections.Generic


'test to remove multiple bodies

Public Module MultipleBodies

    Private theSession As NXOpen.Session
    Private theUfSession As UF.UFSession
    Private Matlab As Object

    Public Sub Main()
        '        System.Diagnostics.Debugger.Launch()
        theSession = NXOpen.Session.GetSession
        theUfSession = NXOpen.UF.UFSession.GetUFSession

        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display

        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim solidBodies As New List(Of Body)

        For Each theBody As Body In workPart.Bodies
            If theBody.IsSolidBody Then 'added lklo - try tempoary comment this line out
            If theBody.Layer <= 256 Then 'added lklo - try tempoary comment this line out
            If theBody.Layer >= 1 Then 'added lklo - try tempoary comment this line out
            ' now we are ready to put only Solid Bodies on layers between =>1 & <=256 into the list
            solidBodies.Add(theBody)
            End If 'added lklo - try tempoary comment this line out
            End If 'added lklo - try tempoary comment this line out
            End If 'added lklo - try tempoary comment this line out
        Next

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "prova")
        theSession.UpdateManager.DoUpdate(markId1)


        lw.WriteFullline("Number of bodies in the entire part: " & workPart.Bodies.ToArray.Length) 'edited lklo

        lw.WriteFullline("") 'added lklo

        lw.WriteFullline("Number of bodies in the List ""solidBodies"": " & solidBodies.Count) 'edited lklo
        lw.WriteFullline("=============================================") 'added lklo
    End Sub

End Module

1.Try create a sheetmetal part which also contains a flatpattern feature...
2.Run the code
3.Result: 3 solidBodies in workpart , 1 solidBody in the List "solidBodies"
Now you are ready to continue your work, by using the List "solidBodies"...

And a small test for your own testing - just to see the difference...
4. Try comment out the six lines I have suggested in the code.
5.Run the code
6.3.Result: 3 solidBodies in workpart , 3 solidBodies in the List "solidBodies"

regards lklo


 
Status
Not open for further replies.
Back
Top