Skip to content

Instantly share code, notes, and snippets.

@ikourfaln
Last active November 13, 2022 15:42
Show Gist options
  • Save ikourfaln/49694524847ab7527f8407bbfd7952a1 to your computer and use it in GitHub Desktop.
Save ikourfaln/49694524847ab7527f8407bbfd7952a1 to your computer and use it in GitHub Desktop.
Grasshopper Ayman
using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
/// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
/// <param name="text">String to print.</param>
private void Print(string text) { /* Implementation hidden. */ }
/// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
/// <param name="format">String format.</param>
/// <param name="args">Formatting parameters.</param>
private void Print(string format, params object[] args) { /* Implementation hidden. */ }
/// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj) { /* Implementation hidden. */ }
/// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion
#region Members
/// <summary>Gets the current Rhino document.</summary>
private readonly RhinoDoc RhinoDocument;
/// <summary>Gets the Grasshopper document that owns this script.</summary>
private readonly GH_Document GrasshopperDocument;
/// <summary>Gets the Grasshopper script component that owns this script.</summary>
private readonly IGH_Component Component;
/// <summary>
/// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
/// Any subsequent call within the same solution will increment the Iteration count.
/// </summary>
private readonly int Iteration;
#endregion
/// <summary>
/// This procedure contains the user code. Input parameters are provided as regular arguments,
/// Output parameters as ref arguments. You don't have to assign output parameters,
/// they will have a default value.
/// </summary>
private void RunScript(Surface surf, Point3d P, double theta, double orientation, ref object A, ref object lignePli, ref object regles, ref object P0)
{
// Paramètres
double step = 0.1; //Longueur entre deux points
//Traçage d'une ligne de pli à 90°
// par Xavier Tellier
// 10 décembre 2020
Brep brep0 = Brep.CreateFromSurface(surf);
double u0;
double v0;
surf.ClosestPoint(P, out u0, out v0);
double u_i = u0;
double v_i = v0;
Plane frame;
surf.FrameAt(u_i, v_i, out frame);
Vector3d n = frame.Normal;
Vector3d w0 = Vector3d.CrossProduct(n, new Vector3d(0, -1, 0));
w0.Unitize(); w0.Rotate(theta * Math.PI / 180, n);
Vector3d w_i = w0; Point3d Pi = surf.PointAt(u_i, v_i);
List<Point3d> listP = new List<Point3d>();
listP.Add(Pi);
List<Vector3d> listNorm = new List<Vector3d>();
int i_max = 0;
for (int i = 0; i < 5000; i++) {
i_max = i; Vector3d ri = surf.NormalAt(u_i, v_i);
listNorm.Add(ri);
ri.Rotate(0.25 * orientation * Math.PI, w_i);
Point3d P_L2 = Pi + w_i * step;
Line L2 = new Line(P_L2 + 5 * step * ri, P_L2 - 5 * step * ri);
Curve Lc2 = new LineCurve(L2);
Curve[] listCurve = new Curve[0];
Point3d[] listX = new Point3d[2];
Rhino.Geometry.Intersect.Intersection.CurveBrep(Lc2, brep0, 0.00001, out listCurve, out listX);
if(listX.Length == 0) { break;}
Point3d Pnext = listX[0];
w_i = Pnext - Pi;
w_i.Unitize();
Pi = Pnext;
surf.ClosestPoint(Pi, out u_i, out v_i);
listP.Add(Pi);
Vector3d N_i = Vector3d.CrossProduct(w_i, ri);
N_i.Unitize();
}
List<Vector3d> listRegles = new List<Vector3d>();
Vector3d r_i = surf.NormalAt(u_i, v_i);
listNorm.Add(r_i);
for (int i = 0; i < i_max; i++) {
Vector3d t = listP[i + 1] - listP[i];
listNorm[i] = Vector3d.CrossProduct(t, listNorm[i]);
}
for (int i = 0; i < i_max - 1; i++) {
Vector3d dn = listNorm[i + 1] - listNorm[i];
Vector3d r = Vector3d.CrossProduct(listNorm[i], dn);
r.Unitize();
listRegles.Add(-r);
}
A = w0;
lignePli = new Polyline(listP);
P0 = surf.PointAt(u0, v0);
regles = listRegles;
}
// <Custom additional code>
// </Custom additional code>
}
// </Custom additional code>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment