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 ?
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 ?
CODE
' 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 ?
RE: How to retrieve all the body from the parts body collection ?
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 ?
www.nxjournaling.com