Convert Handle(Geom2d_BSplineCurve) to Handle(Geom2d_Curve)

Agostino De Marco

CAD practitioner
Hello,
I have this problem:

// a plane
gp_Pln plane = ...

// a collection of 3 points
Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d(1,3);
points->SetValue(1, ... );
points->SetValue(2, ... );
points->SetValue(3, ... );

// a 2d interpolating curve
Geom2dAPI_Interpolate interp(points, false, Precision::Confusion());
interp.Perform();
const Handle(Geom2d_BSplineCurve) curve = interp.Curve();

/*
I need to make an edge with OCCT v7.6.3 using the function:

BRepBuilderAPI_MakeEdge (const Handle< Geom2d_Curve > &L, const Handle< Geom_Surface > &S)

so ...
*/

Handle(Geom_Surface) splane = new Geom_Plane(plane);
// how to get a handle to Geom2d_Curve from a Geom2d_BSplineCurve ??
// this line below doesn't work
Handle(Geom2d_Curve) scurve = interp.Curve();
TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(scurve, splane);


I guess this is a lack that I have in understanding the basics of OCCT.
Thank you in advance for your help,
Agostino
 

blobfish

CAD community veteran
Geom2d_BSpline curve is derived from Geom2d_Curve so you should be able to pass that handle straight into BRepBuilderAPI_MakeEdge. If you can't, I am guessing:
const Handle(Geom2d_BSplineCurve) curve = interp.Curve();
needs to be changed to:
const Handle(Geom2d_BSplineCurve) &curve = interp.Curve(); //preferred
or:
Handle(Geom2d_BSplineCurve) curve = interp.Curve();

I found the comments in the handle definition header to be helpful. opencascade::handle
It is also useful to know that 'Handle(T)' is just a macro for opencascade::handle<T>

The documentation generation must be doing something weird.
from doc: BRepBuilderAPI_MakeEdge(const Handle<Geom2d_Curve> &L, const Handle<Geom_Surface> &S)
The parameter types are conflating the template class syntax with the macro syntax. You can see what the true signature is, in the header
 

Agostino De Marco

CAD practitioner
The problem is that BRepBuilderAPI_MakeEdge wants a Handle(Geom2d_Curve)&
and the following line

Code:
const Handle(Geom2d_Curve)& scurve = interp.Curve();

doesn't work. Visual Studio complains as follows: "no suitable user-defined conversion from to exists"
 
Last edited:

Agostino De Marco

CAD practitioner
@JSlyadne so, also this is wrong:
Code:
Handle(Geom_Surface) splane = new Geom_Plane(plane);
Should it be?
Code:
Handle(Geom_Surface) &splane = Handle(Geom_Surface)::DownCast(new Geom_Plane(plane));
 

JSlyadne

Administrator
Staff member
@JSlyadne so, also this is wrong:
Code:
Handle(Geom_Surface) splane = new Geom_Plane(plane);
Should it be?
Code:
Handle(Geom_Surface) &splane = Handle(Geom_Surface)::DownCast(new Geom_Plane(plane));
@Agostino De Marco,

In case of new object allocation the new operator should be used as below:

Code:
Handle(Geom_Plane) splane = new Geom_Plane(plane);

In case of the existing object type-casting, the DownCast(...) operator of the target class should be used like:

Code:
Handle(Geom_Surface) splane = Handle(Geom_Surface)::DownCast(splane);

Since DownCast(...) returns an object, don't assign it to a reference value. I mean in the code you posted above, the "&" before Handle(Geom_Surface) should be removed.

Also I would recommend to avoid object allocation as an input parameter. There is no guarantee that the object allocated this way won't be destroyed before the created object is assigned to the local variable. This can lead to the memmory corruption.
 

Agostino De Marco

CAD practitioner
@JSlyadne thank you for your suggestions

Also I would recommend to avoid object allocation as an input parameter. There is no guarantee that the object allocated this way won't be destroyed before the created object is assigned to the local variable. This can lead to the memmory corruption
Yes I see. This is probably due to my mind set of Java guy.

Regarding the "&", the statement you suggested above in this thread:
Code:
const Handle(Geom2d_Curve)& scurve = Handle(Geom2d_Curve)::DownCast(interp.Curve());
should then be the following:
Code:
const Handle(Geom2d_Curve) scurve = Handle(Geom2d_Curve)::DownCast(interp.Curve());
no "&"?

However, the Downcast operator when applied to my code produces the following message at compilation time: "down-casting from object of the same or unrelated type is meaningless", as of Standard_Handle.hxx
Code:
// a plane
gp_Pln plane = ...

// a collection of 3 points
Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d(1,3);
points->SetValue(1, ... );
points->SetValue(2, ... );
points->SetValue(3, ... );

// a 2d interpolating curve
Geom2dAPI_Interpolate interp(points, false, Precision::Confusion());
interp.Perform();
const Handle(Geom2d_BSplineCurve) curve = interp.Curve();

/*
I need to make an edge with OCCT v7.6.3 using the function:

BRepBuilderAPI_MakeEdge (const Handle< Geom2d_Curve > &L, const Handle< Geom_Surface > &S)

so ...
*/

const Handle(Geom_Plane) gplane = new Geom_Plane(plane);
const Handle(Geom2d_Curve) scurve = Handle(Geom2d_Curve)::DownCast(interp.Curve());
TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(scurve, gplane);
 
Last edited:

Agostino De Marco

CAD practitioner
[SOLVED]
With proper header inclusion, the following code works. No downcasting required.
Code:
...
#include <Geom_Plane.hxx>
#include <Geom2dAPI_Interpolate.hxx>
#include <Geom2d_BSplineCurve.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
...

// a plane
gp_Pln gp_plane = ...
const Handle(Geom_Plane) gplane = new Geom_Plane(gp_plane);

// a collection of 3 points
Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d(1,3);
points->SetValue(1, ... );
points->SetValue(2, ... );
points->SetValue(3, ... );

// a 2d interpolating curve
Geom2dAPI_Interpolate interp(points, false, Precision::Confusion());
interp.Perform();

TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(interp.Curve(), gplane);
 
Top