I'm trying to export a Rhino.Geometry.Mesh as an STL file with simple and straightforward approach.
Tried
I tried this code:
public static bool ExportMeshToStl(Mesh mesh, string fileName)
{
if (mesh == null || string.IsNullOrEmpty(fileName)) return false;
// Create a temporary layer to isolate the specific mesh
var tempLayer = new Layer { Name = "TempLayerForStlExport" };
int tempLayerIndex = RhinoDoc.ActiveDoc.Layers.Add(tempLayer);
if (tempLayerIndex < 0) return false;
// Add the mesh to the temporary layer
var attributes = new ObjectAttributes { LayerIndex = tempLayerIndex };
var meshId = RhinoDoc.ActiveDoc.Objects.AddMesh(mesh, attributes);
if (meshId == Guid.Empty)
{
RhinoDoc.ActiveDoc.Layers.Delete(tempLayerIndex, true);
return false;
}
// Use RhinoApp.RunScript to export the mesh on the temporary layer as an STL file
var script = $"_-Export \"{fileName}\" _Enter";
RhinoDoc.ActiveDoc.Layers.SetCurrentLayerIndex(tempLayerIndex, true);
bool result = RhinoApp.RunScript(script, false);
// Remove the mesh and the temporary layer from the current document
RhinoDoc.ActiveDoc.Objects.Delete(meshId, true);
RhinoDoc.ActiveDoc.Layers.Delete(tempLayerIndex, true);
return result;
}
But MeshLab cannot open the resulted mesh:
Question
I couldn't find a straightforward & simple approach. Is there any?

Extract buffers
I extract index and vertex buffers of a mesh by this method:
Save buffers as STL
Then, I save the index and vertex buffers as STL by this method:
Call methods
The above methods are called like this:
Test
Tests indicate that the above methods work just fine.