I tried to find out how to get the closest boundary curves (if any) of an ON_Brep object, unsuccessfully. I’m looking for a function like Rhino.DuplicateSurfaceBorder in RhinoScript. Is there a C++ SDK function doing this?

This explanation of ON_Brep is nice, but unfortunately it doesn’t show the case of polysurfaces. I guess I need to traverse the boundary edges in order to achieve what I’m looking for.

# Get closed boundary curves of ON_Brep

**snabela**(Alexander Schiftner) #1

**menno**(Menno Deij - van Rijswijk) #2

This seems to work for an open BREP. As you suggest, you need to traverse the boundary edges.

NOTE I was using the Rhino SDK, not OpenNURBS. But I don’t think I was using any proprietary stuff.

```
const ON_Brep* b; // obtained from document or elsewhere
ON_SimpleArray<const ON_Curve*> curves;
for(int i = 0; i < b->m_L.Count(); ++i)
{
const ON_BrepLoop& bl = b->m_L[i];
// get outer loops
if (bl.m_type == ON_BrepLoop::TYPE::outer)
{
for(int i = 0; i < bl.m_ti.Count(); ++i)
{
const ON_BrepTrim& t = b->m_T[bl.m_ti[i]];
if (t.m_ei < 0)
continue;
// only look at boundary type trims
if (t.m_type != ON_BrepTrim::TYPE::boundary)
continue;
// get the brep edge for the trim
const ON_BrepEdge& e = b->m_E[t.m_ei];
curves.Append(b->m_C3[e.m_c3i]);
}
}
}
for(int i = 0; i < curves.Count(); ++i)
{
const ON_Curve* pCrv = curves[i];
context.m_doc.AddCurveObject(*pCrv);
}
```

**dale**(Dale Fugier) #3

Here is my version:

```
/*
Description:
Duplicate the border curves of a Brep.
Parameters:
InBreps [in] - The input Brep.
OutCurves [out] - The border curves. Note carefully: memory for the curves
is allocated and becomes the responsibility of the caller.
Return:
The number of curves added to the OutCurves array..
Remarks:
In order to to duplicate the border curves of surfaces and polysurfaces,
you need to search the object for naked edges. A naked edges is one that
is not connect to other edges. Also, the OutCurves array will contain
individual, unjoined curves. If you need them joined, use ON_JoinCurves.
*/
int RhinoBrepDuplicateBorder(const ON_Brep* InBrep, ON_SimpleArray<ON_Curve*>& OutCurves)
{
if (0 == InBrep)
return 0;
const int OutCurves_Count = OutCurves.Count();
for (int i = 0; i < InBrep->m_E.Count(); i++)
{
const ON_BrepEdge& edge = InBrep->m_E[i];
// Find only the naked edges
if (edge.m_ti.Count() == 1 && edge.m_c3i >= 0)
{
ON_Curve* curve = edge.DuplicateCurve();
// Make the curve direction go in the natural
// boundary loop direction so that the curve
// directions come out consistantly
if (InBrep->m_T[edge.m_ti[0]].m_bRev3d)
curve->Reverse();
if (InBrep->m_T[edge.m_ti[0]].Face()->m_bRev)
curve->Reverse();
OutCurves.Append(curve);
}
}
return OutCurves.Count() - OutCurves_Count;
}
```