RhinoDoc.ActiveDoc.Import without prompt

I am doing RHINO test automation, when importing a DWG file, RHINO always pops up a prompt asking me to confirm, how to turn off this prompt? Or how to confirm this prompt with code?

Are you using a - (hyphen) in front of the command name? That puts the command in ‘scripting mode’. Then you can put the options on the command line from code.

Thanks

Hello, I am trying to import stl files using

Rhino.RhinoDoc.ActiveDoc.Import(modelPath);

Is there a another way than

Rhino.RhinoApp.RunScript(...)

to get rid of the prompt and proceed with default values or to specify the import options?

Capture d’écran 2022-03-16 120901

As written above, use a hyphen in front of the command name. This will put the command in scripted mode, which allows the options to be set as part of the script.
Something like this should work:

RhinoApp.RunScript("_-Import testImport.stl Weld=Yes SplitDisjointMeshes=No _EnterEnd")

1 Like

Thank you for your answer, but I really should have given more context.

I am building a application that interfaces rhino with a node server using edge, so I first tried with the API import command as it felt the more natural for me. Maybe it does work for formats other thatn STL that do not require option settings?

Anyway then I tried the RhinoApp.RunScript( way and even after I got the syntax Ok :

                string strFileName = "C:\testImport.stl";
                result = Rhino.RhinoApp.RunScript("_-Import " + '\u0022' + strFileName + '\u0022' + " _ModelUnits=_Millimeters _Enter", true);

Nothing is logged in rhino’s console as if the command was not called and the boolean result is false. Whereas if I paste the command printed in my console in rhino’s command bar it does work.

_-Import "C:\testImport.stl" _ModelUnits=_Millimeters _Enter

I also tried with

result = Rhino.RhinoApp.RunScript("_-Line 0,0,0 -10,-10,-10", true);

still nothing int rhino’s command log

To be thorough when Rhino is getting started I use Rhino.RhinoApp.RunScript(" to start grasshopper and it does work at that time. Somehting might be blocking commands later on.

Any idea how to debug this or import stl files another way?

Thank you very much,

Olivier

Hi, If you are trying to run this script from a plug-in context, this page might help : Run a Rhino command from a Plugin with C#

Thank you Aymeric, I tried adding

[Rhino.Commands.CommandStyle(Rhino.Commands.Style.ScriptRunner)]

But it had no effect

Hi @roievil,

Try this:

[CommandStyle(Style.ScriptRunner)]
public class TestRoievil : Command
{
  public override string EnglishName => "TestRoievil";

  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
  {
    var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    var filename = Path.Combine(path, "Test.stl");
    if (!System.IO.File.Exists(filename))
    {
      RhinoApp.WriteLine("File not found.");
      return Result.Failure;
    }

    // Make sure to surround filename string with double-quote characters
    // in case the path contains spaces.
    var script = string.Format("_-Import \"{0}\" _ModelUnits=_Millimeters _Enter", filename);
    RhinoApp.RunScript(script, false);

    return Result.Success;
  }
}

– Dale

Hello Dale,

Thank you very much for your answer. I am having trouble testing it :

How should I execute that command?
Here is the adapted code I want to test :

using Rhino.DocObjects;
using Rhino.FileIO;
using Rhino.Commands;
using Rhino;

namespace InsideNode
{
    [CommandStyle(Style.ScriptRunner)]
    public class TestRoievil : Command
    {
        public override string EnglishName => "TestRoievil";
        public string modelPath;

        protected override Result RunCommand(Rhino.RhinoDoc doc, RunMode mode) 
        {
            Rhino.RhinoApp.WriteLine("From STLImport.cs, Trying to Import the following model" + modelPath);
            //var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //var filename = Path.Combine(path, "Test.stl");
            if (!System.IO.File.Exists(modelPath))
            {
                Rhino.RhinoApp.WriteLine("File not found.");
                return Result.Failure;
            }

            // Make sure to surround filename string with double-quote characters
            // in case the path contains spaces.
            var script = string.Format("_-Import \"{0}\" _ModelUnits=_Millimeters _Enter", modelPath);
            Rhino.RhinoApp.RunScript(script, false);

            return Result.Success;
        }
    }
}

Here is what I tried

                        TestRoievil cmd = new TestRoievil();
                        cmd.modelPath = modelPath;

                        Rhino.Commands.Result r = Rhino.RhinoApp.ExecuteCommand(Rhino.RhinoDoc.ActiveDoc, "TestRoievil");

                        Rhino.RhinoApp.WriteLine("commandResult " + r);

But obviously that’s not it I get an unknown command, also my path is dynamic so I have to pass it.

Thank you very much for you help !

Olivier

Launch Rhino, load your plug-in, and run TestRoievil. Does this work?

– Dale

Ok I am interacting with rhino through a library so I cannot type any command I have to do it programatically.

The user submits a file to be opened/imported in rhino

When the imported file is a 3dm File I am able to open it via Rhino.FileIO.file3dm



                    if (threedm)
                    {
                        //Read the 3dm file
                        Console.WriteLine("reading 3dm file" + modelFileName);
                        file = Rhino.FileIO.File3dm.ReadWithLog(modelPath, out errorLog);
                        Console.WriteLine("file3dm errorLog" + errorLog);
                    }

if it is an STL I would like to have the same kind of process,


                    if (threedm)
                    {
                        ...
                    } else  {
                       // import the STL file
                    }

but when I try with

Rhino.RhinoDoc.ActiveDoc.Import(modelPath);

The workflow is stopped in rhino because it requests import options

imporOptions

if I try with

string strFileName = "C:\testImport.stl";
                result = Rhino.RhinoApp.RunScript("_-Import " + '\u0022' + strFileName + '\u0022' + " _ModelUnits=_Millimeters _Enter", true);

I get nothing in rhino’s command history, and the result of the runScript is false.

Although If I paste the above script in rhino it works, and If I runscript another command like

result = Rhino.RhinoApp.RunScript("_-Line 0,0,0 -10,-10,-10", true);

it does work.

.
.

With the solution you proposed why do I get “Unknown command: TestRoievil” when I try

                        Rhino.Commands.Result r = Rhino.RhinoApp.ExecuteCommand(Rhino.RhinoDoc.ActiveDoc, "TestRoievil");

                        Rhino.RhinoApp.WriteLine("commandResult " + r);

Do I have to register / add the TestRoievil class somewhere special ?

Thank you,

Olivier

Rhino must be aware of your plug-in. This is stored in the registry the first time you load the plug-in. Loading the plug-in is achieved by dragging the RHP file onto rhino. Upon restarting rhino, it will scan the registry and find information about your plug-in, and know about the command(s) in your plug-in.

Hello, Thank you for your answer, the thing is there is no ‘plug-in’, no file, I am using Rhino.Inside and edge to interact with rhino.

so I would need a programmatically way to register the

public class TestRoievil : Command

And I did not find it in rhinoCommon

Do you know of such thing?
.
.

Basically how do you import an STL from rhino inside?

typically I would use FileIO but it seems that STL files are not supported

Thank you very much

Olivier