Export layer and sublayer to a new.3dm

Hi, i am trying to create a c# method that will get all objects from a layer and its sublayer and will export to a new 3dm file keeping all attributes, etc…

What is the appropriate method to add objects to a file3dm?
Do I have to go through all type of geometry? like below:

switch (objectType)
        {
          case ObjectType.Brep:
            Brep brep = geometry as Brep;
            file.Objects.AddBrep(brep, attributes);
            break;
          case ObjectType.Curve:
            Curve curve = geometry as Curve;
            file.Objects.AddCurve(curve, attributes);
            break;
          default:
            return false;
        }

Is there a faster approach to duplicate layers with objects and attributes to a new 3dm file?

  public static void ExportLayerAndSublayerGeometry(string layerName, string outputFile)
  {
    // Find objects in the layer and its sublayers
    var objects = FindObjectsInLayerAndSublayers(layerName);

    if (objects.Count == 0)
    {
      // No objects found
      return;
    }

    // Create a new Rhino document based on a template file
    var newDoc = RhinoDoc.Create("Large Objects - Inches.3dm");

    // Get the object table from the new document
    var objectTable = newDoc.Objects;

    // Create new File3dm
    var file3dm = new File3dm();

    // Add the objects to the new document and the File3dm
    foreach (var obj in objects)
    {
      var newObj = obj.DuplicateGeometry();
      var attributes = obj.Attributes.Duplicate();

      // Add object to newDoc
      objectTable.Add(newObj, attributes);

      // Add object to file3dm
      file3dm.Objects.Add(newObj, attributes);// not working here!
      
    }

    // Save the new document to the specified file path
    file3dm.Write(outputFile, 5); // 5 represents version 5 of the Rhino 3DM file format
  }

Hey @tosiho19,

Regarding adding to the ObjectTable, you should be able to use Add(GeometryBase, ObjectAttributes). Breps, Curves etc are all derived from GeometryBase.