Import a triangle mesh as an editable B-rep body (mesh2step port)

Opening an STL/OBJ in the Design pane now rebuilds it into a real OCCT B-rep
solid that the face/edge feature tools can operate on, instead of a print mesh.

GeometryEngine::mesh_to_brep is a native C++ port of mesh2step
(github.com/tommasobbianchi/mesh2step): vertices and edges are shared across
triangles at construction time (vertex cache by deduped index, edge cache by
unordered index pair), so no BRepBuilderAPI_Sewing pass is needed to rebuild the
topology afterwards, and watertightness falls out of the edge-usage counts for
free. An open mesh is returned as a shell and reported as such — never dressed up
as a fake solid.

It runs in-process on the OCCT kernel libslic3r already links, so no STEP file is
written or re-read. That is not an optimisation but the whole point: a faceted
STEP of a 62k-triangle mesh is ~149 MB and OCCT's STEPControl_Reader takes >300 s
to parse it back, so routing this through a file would hang the GUI.

Coplanar neighbours are merged (ShapeUpgrade_UnifySameDomain, 5° default) so the
body arrives with pickable CAD faces rather than one face per triangle — on the
20,656-triangle test part that is 20,614 faces down to 4,784. Without it the
import is technically a solid but nothing you can meaningfully fillet or extrude.

- Design pane: "Import mesh" button + Shift+M; warns above 50k triangles.
- MCP: import_mesh {path, tolerance, merge_angle_deg}, returning the full
  conversion stats so a caller can tell an honest solid from an open shell.
- Catch2: cube round-trip (exact volume, 12 faceted faces, 6 after merge), open
  mesh stays a shell, and the scale-independent sliver rule that a naive
  area < tolerance^2 test would get wrong.

