Obtain geometry boundingbox center after its position changed

I want to create GeometryB at the boundingbox center of GeometryA,

I tried using the following code, but it didn’t work since centerPointA return {0.0, 0.0, 0.0}.

Did I use CRhinoMeshObject and CRhinoObjRef incorrectly?

myClass{
    ...
public:
    CRhinoMeshObject* m_rGeoA= nullptr;
    CRhinoObjRef m_RefGeoA;
};

void MemberFun1(){
    // Imported a ON_Mesh* GeoA and store its reference
    ...
    m_rGeoA= RhinoApp().ActiveDoc()->AddMeshObject(*GeoA );
    m_RefGeoA= CRhinoObjRef(m_rGeoA);
}

// Drag and move GeoA in Rhino Window by user
...

void MemberFunc2(){
    // Create a ON_Mesh* GeoB with a center point at (0.0, 0.0, 0.0)
    ...
    CRhinoMeshObject* m_rGeoB = RhinoApp().ActiveDoc()->AddMeshObject(*GeoB);

    // Transform its position to the imported mesh
    ON_3dPoint centerPointA = m_RefGeoA.Object()->BoundingBox().Center();
    ON_Xform transform = ON_Xform::TranslationTransformation(centerPointA - ON_3dPoint(0.0, 0.0, 0.0));
    RhinoApp().ActiveDoc()->TransformObject(m_rGeoB , transform, true, true, true);
}

Hi @Liu2,

Maybe this helps?

CRhinoCommand::result CCommandTest::RunCommand(
  const CRhinoCommandContext& context
)
{
  CRhinoGetObject go;
  go.SetCommandPrompt(L"Select meshes");
  go.SetGeometryFilter(CRhinoGetObject::mesh_object);
  go.EnableSubObjectSelect(false);
  go.GetObjects(1, 0);
  if (go.CommandResult() != CRhinoCommand::success)
    return go.CommandResult();

  const int obj_count = go.ObjectCount();
  for (int i = 0; i < obj_count; i++)
  {
    const CRhinoObjRef& obj_ref = go.Object(i);
    const ON_Mesh* pMesh = obj_ref.Mesh();
    if (nullptr == pMesh)
      return CRhinoCommand::failure;

    ON_BoundingBox bbox;
    if (pMesh->GetBoundingBox(bbox))
    {
      ON_3dPoint center = bbox.Center();
      ON_wString str;
      RhinoFormatPoint(center, str);
      RhinoApp().Print(L"Center %d: %ls\n", i, static_cast<const wchar_t*>(str));
      context.m_doc.AddPointObject(center);
    }
  }

  context.m_doc.Redraw();
  return CRhinoCommand::success;
}

– Dale

Thanks, Dale.
My previous description may not have been very clear. Allow me to provide a detailed description of the actual problem:

I am developing a surface parameterization plugin, which requires selecting a surface first and then generating a planar mesh. Since users may move the surface after selecting it, I would like to set the generation position of the planar mesh at the center point of the surface.

Another question:
I’m a bit confused about the relationship between CRhinoMeshObject, CRhinoObjRef, CRhinoMeshObject.Geometry(), and CRhinoMeshObject.Mesh().

When I want to repositioned a user-selected mesh on the screen, which data structure should I be interacting with?

When I want to store a user-selected object for further use, which element type should I use?