mesh2cad 0.13.0 修正 STL 轉 CAD 腳本在打印部件上偏差 8 mm 的問題
Why my STL to CAD script was 8 mm off on a printer part, and the fix
mesh2cad 0.13.0 修正 y-motor-holder 的腳本重建問題,透過分段深度擠出,將最大偏差由 7.924 mm 降至 0.1843 mm,體積誤差降至 0.306%(CadQuery)及 0.298%(OpenSCAD)。六個 Original Prusa i3 MK3S 部件中,現有四個可生成腳本;其餘兩個因幾何形狀複雜仍不生成腳本,但仍可取得精確 STEP 檔。
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 CadQuery or OpenSCAD script with named parameters, so you can change hole_3_d instead of editing triangles.
I test on six real Original Prusa i3 MK3S parts. After the last post all six were exact STEP, but only three had a script. 0.13.0 adds a fourth: y-motor-holder.
What was wrong
Last time I wrote that y-motor-holder was refused because its screw holes are stepped and don't go through the part. That was true, but it wasn't the main problem.
The script vocabulary built a solid by taking the outline of the part seen along X, Y and Z, extruding each one the full length of the part, and intersecting them. That works for blocky parts like the endstop block. It is called a visual hull, and it can only ever be too big: any shape that is hidden from all three directions is filled in.
y-motor-holder is a base 8 mm tall with a taller block and a sloped roof standing on only part of it, up to z=19. Seen from the side, the tall part covers the whole length, so the intersection put material on top of the whole base. The self-check measured it honestly: 7.924 mm max deviation and 30.07% volume error. Fixing the holes would never have closed that.
Partial-depth extrusions
0.13.0 can write a union of extrusions, each with its own outline, start and length. When the intersection misses the mesh, mesh2cad tries this instead and runs the same check.
For y-motor-holder the script is:
- a base from z=0 to z=8 (
extrude_z1_len) - a block and a sloped roof from z=8 to z=19, each with its own clip plane (
extrude_z2_clip1_d,extrude_z3_clip1_d), so a clip only cuts the extrusion it belongs to - three 45 degree edge bevels as short prisms
- cuts for the 0.5 mm slots, two hex pockets and two shallow pockets in the bottom face
- a 0.4 mm 45 degree chamfer on the base edges that are actually chamfered (
extrude_z1_chamfer,extrude_z1_chamfer_at) - two Ø3.2 holes from z=0 to z=8 (
hole_3_z0,hole_3_z1) with an Ø5.6 counterbore 3 mm deep from z=8 and a 0.4 mm chamfer at z=0
That last item is the stepped-hole part. A hole that opens on an intermediate face now carries its own span, and the counterbore is measured from that face, not from the top of the taller block next to it.
The numbers
Every script is rebuilt and compared to the mesh before it is written. The gate hasn't changed: max deviation at most max(4 x tolerance, 0.1 mm), volume error at most 1%. For this part the limit is 0.266 mm.
| script | max deviation | volume error |
|---|---|---|
| CadQuery | 0.1843 mm | 0.306% |
| OpenSCAD | 0.1843 mm | 0.298% |
| 0.12.0 (refused) | 7.924 mm | 30.07% |
The tests include an L-shaped base with a taller block on one arm and a counterbored hole whose mouth is the step. Editing the block length or the hole diameter changes the volume by exactly the analytic amount in both scripts.
Still refused
Four of six parts have a script now. The other two still don't, and I checked why against the original STEP files:
- y-belt-holder (2.517 mm, limit 0.154): the cross-section changes along X and the belt pocket is a B-spline profile. A stack of flat extrusions can't follow that.
- y-rod-holder (1.503 mm, limit 0.138): a Ø6.3 opening is tessellated as a partial ring of small planes, about 140 degrees of arc, so it isn't recovered as one cylinder. Forcing it in still leaves about 0.7 mm, because the body isn't a uniform extrusion either.
Both still get exact STEP. They just don't get a script that would be wrong.
Try it
pip install -U mesh2cad
mesh2cad convert part.stl -o part.step
mesh2cad convert part.stl --openscad part.scad
It also runs as an MCP server (mesh2cad mcp), so an assistant can convert a part and change a parameter for you. If you have an STL of a simple mechanical part that fails, an issue with the file attached helps most: github.com/emirmrkaljevic/mesh2cad.
來源:DEV Community · dev.to