mcneel / rhino.inside-revit

This is the open-source repository for Rhino.Inside®.Revit
https://www.rhino3d.com/inside/revit/
MIT License
266 stars 70 forks source link

Launch Rhino.Inside programmatically. #399

Closed nicholasBimorph closed 3 years ago

nicholasBimorph commented 3 years ago

Is your feature request related to a problem? Please describe. This is not a bug.

Describe the solution you'd like I am wondering if there is a way in the API that I can launch Rhino.Inside. Essentially the same process as clicking the start button but programmatically.

Describe alternatives you've considered I have tried to load RhinoCommon and RhinoLibrary dlls with assembly resolve as a workaround but I get an AccesViolationExceptionon protected memory.

Additional context I currently have a very large application which needs Rhino.Inside to be loaded before the user can run any of our applications, since all our app has a dependency on RhinoCommon. It would be very helpful if I can handle this within the code.

eirannejad commented 3 years ago

@nicholasBimorph We do have an API but it is private at the moment. https://github.com/mcneel/rhino.inside-revit/blob/01da86b87a4c571c06d599fd2c34ff6b7ce1f07a/src/RhinoInside.Revit/UI/Commands/Addin/CommandStart.cs#L114

An option to initialize and start Rhino.Inside.Revit will be part of the v1.0 release to allow other addons to ensure Rhino is loaded.

nicholasBimorph commented 3 years ago

@eirannejad thank you for your quick reply. That would be very useful!

htlcnn commented 3 years ago

You could check loaded assemblies to detect if Rhino Inside has been loaded. If not, use PostCommand to call Rhino. After that, you could reference Rhino Inside. I did that with pyRevit.

htlcnn commented 3 years ago
# -*- coding: utf-8 -*-
from Autodesk.Revit.UI import RevitCommandId
from pyrevit.revit import uidoc
from pyrevit import framework
from pyrevit.forms import alert

loaded_assemblies = framework.AppDomain.CurrentDomain.GetAssemblies()
loaded_rhino_assemblies = [a for a in loaded_assemblies if "Rhino" in a.FullName]
if len(loaded_rhino_assemblies) == 0:
    alert("Rhino Inside Revit not installed. Exit.", exitscript=True)
elif len(loaded_rhino_assemblies) == 1:
    rir_app_id = "CustomCtrl_%CustomCtrl_%Add-Ins%Rhinoceros%CommandRhinoInside"
    addin_id = RevitCommandId.LookupCommandId(rir_app_id)
    uidoc.Application.PostCommand(addin_id)
    alert("Rhino Inside Revit not loaded. Please wait for it to load and run the script again.", exitscript=True)
nicholasBimorph commented 3 years ago

@htlcnn that is actually a very good suggestion.