Skip to content

Instantly share code, notes, and snippets.

@stephensmitchell
Forked from jhorikawa/GearGH.cs
Created January 13, 2020 08:39
Show Gist options
  • Select an option

  • Save stephensmitchell/1281b8e9227ca7160e05fe9f22631687 to your computer and use it in GitHub Desktop.

Select an option

Save stephensmitchell/1281b8e9227ca7160e05fe9f22631687 to your computer and use it in GitHub Desktop.
Grasshopper C# component script for gear simulation.
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(List<Curve> D, Curve FI, Point3d AX, List<Curve> CI, bool P, bool R, ref object FO, ref object CO, ref object AN)
{
List<Curve> list = D;
Curve curve = FI;
Point3d point3d = AX;
List<Curve> list2 = CI;
bool flag = P;
bool flag2 = R;
double num = 0.0001;
double num2 = 0.001;
if (flag2 || !this.initialized)
{
this.turn = 0.0;
this.axleOriginalPosition = point3d;
this.initialized = true;
}
curve.Translate(point3d - this.axleOriginalPosition);
curve.Rotate(this.turn, Vector3d.ZAxis, point3d);
curve.Domain = new Interval(0.0, 1.0);
if (curve.ClosedCurveOrientation(Plane.WorldXY) == CurveOrientation.CounterClockwise)
{
curve.Reverse();
}
foreach (Curve curve2 in list)
{
curve2.Domain = new Interval(0.0, 1.0);
if (curve2.ClosedCurveOrientation(Plane.WorldXY) == CurveOrientation.CounterClockwise)
{
curve2.Reverse();
}
}
double num3 = 1.0;
double num4 = 0.0;
while (num3 > num & num4 < 200.0)
{
num3 = 0.0;
if (flag)
{
Point3d pointA;
Point3d pointA2;
list[0].ClosestPoints(curve, out pointA, out pointA2);
Vector3d vector3d = pointA - point3d;
Vector3d vector3d2 = pointA2 - point3d;
double num5 = Vector3d.VectorAngle(vector3d2, vector3d, Plane.WorldXY);
curve.Rotate(num5, Vector3d.ZAxis, point3d);
this.turn += num5;
num3 += Math.Abs(num5);
}
foreach (Curve curve2 in list)
{
Rhino.Geometry.Intersect.CurveIntersections curveIntersections = Rhino.Geometry.Intersect.Intersection.CurveCurve(curve, curve2, 0.001, 0.001);
if (curveIntersections != null && curveIntersections.Count > 1)
{
for (int i = 0; i < curveIntersections.Count - 1; i += 2)
{
Point3d pointA = curveIntersections[i].PointA;
Point3d pointA2 = curveIntersections[i + 1].PointA;
Vector3d vector3d;
Vector3d vector3d2;
int num6;
if (Vector3d.CrossProduct(pointA - point3d, pointA2 - point3d).Z > 0.0)
{
vector3d = pointA - point3d;
vector3d2 = pointA2 - point3d;
num6 = -1;
}
else
{
vector3d = pointA2 - point3d;
vector3d2 = pointA - point3d;
num6 = 1;
}
double num7 = vector3d2.Length - vector3d.Length;
double num5 = num7 * (double) num6 * num2;
curve.Rotate(num5, Vector3d.ZAxis, point3d);
this.turn += num5;
num3 += Math.Abs(num5);
}
}
}
num4 += 1.0;
}
FO = curve;
List<Curve> list3 = new List<Curve>();
for (int i = 0; i < list2.Count; i++)
{
if (list2[i] != null)
{
list2[i].Translate(point3d - this.axleOriginalPosition);
list2[i].Rotate(this.turn, Vector3d.ZAxis, point3d);
list3.Add(list2[i]);
}
}
CO = list3;
AN = this.turn;
}
// <Custom additional code>
public double turn;
public bool initialized;
public Point3d axleOriginalPosition;
// </Custom additional code>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment