Creating new Brep from curve on surface

Hi Everyone
I’m new at this Brep stuff. I have one question. Using Rhino SDK I’m trying to create new Brep, I have surface (let’s say plane) and closed curve on it. I want new Brep with the same face as the surface and closed curve as edge (Like cutting up from the surface). Please explain to me the procedure of this.
I do the following:

  • brep->m_S.Append(const_cast<ON_Surface*> (srf)); // adding pointer to the surface
  • brep->m_C3.Append(const_cast<ON_Curve*> (crv)); // adding pointer to the curve
  • ON_BrepVertex& v = brep->NewVertex(crv->PointAtStart()); // adding vertex
  • ON_Curve* crv2d = brep->m_S[0]->Pullback(*crv, 0.0); // pullback and adding 2d curve
  • ON_BrepEdge& edge = brep->NewEdge(v, v, 0); // adding edge
  • ON_BrepFace* face = brep->NewFace(*srf); //This is WRONG
  • //ON_BrepFace* face = brep->NewFace(const_cast<ON_Surface*> (srf), 0, 0, false); // This is NOT WORKING
    … ect…
    Please Help…
    Thanks, Filip

I think you need to use the following

// get the index of the surface that will be added
int si = brep->m_S.Count();
// add the surface
// create a face with the surface index
ON_BrepFace face = brep->NewFace(si);

Thanks menno but I tried that and no result, actually I’m doing this:
-after selecting the surface and curve…

ON_Brep* brep = ON_Brep::New();
brep->m_S.Append(const_cast<ON_Surface*> (surface));
brep->m_C3.Append(const_cast<ON_Curve*> (curve));
ON_Curve* curve2d = brep->m_S[0]->Pullback(*curve, 0.0);
ON_BrepVertex& vertex = brep->NewVertex(curve->PointAtStart());
vertex.m_tolerance = 0.0;
ON_BrepEdge& edge = brep->NewEdge(vertex, vertex, 0);
edge.m_tolerance = 0.0;
ON_BrepTrim& trim = brep->NewTrim(brep->m_T.Count());
ON_BrepLoop& loop = brep->NewLoop(trim.Loop()->outer);
ON_BrepFace& face = brep->NewFace(brep->m_S.Count());

But the topology is not valid and I’m not sure about validation of trim and loop.

The creation of the BREP is quite sensitive in my experience. In the past I had some help using

ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;

on the created BREP to get some information on why the BREP was not valid.

The order in which I do trim creation is:

Create the vertices - returns vertex indices
Create the 3D curves - returns c3 indices
Create the edges - return edge indices
Create the face - returns face
Create the loop - outer or inner loop depending on trim orientation
Create a trim on each edge:
*create the 2D curves - return c2 indices
determine the iso-type (in your case this is not_iso in all cases
create the trim using edge, loop and c2-index



Hi Filip,

In the case of a plane with a closed trimming curve, use ON_BrepTrimmedPlane. See opennurbs_brep.h for details. If you want to know what this function does “under the hood”, download the openNURBS source code from here:

Thank you menno, I use already textlog and helped me a lot. I will try again with this order and I give the best…
PS: Where I can find some examples of building the brep?

Thanks dale, but I want to create Brep step by step to understand better…

There are some examples here

In particular interest, I think, is FaceWithHole and TrimmedPlane.

Like I said, if you want to know what happens “under the hood”, download the openNURBS source…

Hi again
Thanks for your efforts
Now, I try this:

FILE* log_file = ON::OpenFile( L"C:\\report\\bug_report.txt", L"w" );
ON_TextLog textLog(log_file);

CRhinoGetObject* surface_object = new CRhinoGetObject();
surface_object->SetCommandPrompt(L"Select surface");
surface_object->GetObjects(1, 1);

if (surface_object->CommandResult() != CRhinoCommand::success)
	return surface_object->CommandResult();

CRhinoObjRef surface_ref = surface_object->Object(0);
const ON_Surface* surface = surface_ref.Surface();

CRhinoGetObject* curve_object = new CRhinoGetObject();
curve_object->SetCommandPrompt(L"Select closed curve on surface");
curve_object->GetObjects(1, 1);

if (curve_object->CommandResult() != CRhinoCommand::success)
	return curve_object->CommandResult();

CRhinoObjRef curve_ref = curve_object->Object(0);
const ON_Curve* curve = curve_ref.Curve();

ON_Brep* brep = ON_Brep::New();

ON_BrepVertex& vertex = brep->NewVertex(curve->PointAtStart());
vertex.m_tolerance = 0.0;
if (!vertex.IsValid(&textLog))
	RhinoApp().Print(L"Vertex ERROR!!!\n");

int c3i = brep->AddEdgeCurve(curve->DuplicateCurve());

ON_BrepEdge& edge = brep->NewEdge(vertex, vertex, c3i);
edge.m_tolerance = 0.0;
if (!edge.IsValid(&textLog))
	RhinoApp().Print(L"Edge ERROR!!!\n");

int si = brep->AddSurface(surface->DuplicateSurface());
ON_BrepFace& face = brep->NewFace(si);

ON_BrepLoop& loop = brep->NewLoop(ON_BrepLoop::outer, face);

if (!loop.IsValid(&textLog))
	RhinoApp().Print(L"Loop ERROR!!!\n");

ON_Curve* curve2d = brep->m_S[0]->Pullback(*curve, 0.0);
int c2i = brep->AddTrimCurve(curve2d);

ON_BrepTrim& trim = brep->NewTrim(edge, false, loop, c2i);
trim.m_tolerance[0] = 0.0; // parametar u
trim.m_tolerance[1] = 0.0; // parametar v
//trim.m_type = ON_BrepTrim::boundary;
//trim.m_iso = ON_Surface::not_iso;

if (!trim.IsValid(&textLog))
	RhinoApp().Print(L"Trim ERROR!!!\n");

if (!brep->IsValidGeometry(&textLog))
	RhinoApp().Print(L"Geometry ERROR!!!\n");

if (!brep->IsValidTopology(&textLog))
	RhinoApp().Print(L"Topology ERROR!!!\n");

if (!brep->IsValid(&textLog))
	RhinoApp().Print(L"Brep ERROR!!!\n");




But the loop is invalid and I don’t know why…

here is a 3dm file … … please help

@GregArden, can you look into this?

Yes I can, but no help :frowning: