Grasshopper

algorithmic modeling for Rhino

Hi, I'm trying to make a component that either accepts lines or numbers in the same parameter

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddGenericParameter("stuff", "s", "line or number", GH_ParamAccess.list);
}

In solveInstance I have tried

List<Line> lineList= new List<Line>(); ;
List<double> numberList= new List<double>();

if ((!DA.GetDataList(0, lineList)))
   if ((!DA.GetDataList(0, numberList)))
      return;

and  (not so pretty use of try statements....)

try{DA.GetDataList(0, lineList)}

catch{}

try{DA.GetDataList(0, nuimberList)}

catch{}

Both give casting error.. What am I doing wrong?

Views: 615

Replies to This Discussion

To add to this. I do not want to accept a list of BOTH numbers and lines, but one or the other.

You will indeed need a generic parameter as lines and numbers do not share a common parent type. Then you'll have to get IGH_Goo from that parameter and see if you can cast/convert it into GH_Line or GH_Number or GH_Integer or...

You may want to heavily rely on conversion methods provided in Grasshopper.Kernel.GH_Convert

Thank you very much for your reply!

However, I'm not sure I know exactly how to proceed, and how to implement the advice you're giving (still fairly new to component development). Basically, all I need is the length of the lines as soon as they are inputted, so my pseudo code would be something like

input; //Either List of numbers or list of lines

List numberList;

if Input.isAListOfNumbers:

   numberList = input

else if Input.isAListOfLines:

   foreach line in input:

      numberlist.Add(line.length)

else

   show error

You can't use Grasshopper's in build type filter because you accept two types that have nothing in common. So once you get your List out of your generic input parameter, you then have check each entry in the list yourself to see whether you have a list of only numbers, only lines, a mixture of both, or even a list containing other types.

I only have access to an iPad right now, so typing up example code is pretty cumbersome.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service