Is it possible to RunScript() from Modal windows forms

Is it possible to post a rhino command while a modal window forms window is open? I am trying to post:

doc.Objects.UnselectAll();
doc.Objects.Select(this.ObjToExport);

string objOptions = Utility.GetOBJOptions();
string fileName = "\\" + Path.GetFileNameWithoutExtension(doc.Name) + ".obj ";
string filePath = this.UnityProjectPath + "\\Resources\\Model" + fileName;
string script = string.Concat("_-Export ", filePath, objOptions, "y=y", " _Enter _Enter");
RhinoApp.RunScript(script, false);
RhinoApp.RunScript("_-SelNone", true);

When I call this from my Modal window it doesn’t execute properly. I can see that _-SelNone gets executed but the OBJ export doesn’t. Ideas?

Does this information help?

http://developer.rhino3d.com/guides/rhinocommon/run_rhino_command_from_plugin/

– Dale

Hi @dale,

I did have proper Attributes for the command:

[System.Runtime.InteropServices.Guid("7cce0799-e273-49c2-ab0b-fe44c5ba3417"),
Rhino.Commands.CommandStyle(Rhino.Commands.Style.ScriptRunner)]

public class UnifyCommand : Command
{
    public UnifyCommand()
    {
        Instance = this;
    }

Then I was calling the form like so:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // launch Unify window
            var result = Result.Cancel;
            if (mode == RunMode.Interactive)
            {
                InputData inputData = new InputData(doc);
                var form = new UnifyForm(inputData) { StartPosition = System.Windows.Forms.FormStartPosition.CenterParent };
                var dialog_result = form.ShowDialog(RhinoApp.MainWindow());
                if (dialog_result == System.Windows.Forms.DialogResult.OK)
                {
                    result = Result.Success;
                }
            }
            return result;
        }

Then I would use the form to collect some info from the model and finally try to execute the RunScript() call when user pressed Export on the form.

private void btnExport_Click(object sender, System.EventArgs e)
        {
            this.inputData.ProcessExports();

            // close form
                this.Close();
            }

Where ProcessExports() is:

public void ProcessExports()
        {
            // run OBJ Export
            doc.Objects.UnselectAll();
            doc.Objects.Select(this.ObjToExport);

            string objOptions = Utility.GetOBJOptions();
                string fileName = "\\" + Path.GetFileNameWithoutExtension(doc.Name) + ".obj ";
                string filePath = this.UnityProjectPath + "\\Resources\\Model" + fileName;
                string script = string.Concat("_-Export ", filePath, objOptions, "y=y", " _Enter _Enter");
                RhinoApp.RunScript(script, false);
                RhinoApp.RunScript("_-SelNone", true);
            }

I think I am figuring out what is going on here. It fails because I have a space in my file path that somehow is messing up the command that is being submitted to RunScript()

@dale is there a good way to handle spaces in strings submitted to RunScript() ?

Surround string with spaces with double-quote characters.

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

That works. Thanks!