[A] can not be cast to [B]


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.


1 Like

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:



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

to my knowledge it should not.
– Clemens

1 Like

Hello @karamba3d,

I created a new project called ZZZPlugin in .NET 4.6.1 and have tried various locations as well as Karamba 1.3 and 2.0 (Nightly Build) and I cannot seem to get it to load after Karamba. Could you create a simple example project of a Karamba Plugin that works? That would be extremely helpful as I’m still banging my head against the wall :joy:

– Callum

Hello @csykes,
we will add a corresponding example to the scripting manual.
In the meantime I found this TensionElim_K3D131.rar (519.1 KB) which works with Karamba3D 1.3.1. Maybe it is helpful.
– Clemens

Good morning @karamba3d,
Thanks for the example, I tried the TensionElim Plugin with Karamba 1.3.1 (From Food4Rhino) as well as Karamba 2.0 (From GitHub) with Rhino 6 and 7 and it loaded before Karamba did each time when adding the plugin via the GrasshopperDeveloperSettings path, so still no luck :confused: .

– Callum

Hello everyone,
There are a few steps that could help get the component running:

  • Reference “karamba.gha” as descripted in the ScriptingManual (Page 24)

Open your VS Project file *****.csproj with NotePad++ or any other editor and find the line where “karambaComon.dll” is referenced.
Underneath insert:

<Reference Include="karamba">
<HintPath>..\..\..\..\..\..\..\Program Files\Rhino 7\Plug-ins\Karamba\karamba.gha</HintPath>

Save and restart Visual Studio.

  • Check if the referenced .dlls are the same version as the ones loaded in GH.
    (if you have for example RH6 with K3D_1.3.3 and RH7 with K3D_2.0.0 installed)
    Also check the Framework version.

  • Put your build .gha file in the Plug-ins\Karamba folder next to the referenced .dlls.
    Make sure your .gha starts with a letter after K…

Because of changes in the structure from version 1.3.3 to 2.0.0 the API changed a little.
This means for the TensionElim example changing one line of code from

elem.resultantCroSecForces(model, lc_num, out N, out V, out M);


elem.resultantCroSecForces(model, new Karamba.Loads.Combinations.LCSuperPosition(lc_num, model), out N, out V, out M);

ScriptingManual and Examples can be found here.
API for Karamba1.3.3 is available here.

– Georg

Hello @gl_karamba3d,

Adding the plugin into the Karamba folder and having it named after K works perfectly. This must have been the key difference to everything we tried before, thanks so much :slight_smile: !

– Callum

Hello @gl_karamba3d,

I am facing the same casting problem. But everything what is suggested doesn’t work. Will this problem be solved for next version of Karamba like 2.2.0?

  • Jurriaan