How to delete a face from a Brep and another question

Simply draw a Brep in Rhino, a cube, and run the following code

 var rhinoObject = doc.Objects.OfType<BrepObject>().FirstOrDefault();
            rhinoObject.BrepGeometry.Faces.RemoveAt(0);
            rhinoObject.BrepGeometry.Compact();
//Why is this line necessary?
            rhinoObject.CommitChanges();
            doc.Views.Redraw();
            return Result.Success;

Why do I need to call CommitChanges for changes to take effect?

Another question

We want to use the Brep.AddEdgeCurve method to dynamically build the model. Where can I use this API?

You cannot directly modify document objects. That is, document objects are read-only. Thus, there is a process you must go through in order to modify document objects.

Here is another approach (that I prefer).

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var brepObject = doc.Objects.OfType<BrepObject>().FirstOrDefault();
  if (null == brepObject)
    return Result.Failure;

  if (brepObject.BrepGeometry.Faces.Count > 1)
  {
    var duplicateBrep = brepObject.BrepGeometry.DuplicateBrep();
    duplicateBrep.Faces.RemoveAt(0);
    duplicateBrep.Compact();

    doc.Objects.Replace(brepObject.Id, duplicateBrep);
    doc.Views.Redraw();
  }

  return Result.Success;
}

Also Brep.AddEdgeCurve is an expert tool that you’d use if you were constructing a Brep from scratch. Why do you want to use this method?

– Dale

1 Like

thank you for your reply.

We hope to realize free modeling on the surface of Brep by combining the function of capturing surfaces. For example, draw a line on one surface, and then this surface is cut into two or more surfaces. At the same time, the topology relationship of the entire Brep is also Corresponding changes have taken place.

We thought we should start with AddEdgeCurve, but didn’t find the right way.

You might try using BrepFace.Split.

– Dale

Tried it, it works. But I can’t split the original Brep like the SplitFace command, instead of producing a new one. Or is there any way to call the SplitFace command by way of API?

I got it