Grasshopper

algorithmic modeling for Rhino

Hi guys.

Is it somehow possible add different types of data to the same datatree? 

I want to put both numbers(floats) and text(strings) in the same GHdataTree.

I am just wondering if there is a type instance to put in:

excelTree = DataTree[System.String]()


that will accept both strings and floats? Or is there some other work aroung?

Thanks

Rasmus

Views: 947

Replies to This Discussion

You need to declare the datatree with type that encapsulates both strings and numbers. In this particular case, you'll have to use object as that is the only type that allows for both value types such as doubles and reference types such as string.

A better option would be to create a brand new class that can hold either a string or a number. In C#:

public class TextOrNumber

{

  private readonly string _text;

  private readonly double _number;

  public TextOrNumber(string text)

  {

    if (text == null)

      text = string.Empty;

    _string = text;

  }

  public TextOrNumber(double number)

  {

     _number = number;

  }

  public bool IsText {get { return _string != null; }}

  public string Text {get { return _string; }}

  public double Number {get {return _number; }}

}

Then create a DataTree of type TextOrNumber and you're all set. Maximum amount of type-safety.

--

David Rutten

david@mcneel.com

I think what you are looking after is System.Object.

Best,

M.

Oh yes, if you're outputting the datatree, then using object is indeed the better approach as GH won't know how to convert a custom class into proper GH data.

--

David Rutten

david@mcneel.com

I think I asked for this before, but it would be nice if it was possible to have a single place in a GH document (similar to "custom additional code") that is referenced in all scripting components of that document so I can create a class or a function once and call it from any scripting component and pass the custom data between them.

You might think that if you want to do this just open visual studio and create a proper plugin or a library but I think this middle ground would be very useful, specially if it's for single use (a single definition).

Thanks guys. System.Object worked perfectly in this case. But it is clear, that the class option would prove better in the future. 

Now that we are at it - I'am doing this to import Excel Data. So far I have been using the Python-Excel components xlrd and xlwt. I can only get xlrd to work in release 0.8.0 which only works with .xls files and not the "new" format .xlsx.

I have been trying out release 0.9.2 which should be able to read .xlsx, but I am not able to import/install it in the current version of GHPython. Does anybody have any experience with this or other modules? (Which are easy to install) :)

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service