Skip to content

Instantly share code, notes, and snippets.

@pearswj
Created February 4, 2021 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pearswj/57a777bd43a16bd26af78cd7a88602bc to your computer and use it in GitHub Desktop.
Save pearswj/57a777bd43a16bd26af78cd7a88602bc to your computer and use it in GitHub Desktop.
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;
using System.IO;
using System.Threading.Tasks;
/// <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(string data, ref object A)
{
Print("{0}", done);
if(!done)
{
start = DateTime.UtcNow;
this.Component.Message = "Running...";
Task.Run(() => DoStuff(data)).ContinueWith(t =>
{
this.Component.Message = string.Format("Done! ({0:0}ms)", (DateTime.UtcNow - start).TotalMilliseconds);
result = t.Result;
done = true;
Component.ExpireSolution(true);
});
}
A = result;
// reset
done = false;
result = null;
}
// <Custom additional code>
bool done = false;
string result = null;
DateTime start;
string DoStuff(string data)
{
System.Threading.Thread.Sleep(2000);
return "Good morning!";
}
// </Custom additional code>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment