RhinoInside. Cannot run it

Hello dear Rhino forum.

I am trying to create a simple console app (before I’ll jump to excel), where I will learn how to use RhinoInside. I creata a project according to the sample files on GIT HUB but I get exception error. Unfortunatelly, I am not able to find out where is the problem.

My code is very simple (see below) and I get this exception (in czech):

System.TypeInitializationException: 'Inicializační metoda typu ConsoleApp1.Program vyvolala výjimku.'
    
Inner Exception:
BadImageFormatException: Nelze načíst soubor nebo sestavení RhinoInside, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null nebo jeden z jejich závislých prvků. Byl učiněn pokus načíst program v nesprávném formátu.

I also found out that all referenced libraries and packages are marked with processorArchitercuture = MSIL but RhinoInside is AMD64. RhinoInside was installed via NuGet package.

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using Rhino.Runtime.InProcess;

namespace ConsoleApp1
{
    class Program
    {
        static Program()
        {
            RhinoInside.Resolver.Initialize();
        }
        static void Main(string[] args)
        {

            System.IO.Directory.SetCurrentDirectory(RhinoInside.Resolver.RhinoSystemDirectory);
            Rhino.Runtime.InProcess.RhinoCore m_rhino_core = new Rhino.Runtime.InProcess.RhinoCore(new[] {"/NOSPLASH" }, WindowStyle.Hidden);

        }
    }
}

Thank you for your reply.

Ondřej

AMD64 is not a valid target platform for Rhino for Windows. Set your C# project to target x64.

HelloWorld.zip (2.9 KB)

– Dale

Hi @dale,

thank you for you reply. Your code worked for me. It think it is same as I already have from GitHub but used it without success. Now I can use RhinoCommon without Rhino. Thank you again.

Unfortunately, my goal little bit harder. I would like to open, run and recompute GH from excel. Is it even possible?

I have searched this forum and GitHub for some sample. Unfortunately, again without success. I am able to create a excel Plugin (VSTO) and combine it with this code. When I run project it raise a exception that
RhinoCommon.dll cannot be found.

My code for plugin is below:

  public partial class ThisAddIn
    {
        Rhino.Runtime.InProcess.RhinoCore m_rhino_core;


        #region Plugin static constructor
        static readonly string SystemDir = (string)Microsoft.Win32.Registry.GetValue
        (
          @"HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install", "Path",
          Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Rhino 7", "System")

        );

        static ThisAddIn()
        {
            ResolveEventHandler OnRhinoCommonResolve = null;
            AppDomain.CurrentDomain.AssemblyResolve += OnRhinoCommonResolve = (sender, args) =>
            {
                const string rhinoCommonAssemblyName = "RhinoCommon";
                var assembly_name = new AssemblyName(args.Name).Name;

                if (assembly_name != rhinoCommonAssemblyName)
                    return null;

                AppDomain.CurrentDomain.AssemblyResolve -= OnRhinoCommonResolve;
                return Assembly.LoadFrom(Path.Combine(SystemDir, rhinoCommonAssemblyName + ".dll"));
            };
        }
        #endregion // Plugin static constructor

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            try
            {
                //TODO >> grab Excel product name for scheme
                var scheme_name = "RhinoInsideExcel";
                //Run Rhino.Inside no UI
                m_rhino_core = new Rhino.Runtime.InProcess.RhinoCore(new[] { $"/scheme={scheme_name}" });
                //TODO >> Run Rhino.Inside with UI - Error -200
                //m_rhino_core = new Rhino.Runtime.InProcess.RhinoCore(new[] { $"/scheme={scheme_name}" }, WindowStyle.Maximized);
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.Message);
                System.Windows.Forms.MessageBox.Show("Cannot load Rhino Inside Addin");

            }

        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            try
            {
                m_rhino_core?.Dispose();
            }
            catch
            {
                // ignored
            }
        }

What I have discovered is that SystemDir is always null. Frankly, I am not sure if I understand code correctly but SystemDir should be a path to a Rhino itself and this line

    return Assembly.LoadFrom(Path.Combine(SystemDir, rhinoCommonAssemblyName + ".dll"));

should be direction to RhinoCommon. My program fails and this line as RhinoCommon is not loaded.

    m_rhino_core = new Rhino.Runtime.InProcess.RhinoCore(new[] { $"/scheme={scheme_name}" });

So my questions are follows:

  • What am I doing wrong?
  • Is it possible to combine Excel na Rhin/GH in the way I would like?
  • How to run GH from Excel and load RhinoCommon properly?
  • Is there some kind of a guide for RhinoInside/RhinoInsideExcel?
  • What are differences between RhinoInside and RhinoInterface, regarding this post?

Thank you for your time and reply.

Best regards

Ondřej

Hi @janotaondrej91,

I don’t know. I don’t have a copy of Excel, nor do I know anything about creating Excel add-ins.

Does the project sample you reference work?

In what way? So far, all we’ve have is that you want to run Rhino inside of Excel. We’ve never been told why.

Please refer to my answer to your first question.

For Rhino.Inside, it’s pretty simple. You’ve seen a sample of running Rhino inside of a console application. Obviously there is more to getting it to work with Excel. Of course, Excel must be a 64-bit application, as Rhino.Inside will try to load a 64-bit Rhino DLL. Perhaps ou need a custom assembly resolver. I believe we needed to do this for a Rhino.Inside Revit project.

Rhino.Application and Rhino.Interface is a legacy COM objects. Rhino.Inside does not use COM. Rather, it load Rhino into the memory space of the host application.

– Dale

@dale thank you for your reply.

I am sorry if my questions are unclear or much general. I am not pro-coder and this is little harder for me then working with Rhino and GH API. My goal is following:

Develop a simple excel plug-in using VSTO. That is what I have already done.
This plugin opens a Rhino or GH file and if button of this plugin is pushed. It will iterate over GH file. Find a component with defined name and resolve this component.

Hope my goal is clearer now. So far, it looks like problem is with loading Rhino DLL file. I will check 64 version of excel and Rhino.Inside.Revit link.

Thank you

Ondřej

Ok, now I am able to run VSTO plugin which starts Rhino and does not fail. I am able to get to get active doc. Unfortunately if I create a click event on my button it does not fire.

I went over few sample files for AutoDesk, Revit, Microsoft, UE, WinForms, etc. What I really don’t understand is why each sample file uses different approach. When to use Rhino.Inside and when RhinoCommon as references (nuGet package).

Thank you

Ondřej