brep.Faces return untrimmed faces

Hi everyone!

I am trying to highlight the face of the selected polysurface according to the algorithm below, but when I add BrepFace as a new object to the ActiveDoc, an untrimmed version of the face appears. What is the reason for that? and how to fix it? Thanks!

    public static (Surface,string) extractSurface(ObjRef objRef)
    {
        RhinoDoc doc = RhinoDoc.ActiveDoc;
        // set point
        RhinoObject obj = objRef.Object();
        Point3d Pt= new Point3d(23000, 0.5, 13000); // example point
        // get thickness value of object
        string thickness = obj.Attributes.GetUserString("Thickness"); // not important
        if (thickness == null) thickness = CommonMethods.getThickness(objRef).ToString();

        string layerName = doc.Layers[obj.Attributes.LayerIndex].Name;
        string objName = obj.Attributes.Name;

        var brep = objRef.Brep();
        var faces = brep.Faces;
        Dictionary<BrepFace, double> faceDict = new Dictionary<BrepFace, double>();
        foreach(var face in faces)
            faceDict.Add(face, AreaMassProperties.Compute(face).Area);
        var faceList = faceDict.ToList();
        faceList.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value)); faceList.Reverse();
        var face1 = faceList[0].Key;
        var face2 = faceList[1].Key;

        var dist1 = AreaMassProperties.Compute(face1).Centroid.DistanceTo(Pt);
        var dist2 = AreaMassProperties.Compute(face2).Centroid.DistanceTo(Pt);

        BrepFace finalFace;
        if (layerName.Contains("ControlString"))
        {
            if (dist2 > dist1) finalFace = face1;
            else finalFace = face2;
        } else
        {
            if (dist1 > dist2) finalFace = face1;
            else finalFace = face2;
        }
        finalFace.SetUserString("Thickness", thickness);
        var guid = doc.Objects.Add(finalFace);
        var faceObj = doc.Objects.FindId(guid);
        faceObj.Attributes.ColorSource = ObjectColorSource.ColorFromObject;
        faceObj.Attributes.ObjectColor = Color.Red;
        faceObj.CommitChanges();
        return (finalFace, thickness);
    }

brepFaceUntrimmed.3dm (387.7 KB)

A BrepFace is not a stand-alone geometric entity. It is part of a Brep where the trimming information is stored, but it also inherits from Rhino.Geometry.Surface. So when you treat it as a standalone entity, it will fall back to its parent class and loose all trimming information.

If you want to “extract” a face including its trimming information, you must create a new BRep that consists of only the face. This is done using BrepFace.DuplicateFace method, which returns a Brep object.

2 Likes

Thanks a lot !

Hi @menno, it’s me again, I am facing with a similar issue but this time with Curve. I tried “DuplicateCurve” similar to the previous solution, but it didn’t work. This time I add a closed curve to the doc, move corner points, get edited curve data with the same GUID. I also tried “DuplicateGeometry” while getting Curve from the found object with the same Guid, but the same result.

            RhinoObject obj = RhinoDoc.ActiveDoc.Objects.FindId(guid);
            if(obj != null)
            {
                Curve curve = obj.DuplicateGeometry() as Curve;
                curves.Add(curve.DuplicateCurve());
            }

You’re probably better of by creating an object reference from the guid and getting the curve directly:

Guid guid; // defined elsewhere
ObjRef a = new ObjRef(guid);
Curve crv = a.Curve(); 
if (null != crv)
{
  curves.Add(crv.DuplicateCurve());
}
else
{
  // handle error situation
}
1 Like

Thanks for the answer, unfortunately using “ObjRef” didn’t solve the issue.

Then I don’t know, sorry. Are you sure that the object is still active in the document, i.e. not deleted?

1 Like

Ok, I found the issue, it is because of another function I missed to check. The function changes the result curve so I thought that the error comes from Curve data itself. Stopping debugger at suspicious line and adding curve to the doc helped me to realize that the error occurs at another point. Sorry for the interruption. Thanks