Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

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

Status
Not open for further replies.

biw01

Automotive
Dec 31, 2011
152
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 ?
 
Replies continue below

Recommended for you

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

 
Thanks for the code . i will deeply examine the code and get back to you if i need more help.
 
what does this piece of code do ?
PartCollection.RefsetOption.Current

I also see an option of EntirePart in the RefsetOption.
Which one to chose ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor