Hi,
I am trying to create breps from pure NURBS data (i.e. control points, weights, knots, degrees, etc.).
Each surface consists of parametric trimming curves.
Here is my code:
protected override bool ReadFile(string filename, int index, RhinoDoc doc, FileReadOptions options)
{
Surface surface = new Surface();
NurbsSurface nurbsSurface = CreateNurbsSurface(surface);
if (nurbsSurface.IsValid)
{
Brep brep = new Brep();
int surfaceIndex = brep.AddSurface(nurbsSurface);
BrepFace face = brep.Faces.Add(surfaceIndex);
if (surface.TrimmingCurves.Any())
{
if (surface.HasOuterTrim)
{
Curve2D curve = surface.TrimmingCurves[0];
BrepLoop loop = brep.Loops.Add(BrepLoopType.Outer, face);
CreateNurbsCurve(curve, brep, loop, nurbsSurface);
for (int i = 1; i < surface.TrimmingCurves.Length; i++)
{
Curve2D innerCurve = surface.TrimmingCurves[i];
BrepLoop innerLoop = brep.Loops.Add(BrepLoopType.Inner, face);
CreateNurbsCurve(innerCurve, brep, innerLoop, nurbsSurface);
}
}
else
{
foreach (Curve2D trimmingCurve in surface.TrimmingCurves)
{
BrepLoop loop = brep.Loops.Add(BrepLoopType.Inner, face);
CreateNurbsCurve(trimmingCurve, brep, loop, nurbsSurface);
}
}
}
string log;
if (brep.IsValidWithLog(out log))
{
if (doc.Objects.AddBrep(brep) == Guid.Empty)
{
Debug.WriteLine("Failed to create Brep!");
return false;
}
}
else
{
Debug.WriteLine("Invalid brep!");
Debug.WriteLine(log);
return false;
}
}
else
{
Debug.WriteLine("Invalid NURBS surface!");
}
}
private NurbsSurface CreateNurbsSurface(Surface surface)
{
Debug.WriteLine("Creating NURBS surface...");
NurbsSurface nurbsSurface = NurbsSurface.Create(3, true, surface.DegreeU + 1, surface.DegreeV + 1,
surface.ControlPointsULength, surface.ControlPointsVLength);
for (int u = 0; u < nurbsSurface.KnotsU.Count; u++)
{
nurbsSurface.KnotsU[u] = surface.KnotsU[u + 1];
}
for (int v = 0; v < nurbsSurface.KnotsV.Count; v++)
{
nurbsSurface.KnotsV[v] = surface.KnotsV[v + 1];
}
ControlPoint[,] controlPoints = new ControlPoint[surface.ControlPointsULength, surface.ControlPointsVLength];
for (int u = 0; u < surface.ControlPointsULength; u++)
{
for (int v = 0; v < surface.ControlPointsVLength; v++)
{
Common.ControlPoint controlPoint = surface.ControlPoints[u, v];
controlPoints[u, v] = new ControlPoint(controlPoint.X, controlPoint.Y, controlPoint.Z, controlPoint.W);
}
}
for (int u = 0; u < nurbsSurface.Points.CountU; u++)
{
for (int v = 0; v < nurbsSurface.Points.CountV; v++)
{
nurbsSurface.Points.SetControlPoint(u, v, controlPoints[u, v]);
}
}
return nurbsSurface;
}
private void CreateNurbsCurve(Curve2D curve, Brep brep, BrepLoop loop, NurbsSurface surface)
{
NurbsCurve nurbsCurve = new NurbsCurve(2, true, curve.Degree + 1, curve.ControlPoints.Length);
for (int i = 0; i < curve.ControlPoints.Length; i++)
{
ControlPoint2D controlPoint = curve.ControlPoints[i];
nurbsCurve.Points.SetPoint(i, controlPoint.X, controlPoint.Y, 0.0, controlPoint.W);
}
for (int i = 0; i < nurbsCurve.Knots.Count; i++)
{
nurbsCurve.Knots[i] = curve.Knots[i + 1];
}
if (nurbsCurve.IsValid)
{
int curve2DIndex = brep.Curves2D.Add(nurbsCurve);
Curve curve3D = surface.Pushup(brep.Curves2D[curve2DIndex], 0.001);
if (curve3D.IsValid)
{
int curve3DIndex = brep.Curves3D.Add(curve3D);
BrepVertex firstVertex = brep.Vertices.Add(curve3D.PointAtStart, 0.001);
BrepVertex lastVertex = brep.Vertices.Add(curve3D.PointAtStart, 0.001);
BrepEdge edge = brep.Edges.Add(firstVertex, lastVertex, curve3DIndex, 0.001);
if (edge.IsValid)
{
BrepTrim trim = brep.Trims.Add(edge, false, loop, curve2DIndex);
trim.TrimType = BrepTrimType.Boundary;
}
else
{
Debug.WriteLine("Invalid edge!");
}
}
else
{
Debug.WriteLine("Invalid 3D curve!");
}
}
}
The log from the brep is:
brep.m_E[0] edge is not valid.
edge.m_vi[]=(0,1) but edge.IsClosed() is true
ON_Brep.m_E[0] is invalid.
Hope anyone can help me.
Thanks,
Mike