How to get an ON_BrepEdge * from an ON_Curve *?

Given an ON_Curve *, how to get the ON_BrepEdge * ?

Hi @XNurbs,

How are you obtaining the ON_Curve*?

– Dale

If it was originally a true ON_BrepEdge (which is basically what @dale is asking), you can try to cast it like so:

ON_Curve* crv; // how was this obtained?
ON_BrepEdge* be = ON_BrepEdge::Cast(crv);
if (be != nullptr)
{
  // ok we're good, continue
}

Here is the code:
go.GetObjects(1, 0);

CRhinoObjRef obj_ref = go.Object(0);

// Get the Rhino object's geometry
const ON_Geometry* geo = obj_ref.Geometry();

// Try casting as a point object
const ON_Point* prnPoint = ON_Point::Cast(prnGeo);
if (prnPoint)
{
	// todo...
	return TRUE;
}

// Try casting as a curve object
const ON_Curve* prnCurve = ON_Curve::Cast(prnGeo);
if (prnCurve)
{
	// todo...

}

So do you mean that we should cast it to an ON_BrepEdge first even using ON::curve_object? could you show me the right code? Many thanks.

Hi @XNurbs,

In the case of edge curves, you can just do this:

const ON_BrepEdge* edge = obj_ref.Edge();
if (nullptr != edge)
{
  // todo...
}

– Dale

Could you tell me the difference between CRhinoGetObject::curve_object and CRhinoGetObject::edge_object? It appears that you can use CRhinoGetObject::curve_object to select an ON_BrepEdge.

Is there any difference between the following code?

const unsigned int filter = ON::curve_object;
CRhinoGetObject go;
go.SetGeometryFilter(filter);

VS

const unsigned int filter = ON::curve_object | CRhinoGetObject::edge_object;
CRhinoGetObject go;
go.SetGeometryFilter(filter);

Many thanks.

The difference between CRhinoGetObject::curve_object and CRhinoGetObject::edge_object is what kind of geometry you’re actually selecting.

An edge_object selects a trimming edge of a brep and generally has type ON_BrepEdge, which can be casted to a curve or used to select the underlying ON_Brep object. This will not however select loose curves.

A curve_object on the other hand selects any curve. The curve may be a circle, a polyline, or a curve on a brep (surface / solid / …) which may be an edge. A generic curve has type ON_Curve where any curve from an edge on a brep can be retrieved as a ON_BrepEdge.

For more information the best place to start is always the Official documentation.

~robin

1 Like

Robin,

Thank you. We need select both edges and curves. If we use the following code, then how could we know if a curve or edge is selected? If you click on a trimming edge of a brep, would the following code select a curve or an edge?

const unsigned int filter = ON::curve_object | CRhinoGetObject::edge_object;
CRhinoGetObject go;
go.SetGeometryFilter(filter);

Selecting curves already allows selecting edges, so adding CRhinoGetObject::edge_object makes no difference as far as I know.

This should work :

CRhinoGetObject go;
go.SetCommandPrompt(L"Select curve or edge");
go.SetGeometryFilter(CRhinoGetObject::curve_object);
go.GetObjects(1, 1);

CRhinoObjRef obj_ref = go.Object(0);
const ON_BrepEdge *edge = obj_ref.Edge();
const ON_Curve *curve = obj_ref.Curve();
if (edge) { // false if edge is a nullptr, same as writing edge != nullptr
    // geometry is a ON_BrepEdge
    RhinoApp().Print("IS AN EDGE \n");
} else if (curve) {
     // geometry is a ON_Curve
    RhinoApp().Print("IS A CURVE \n");
} else {
    // geometry is invalid or not what we're looking for
    return CRhinoCommand::failure;
}

~robin