You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求简洁方法导出Rhino.Geometry.Mesh为STL文件,现有导出文件MeshLab无法打开

问题描述

我尝试通过简单直接的方法将Rhino.Geometry.Mesh导出为STL文件。

已尝试方案

我使用了以下代码:

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

但导出的网格无法被MeshLab打开,提示“Error: faulty mesh”。

需求

我未能找到简单直接的实现方法,请问是否存在这样的方案?


解决方案

不需要通过临时层和脚本调用的迂回方式,直接使用Rhino官方提供的Rhino.FileIO.FileStl类即可实现Mesh到STL文件的直接导出,这种方法简洁可靠,且无需操作当前Rhino文档。

实现代码如下:

using Rhino.FileIO;
using Rhino.Geometry;

public static bool ExportMeshToStl(Mesh mesh, string fileName)
{
    // 校验输入合法性
    if (mesh == null || string.IsNullOrEmpty(fileName))
        return false;

    // 可选:提前校验网格有效性,排除本身有问题的网格
    if (!mesh.IsValid)
        return false;

    // 直接导出网格为STL
    // 第三个参数binary为true时导出二进制STL(体积小、兼容性好),false则导出ASCII格式
    return FileStl.Write(fileName, new[] { mesh }, true);
}

关键说明

  • FileStl.Write方法支持直接传入Mesh数组,无需将网格添加到Rhino文档中,避免了临时层、对象操作等额外步骤
  • 二进制STL格式是大多数3D软件(包括MeshLab)的优先选择,兼容性更强
  • 提前检查mesh.IsValid可以在导出前排除无效网格,减少后续报错概率

这种方法是目前最直接的实现方式,导出的STL文件可被MeshLab正常识别。

内容的提问来源于stack exchange,提问作者Megidd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:02:05