Grasshopper

algorithmic modeling for Rhino

I am creating a custom component using C# that will be used for a variety of data-types - numbers, strings, points, etc - to be determined at runtime. I would like to add a "type hint" menu similar to that used by the Python and C# scripting components. Does anyone know where to start with this?

Thanks,

Ned R

Views: 1253

Replies to This Discussion

You can determine at runtime what data has been provided in a generic parameter. The only reason there's typehints on the scripting compinents is to make the code UI more type-safe.

If you want to replicate this anyway, you'll have to make your own parameter type, derive it from GH_Param and provide custom menus.

Can you tell me how I would do this ?

public class myParam:GH_Param<T>{...}

what would be T in this case given the incoming data format is not fixed ?

T should be the most generic type available, which in the case of parameters is IGH_Goo. ALL data in Grasshopper ultimately implements this interface, so this will allow you to create a parameter which can contain all types of data.

This is where I've gotten. Is this the best way?

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.Register_GenericParam("G", "G", "Object to test.", GH_ParamAccess.item);

}


protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.Register_GenericParam("S", "S", "Type of object.", GH_ParamAccess.item);
pManager.Register_GenericParam("V", "V", "Value", GH_ParamAccess.item);

}


protected override void SolveInstance(IGH_DataAccess DA)
{
object obj_in = new object();
object out_val = new object();


if (!DA.GetData(0, ref obj_in)) return;


if (obj_in.GetType() == typeof(GH_Number)) {
double n = new double();
DA.GetData(0, ref n);
out_val = n;
}


if (obj_in.GetType() == typeof(GH_String)) {
string s = "";
DA.GetData(0, ref s);
out_val = s;
}


var t_string = obj_in.GetType().ToString();
DA.SetData(0, t_string);
DA.SetData(1, out_val);

Attachments:

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service