Turn ExtrusionObject into BrepObject

I’m noticing that when I go to GetObject with GeometryFilter = Brep, sometimes I get a BrepObject, and sometimes I get an ExtrusionObject. Is there a good way to turn the ExtrusionObject into a BrepObject? Must I delete the extursion object from the doc and add it back in its brep form? This is a little bit annoying because I would also have to sychronize their attributes, and add this code everywhere I prompt a user to select a Brep, I guess I was just wondering If there is a better way?

Best,
Nick

Hi @Nick_Drian,

A couple of options:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  GetObject go = new GetObject();
  go.SetCommandPrompt("Select surface or polysurface");
  go.GeometryFilter = ObjectType.Surface | ObjectType.PolysrfFilter;
  go.SubObjectSelect = false;
  go.Get();
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  ObjRef objRef = go.Object(0);
  RhinoObject rhinoObject = objRef.Object();
  if (null == rhinoObject)
    return Result.Failure;

  ObjectType objectType = rhinoObject.ObjectType;
  RhinoApp.WriteLine("Object type: {0}", objectType.ToString());

  // Option 1:
  if (objectType == ObjectType.Extrusion)
  {
    if (rhinoObject is ExtrusionObject extrusionObject)
    {
      Extrusion extrusion = extrusionObject.ExtrusionGeometry;
      if (null != extrusion)
      {
        // TODO...
      }
    }
  }
  else if (objectType == ObjectType.Brep)
  {
    if (rhinoObject is BrepObject brepObject)
    {
      Brep brep = brepObject.BrepGeometry;
      if (null != brep)
      {
        // TODO...
      }
    }
  }
      
  // Option 2:
  if (rhinoObject.Geometry is Extrusion extrusionGeometry)
  {
    // TODO...
  }
  else if (rhinoObject.Geometry is Brep brepGeometry)
  {
    // TODO...
  }

  return Result.Success;
}

– Dale

The solution that I was wondering about would be a more convenient way to make sure that all objects returned by GetObject() GeometryFilter=Brep are infact BrepObjects. The following seems to work, it is just more complicated than I would like:

var brepObj = obj as BrepObject;

if (brepObj != null)
{
    m_Brep = brepObj.BrepGeometry;
    m_BrepId = brepId;
}
else
{
    var extrusionObj = obj as ExtrusionObject;
    if (extrusionObj == null)
    {
        throw new Exception(
            "GetObject() GeometryFilter = Brep seems to get things that are not breps or extrusions"
        );
    }
    m_Brep = extrusionObj.ExtrusionGeometry.ToBrep();
    var attributes = extrusionObj.Attributes;
    m_BrepId = RhinoDoc.ActiveDoc.Objects.Add(m_Brep);
    obj = RhinoDoc.ActiveDoc.Objects.FindId(m_BrepId);
    brepObj = obj as BrepObject;
    brepObj.Attributes = attributes;
    brepObj.CommitChanges();
    RhinoDoc.ActiveDoc.Objects.Delete(extrusionObj, true, true);
}

Hi @Nick_Drian,

If you only want to select Breps, then perhaps something like this?

GetObject go = new GetObject();
go.SetCommandPrompt("Select surface or polysurface");
go.SetCustomGeometryFilter((rhinoObject, geometry, componentIndex) =>
{
  return rhinoObject.ObjectType == ObjectType.Brep;
});
go.SubObjectSelect = false;
go.Get();
if (go.CommandResult() != Result.Success)
  return go.CommandResult();

– Dale