Rhino Command Export in C#

grasshopper

(Aleksanderdynarek) #1

Hi

I have a script written in C# that creates, bakes and selects baked geometry.

I am trying to add some lines to export selected geometry into the dxf file. How to do it?


(Dale Fugier) #2

Hi @aleksanderdynarek,

Select the newly baked geometry and then script the Export command using RhinoApp.RunScript.

Here is the plug-in command equivalent of what you’ll want to do:

https://github.com/mcneel/rhino-developer-samples/blob/6/rhinocommon/cs/SampleCsCommands/SampleCsExportDXF.cs

– Dale


(Aleksanderdynarek) #3

Hi!

How exactly should I do it in this code?

   if(x)
    {
      Rhino.DocObjects.Tables.ObjectTable ot = Rhino.RhinoDoc.ActiveDoc.Objects;
      ot.AddLine(new Point3d(0, 0, 0), new Point3d(1, 1, 1));
      //get guid of the most recent object
      Guid id = ot.ElementAt(0).Id;

      //deletes this object
      //if(z) ot.Delete(id, true);
      ot.Select(id);
    }

(David Rutten) #4

Be aware that ActiveDoc may return a null reference under certain circumstances. You should probably check for null before using it:

var doc = Rhino.RhinoDoc.ActiveDoc;
if (doc == null) return;

doc.UnselectAll();
if (x)
{
  var lineId = doc.Objects.AddLine(new Point3d(0,0,0), new Point3d(1,1,1));
  doc.Objects.Select(lineId);
}
...
RhinoApp.RunScript("-_ExportCommandHere", true);

(Aleksanderdynarek) #5

I think that the problem is, that I do not now exactly how to write RunScript command :slight_smile: I mean, full one, with directory, file name, ENTER, etc. Could you help please?


(David Rutten) #6

It’s whatever you would type into the Rhino command line to make it work. It usually takes me a while to figure out all the exact options and _Enters required to make it work.

The basic approach will be running the -_Export command (with the hyphen and the underscore; the hyphen suppresses dialog boxes and the underscore forces the English command name). Then you must include the filename with the correct extension. You’ll want to encase it in double quotes in case it contains any spaces. Then it seems you can either select a scheme, or use the default.

string command = "-_Export \"C:\Users\David Rutten\Desktop\export.dxf\" _Enter";

(Aleksanderdynarek) #7

thanks, but unfortunately it show some error :frowning:

var doc = Rhino.RhinoDoc.ActiveDoc;
if (doc == null) return;

doc.UnselectAll();
if (x)
{
  var lineId = doc.Objects.AddLine(new Point3d(0, 0, 0), new Point3d(1, 1, 1));
  doc.Objects.Select(lineId);
}

string command = "-_Export \"D:\Profile\adminek\Desktop\export.dxf\" _Enter";
RhinoApp.RunScript(command, true)

error:

  1. Error (CS1009): Nierozpoznana sekwencja specjalna (line 82)
    (its in Polish, in English it would be something like “unidentified special sequence”)
    line 82 is the line with string command

(Pelle) #8

I was googling around for a possibility to in a definition automatically bake a geometry - stop and let me execute regular rhino fillets (or any other regular command) - then automagically pick this new geometry and continue in grasshopper.
Would this be possible?
Pelle


(David Rutten) #9

Yeah my bad, backslashes are escape chars in C#, so it should have been:

string command = "-_Export \"C:\\Users\\David Rutten\\Desktop\\export.dxf\" _Enter";

(Aleksanderdynarek) #10

works :slight_smile: so cool, thanks!