Passing around custom class objects in GH (C#)

unhandled

(Andrew Jinho AHN) #1

I am trying to pass custom class objects created in c# component to another c# component, but since I am coding inside the GH C# scripting window, this seems impossible.

As I do my research on this matter, what I realize are followings.

  1. Every data should be passed to one another with certain wrapper type.
  2. As I understand, IGH_Goo or GH_Goo should be inherited when I declare custom class to be able to pass object to another component.

I have looked up and read almost all topics related to similar issues but due to my super limited knowledge in programming, I couldn’t understand what they were talking about. especially the part ‘implementing IGH_Goo’.

I have posted very similar question to old grasshopper3d.com forum and do remember David answering my question with elaboration but still having very hard time dealing with data types in grasshopper.

my questions are:

  1. What are IGH_Goo, GH_Goo, Wrapper type? Could anyone explain them with some example code?
  2. How can I pass around custom class object within Grasshopper? (see attached image and file please)
  3. I see Grasshopper has types such as GH_Point, … etc. How are these types differ from Point3d and others?

question.gh (3.8 KB)


(David Rutten) #2

True, types defined inside a script component cannot be shared with others. Even if the other script component has the exact same code, it will still be compiled into a different type and casting is not allowed.

Yes, but you can just use GH_ObjectWrapper if you don’t have a specific wrapper type. You don’t have to write a proper IGH_Goo implementation if you just want to share data between script components.

Here are your options:

  1. Instead of a custom type, use an existing .NET framework type to store your data, for example Dictionary<TKey, TValue>, or List<T>, or even string if your data is reasonably simple.
  2. If your data is quite complicated and you really want to use a specific class for it, make the class (de)serializable. You can use GH_IO.dll to convert a class instance into an xml string or a byte-array, then pass this serialized data between components and deserialize at the other end. You’ll still have to put the code in both script components, but this way you can convert one into the other.
  3. Create a separate dll using Visual Studio which declares your custom data type and reference it from all script components.

I’ll get you an example of (2).


(David Rutten) #3

type exchange.gh (19.9 KB)


(Andrew Jinho AHN) #4

Thanks David. That was very helpful. I have one more question. if I want to go with 3rd option, where should the dll file go?


(Andrew Heumann) #5

Shouldn’t really matter where the DLL file lives, but if I’m not mistaken keeping it in the same directory as your GH file should make it easier for GH to locate it automatically.


#6

I agree it does not matter where file is, but to my understanding GH won’t recognize it automatically… you’d have to reference it in the c# script (“manage assemblies”).


(Andrew Heumann) #7

I thought (though could be mistaken) it would automatically locate a DLL if it was in the same folder as the .gh file. Is that not the case?


(Andrew Heumann) #8

oh I misunderstood - yes you’re correct that you have to explicitly reference the script in manage assemblies regardless :slight_smile:


(Shimpei) #9

Hi all,

I’m using option 3 David suggested.
It works well at first but now It occurs error message.
I thought it depends on .dll file location so I made my component refer same file location.
However there is still this message…

[A]Analysis.defExportModel cannot to be cast to [B]Analysis.defExportModel 
Type A originates from 'Analysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' (in the context 'LoadNeither'、at location 'C:\Users\kjmsn\AppData\Roaming\Grasshopper\Libraries\Analysis.dll')
Type B originates from 'Analysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' (in the context 'LoadFrom' at ocation 'C:\Users\kjmsn\Documents\Visual Studio 2015\Projects\Analysis\Analysis\bin\Debug\Analysis.dll')

There was discussion about this error but it seems the topic author gave up to solve it.

Does anyone know how to solve it?


(David Rutten) #10

They’re clearly loaded from different locations. Can you upload the dll and the gh file which references it?


(qythium) #11

Did you uncheck “Local Copy” under the VS reference of your dll? I had this same error before and that solved it for me.

Edit: oops, I see that the discussion you linked to mentioned this already


(Shimpei) #12

Hi David,
Thanks for always being a great support.

Here is my .gh files(zip)GHfiles.zip (64.9 KB)


(Shimpei) #13

Hi qythium,
Thank you for the reply!

I already tried but failed as you saw in the discussion…


(David Rutten) #14

You have lots of GHA files. Why not just put all your classes into a single assembly (i.e. GHA file?). I though you were trying to pass data from one C# script component to another.


(Shimpei) #15

Hi David,
I’m sorry I didn’t know it can be compiled in single gha.
I modified it to be single gha.

Always I try to script on GH C# component.
When it goes well, I move to VisualStudio.

I add .cs file that returns null.
The other day, you told me how to use ‘param.optional’ and I succeeded to avoid error, but it generates null data.

Here is zip file(single gha .dll.ghGHfiles.zip (56.5 KB)
.cs)