What is the detail difference between Add and Replace?

   for (int i = 0; i < selectedBrep.Faces.Count; i++)
                        if (Face == selectedBrep.Faces[i])
                    var joinedBrep = Brep.JoinBreps(new[] { newBrep,selectedBrep }, 1)?.OrderByDescending(x => x.GetVolume())?.FirstOrDefault();
                    if (joinedBrep != null)
                        doc.Objects.Replace(SelectedBrepObject.Id, joinedBrep);
                        brepJoined = true;

As you can see, the result after Replace is wrong;

I need to use Replace because selectedBrep is an existing Brep and I want to keep some information on it

Hi @ken_zhang,

The ObjectTable.Add methods add new geometry to the document.

The ObjectTable.Replace methods replace one object with another. Conceptually, this function is the same as calling ObjectTable.Delete and then ObjectTable.Add.

If the intent is to replace existing geometry, then the best practice is to make a copy of the original object’s geometry, modify it, and then call ObjectTable.Replace.

For example:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
  var go = new GetObject();
  go.SetCommandPrompt("Select polysurface");
  go.GeometryFilter = ObjectType.PolysrfFilter;
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  var objref = go.Object(0);
  var brep = objref.Brep();
  if (null == brep || brep.Faces.Count < 2)
    return Result.Failure;

  var new_brep = brep.DuplicateBrep();

  doc.Objects.Replace(objref.ObjectId, new_brep);

  return Result.Success;

Hope this helps.

– Dale


I want to extrude a new solid on a face of a solid, then merge it with the original and replace the original, and make sure the faces are not duplicated.

Hi @ken_zhang,

Have a look at BrepFace.CreateExtrusion.

– Dale

Hi @dale

BrepFace.CreateExtrusion solved the problem to some extent, but also encountered other problems.
But sometimes the Extrusion result will be a Non-Manifold Brep, and I initially tried some methods to fix it, but the results are not very good.

And I found that the Extrusion function that comes with Rhino does not have this problem, that is, the Extrusion function is realized by dragging the Gumbal, and I found that this function handles many details much better than us.

So we hope to directly call the Extusion function that comes with Rhino, but this should not end with a simple CreateExtrusion.

The reason we don’t want to use Gumbal directly is that we want to provide users with a more concise and comfortable operating experience, and Rhino’s own functions are more serious.

I seem to have some clues.

If I use Face.CreateExtrusion, I need to deal with a lot of details, but at this time Replace is in effect

If Brep.CreateBooleanUnion is used, there are no details, but Replace is invalid at this time, and the problem at this time is the title of this topic.

I think I found the solution and everything is perfect.