STEP sample mechanisms with a sampled minimum-gap truth

assets/step-samples/ holds two small mechanisms as STEP files. Next to each STEP file sits a *.truth.json that lists the minimum gap between the two parts over a grid of poses. The gaps come from src/tolerance_arena/assembly/sample_truth.py, which places the parts with its own numpy rigid transforms, samples both surfaces densely and takes the nearest pair of samples with a KD-tree. It never asks the kernel for a distance. A later check can therefore compare the kernel distance query against numbers the kernel did not produce.

scripts/make_step_samples.py writes all four files. tests/assembly/test_assembly_step_samples.py checks them. The evidence behind the numbers below is in docs/assembly-log/step-samples.md.

Each mechanism also has a sidecar, <mechanism>.assembly.yaml, that arena assembly validate loads and that docs/bring-your-own-step.md uses. The sidecars are written by hand from the truth files and the generator does not write them. A regenerated STEP file with other bytes needs its sha256 updated in the sidecar; tests/assembly/test_bring_your_own_step.py compares each sidecar with the truth file and the STEP bytes.

The files

FileContentSize
latch-hook.stepsolids arm and striker, in that order146 160 bytes
latch-hook.truth.json231 poses, the planted pose, the contract fields32 874 bytes
drawer-carriage.stepsolids carriage and bracket, in that order250 428 bytes
drawer-carriage.truth.json231 poses, the planted pose, the contract fields32 992 bytes

SHA-256 of the STEP files as committed: latch-hook.step 46faa42d462b724ae395785864ce5cf45f748a47ba60731a8cd60b53c6e9ca32, drawer-carriage.step 14702f7dd79557461cf88b8b3fe0f84f85878eec5fb05643f3ad82f11709bed2.

The mechanisms

Both mechanisms follow the placement rule of src/tolerance_arena/assembly/types.py: the deviation moves its part by delta along direction in the world frame, the fixed part is placed with its deviation only, and the moving part is placed with its deviation first and the joint motion second. A joint value or a deviation outside its range is refused, never clamped.

latch-hook (revolute)

A latch arm swings about the Z axis through the origin towards a striker pin. The arm is a link between a pivot boss and a C-shaped hook head, 8 mm thick. The hook head has a throat bore of radius 5.6 mm whose mouth is cut by a chord, so the mouth is narrower than the throat. The two mouth corners carry a 0.4 mm fillet, the top edges a 0.3 mm chamfer, and a slot (20 mm by 5 mm) runs along the link at an angle to every axis. The pivot bore has radius 3 mm. The striker is a 22 mm by 16 mm base plate with rounded corners and two bores, a pin of radius 3 mm with a 1 mm tip chamfer and a 1.5 mm base fillet. At the end of travel the pin sits at the centre of the throat; on the way there it passes the two mouth corners.

FieldValue
jointpivot, revolute, fixed part striker, moving part arm, axis (0, 0, 1), origin (0, 0, 0) mm, range 0 to 30 degrees
deviationarm_offset, part arm, direction (1, 0, 0), range -0.6 to 0.6 mm
requirementhook_clearance, minimum_clearance between arm and striker, minimum 1.5 mm

The deviation is on the moving part, so the placement order matters: the offset is applied first and the arm then turns about the pivot, which carries the offset along an arc.

drawer-carriage (prismatic)

A carriage slides along +X towards a bracket. The carriage is a 40 mm by 24 mm by 10 mm block with rounded vertical edges, a T-slot underneath, four screw bores of radius 1.5 mm, a handle slot, a 0.6 mm top chamfer and a finger on its side with two 2 mm chamfers. The bracket is a plate with rounded corners, a stop with a 2.5 mm chamfer, a 1.5 mm edge fillet and a 1.5 mm base fillet, and a guide post of radius 3.5 mm with a bore, a top chamfer and a base fillet. The finger passes the post half way along the stroke.

FieldValue
jointslide, prismatic, fixed part bracket, moving part carriage, axis (1, 0, 0), origin (0, 0, 0) mm, range 0 to 30 mm
deviationbracket_offset, part bracket, direction (0, 1, 0), range -0.5 to 0.5 mm
requirementpost_clearance, minimum_clearance between carriage and bracket, minimum 1.5 mm

The deviation is on the fixed part, so the fixed part is placed with the deviation only and the carriage slides along the nominal axis.

The truth files

<mechanism>.truth.json holds:

  • parts: for each solid its id (the STEP label), its index in the file, its volume and its bounding box. A regenerated STEP file must re-import with the same volumes within a relative 1e-6 and the same boxes.
  • joint, deviation, requirement: the exact fields of JointSpec, DeviationSpec and RequirementSpec in src/tolerance_arena/assembly/types.py. types.JointSpec(**document["joint"]) builds the spec.
  • placement: the placement rule in one paragraph.
  • reference: the method, the tessellation (linear_deflection_mm, angular_deflection_rad, deflection_is_relative: false), the sampling (spacing_mm, covering_radius_mm, samples_per_part), error_bound_mm and its formula, and kernel_distance_used: false.
  • grid and poses: every pose of the grid with its joint value q, its deviation delta_mm, the sampled minimum gap gap_mm and below_requirement.
  • planted: the interior pose, the scan around it and the four-point grid it defeats.

The grid

MechanismJoint valuesDeviation valuesPoses
latch-hook0 to 30 degrees in steps of 1.5 (21 values)-0.6 to 0.6 mm in steps of 0.12 (11 values)231
drawer-carriage0 to 30 mm in steps of 1.5 (21 values)-0.5 to 0.5 mm in steps of 0.1 (11 values)231

Every pose lies inside the declared ranges. At the nominal deviation both mechanisms stay above the requirement: the lowest nominal gap is 1.891124 mm at q = 27 degrees for the latch and 1.701088 mm at q = 15 mm for the drawer. The deviation creates the violations: 4 of the 231 latch poses (lowest 1.299002 mm) and 9 of the 231 drawer poses (lowest 1.201089 mm) read below 1.5 mm.

Each part is sampled with 561 540 (striker), 1 317 238 (arm), 2 562 148 (bracket) and 1 602 306 (carriage) points at the spacing of 0.1 mm.

The reference method

sample_truth.py uses numpy and scipy only. A test parses its imports and fails if it imports a CAD library.

  1. Tessellation. The script meshes each solid with OCCT's BRepMesh_IncrementalMesh at an absolute linear deflection of 0.005 mm and an angular deflection of 0.1 rad, then reads the triangles of every face with the face orientation. The mesher call is made directly because the build123d Shape.tessellate deflection is relative to the size of the part: on a cylinder of radius 40 mm, tessellate(0.005) leaves a sag of 0.0124 mm between mesh and surface, where the direct call keeps it under 0.005 mm. A test meshes that cylinder and checks the sag of every wall edge.
  2. Placement. sample_truth.place builds a rigid transform for each part from the joint, the deviation and the part name with its own numpy code: the deviation first, then the joint motion. A q or delta outside its range raises SampleTruthError.
  3. Sampling. Each triangle is cut into rows parallel to its longest edge. The row gap is r / sqrt(2) and the step along a row is r * sqrt(2), with r = h / sqrt(3) and h the spacing (0.1 mm). Because the longest edge is the base, the two base angles are not obtuse, so every cross-section parallel to the base lies inside the row below it. A point between two rows is at most sqrt(s^2 + u^2/4) = r from a sample of the lower row. Corners, edges and face interiors are all sampled. A sliver costs samples in proportion to its length, which keeps the long thin triangles OCCT produces on fillets affordable.
  4. Nearest pair. The moving samples are carried into the frame of the fixed part and the nearest pair is taken with a scipy.spatial.cKDTree. To avoid querying millions of samples at every pose, the search first runs on two coarser sample sets (spacing 2 mm and 0.5 mm) and keeps only the samples that can still hold the nearest pair, then runs on the 0.1 mm samples around them. The result equals the minimum over every pair of 0.1 mm samples, and tests compare it with a brute-force minimum on random meshes and on a case built to defeat a too-tight prefilter.

Error bound

Let d be the true gap between the two solids, tol the linear deflection of the tessellation (every mesh point is within tol of the true surface and every true surface point within tol of the mesh), h the sampling spacing and r = h / sqrt(3). Write g for the sampled gap.

  1. The samples are points of the meshes, so g is at least the gap between the meshes, which is at least d - 2 tol.
  2. The two nearest points of the meshes each have a sample within r, so g is at most the mesh gap plus 2 r, which is at most d + 2 tol + 2 r.

