RhinoCommon V6: Import specific geometry from 3DM file?

Hi all,

I want my C# RhinoCommon (V6) plugin to selectively import geometry from another .3dm file.

For instance, I would like to import a box object that’s on a specific layer.

The information I found on the forum is rather inconclusive, also most relevant links are outdated.

What I can do is import the entire file:

  private Rhino.Commands.Result testLoadingFrom3dm(RhinoDoc doc)
        {
            var options = new Rhino.FileIO.FileReadOptions();
            options.ImportMode = true;

            bool result = RhinoDoc.ReadFile("../testfile.3dm", options);

            if (result)
                return Result.Success;
            else
                return Result.Failure;
        }

Any help appreciated!

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
    var file3dm = File3dm.Read(@"C:\test.3dm");
    foreach (var obj in file3dm.Objects)
    {
        if (file3dm.AllLayers.FindIndex(obj.Attributes.LayerIndex).Name != "Default") continue;
        if (!(obj.Geometry is Extrusion extrusion)) continue;
        if (!extrusion.IsSolid || extrusion.ProfileCount != 1) continue;
        var profile = extrusion.Profile3d(0, 0);
        if (!profile.IsPolyline()) continue;
        profile.TryGetPolyline(out var pl);
        Rectangle3d.CreateFromPolyline(pl, out var deviation, out var angle);
        if (deviation < doc.ModelAbsoluteTolerance && angle < doc.ModelAngleToleranceRadians)
            doc.Objects.AddExtrusion(extrusion);
    }
    return Result.Success;
}
1 Like

@Mahdiyar thank you so much, this is EXACTLY what i was searching for!