Copy Code
// Grasshopper Script Instance
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
public class Script_Instance : GH_ScriptInstance
{
private void RunScript(DataTree<Point3d> inputPts, ref object a)
{
// Write your logic here
a = CreateMeshFromTree(inputPts);
}
Mesh CreateMeshFromTree(DataTree<Point3d> inputPts)
{
Mesh mesh = new Mesh();
for (int i = 0; i < inputPts.BranchCount - 1; i++)
{
List<Point3d> currentBranch = inputPts.Branch(i);
List<Point3d> nextBranch = inputPts.Branch(i+1);
if (currentBranch.Count != nextBranch.Count) continue;
for (int j = 0; j < currentBranch.Count - 1; j++)
{
Point3d pt1 = currentBranch[j];
Point3d pt2 = currentBranch[j+1];
Point3d pt3 = nextBranch[j+1];
Point3d pt4 = nextBranch[j];
int v1 = mesh.Vertices.Add(pt1);
int v2 = mesh.Vertices.Add(pt2);
int v3 = mesh.Vertices.Add(pt3);
int v4 = mesh.Vertices.Add(pt4);
mesh.Faces.AddFace(v1, v2, v3, v4);
}
}
mesh.Vertices.CombineIdentical(true, true);
return mesh;
}
}