How to save objects' geometries into separate 3dm files

Hello,
I found this thread where I see I can use File3dm class to inject some geometries (LineCurve and Point3d in the thread’s example by @dale ).

My purpose is to start from the acrive document, browse the objects contained in the document and save N 3dm files, each containing one object. I don’t understand how to inject into del File3dm class the geometry from one of those objects. The code could be something like

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
        foreach (var obj in doc.Objects)
        {
            SaveGeom(obj.Geometry);
        }
    }

    private void SaveGeom(GeometryBase geom)
    {
        var destfile = "mygeom.3dm"; //ok, here I must have different filenames

        using (var file = new File3dm())
        {
            var layer = new Layer { Name = "Default", Color = Color.Black };
            file.Layers.Add(layer);

            var layer_index = file.Layers.Count - 1;
            var attributes = new ObjectAttributes { LayerIndex = layer_index };

            //file.Objects.AddArc ?
            //file.Objects.AddBrep ?
            //file.Objects.AddCircle ?
            //file.Objects.AddCurve ?
            //file.Objects.AddExtrusion ?

            file.Write(destfile, 6);
        }
    }

Where the code is commented, I would aspect to be able to call something like

file.Objects.AddGeometry(geom)

How can I achieve this?
Thank you for any hint.

Hi @mcollareda,

Try doing something like this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  if (0 == doc.Objects.Count || string.IsNullOrEmpty(doc.Path))
    return Result.Nothing;

  string path = doc.Path;
  string filename = Path.GetFileNameWithoutExtension(doc.Path);

  foreach (var rh_obj in doc.Objects)
  {
    var new_filename = $"{filename}-{rh_obj.ObjectType}-{rh_obj.Id}";
    var new_path = path.Replace(filename, new_filename);

    using (var file = new File3dm())
    {
      var layer = new Layer { Name = "Default", Color = Color.Black };
      file.AllLayers.Add(layer);

      var layer_index = file.AllLayers.Count - 1;
      var attributes = new ObjectAttributes { LayerIndex = layer_index };

      switch (rh_obj.ObjectType)
      {
        case ObjectType.Curve:
          file.Objects.AddCurve(rh_obj.Geometry as Curve, attributes);
          break;
        case ObjectType.Brep:
          file.Objects.AddBrep(rh_obj.Geometry as Brep, attributes);
          break;
        case ObjectType.Extrusion:
          file.Objects.AddExtrusion(rh_obj.Geometry as Extrusion, attributes);
          break;
        case ObjectType.Mesh:
          file.Objects.AddMesh(rh_obj.Geometry as Mesh, attributes);
          break;
      }

      file.Write(new_path, 6);
    }
  }

  return Result.Success;
}

– Dale

Thank you @dale for your kind answer.

I will test your code as soon as possibile and I will let you know.