Close the istance of automated rhino

rhino

#1

hi all, i have a C# application that automates rhino like this

            string rhinoId = "Rhino5x64.Application";
            System.Type type = System.Type.GetTypeFromProgID(rhinoId);
            rhino = System.Activator.CreateInstance(type);

but when it comes to close it there are some problem:
I can’t use rhino.Exit() because i get a runtime binder exception(method does not exist), i can’t use rhino.RunScript(_Exit, fasle) because it waits for an user input to choose wheter to save or not the edited file.

So how can i close rhino?

Thanks


(Menno Deij - van Rijswijk) #2

It seems that the only reliable way is to enumerate the process before and after the call to Activator.CreateIntance and see which process is new. Then, you can selectively kill that process.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/00cdd6f9-ffc9-497f-b199-53385272912f/how-to-get-the-process-of-activatorcreateinstance?forum=netfxbcl


#3

Thank you menno,

can’t we just disable the popup when exiting from rhino?


(Menno Deij - van Rijswijk) #4

Unfortunately, even in scripted mode, the Exit command pops up a dialog.


#5

Maybe loop through your processes and search for Rhino4 or Rhino5x64 and kill that process?
Or do you need to save before closing?


(Menno Deij - van Rijswijk) #6

In that case, you run the risk of closing an instance that the user opened him/her self.


#7

Thank you all for your responses.

I’m thinking to do this by another way: since my c# application is a multithreaded rhino automator( and i will have more than one istance of rhino), i will create a “garbage collector” that kills all the rhino process older than X.


(Dale Fugier) #8

Hi Alessio,

If you modify the document in anyway, you will be prompted to save. To suppress the “do you want to save” dialog, then get the RhinoScript object and call its DocumentModfied member. Here are some examples:

https://github.com/mcneel/rhinoscript/blob/master/BatchRender.vbs
https://github.com/mcneel/rhinoscript/blob/master/RhinoRender.vbs

Here are some other C#, automation samples.

https://github.com/dalefugier/SampleCsAutomation

– Dale


#9

thanks a lot dale!


#10

Hi Dale,

Do you have an updated sample file for Rhino 6?
If I am automating opening a new session of Rhino, doing some operations, save and Close the session from my Grasshopper plugin, is this the right approach?

Thanks,
Alan


(Dale Fugier) #11

I don’t know - does it work?

– Dale


#12

Dale,

This is working until I try to load grasshopper from the script object.

 object obj=rhino.GetScriptObject();
 IRhinoScript script=(IRhinoScript) obj;
 dynamic gh=null;
gh=script.GetPlugInObject("Grasshopper");

It throws an exception while grasshopper is loading GHPython at startup and stops loading grasshopper:
An exception of type ‘System.Runtime.InteropServices.COMException’ occurred in System.Dynamic.dll but was not handled in user code
Additional information: The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

Thanks,
Alan


(Dale Fugier) #13

Hi @AlanTai,

This probably isn’t enough information for me to help. Where is this code executiing? What the code, overall, do? Can you provide me a ‘simple’ example that I can run here that repeats this?

– Dale


#14

Dale,

I am working in my custom grasshopper component but, for example, in a grasshopper c# script component:

private void RunScript(bool x, ref object A)
  {
    if(!x) return;
    dynamic rhinoApp = null;
    try
    {
      string rhinoId = "Rhino.Application";
      System.Type type = System.Type.GetTypeFromProgID(rhinoId);
      rhinoApp = System.Activator.CreateInstance(type);
    }
    catch
    {
    }

    if (rhinoApp == null)
    {
      Console.WriteLine("Failed to create Rhino application");
      return;
    }

    const int bail_milliseconds = 15 * 1000;
    int time_waiting = 0;
    while (0 == rhinoApp.IsInitialized())
    {
      System.Threading.Thread.Sleep(100);
      time_waiting += 100;
      if (time_waiting > bail_milliseconds)
      {
        Console.WriteLine("Rhino initialization failed");
        return;
      }
    }

    rhinoApp.Visible = 1;

    object obj = rhinoApp.GetScriptObject();
    IRhinoScript script = (IRhinoScript) obj;
    script.Print("test print");

    dynamic gh = null;
    gh = script.GetPlugInObject("Grasshopper");
    gh.ShowEditor();
    gh.DisableSolver();
  }

From component output:

  1. The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) (line: 0)

(Dale Fugier) #15

Hi @AlanTai,

If I understand correctly, you are in Grasshopper, some custom C# component, and you want to launch another copy of Rhino and have it load Grasshopper? What is it you are trying to do an why?

– Dale


#16

Dale,

Yes, I am in grasshopper. I would like to automate running multiple other grasshopper definitions in a particular order (each grasshopper file has its own paired rhino worksession file) from my grasshopper component.

Thanks,
Alan