Late to the party, I have a Divide Distance Multiple
component.
magicteddy.gha (78 KB)
protected override void SolveInstance(IGH_DataAccess DA)
{
Curve curve = null;
bool wrap = true;
List<double> distances = new List<double>();
if (!DA.GetData(0, ref curve))
{
return;
}
if (!DA.GetDataList(1, distances))
{
return;
}
if (!DA.GetData(2, ref wrap))
{
return;
}
if (curve==null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Null Curve.");
return;
}
if (distances.Count==0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "At least one distance is required.");
return;
}
if (curve.GetLength() < distances[0])
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Curve total length is smaller than the first distance.");
}
Point3d currPt = curve.PointAtStart;
List<Point3d> pts = new List<Point3d>() { currPt };
List<double> dividedParams = new List<double>() { curve.Domain.T0 };
List<Vector3d> tangents = new List<Vector3d>() { curve.TangentAtStart };
Curve c = curve.DuplicateCurve();
int i = 0;
double tol = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;
while (i < distances.Count)
{
double r = distances[i];
Sphere s = new Sphere(currPt, r);
if (Rhino.Geometry.Intersect.Intersection.CurveBrep(c, s.ToBrep(), tol, out Curve[] cc, out Point3d[] pc, out double[] t))
{
if (t.Length > 0)
{
Array.Sort(t, pc);
currPt = pc[0];
pts.Add(currPt);
dividedParams.Add(t[0]);
tangents.Add(c.TangentAt(t[0]));
c = c.Trim(t[0], c.Domain.T1);
if (wrap)
{
distances.Add(distances[i]);
}
i++;
}
else
{
//no more tries
break;
}
}
else
{
//no more tries
break;
}
}
DA.SetDataList(0, pts);
DA.SetDataList(1, tangents);
DA.SetDataList(2, dividedParams);
}