Hence -2 tol <= g - d <= 2 tol + 2 r, and |g - d| <= 2 tol + 2 r is the stated bound. With tol = 0.005 mm and h = 0.1 mm the bound is 0.12547 mm. It is a worst case: it needs the nearest points to fall at the worst place between samples and in the worst place of a triangle, and it ignores that the nearest points of two parallel faces lie anywhere on the faces. The kernel agreement below is far tighter than the bound.

tests/assembly/test_assembly_sample_truth.py argues the bound in closed form: two parallel cylinders and a box against a cylinder in a tilted frame have exact gaps, and the sampled gap stays within the bound. A negative control uses a lattice of 5 mm and shows the bound fails when h is too large, so the test would notice a sampler that does not reach its stated density. The agreement with the kernel on the sample mechanisms is checked separately, on every pose.

Agreement with the kernel

test_every_sampled_pose_agrees_with_the_kernel_distance_within_the_tolerance places both solids of every truth pose with a helper of its own (build123d translate and rotate, which shares no code with the numpy transforms) and calls native.measure_distance. The test fails if one pose differs from the sampled gap by more than 0.02 mm. No pose does, so no exception needs an explanation.

MechanismPosesLargest abs(sampled - kernel)WhereMean sampled - kernelPoses over 0.02 mm
latch-hook2310.001715 mmq = 30.0, delta = 0.0+0.000125 mm0
drawer-carriage2310.001091 mmq = 13.5, delta = -0.2+0.000358 mm0

The stated error bound is 0.1255 mm, so the measured agreement is at least 73 times tighter than the bound and at least 11 times tighter than the 0.02 mm tolerance. Output of the test, one row in every 19 poses sorted by kernel gap (millimetres):

latch-hook: 231 poses, bound 0.1255 mm
   q      delta    sampled   kernel   sampled-kernel
  27.00    0.600   1.29900   1.29881    +0.000192
  27.00   -0.240   1.79284   1.79184    +0.000999
  28.50    0.000   2.12015   2.11924    +0.000910
  22.50    0.360   2.60619   2.60514    +0.001055
  21.00   -0.600   3.92282   3.92268    +0.000140
  12.00    0.360   5.00000   5.00000    +0.000004
  16.50   -0.480   5.00000   5.00000    +0.000000
  18.00    0.480   5.00000   5.00000    +0.000000
   9.00    0.000   5.01303   5.01299    +0.000039
   9.00   -0.360   5.04459   5.04458    +0.000006
   4.50    0.120   5.11123   5.11110    +0.000125
   1.50    0.600   5.61022   5.61009    +0.000127
   0.00   -0.360   6.10162   6.10162    +0.000002

drawer-carriage: 231 poses, bound 0.1255 mm
   q      delta    sampled   kernel   sampled-kernel
  15.00   -0.500   1.20109   1.20000    +0.001089
  18.00   -0.400   1.70105   1.70000    +0.001052
  30.00   -0.200   2.00000   2.00000    +0.000000
  16.50    0.500   2.22255   2.22189    +0.000664
  10.50    0.000   2.76915   2.76817    +0.000978
  28.50   -0.200   3.50000   3.50000    +0.000000
  21.00    0.200   3.86043   3.85935    +0.001084
   7.50    0.100   4.84425   4.84386    +0.000393
   7.50    0.500   5.12703   5.12670    +0.000326
   0.00   -0.400   6.00000   6.00000    +0.000000
   1.50    0.400   6.00000   6.00000    +0.000000
   4.50    0.100   6.00000   6.00000    +0.000000
  25.50    0.300   6.00000   6.00000    +0.000000

The sampled gap is larger than the kernel gap on average. The nearest pair of samples is never closer than the nearest pair of mesh points, and the mesh of a convex feature lies inside the solid, so both push the sampled gap up. The mesh of a bore lies in the void and pushes it down.

The planted interior pose

For each mechanism the script takes the grid deviation with the lowest gap (the adverse deviation) and scans the joint range around that lowest pose in steps of one tenth of a grid step, three grid steps either side. The planted pose is the middle of the interval where the scan reads below the requirement. The script refuses the pose unless the dip ends inside the scan, the planted gap is below the requirement and every point of the four-point grid at the same deviation is at or above it. The four-point grid is the joint range cut into three equal parts: the two ends and the two thirds points.

test_the_planted_pose_is_below_the_requirement_while_the_four_point_grid_reads_above_it prints the numbers and checks them against the kernel as well (requirement 1.5 mm for both):

MechanismAdverse deviationFour-point grid, joint valuesFour-point gaps, sampledFour-point gaps, kernelPlanted joint valuePlanted gap, sampledPlanted gap, kernelBelow the requirement from, to
latch-hook0.6 mm0, 10, 20, 30 degrees6.098675, 5.000001, 3.888321, 1.9984886.098421, 5.0, 3.887532, 2.026.7 degrees1.293007 mm1.292882 mm25.05 to 28.35 degrees
drawer-carriage-0.5 mm0, 10, 20, 30 mm6.0, 2.672648, 2.672648, 2.06.0, 2.67171, 2.67171, 2.015.0 mm1.201089 mm1.2 mm12.3 to 17.7 mm

The four-point grid reads at least 1.998488 mm for the latch and at least 2.0 mm for the drawer, 0.5 mm above the requirement, while the planted pose reads 0.2 mm (latch) and 0.3 mm (drawer) below it. The dips are 3.3 degrees and 5.4 mm wide and the four-point values are 10 degrees and 10 mm apart, so the dip falls between two of them. The deviation also decides the outcome: at the nominal deviation the planted pose reads above the requirement (test_the_deviation_is_what_exposes_the_violation_the_nominal_pose_stays_above).

STEP bytes and regeneration

scripts/make_step_samples.py writes the STEP files with build123d.export_step and a pinned header timestamp (2026-12-02T00:00:00). STEP carries a timestamp, and the default export also differs between two exports in one process through the NEXT_ASSEMBLY_USAGE_OCCURRENCE identifiers, which come from a counter that lives as long as the process.

  • A fresh run of the script writes the committed bytes. test_a_fresh_run_of_the_generator_writes_the_committed_step_bytes runs the script in a subprocess and compares the SHA-256 of both files with the committed ones: fresh 46faa42d462b724a and 14702f7dd7955746, committed the same. The counter restarts with the process, so writing the drawer alone with --mechanism drawer-carriage gives a different usage identifier in its bytes than the second file of a default run does.
  • Without the pinned timestamp two exports differ, and test_without_a_pinned_timestamp_two_exports_differ_only_in_the_header_and_the_usage_ids shows that the differing lines are the FILE_NAME header and the usage identifiers.
  • Whatever the bytes, a regenerated file re-imports to the same two solids: test_a_regenerated_step_file_reimports_to_the_same_volumes_and_bounding_boxes compares volumes within a relative 1e-6 and bounding boxes within 1e-6 mm with the numbers in the truth file. Each STEP file is below 1 MB.

To regenerate the sampled truth run .venv/bin/python scripts/make_step_samples.py. The options are --mechanism NAME for one mechanism, --step-only for the STEP files without the truth, and --jobs N to compute the pose grid in N forked processes (Linux; the default is 1). The truth run is slow because each part carries between 0.5 and 2.6 million samples. With a load of about 30 on 12 cores the latch took 25 minutes in one process and the drawer 74 minutes with five processes. After the truth run, --step-only rewrites the STEP files from one fresh process, which is how the committed STEP bytes were produced.

Limits

  • The gap is a surface-to-surface distance. Two solids that interpenetrate are not detected by sample_truth. The mechanisms are built so that no pose in the declared ranges overlaps; the lowest sampled gap is 1.2 mm.
  • The tessellation comes from OCCT. The distance computation shares no code with the kernel query, but the sampled gap rests on the OCCT mesher keeping the mesh within tol of the surface. The sag test checks that on a cylinder only.
  • The agreement table proves the sampled gap and the kernel gap agree on the listed poses. Between the poses the claim rests on the error bound.
  • The planted pose is one pose per mechanism. The four-point comparison is made at the adverse deviation of the grid, at the four joint values 0, one third, two thirds and the end of the range.
  • The poses whose nearest features are two parallel faces (the carriage bottom over the bracket plate, the carriage front against the stop) are the slowest to compute, because every sample of the face pair lies at nearly the same distance and no prune can drop any of them.