mesh2cad 0.16.0 新增可編輯的 FreeCAD 巨集:改一個數字即可重新執行
From STL to an editable FreeCAD macro: change one number, run it again
mesh2cad 0.16.0 新增 FreeCAD 巨集輸出,讓用戶把 STL 零件轉成可編輯的 CAD 模型,修改參數後重新執行即可生成新零件及 STEP 檔。巨集寫入前會以輸出實體與原始 STL 比對,要求最大偏差不超過 max(4 x tol, 0.1 mm),體積誤差不超過 1%。若找不到 freecadcmd,仍會寫出巨集,但會標示未經驗證。
mesh2cad turns an STL into CAD. Every part gets a STEP file with real planes, cylinders and cones. When the part is simple enough it also gets a script with named parameters, so "make the hole 2 mm bigger" is one edited number instead of an afternoon of mesh surgery.
Until now that script was CadQuery or OpenSCAD. A lot of people who print parts use FreeCAD, though, and asking them to install CadQuery just to change a hole felt wrong. So 0.16.0 adds a third output: a FreeCAD macro.
pip install -U mesh2cad
mesh2cad convert plate.stl --freecad plate.FCMacro
freecadcmd plate.FCMacro
What the macro looks like
For a 40 x 30 x 6 plate with a 10 mm hole, the part that matters is this:
# --- parameters ---
base_x = 40.0 # base length (X)
base_y = 30.0 # base width (Y)
base_t = 6.0 # base thickness (Z)
base_cx = 0.0
base_cy = 0.0
base_z0 = -3.0 # bottom face height
hole_1_d = 10.0 # diameter
hole_1_x = 0.0
hole_1_y = 0.0
# --- model ---
result = Part.makeBox(base_x, base_y, base_t, App.Vector(base_cx - base_x / 2, base_cy - base_y / 2, base_z0))
top = base_z0 + base_t
result = result.cut(Part.makeCylinder(hole_1_d / 2, base_t + 2, App.Vector(hole_1_x, hole_1_y, base_z0 - 1), App.Vector(0, 0, 1)))
Set hole_1_d = 12.0, run the macro again, and you have a Part::Feature with the bigger hole plus a fresh plate.step next to the macro. The cylinders and cones are analytic in FreeCAD, not $fn polygons like in OpenSCAD.
The parameter names are the same as in the CadQuery and OpenSCAD scripts (base_t, hole_1_d, hole_1_cbore_d, fillet_r, extrude_len, outline, cut_1, clip_1_d), so if you compare outputs side by side they line up. Under the hood it is FreeCAD's Part API: makeBox, makeCylinder, makeCone, a polygon extruded along X, Y or Z, fuse / cut / common, and makeFillet on the four vertical corners of a rectangular base.
It is checked before it is written
This is the rule I care about most in this project. A script that looks plausible but builds the wrong part is worse than no script.
So mesh2cad runs the macro with freecadcmd, meshes the resulting solid finely, and compares it with the input STL. The gate is the same as for the other two exporters: maximum deviation at most max(4 x tol, 0.1 mm), and volume error at most 1%. If the part is outside the supported shapes, or the solid misses, nothing is written and the command exits with code 2 and says why.
FreeCAD is not a Python dependency. If freecadcmd isn't on your PATH, the macro is still written, but the command tells you it is unverified.
Six real printer parts
I test on six printed parts from the Original Prusa i3 MK3S (Prusa Research's files, GPL-2.0). All six now export a FreeCAD macro that passes the check:
| part | FreeCAD macro deviation | limit |
|---|---|---|
| plug-aligner | 0.0000 mm | 0.1 mm |
| endstop-block | 0.0421 mm | 0.1 mm |
| y-belt-idler | 0.1693 mm | 0.192 mm |
| y-belt-holder | 0.0896 mm | 0.154 mm |
| y-motor-holder | 0.1843 mm | 0.266 mm |
| y-rod-holder | 0.1300 mm | 0.138 mm |
Each also stays under 1% volume error. Four of them match the CadQuery deviation exactly. The idler is a little farther from the mesh than the CadQuery script, and the belt holder is slightly closer than both other exporters.
The honest limits
- The macro is a plain Part script, not a PartDesign body with constrained sketches. Editing means changing a number at the top and running it again. You don't get a feature tree you can click through.
- It covers the same shapes as the other exporters: boxes, extruded outlines, holes and counterbores, 45 degree chamfers, drafted bosses, planar clips, fillets on four vertical corners. A boss on an extruded outline, a sphere, or a region that wasn't fitted is refused, and the message says which.
- Six boxy parts from one printer is a small test set. Organic shapes, fillets everywhere and scanned meshes still get STEP at best.
For AI agents too
mesh2cad is also an MCP server (pip install "mesh2cad[mcp]", then mesh2cad mcp). There is a new to_freecad(input_path, output_path?, tol?) tool next to to_cadquery and to_openscad. It returns the parameters and, when FreeCAD ran it, the measured deviation, or verified: false when freecadcmd is missing.
If you want to see the before and after without installing anything, there is a small gallery on Hugging Face with precomputed parts.
If you try it on a part of your own and it gets refused, I'd like to see it. Open an issue with the STL (or a link) and I'll look at what is missing.
來源:DEV Community · dev.to