C# .DXF import

Hello,
I am trying to import .dxf file via grasshopper with c# component. Everytime I run the script dialog box is opened to select a path. What I am looking for is…

a) it shall take file path as an input and import it without opening dialog box.
b) I do not know if it is possible, but I want to have the imported .dxf inside grasshopper and not in rhino so only if it is necessary i ll bake it.

Any help is this regard would be great!

here is the code i used to import

Rhino.RhinoApp.RunScript("_Import", false); // and the it opens up the dialog box to select path

Image

That runs the command in UI mode. If you prefix a hyphen, you’ll run the command in command-line mode which allows you to compose the command fully in text:

string command = “-_Import " + '”’ + filepath + ‘"’ + " _Enter";
Rhino.RhinoApp.RunScript(command, false);

Or something along those lines, you’ll hae to try in the Rhino command line first what sequence of commands actually works. Don’t forget to surround your filepath by double-quotes lest the spaces in it terminate the command early.

1 Like

@DavidRutten
Can you also suggest anything for this. I have many profiles of mullions(around 100.dxf files). I dont need .dxf file in rhino every time I import (I want to read the .dxf and display it in viewport using grasshopper).

what I did is, I referenced “.netDxf” library in c# and tried reading the file
however it does read the file but does not show the geometry in the viewport, not sure if this is the right way.

i2

1 Like

@stevebaer there was talk about exposing the file import/export plugins in the SDK by having them populate separate File3dm instances. Is that available in Rhino6? I couldn’t find anything in the api docs.

1 Like

No, this is a large project that I am hoping to tackle during the V7 development. This feature is not currently available.

@stevebaer any progress on this in the v7 wip? would be great to be able to programmatically read a DXF without cmd line scripting.

Yes, I was writing up a “headless doc” sample for you and found that the DXF importer hangs on the command line when reading a doc. I’m working on a fix for this now.

You’ll need to wait until the next V7 WIP (probably available on Feb 25, 2020). At that point you should be able to use a script like the following.

private void RunScript(string path, ref object A)
{
  using( var doc = Rhino.RhinoDoc.CreateHeadless(null) )
  {
    doc.Import(path);
    var geometry = new System.Collections.Generic.List<GeometryBase>();
    foreach(var obj in doc.Objects)
    {
      geometry.Add(obj.Geometry.Duplicate());
    }
    A = geometry;
  }
}

4 Likes

Will it be possible to know the attributes of the imported geometries through this function?

The code creates a temporary Rhino document that doesn’t replace the active document. All of the geometry and attributes that you would get from a normal document are available.

1 Like

May I ask what’s the plan of RhinoDoc.Import/Export supporting importer/exporter with command-line getters?

I don’t quite understand your question.

Sorry for that :sob:, let me rephrase.
For example, when I’m calling RhinoDoc.Export to export a dwg file, I can still see command line asking me for export options and the Export method is stuck until I type enter in the command line window.
It seems due to many plugins literally interprets FileWriteOptions.SuppressDialogBoxes as suppressing dialog boxes, so they ask options on the command line.
I can understand importer/exporter plugins will be updated eventually but it seems a big project.

This is exactly what I’m cleaning up when we find out about it. Initially we are just eliminating all coomand line and dialog input and choosing ‘default’ options. Over time we can add file format specific options/functions to RhinoCommon as needed.

Thank you for this script in latest wip, I succeed in collecting the dxf files I want, how to extract their respective layer structure, and filter the objects to be processed? I join sample dxf files:Limon1Gche.DXF (19.9 KB) Limon2Dte.DXF (22.1 KB) Limon2Gche.DXF (19.0 KB)

Here is an example of my actual definition, I’d like not to insert each file one by one:

Here is how you would extract a list of layer names along with the geometry when reading dxf files

private void RunScript(string path, ref object geometry, ref object layerNames)
{
  using( var doc = Rhino.RhinoDoc.CreateHeadless(null) )
  {
    doc.Import(path);
    var geometryList = new System.Collections.Generic.List<GeometryBase>();
    var names = new System.Collections.Generic.List<string>();
    foreach(var obj in doc.Objects)
    {
      geometryList.Add(obj.Geometry.Duplicate());
      var layer = doc.Layers[obj.Attributes.LayerIndex];
      names.Add(layer.FullPath);
    }
    geometry = geometryList;
    layerNames = names;
  }
}

6 Likes

Thank you very much, I’m going to try to adapt this to my workflow. :+1:

1 Like

Thank you very much mr. @stevebaer,
It looks really promising method since I’m trying import dxf files on rhino.compute locally.
But when I try to use it it gives me some error:

Error (CS1502): The best overloaded method match for 'Rhino.RhinoDoc.Import(string)' has some invalid arguments (line 59)
Error (CS1503): Argument 1: cannot convert from 'object' to 'string' (line 59)

Is it because I am trying to import more than one type of object, or the import method is wrong in this case?

Thank you.

Oops, I changed the type of the “path” into string and it worked now.