×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to retrieve all the body from the parts body collection ?

How to retrieve all the body from the parts body collection ?

How to retrieve all the body from the parts body collection ?

(OP)
I am trying to fetch all the bodies from the body collection and then get a face from a particular body inside a part.The way i retrieve the face is by checking its attributes.

My code is as below:
Public Function FnGetFace(ByRef objPart As Part, ByVal faceName As String, ByVal sAttributeType As String) As String
        FnGetFace = Nothing
        Dim wPart As Part = FnGetNxSession().Parts.Work
        For Each body As Body In objPart.Bodies
            FnGetNxSession().Parts.SetWork(objPart)
            For Each face As Face In body.GetFaces()
                'On Error Resume Next
                If face.GetAttributeTitlesByType(NXObject.AttributeType.String).Length <> 0 Then
                    Try
                        If face.GetStringAttribute(sAttributeType) = faceName Then
                            'If face.Name = faceName Then
                            Dim sJournalIdName As String
                            sJournalIdName = body.JournalIdentifier + "|" + face.JournalIdentifier
                            FnGetFace = sJournalIdName
                            SSetWorkPart(wPart)
                            Exit Function
                            'End If
                        End If
                    Catch ex As Exception
                    End Try
                End If
            Next
        Next
        SSetWorkPart(wPart)
        Err.Clear()
    End Function

Can someone please point out what i have missed in the code as i am not able to retrieve all the bodies from the part ?

RE: How to retrieve all the body from the parts body collection ?

Below is an admittedly rough journal that will search through an assembly for a face that has an attribute with a given title. Hopefully, you can adapt it for your needs or at least borrow from the strategy it uses.

CODE

' This assumes that the work part is the top-level assembly part.
' journal walks the assembly structure searching for a face with the given attribute title

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies

Module NXJournal

    Public s As Session = Session.GetSession()
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = s.ListingWindow
    
    Sub Main()
        
    Dim dispPart As Part = s.Parts.Display
    'change the following to the title of the attribute you want to search for
    Dim faceAttribute as String = "MYFACENAME"
    
    lw.Open
    Try
        Dim part1 As Part
        part1 = s.Parts.Work
        lw.WriteLine("Top: " & part1.Name)
        Dim c As ComponentAssembly = part1.ComponentAssembly
        Walk(c.RootComponent, 0, faceAttribute)
    Catch e As Exception
        s.ListingWindow.WriteLine("Failed: " & e.ToString)
    Finally
        Dim nullAssemblies_Component As Assemblies.Component = Nothing
        Dim partLoadStatus2 As PartLoadStatus
        s.Parts.SetWorkComponent(nullAssemblies_Component, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus2)
        
        Dim workPart As Part = s.Parts.Work
        partLoadStatus2.Dispose()

    End Try
    lw.Close
    
    End Sub
    
'**********************************************************
    Sub Walk(c As Component, level As Integer, myFaceAttribute as String)
        Dim children As Component() = c.GetChildren()
        Dim child As Component
        Dim prototype As Part
        If TypeOf c.Prototype Is Part Then
            prototype = CType(c.Prototype, Part)
            For Each child In children
                lw.WriteLine("")
                lw.WriteLine("child: " & child.Name)
                'lw.WriteLine("path: " & child.Prototype.OwningPart.FullPath)
                FindFace(child, myFaceAttribute)
                Walk(child, level + 1, myFaceAttribute)
            Next

        End If
    End Sub
'**********************************************************
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function
'**********************************************************
Sub FindFace(myComp as Component, myFaceAttribute as String)

    Dim partLoadStatus1 As PartLoadStatus
    s.Parts.SetWorkComponent(myComp, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus1)
    
    Dim workPart As Part = s.Parts.Work
    partLoadStatus1.Dispose()
    
    Dim myFaceName as String
    Dim found as Boolean
    
    lw.Open
    
    For Each myBody as Body in workPart.Bodies
        'lw.WriteLine("Found a body: " & myBody.Tag.ToString)
        For Each myFace as Face in myBody.GetFaces
            Try
                myFaceName = myFace.GetStringAttribute(myFaceAttribute)
                lw.WriteLine("Face found, name: " & myFaceName)
                'if myFaceName = "value of attribute you are searching for" then...
                myFace.Highlight
                exit sub
            Catch ex as Exception
            End Try
        Next
    Next
    
    if Not found then
        lw.WriteLine("Face not found")
    end if
    
End Sub
'**********************************************************
End Module

www.nxjournaling.com

RE: How to retrieve all the body from the parts body collection ?

(OP)
Thanks for the code . i will deeply examine the code and get back to you if i need more help.

RE: How to retrieve all the body from the parts body collection ?

(OP)
what does this piece of code do ?
PartCollection.RefsetOption.Current

I also see an option of EntirePart in the RefsetOption.
Which one to chose ?

RE: How to retrieve all the body from the parts body collection ?

When you make a component the work part, do you want to display the current reference set or the entire part reference set?

www.nxjournaling.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources