Grasshopper

algorithmic modeling for Rhino

Been trying to compare two structures specifically to detect changes in number values within them. So no full-fledged structure comparison, just a basic value comparison. This is what I have:

 

     int pc = Math.Min(struct1.Paths.Count, struct2.Paths.Count);          for (int i = 0; i < pc; i++)
{
IList b1 = struct1.get_Branch(struct1.Paths[i]);
IList b2 = struct2.get_Branch(struct2.Paths[i]);
int bc = Math.Min(b1.Count, b2.Count);
for (int j = 0; j < bc; j++)
{
IGH_Goo item1 = (IGH_Goo)b1[j];
IGH_Goo item2 = (IGH_Goo)b2[j];
GH_Number n1;
GH_Number n2;
if (item1.CastTo(out n1) && item2.CastTo(out n2))
{
if (Math.Round(n1.Value, 12) != Math.Round(n2.Value, 12)) return false;
}
}
}

But unfortunately it doesn't work ;'(

 

Any suggestions?

Views: 539

Replies to This Discussion

"It doesn't work" meaning actually that the CastTo conditions are never met...sorry for the vague definition!

CastTo is used in case of not-obvious conversions. If you know your data is a GH_Number, you can just use:

 

GH_Number n1 = b1[j] as GH_Number;
GH_Number n2 = b2[j] as GH_Number;

if (n1 != null && n2 != null)
{
  //...blahblahblah
}

Also, the CastTo and CastFrom functions are pretty low level. It's easier to use the conversion methods in Grasshopper.Kernel.GH_Convert.

 

--

David Rutten

david@mcneel.com

Leusden, Holland

Thanks! They were actually integers that's why it didn't work! I suppose that GH_Integer needs to be explicitly cast to GH_Number in contrast to the basic types right?

If you use GH_Convert to do the conversions, you can pretty much use any data type you want. But GH_Number does not derive from GH_Integer (or vice versa) so you cannot directly cast between those anyway.

 

GH_Integer wraps around System.Int32, GH_Number wraps around System.Double.

 

--

David Rutten

david@mcneel.com

Leusden, Holland

Thank you David for the help!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service