iLogic to select the specific face?

iLogic to select the specific face?

jingying_1213
Advocate Advocate
506 Views
6 Replies
Message 1 of 7

iLogic to select the specific face?

jingying_1213
Advocate
Advocate

Hi Experts,

 

I want to select a specific face from a part. But how can I determine which one I want?

For example, please see below, I want the highlight planar face and I have below codes to show the surface type of these faces but I donot know the index for this specific face.

Can someone help?

 

	Dim PDoc As PartDocument = ThisApplication.ActiveDocument
	Dim PDComp As PartComponentDefinition = PDoc.ComponentDefinition
	Dim SB As SurfaceBody = PDComp.SurfaceBodies.Item(1)
	Dim oFaces As Faces = SB.Faces
	For i = 1 To oFaces.Count
		Dim oFace As Face = oFaces.Item(i)
		MessageBox.Show(oFace.SurfaceType.ToString, "SurfaceType")
	Next

 

 

jingying_1213_0-1678324367623.png

 

0 Likes
Accepted solutions (2)
507 Views
6 Replies
Replies (6)
Message 2 of 7

matt_jlt
Collaborator
Collaborator

Not exactly sure what you are after but try this, it asks to select a face then gives you the index and face type when you select one.

Dim PDoc As PartDocument = ThisApplication.ActiveDocument
Dim PDComp As PartComponentDefinition = PDoc.ComponentDefinition
Dim SB As SurfaceBody = PDComp.SurfaceBodies.Item(1)
Dim oFaces As Faces = SB.Faces

' Create selected face object
Dim oSelectedFace As Inventor.Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a part face")

' Face found boolean flag
Dim bFound As Boolean = False

' Check to make sure a face was selected
If oSelectedFace Is Nothing Then
	MessageBox.Show("No face selected")
	Exit Sub
End If

For i = 1 To oFaces.Count
	Dim oFace As Face = oFaces.Item(i)
	' Check if this face in the collection matches the selected face
	If oFace Is oSelectedFace Then
		' Set the found flag to true
		bFound = True 
		' Show user index + type
		MessageBox.Show("Index: " & i & ControlChars.NewLine & "Type: " & oFace.SurfaceType.ToString, "SurfaceType")
	End If
		
Next

' Check a matching face was found, prompt of not
If bFound = False Then MessageBox.Show("Selected face was not found in the first surface body")

 

Message 3 of 7

jingying_1213
Advocate
Advocate

@matt_jlt 

 

Thanks. Maybe there is some confusion from my expression.

What I mean is -

There are 3 planar faces and 2 cylinder faces from the example. I donot want to pick any face in advance, however, I want to know the index of the highlight face. I can know the index (2, 4 & 5) is the planar face by , but which index is for the highlight face? 

0 Likes
Message 4 of 7

matt_jlt
Collaborator
Collaborator
Accepted solution

I am not quite sure what you mean / what the purpose is for. Maybe i am misunderstanding your requirements?

 

If you want to find the highlighted face index you can use my code, get the index number of the selected face and hard code that in?

for example, if you know the index number for that face is 3 you can either loop through the code or select it directly.

But that face index may change if you adjust the geometry.

 

You can also assign a name or attribute to the face, and use that to find the predetermined face.

 

This is how you get the face directly using the index.

Dim PDoc As PartDocument = ThisApplication.ActiveDocument
Dim PDComp As PartComponentDefinition = PDoc.ComponentDefinition
Dim SB As SurfaceBody = PDComp.SurfaceBodies.Item(1)
Dim oFaces As Faces = SB.Faces

' Clear selection set
PDoc.SelectSet.Clear

' Directly access the face from the faces object
Dim oFace As Inventor.Face = oFaces.Item(2)

'Select the target face
PDoc.SelectSet.Select(oFace)

 

 

 

 

0 Likes
Message 5 of 7

petr.meduna
Advocate
Advocate
Accepted solution

You need something to identify the face. Right click on face and add named entity. Then you can make reference to that face in code. For example:

Sub main()
Dim oPart As PartDocument = ThisApplication.ActiveDocument
Dim oFace As Face = GetFace(oPart.ComponentDefinition, "MyNamedFace")
If oFace IsNot Nothing
	MessageBox.Show("Named face found.")
Else
	MessageBox.Show("Named face not found.")
End If
End Sub

Function GetFace(ByVal comp As PartComponentDefinition, ByVal faceName As String) As Face
	Dim tempStr As String
	For Each oBody As SurfaceBody In comp.SurfaceBodies
		For Each oFace As Face In oBody.Faces
			Try
				tempStr = oFace.AttributeSets.Item("iLogicEntityNameSet").Item(1).Value
				If tempStr = faceName Then
					Return oFace
				End If
			Catch
				Continue For
			End Try
		Next
	Next
	Return Nothing
End Function

 

You can also differentiate faces by measuring area if you know how big will be the area.

0 Likes
Message 6 of 7

jingying_1213
Advocate
Advocate
Thanks, your method can let me identify which face is the specific face I need.
But see below post from @petr.meduna, who uses another way to add a name to this face. That is also a good way.
0 Likes
Message 7 of 7

jingying_1213
Advocate
Advocate

@petr.meduna 

Thanks for your help. Your codes work well to me and provide a good solution.

Then, I find another quick code as below:

iLogic API provides a very simple code that can find the named entity easily.

 

Dim oFace As Face = ThisDoc.NamedEntities.FindEntity("MyNamedEntity")

 

0 Likes