"""Independent STEP check with the OpenCASCADE kernel: validity, units, solids, volume, extents, radii, mouth.""" import argparse import json parser = argparse.ArgumentParser( description="Read STEP solids with OpenCASCADE and print measurements.", epilog="Requires cadquery-ocp. Output includes radii and plane coordinates; compare with the article’s table.", ) parser.add_argument("step_file", nargs="+", help="one or more STEP files to inspect") args = parser.parse_args() from OCP.STEPControl import STEPControl_Reader from OCP.IFSelect import IFSelect_RetDone from OCP.BRepCheck import BRepCheck_Analyzer from OCP.TopExp import TopExp_Explorer from OCP.TopAbs import TopAbs_SOLID, TopAbs_SHELL, TopAbs_FACE, TopAbs_VERTEX from OCP.TopoDS import TopoDS from OCP.GProp import GProp_GProps from OCP.BRepGProp import BRepGProp from OCP.Bnd import Bnd_Box from OCP.BRepBndLib import BRepBndLib from OCP.BRepAdaptor import BRepAdaptor_Surface from OCP.GeomAbs import GeomAbs_Cylinder, GeomAbs_Plane from OCP.BRep import BRep_Tool from OCP.collections import Sequence_TCollection_AsciiString as Names def walk(shape, kind): e = TopExp_Explorer(shape, kind) while e.More(): yield e.Current(); e.Next() for path in args.step_file: r = STEPControl_Reader() assert r.ReadFile(path) == IFSelect_RetDone, "read failed" length, angle, solid_angle = Names(), Names(), Names() r.FileUnits(length, angle, solid_angle) units = [length.Value(i).ToCString() for i in range(1, length.Length() + 1)] r.TransferRoots() s = r.OneShape() solids = list(walk(s, TopAbs_SOLID)) props = GProp_GProps(); BRepGProp.VolumeProperties_s(s, props, 1e-9) box = Bnd_Box(); BRepBndLib.AddOptimal_s(s, box, False, False) lo, hi = box.CornerMin(), box.CornerMax() x0, y0, z0, x1, y1, z1 = lo.X(), lo.Y(), lo.Z(), hi.X(), hi.Y(), hi.Z() cyl, planes_x = [], [] faces = list(walk(s, TopAbs_FACE)) for f in faces: a = BRepAdaptor_Surface(TopoDS.Face(f)) if a.GetType() == GeomAbs_Cylinder: c = a.Cylinder(); d = c.Axis().Direction(); o = c.Location() cyl.append({"radius": round(c.Radius(), 6), "axis": [round(abs(v), 6) for v in (d.X(), d.Y(), d.Z())], "axis_x": round(o.X(), 6) + 0.0, "axis_z": round(o.Z(), 6)}) elif a.GetType() == GeomAbs_Plane: p = a.Plane(); n = p.Axis().Direction() if abs(abs(n.X()) - 1) < 1e-7: planes_x.append(round(p.Location().X(), 6)) print(json.dumps({ "file": path.split("/")[-1], "length_units": units, "valid": BRepCheck_Analyzer(s).IsValid(), "solids": len(solids), "shells": len(list(walk(s, TopAbs_SHELL))), "closed": all(BRep_Tool.IsClosed_s(sh) for sh in walk(s, TopAbs_SHELL)), "faces": len(faces), "volume_mm3": round(props.Mass(), 6), "bbox_min": [round(v, 5) for v in (x0, y0, z0)], "bbox_max": [round(v, 5) for v in (x1, y1, z1)], "cylinders": sorted({json.dumps(c) for c in cyl}), "x_plane_positions": sorted(set(planes_x)), }, indent=1))