Grasshopper

algorithmic modeling for Rhino

Hello there,

I'm writing a component in c# that needs to proccess a relativly small matrix and pass it to another component. 

I can't understand what might be the easiest way to do this - using DataTrees, array[][] or GH_structures.

thanks

Views: 1289

Replies to This Discussion

You can use Rhino.Geometry.Matrix in Grasshopper scripting components. Here is how.The only missing bit is the automatic casting made by the 'TypeHint' of the input, but we can do that in scripting directly, too.

Matrix m = null;
if (x is Matrix)
{
  m = (Matrix) x; //cast if it is a Matrix
}
else if(x == null)
{
  m = new Matrix(3, 3); //what should happen if it is null?
}
else
{
  throw new ArgumentException( //some other type: exception
    string.Format("Conversion of x failed from {0} to Matrix.",
    x.GetType().ToString())
  );
}

//do something with m...
m.Invert(0.001); A = m;

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Attachments:
I prefer to use the as keyword, but it only works for reference types

Matrix m = x as Matrix;
if (m == null)
throw new ArgumentException("x must be a matrix");

I opted for is because it's slightly more self-documenting, and as components are compiled in debug mode, I doubt this will make a big difference in speed. Good point though.

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Thanks for the fast help

I think I'm almost there but now i get this error:

1. Solution exception:Object reference not set to an instance of an object.

I work in VS enviorment

thanks

That exception means you're trying to call properties or methods on a null reference. Before you call any methods or access any fields, you must make sure the instance you're calling it on actually exists.

In Visual Studio, these problems are usually solved best using the Debugger. If you do not know how to use the debugger, that should be research project #1 for you. There is no tool out there which will help you develop better code than a working debugger.

Hi Roey,

in addition to what David suggested, if you use the code above with the is keyword, you will get a special case for null items. You can then treat them in the way you think will work better for you.

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service