I am trying to split a brep with some curves. The split does not work in a plugin setting using the RhinoCommon Brep.Split function, but works if I manually use the Rhino Split command.
To replicate, use the following .3dm file:
customSplit.3dm (416.2 KB)
and the following plugin script:
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Geometry.Intersect;
using Rhino.Input;
using Rhino.Input.Custom;
public class MyCurveCutter : Command
{
public MyCurveCutter()
{
Instance = this;
}
public static MyCurveCutter Instance { get; private set; }
public override string EnglishName => "MyCurveCutter";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
Brep a;
using (GetObject go = new())
{
go.SetCommandPrompt("Choose first object, which will create splitting curve");
go.GeometryFilter = ObjectType.Brep;
go.DisablePreSelect();
if (go.Get() == GetResult.Cancel)
{
return Result.Cancel;
}
a = go.Object(0).Brep();
}
Brep b;
using (GetObject go = new())
{
go.SetCommandPrompt("Choose second object, which will be split");
go.GeometryFilter = ObjectType.Brep;
go.DisablePreSelect();
if (go.Get() == GetResult.Cancel)
{
return Result.Cancel;
}
b = go.Object(0).Brep();
}
Intersection.BrepBrep(a, b, 0.1, true, out Curve[] curves, out _);
curves = Curve.JoinCurves(curves, 0.1);
if (curves == null || curves.Length != 1)
{
RhinoApp.WriteLine("breps did not intersect correctly");
if (curves == null)
{
RhinoApp.WriteLine("intersection returned null");
}
else
{
RhinoApp.WriteLine("intersection found " + curves.Length + " curves");
foreach (Curve c in curves)
{
doc.Objects.Add(c);
}
doc.Views.Redraw();
}
return Result.Failure;
}
Curve curve = curves[0];
// these use the assumption that the second selected thing is in line with the Y axis
Curve startExtension = new LineCurve(new Line(curve.PointAtStart, 1000 * Vector3d.YAxis));
Curve endExtension = new LineCurve(new Line(curve.PointAtEnd, 1000 * Vector3d.YAxis));
Brep[] splits = b.Split([startExtension, curve, endExtension], 0.1);
if (splits == null || splits.Length != 2)
{
RhinoApp.WriteLine("split failed?");
if (splits == null)
{
RhinoApp.WriteLine("split returned null");
}
else
{
RhinoApp.WriteLine("split returned " + splits.Length + " pieces");
foreach (Brep piece in splits)
{
doc.Objects.Add(piece);
}
}
// bake curves so you can try it manually
doc.Objects.Add(curve);
doc.Objects.Add(startExtension);
doc.Objects.Add(endExtension);
doc.Views.Redraw();
return Result.Failure;
}
else
{
RhinoApp.WriteLine("split succeeded");
foreach (Brep piece in splits)
{
doc.Objects.Add(piece);
}
doc.Views.Redraw();
return Result.Success;
}
}
}
First select the round shape, then select the plane. The script fails but will add the splitting curves to the document. Then, use the Rhino Split command with the plane and the added curves – it succeeds. Clearly either something happens in the process of adding the curves to the document or something is done differently in the Brep.Split method and the Split command.
My question is: How can I get RhinoCommon to successfully perform this split?
There are a number of assumptions made in the command code for the sake of replicating the behavior (e.g. extending the curve along the Y axis). The code I actually care about lives in a larger workflow, so in the general case I can’t make certain assumptions. In particular, splitting with curve projection is not what I want.
Thanks,
- Russell Emerine
