I am trying to split a bunch of surfaces using curves but the component takes so long… I wonder if there is a way of optimizing the script, maybe using a c# magic component or similar.
The input I have is a data tree of polylines, and a x,y,z interval which is used to divide the polylines. I would like to keep the data structure used in the initial polylines input. Maybe it is just better to operate in polylines instead of surface to reduce the computational time but I don’t know how to split closed polylines by lines.
Also, the final output can be either surfaces or closed polylines
It is possible to intersect in parallel using C# script, this speeds up the intersections on my machine by about three-fold, going from 8 seconds to 2.7 or so.
Take care to put input Tree Access and type hint Brep on the input and output ports.
// Grasshopper Script Instance
#region Usings
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Rhino;
using Rhino.Geometry;
using System.Threading.Tasks;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
#endregion
public class Script_Instance : GH_ScriptInstance
{
private void RunScript(DataTree<Brep> x, DataTree<Curve> y, ref object a)
{
// check that all paths exist on both trees
foreach(var path in x.Paths)
{
if (!y.PathExists(path))
{
Console.WriteLine("Path not found");
return;
}
}
foreach(var path in y.Paths)
{
if (!x.PathExists(path))
{
Console.WriteLine("Path not found");
return;
}
}
DataTree<Brep> result = new DataTree<Brep>();
// use a parallel task to perform the intersections
Parallel.For(0, x.Paths.Count, i =>
{
GH_Path p = x.Paths[i];
Brep b = x[p, 0];
IEnumerable<Curve> splitters = y.Branch(p);
Brep[] splitted = b.Split(splitters, 0.001);
result.AddRange(splitted, p);
});
a = result;
}
}