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;
}
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;
}
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";
}
}
}