Best method to export large amount of dxf/stp

Hi all.

I’m currently using a c# script that iterate through:

  • add to current Rhino document a set of geometries with attributes (layers, colors, etc)
  • select those geometries
  • run a silent command "-_Export " (to have them as .dxf or .stp)
  • delete the geometries
  • next branch

Doing this while many objects are visible on rhino document OR in grasshopper document, happens to be very slow sometime. Less than 1 file export per second.
If you remember to hide everything in Rhino and turn off global GH preview, it exports like 10 files per second, if not, you have to wait minutes until export script end…


Instead of passing through -_Export one by one… there is a more modern and reliable method?
… possibly without using a plugin, to properly take advantage of the new [Rhino] tab in Grasshopper.

Instead of adding the objects you want to write to the Rhino document that is currently open, you could instead open a document in headless mode, add everything to that headless doc, and write it to STEP using FileSTP (see below).

// add these 
using System.IO;
using Rhino.FileIO;

    private void RunScript(List<GeometryBase> x, string y, ref object a)
    {        
        RhinoDoc export = RhinoDoc.CreateHeadless(null);
        foreach(var item in x)
        {
            export.Objects.Add(item);
        }

        FileStpWriteOptions opt = new FileStpWriteOptions();
        if (!FileStp.Write(y, export, opt))
        {
            Print("Export failed.");
        }
        else Print("Export succeeded.");

        a = null;
    }
1 Like

Thank you!
This seems perfect!
I will try and I’ll report here if I have some problem…

We have an article on this new feature in Rhino 8 here

You will probably also want to dispose the doc when done.

1 Like

That’s good to know, thanks.

About using the new Rhino tab in grasshopper in Rhino 8…


To properly export objects with attributes and layers, I guess I have to stick with the “No type hint” and inside the code use a proper cast?
(If I’m not lost, GeometryBase type would lose all attributes…)

So, you would need to cast the object using the ModelObject.Cast method. So something like this would let you cast the incoming object to a model object and then output the layer name.

private void RunScript(object obj, ref object a)
{
    var modelObject = ModelObject.Cast(obj);
    a = modelObject.Layer.Name;
}

CastObject.gh (16.1 KB)

1 Like

If you wanted to export the objects with their attributes, you would do something like this:

private void RunScript(object obj, string path, ref object a)
{
    using (var doc = RhinoDoc.CreateHeadless(null))
    {
        if(obj is IGH_BakeAwareData bakeable)
        {
            var objID = Guid.Empty;
            bakeable.BakeGeometry(doc, null, out objID);
            var result = doc.SaveAs(path);
            if(result) a = $"File saved to {path}";
            else a = "File did not export properly";
        }
    }
}

ExportObject.gh (17.0 KB)

1 Like

Awesome! Thanks!

And then putting this all together, you can do something like this.

private void RunScript(List<object> objects, string path, ref object a)
{
    using (var doc = RhinoDoc.CreateHeadless(null))
    {
        foreach(var item in objects)
        {
            var bakeable = item as IGH_BakeAwareData;
            var objID = Guid.Empty;
            bakeable.BakeGeometry(doc, null, out objID);
        }
        FileDwgWriteOptions opt = new FileDwgWriteOptions();
        if (!FileDwg.Write(path, doc, opt))
        {
            Print("Export failed.");
        }
        else Print("Export succeeded.");
    }        
}

ExportDWG.gh (17.6 KB)

2 Likes