WriteFile with No User Input

Hi, I’m fairly new to Rhino development so excuse me if this has already been asked.

I’m trying to write C# code that saves the current document to an .obj format, but I don’t want to have any user input necessary. I’d like to be able to specify or automatically provide all options necessary. Currently, there are a number of prompts in the methods I have tried.

Approach One:

RhinoApp.RunScript("-_SelAll \n_Export \"C:\myfile.obj\" \n _Enter");

This doesn’t seem to work for me

Approach Two:

Rhino.FileIO.FileWriteOptions options = new Rhino.FileIO.FileWriteOptions();
options.WriteSelectedObjectsOnly = true;
options.SuppressDialogBoxes = true;
doc.WriteFile("C:\myfile.obj", options);

Nicely suppresses the popup dialogs, but then prompts for the options anyway.


Hi John,

Scripting the Save, SaveAs, or Export command is the preferred approach.

A couple of hints:

1.) In order for a plug-in command to run a Rhino command, your command class must have the ScriptRunner command style attribute.


2.) Best to surround path strings with double-quote characters (just in case the path string contains spaces).

3.) Make sure to save to a folder that you have rights to save in.

A quick example:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
  var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  path += "\\test.obj";

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

  RhinoApp.RunScript(script, false);

  return Result.Success;

– Dale

Thank you. I’ve got it now thanks to this help. I basically had the right idea but was missing a couple of things:

  1. The ScriptRunner attribute
  2. An extra “_Enter” command to deal with the two followup inputs

Minor point:

Use path = Path.Combine(path, "test.obj"); to make it work on Macs too. Now you are hard-coding the backslash directory separator; Path.Combine will respect the forward slash on other platforms than Windows.

Good advice. That is actually how my code is written, but I stripped it out to keep the example simple. I’m not actually saving directly to C: :slight_smile: