Grasshopper

algorithmic modeling for Rhino

Hi,

I get strange behavior when passing custom class through components.

(This custom class does not implement Goo and I unwrap it using GH_ObjectWrapper and then use CastTo)

But what is happening:

I have a custom class in component A and I pass it to component B.

Component B receives it and does something.

But when I move a slider and change some value in component B, component B does not receive anything from component A.  It does stuff only in once.

What is a correct way to pass custom class around in gh and is it possible get an example?

Views: 3682

Replies to This Discussion

Since each script is compiled into a separate assembly, you cannot share classes between scripts. Or rather, you can share instances of a class A defined in one C# component to another C# component, but that second C# component does not know of type A and therefore cannot interact with it in a typesafe way.

There are several ways around this:

  • Write your class in Visual Studio and compile it to a dll, then reference this dll in both script components. I realise this is a supremely unpleasant solution, especially when the classes involved are tiny.
  • Access you class using either the dynamic keyword or via Reflection in the second script. The code won't be pretty (and you'll have trouble using dynamic, I'm not sure the C# component can handle it) but it will allow you to keep all type definitions in-file.
  • Implement some .NET framework interface on your class that allows other assemblies to cast to it and interact with it. This of course is only an option if there is a framework interface that actually does what you need.
  • Don't create custom classes. Instead, store your data in existing classes (strings, dictionaries, lists, ...) and pass those around.

Hi,

I already have one class compiled in one .dll and use them in components that are compiled in one .gha file that have reference to .dll where that class is location. I create all components in one project and all the classes that have nothing to do with GH are compiled in one dll.

I will try to implement C# interface as dynamic thing seems to be very complex.

The issue is that I have to use classes, because all methods and properties are stored within them.

The componentB runs one time and does proper calculation within that object, but after changing other parameter in the component (changing slider that does nothing), it looks like data is lost. Or there is a bug that I cannot understand.

Thank you for explanation:)

Sorry, I thought you were using standard C# components, rather than compiled GHA code. That wholly changes the picture and yes, you should be able to send custom types from one component to another within the same gha assembly.

So I'm guessing this "But when I move a slider and change some value in component B, component B does not receive anything from component A.  It does stuff only in once." is the crux of the problem?

Component inputs are not expired along with the component itself, only inputs that change directly will be recalculated. Thus if your component has two inputs, one of which is your custom type and the other a number, if you change the number the custom type input will retain its old data.

Perhaps a more detailed explanation about what exactly isn't working as expected is in order...

Thank you, i thought that when I change one of the input all the rest will be initialized from scratch. 

As long as I am changing custom class : slowing deleting items from it. After solution it becomes empty.

I just need to copy that object.

One more question I have is about GH_Param.

I attached 2 .cs files, they are very simple.

Tile is defined within separate .dll file and other is in .gha assembly.

In explode tile I have always retrieve GH_ObjectWrapper and then cast to Tile object.

I saw that some users are creating custom paramaters to avoid this casting. 

How to create a GH_Tile paramater so that in inputs or outputs i could write pManager.AddParameter(new GH_MyTile);

?

Thank you a lot I would really like to learn this:)

Attachments:

You must create a new type which inherits from GH_Goo<Tile>, let's say it's called TileGoo. Then you create a new class which derives from GH_Param<TileGoo> or GH_PersistentParam<TileGoo>. Pick the latter if you can (de)serialize your tile class. I suppose it depends on what type Geo is. Is it serializable?

The geomety will later be changed to meshes instead of object.

Would it be possible to see an example how it looks like in code?

And for instance if I have an object tile which is simply 1 mesh and a few planes.

When having this custom is it possible to for instance to use rotate or orient component from native grasshopper to perform a set of transformations. In the same manner as working with groups in grasshopper.

Hi,

I implemented two additional classes according to your message.

It is first time I am doing this and do not understand all the workflow.

Could you please take a look these 3 simple classes if I am doing it correctly?

Attachments:

It will have to wait until after Christmas... It's my turn to cook christmas dinner this year.

Happy New Year:)

Do you have time to answer previous message? :)

Oops, I forgot about this. Sorry.

I can make some time later tonight after the McNeel weekly meeting.

No rush I am just learning :) I seems like lots of people using this but I have no idea how to do it correctly.

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service