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.
Every data should be passed to one another with certain wrapper type.
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:
What are IGH_Goo, GH_Goo, Wrapper type? Could anyone explain them with some example code?
How can I pass around custom class object within Grasshopper? (see attached image and file please)
I see Grasshopper has types such as GH_Point, ⌠etc. How are these types differ from Point3d and others?
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:
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.
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.
Create a separate dll using Visual Studio which declares your custom data type and reference it from all script components.
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.
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â).
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.
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.
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)