Hello,
I have this CSharp Script that I want to code to convert a list of curves in a list of points, in order to later generate gCode. I want to add UserText text to each point, but I am struggling with the (lack of?) documentation.
In a first script, I derived the type of object that flows out of the new QueryModelObjects component of Grasshopper WIP : ‘Grasshopper.Rhinoceros.Model.GH_ModelObject’, using the .GetType() method. And I was able to read the UserText of my curves that I had manually set in rhino.
//#! csharp
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Rhino;
using Rhino.Geometry;
using Rhino.DocObjects;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using Grasshopper.Rhinoceros.Model;
public class Script_Instance : GH_ScriptInstance
{
/*
Members:
RhinoDoc RhinoDocument
GH_Document GrasshopperDocument
IGH_Component Component
int Iteration
Methods (Virtual & overridable):
Print(string text)
Print(string format, params object[] args)
Reflect(object obj)
Reflect(object obj, string method_name)
*/
private void RunScript(RhinoCodePluginGH.Legacy.ProxyDocument ghdoc, RhinoCodePlatform.GH.ScriptEnv ghenv, System.Collections.Generic.IEnumerable<object> curves, Rhino.Geometry.Point3d entryPoint, object x)
{
// Create a new list to store the GH_ModelObjects
List<GH_ModelObject> list = new List<GH_ModelObject>();
foreach (object o in curves)
{
Console.WriteLine("o.GetType()" + o.GetType());
// if the object is a GH_ModelObject
if (o is GH_ModelObject modelObject)
{
if (modelObject.UserText.ContainsKey("Case"))
{
Console.WriteLine("Case : " + modelObject.UserText["Case"]);
}
}
}
}
}
Now I want to set UserText to grasshopper Objects in a CSharp Script and I am failing (comments in the foreach loop) :
// Grasshopper Script Instance
//#! csharp
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Rhino;
using Rhino.Geometry;
using Rhino.DocObjects;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using Grasshopper.Rhinoceros.Model;
public class Script_Instance : GH_ScriptInstance
{
/*
Members:
RhinoDoc RhinoDocument
GH_Document GrasshopperDocument
IGH_Component Component
int Iteration
Methods (Virtual & overridable):
Print(string text)
Print(string format, params object[] args)
Reflect(object obj)
Reflect(object obj, string method_name)
*/
private void RunScript(RhinoCodePluginGH.Legacy.ProxyDocument ghdoc, RhinoCodePlatform.GH.ScriptEnv ghenv, System.Collections.Generic.IEnumerable<Rhino.Geometry.Curve> curves, Rhino.Geometry.Point3d entryPoint, object x, out object a)
{
// Create a new list to store the GH_ModelObjects
List<GH_ModelObject> list = new List<GH_ModelObject>();
// Iterate over the curves
foreach (Curve curve in curves)
{
// Divide the curve into 10 points with kink option set to true
double[] parameters = curve.DivideByCount(10, true);
// Create a new list to store the points
List<Point3d> points = new List<Point3d>();
// Add the divided points to the list
foreach (double parameter in parameters)
{
GH_ModelObject modelObject = new GH_ModelObject();
Point3d point = curve.PointAt(parameter);
modelObject.Geometry = point; // modelObject.Geometry is read only how do I set it?
modelObject.UserText.append("gCode", "G1"); // also failing
// Add the modelObject to the list
list.Add(modelObject);
}
}
// Assign the list to the 'a' out parameter
a = list;
}
}
What am I missing? Where can I find the Grasshopper.Rhinoceros.Model.GH_ModelObject documentation/code?
How can I set the geometry of a GH_ModelObject ? I did not find it in Grasshopper API - Redirect or rhinocommon.
Thank you,
Olivier