How to filter the smallest face in the component

How to filter the smallest face in the component

623526308
Participant Participant
463 Views
8 Replies
Message 1 of 9

How to filter the smallest face in the component

623526308
Participant
Participant

I want to write a loop function that filters out the faces with the smallest area in the component and that also contains curved surfaces, how do I construct this function?

0 Likes
Accepted solutions (2)
464 Views
8 Replies
Replies (8)
Message 2 of 9

gilsdorf_e
Collaborator
Collaborator

Hmm, quick reply before heading home:

 

I think I would get all SurfaceBodies in PartComponentDefinition.SurfaceBodies and iterate through them.

For each SurfaceBodies get the faces from SurfaceBody.Faces.

 

You can set the first one's area as start value with Face.Evaluator.Area and then iterate through all face if you find a smaller one. If yes, replace the comparer with that face's area.

 

 

 

 

0 Likes
Message 3 of 9

JelteDeJong
Mentor
Mentor
Accepted solution

You can loop over bodies/faces like this:

Dim doc As PartDocument = ThisDoc.Document
Dim bodies = doc.ComponentDefinition.SurfaceBodies

Dim smallestFaceArea As Double = Double.MaxValue
Dim smallestFace As Face = Nothing
For Each body As SurfaceBody In bodies
    For Each face As Face In body.Faces
        Dim area = face.Evaluator.Area
        If (smallestFaceArea > area) Then
            smallestFace = face
            smallestFaceArea = area
        End If
    Next
Next

doc.SelectSet.Clear()
doc.SelectSet.Select(smallestFace)

or if you don't want to loop, then this is also possible:

Dim doc As PartDocument = ThisDoc.Document

Dim smallestFace = doc.ComponentDefinition.SurfaceBodies.
	Cast(Of SurfaceBody).
	SelectMany(Of Face)(Function(f) f.Faces.Cast(Of Face)).
	Aggregate(Function(face, nextFace) If(face.Evaluator.Area < nextFace.Evaluator.Area, face, nextFace))

doc.SelectSet.Clear()
doc.SelectSet.Select(smallestFace)

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 4 of 9

623526308
Participant
Participant

I wrote the code in C# according to the code you gave me, but it didn't work, could you please help me to check the problem? Error reported on the last line of the runtime, parameter error.

var bodieError reported on the last line of the runtime, parameter errors = compDef.SurfaceBodies;
                Double smallestFaceArea=double.MaxValue;
                Face smallestFace=null;
                foreach (SurfaceBody body in bodies)
                {
                    foreach (Face face in body.Faces)
                    {
                        var area = face.Evaluator.Area;
                        if (smallestFaceArea > area)
                        {
                            smallestFace = face;
                            smallestFaceArea = area;
                        }
                    }
                }
               
                compDef.Features.DeleteFaceFeatures.Add(smallestFace, true);

what is the problem?

0 Likes
Message 5 of 9

gilsdorf_e
Collaborator
Collaborator
Accepted solution

If you use "Heal = true" parameter, according to help your input needs to be a FaceCollection not a Face.

 

Inventor 2022 Help | DeleteFaceFeatures.Add Method | Autodesk

0 Likes
Message 6 of 9

623526308
Participant
Participant

Thank you. I got it

 

0 Likes
Message 7 of 9

623526308
Participant
Participant

I'm sorry to bother you again, but I'm having a new trouble about this issue. I have created the FaceCollection, but when Heal=true, the program still doesn't run, like this.

 Face[] deleteFace = new Face[8];
            for (int i = 0; i < 8; i++)
            {
                var bodies = compDef.SurfaceBodies;
                Double smallestFaceArea=double.MaxValue;
                Face smallestFace=null;
                foreach (SurfaceBody body in bodies)
                {
                    foreach (Face face in body.Faces)
                    {
                        var area = face.Evaluator.Area;
                        if (smallestFaceArea > area)
                        {
                            smallestFace = face;
                            smallestFaceArea = area;
                        }
                    }
                }
            deleteFace[i] = smallestFace;
            Inventor.FaceCollection delete = inventorApp.TransientObjects.CreateFaceCollection();
            delete.Add(deleteFace[i]);
            compDef.Features.DeleteFaceFeatures.Add(delete,true);
            }

but when I delete the value ‘true’. It can work.

but I really want to heal the face.

0 Likes
Message 8 of 9

gilsdorf_e
Collaborator
Collaborator

Hi, your iteration looks odd. Are you trying to delete the 8 smallest faces of each SurfaceBody? 

You should add a condition that handles the case that no face is smaller than the initial value. Otherwise smallestFace is null when trying to delete it.

 

Can you interactively delete the exact faces and let Inventor heal that? Maybe Inventor cannot automatically can heal this case.

 

0 Likes
Message 9 of 9

623526308
Participant
Participant

yes. I want to delete the 8 faces in my model and I am pretty sure that the iteration can select these faces.

Because when I change the last line to

compDef.Features.DeleteFaceFeatures.Add(delete)

It works well.

 

the problem is the Boolean parameter in DeleteFaceFeature. When I change the parameter to true. it will have error

compDef.Features.DeleteFaceFeatures.Add(delete,true)

0 Likes