[A] can not be cast to [B]

Hello,

I have a question regarding custom made Karamba components in visual studio and how to use them in GH.

I generate a Karamba beam element in my custom component and when using it in Grashopper it is defined as a Karamba GH Element. However when I try to add it to my assembly model in GH then I get the error that [A] can not be cast to [B].

I reference my .gha file by making a copy in a custom folder and renaming it to .dll. I also checked that the name of my grasshopper component comes after the name of Karamba in order to load it later in the alphabetical order. I am aware that in the Karamba guide it is suggested that I should reference the .gha file by manipulating the .csproj file. I tried this option as well and it is not working either, throwing me the error that the assembly could not be found.

Does anyone have a suggestion what I might be doing wrong? Appreciate every suggestion.

Cheers!

Hello @lfuhrimann,
for some reason karamba.gha is loaded twice during GH startup causing the problem you describe. Copying karamba.gha to karamba.dll is probably the reason. Where do you place your plug-in? Are there duplicate karamba.gha-files somewhere in your default dll search paths? If yes remove them
You could try to unhook ‘Memory load *.GHA…’ in the GrasshopperDeveloperSettings.
– Clemens

Thank you for the response @karamba3d,

My plug-in is placed under the bin/debug path of the development directory and then referenced in through GrashopperDeveloperSettings. The “Memory load” was unhooked already before.

You are correct by assuming that karamba.gha was loaded twice. I changed it to referencing the .gha directly in the .csproj but this gives me another error: “Assembly could not be found”. I assume this has something to do with the loading order, which apparently is very complex for Grashopper as discussed in this stream:

What dictates the load order for Grasshopper Plugins?

There are other people that ran into the same problem.

I have a similar issue. I ended up with a loader plug-in that forces loading the main library first and the extension later.

1 Like

Thank you for sharing your approach @gankeyu .

Would you be willing to share the loader plug-in that you developed with me? That would be very helpful!

You could try to use a post-build event in your Visual Studio project to copy your plug-in to the GH library folder or to ‘Rhino/Plug-ins’.
– Clemens

Good morning @karamba3d, myself and @lfuhrimann tried creating a plugin titled ZZZZZ which we found did not load last whether in the bin directory or in the GH Library / Rhino Plugins directory. We assumed that should work as per the Karamba 3D Scripting Guide (1.3.2).

As per the thread @lfuhrimann posted, @DavidRutten confirmed that plugins load ‘pretty much randomly’:

So our next idea was to try as others have suggested creating a shell plugin that points to solvers in a separate DLL, which means Karamba is only called upon on a component solving. However this means custom components can’t utilise the Karamba Types when creating In/Out parameters as that needs to be done in the plugin project, which can’t reliably reference Karamba.

Evidently people have written custom components using this Scripting Guide, so we must be missing a key detail, any ideas? :slight_smile:

hello @csykes,
did you set ‘Copy Local’ to false when referencing ‘karambaCommon.dll’ and ‘karamba.gha’ in you Visual Studio Project?
Did you try out different locations for your plugin (‘Grasshopper\Libraries’, ‘Rhino 6\Plug-ins’)?
– Clemens

Hello @karamba3d,
Yes I removed the copy local, and also tried different locations.
None of the locations ensured my plugin would load last.

– Callum

hello @csykes,
do you directly reference the ‘karamba.gha’ file in your Visual Studio project?
Do you use .NET Framework 4.6.1?
– Clemens

1 Like

IIRC, VS cannot reference “gha”-terminated files.

Hello @karamba3d,
Yes I do.
@gankeyu, you are correct, you have to be sneaky. I renamed it to Karamba2.dll, referenced that, then renamed it back to Karamba.gha and then changed the reference in the csproj manually to Karamba.gha and it worked perfectly!

@karamba3d, I’m using .Net 4.5.0, I will try 4.6.1 :slight_smile:

–Callum

@karamba3d,

As another thought, do you know if Rhino 7 vs Rhino 6 makes a difference to this?

@csykes,
to my knowledge it should not.
– Clemens

1 Like