Verified end-to-end on the real 20,656-triangle ir3v2 hotend STL: reproduces
mesh2step's Python run exactly (20,614 kept, 42 degenerate, 0 boundary edges,
2 non-manifold edges, not watertight) and the resulting body's bbox matches the
one FreeCAD reports for the same part.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BVzKmX6Y1aEteit1HTXG4Q
This commit is contained in:
Tommaso Bianchi
2026-07-11 07:02:33 +02:00
co-authored by Claude Opus 4.8
parent c618965a4c
commit 343a0439f1
6 changed files with 472 additions and 0 deletions
+128
View File
@@ -2,6 +2,12 @@
#include "DesignCanvas.hpp"
#include "DesignSketchTool.hpp"
#include "libslic3r/GeometryEngine.hpp" // face_by_index for face-extrude gizmo anchor
#include "libslic3r/TriangleMesh.hpp" // mesh import: STL/OBJ -> indexed_triangle_set
#include "libslic3r/Format/OBJ.hpp"
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/filesystem/path.hpp>
#include <Standard_Failure.hxx>
#include <wx/sizer.h>
#include <wx/button.h>
@@ -52,6 +58,17 @@
namespace Slic3r { namespace GUI {
// Mesh -> B-rep import defaults (see GeometryEngine::mesh_to_brep).
// Tolerance is a vertex-dedup cell, not a sew tolerance: 10 um is well under any printable
// feature yet coarse enough to weld the float noise a mesh exporter leaves on shared vertices.
static constexpr double MESH_IMPORT_TOLERANCE = 0.01; // mm
// Merge coplanar neighbours so the body has real, pickable faces instead of one face per
// triangle. 5 deg tolerates the small normal jitter of an exported/scanned flat face while
// still keeping genuinely curved regions faceted.
static constexpr double MESH_IMPORT_MERGE_ANGLE_DEG = 5.0;
// Above this, warn before converting: the build is one OCCT face per triangle.
static constexpr size_t MESH_IMPORT_TRIANGLE_WARN = 50000;
// Format a value with the international ('.') decimal separator regardless of the
// app's LC_NUMERIC locale (wx sets it to the user locale at startup). snprintf may
// emit a comma, so normalise it.
@@ -628,6 +645,12 @@ DesignPanel::DesignPanel(wxWindow* parent)
b_step->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { on_import_step(); });
m_keys_feature[SHIFT('I')] = [this] { on_import_step(); };
fadd(b_step);
// Import mesh — same destination as STEP (an editable B-rep body), but the geometry has
// to be reconstructed from triangles first (GeometryEngine::mesh_to_brep).
auto* b_mesh = icon_btn("design_step", _L("Import mesh (STL/OBJ) as an editable B-rep solid"));
b_mesh->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { on_import_mesh(); });
m_keys_feature[SHIFT('M')] = [this] { on_import_mesh(); };
fadd(b_mesh);
add_sep(m_tb_feature);
auto* b_constrain = icon_btn("design_constrain", _L("Constrain selected sketch"));
b_constrain->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) {
@@ -2392,6 +2415,111 @@ void DesignPanel::on_import_step()
m_status->Refresh();
}
// Import a triangle mesh as a real B-rep body: the triangles are rebuilt into OCCT faces with
// shared topology, then coplanar neighbours are merged so the result has pickable CAD faces
// rather than one face per triangle. Lands in the same CadFeatureType::Import as a STEP, so
// every downstream feature tool (fillet / cut / shell / face-extrude) works on it unchanged.
void DesignPanel::on_import_mesh()
{
wxFileDialog dlg(this, _L("Import mesh"), wxEmptyString, wxEmptyString,
"Mesh files (*.stl;*.obj)|*.stl;*.obj|All files|*.*",
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (dlg.ShowModal() != wxID_OK)
return;
const std::string path(dlg.GetPath().ToUTF8().data());
auto fail = [this](const wxString& msg) {
m_status->SetForegroundColour(wxColour(235, 110, 110));
m_status->SetLabel(msg);
m_status->Refresh();
};
// Load the triangles with the slicer's own readers — no new mesh dependency.
TriangleMesh mesh;
const std::string ext = boost::algorithm::to_lower_copy(
boost::filesystem::path(path).extension().string());
if (ext == ".stl") {
if (!mesh.ReadSTLFile(path.c_str())) { fail(_L("Could not read the STL file")); return; }
} else if (ext == ".obj") {
ObjInfo obj_info;
std::string obj_err;
if (!load_obj(path.c_str(), &mesh, obj_info, obj_err)) {
fail(_L("Could not read the OBJ file: ") + wxString::FromUTF8(obj_err));
return;
}
} else {
fail(_L("Unsupported mesh format (STL and OBJ are supported)"));
return;
}
if (mesh.its.indices.empty()) { fail(_L("The mesh contains no triangles")); return; }
// One planar face per triangle before merging, so the cost is driven by the triangle count.
// A dense organic scan has few coplanar neighbours to merge away and stays heavy afterwards;
// warn rather than silently freezing the CAD kernel on every subsequent recompute.
if (mesh.its.indices.size() > MESH_IMPORT_TRIANGLE_WARN) {
const wxString q = wxString::Format(
_L("This mesh has %d triangles. Every triangle becomes a B-rep face before coplanar "
"merging, so importing it may take a long time and leave a body that is slow to "
"edit. Decimating the mesh first is usually better.\n\nImport anyway?"),
int(mesh.its.indices.size()));
if (wxMessageBox(q, _L("Large mesh"), wxYES_NO | wxICON_WARNING, this) != wxYES)
return;
}
wxBusyCursor busy;
GeometryEngine::MeshBrepStats stats;
TopoDS_Shape shape;
try {
shape = GeometryEngine::mesh_to_brep(mesh.its, MESH_IMPORT_TOLERANCE,
MESH_IMPORT_MERGE_ANGLE_DEG, stats);
} catch (const std::exception& e) {
fail(_L("Mesh conversion failed: ") + wxString::FromUTF8(e.what()));
return;
} catch (const Standard_Failure& e) { // OCCT throws outside std::exception
fail(_L("Mesh conversion failed: ") + wxString::FromUTF8(
e.GetMessageString() ? e.GetMessageString() : "OCCT error"));
return;
}
if (shape.IsNull()) { fail(_L("Mesh conversion produced no geometry")); return; }
m_doc.checkpoint(); // undo boundary: importing a mesh as a B-rep body
m_feature_counter++;
CadFeature f;
f.type = CadFeatureType::Import;
f.name = std::string("Mesh") + std::to_string(m_feature_counter);
f.imported_solid = shape;
f.mode = BooleanMode::New; // its own coexisting body, like a STEP solid
m_doc.features.push_back(f);
if (!m_doc.recompute()) {
fail(_L("Mesh import failed: ") + wxString::FromUTF8(m_doc.error));
return;
}
set_ui_mode(UiMode::Feature);
refresh_tree();
set_tree_selection(int(m_doc.features.size()) - 1);
set_status_ok();
// Report what the mesh actually was, never dress an open shell up as a solid: if it is not
// watertight, say so and say why (boundary vs non-manifold edges) — that is a defect in the
// source mesh the user needs to know about before they start cutting features into it.
if (stats.is_solid) {
m_status->SetForegroundColour(wxNullColour);
m_status->SetLabel(wxString::Format(
_L("Imported solid — %d triangles → %d faces, volume %.2f mm³. Pick a face or edge, "
"then Fillet / Cut / Shell to modify"),
stats.kept_tris, stats.faces_final, stats.volume));
} else {
m_status->SetForegroundColour(wxColour(220, 160, 60)); // warning, not an error
m_status->SetLabel(wxString::Format(
_L("Imported as an open shell (not watertight): %d boundary edge(s), %d non-manifold "
"edge(s) — %d triangles → %d faces. The source mesh has holes or duplicated "
"geometry; boolean features may fail on it"),
stats.boundary_edges, stats.nonmanifold_edges, stats.kept_tris, stats.faces_final));
}
m_status->Refresh();
}
void DesignPanel::add_imported_sketch(
const std::vector<std::vector<std::vector<Vec2d>>>& regions,
const wxString& base_name)
+1
View File
@@ -90,6 +90,7 @@ private:
void on_add_text();
void on_import_svg();
void on_import_step(); // STEP -> editable B-rep body (keeps the OCCT solid, not a mesh)
void on_import_mesh(); // STL/OBJ -> B-rep body via GeometryEngine::mesh_to_brep
bool place_on_face(); // Prepare's Place on Face (F): lay the selected body face on the bed
void add_imported_sketch(const std::vector<std::vector<std::vector<Vec2d>>>& regions,
const wxString& base_name);
+63
View File
@@ -29,6 +29,9 @@
#include "libslic3r/SketchEngine.hpp"
#include "libslic3r/GeometryEngine.hpp"
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/Format/OBJ.hpp"
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/filesystem/path.hpp>
#include "libslic3r/BoundingBox.hpp"
#include <gp_Pln.hxx>
@@ -194,6 +197,12 @@ json describe_tools()
{"params", json::array({
json{{"name", "path"}, {"type", "string"}},
})}},
json{{"name", "import_mesh"}, {"summary", "Convert a triangle mesh (STL/OBJ) into an editable B-rep body. Reports whether the result is a real solid or an open shell, and why."},
{"params", json::array({
json{{"name", "path"}, {"type", "string"}},
json{{"name", "tolerance"}, {"type", "number"}, {"default", 0.01}},
json{{"name", "merge_angle_deg"}, {"type", "number"}, {"default", 5.0}},
})}},
json{{"name", "validate_against"}, {"summary", "Volume + bbox/centroid + surface deviation (max/mean/rms mm) of a body vs a reference {step:path|body:id} (the RE acceptance metric)."},
{"params", json::array({
json{{"name", "body"}, {"type", "integer"}, {"default", 0}},
@@ -449,6 +458,59 @@ json import_step(DesignPanel* panel, const json& params)
{"bodies", int(doc.bodies.size())}, {"error", doc.error}};
}
// --- Import a triangle mesh as a B-rep body (GeometryEngine::mesh_to_brep) ---
// Same destination as import_step: a CadFeatureType::Import body every feature tool can edit.
// The full conversion stats come back so a caller can tell an honest solid from an open shell
// instead of discovering it later when a boolean silently fails.
json import_mesh(DesignPanel* panel, const json& params)
{
if (!params.contains("path")) throw std::runtime_error("import_mesh needs 'path'");
const std::string path = params["path"].get<std::string>();
const double tolerance = params.value("tolerance", 0.01);
const double merge_angle_deg = params.value("merge_angle_deg", 5.0);
TriangleMesh mesh;
const std::string ext = boost::algorithm::to_lower_copy(
boost::filesystem::path(path).extension().string());
if (ext == ".stl") {
if (!mesh.ReadSTLFile(path.c_str())) throw std::runtime_error("could not read STL: " + path);
} else if (ext == ".obj") {
ObjInfo obj_info; std::string obj_err;
if (!load_obj(path.c_str(), &mesh, obj_info, obj_err))
throw std::runtime_error("could not read OBJ: " + obj_err);
} else {
throw std::runtime_error("unsupported mesh format (want .stl or .obj): " + ext);
}
GeometryEngine::MeshBrepStats st;
const TopoDS_Shape shape = GeometryEngine::mesh_to_brep(mesh.its, tolerance, merge_angle_deg, st);
if (shape.IsNull()) throw std::runtime_error("mesh conversion produced no geometry");
CadDocument& doc = panel->mcp_doc();
doc.checkpoint();
const int first = int(doc.features.size());
CadFeature f;
f.type = CadFeatureType::Import;
f.name = "Mesh" + std::to_string(first + 1);
f.imported_solid = shape;
f.mode = BooleanMode::New;
doc.features.push_back(f);
const bool ok = doc.recompute();
if (!ok) doc.undo();
panel->mcp_after_change();
return json{{"ok", ok}, {"first_feature", first}, {"bodies", int(doc.bodies.size())},
{"input_triangles", st.input_tris}, {"kept_triangles", st.kept_tris},
{"degenerate_collapsed", st.degenerate_collapsed},
{"degenerate_sliver", st.degenerate_sliver},
{"faces_built", st.faces_built}, {"faces_failed", st.faces_failed},
{"faces_final", st.faces_final}, {"unique_edges", st.unique_edges},
{"boundary_edges", st.boundary_edges},
{"nonmanifold_edges", st.nonmanifold_edges},
{"watertight", st.watertight}, {"is_solid", st.is_solid},
{"volume", st.volume}, {"error", doc.error}};
}
// --- Validate: volume + bbox deviation of a body vs a reference (the "scarto %") --
// ponytail: volume delta + bbox/centroid offset (the RE skill's actual acceptance metric).
// Surface-deviation heat-map is the upgrade path (per-vertex BRepExtrema), add when needed.
@@ -719,6 +781,7 @@ std::string handle_on_main(const std::string& method, const json& params, const
if (method == "measure") return rpc_result(id, measure(panel, params));
if (method == "slice_body") return rpc_result(id, slice_body(panel, params));
if (method == "import_step") return rpc_result(id, import_step(panel, params));
if (method == "import_mesh") return rpc_result(id, import_mesh(panel, params));
if (method == "validate_against") return rpc_result(id, validate_against(panel, params));
if (method == "extrude") return rpc_result(id, action_extrude(panel, params));
if (method == "revolve") return rpc_result(id, action_revolve(panel, params));