Files
make-cad/test/test_helix_handedness.py
NiJingzhe 8971279494 feat: the geometry engine is C++ on OpenCASCADE, one package, prebuilt wheels
The modeling API (shapes, operators, sketches, products, queries, the reactive graph) is implemented in
C++20 on OpenCASCADE (src/cpp) and ships inside the wheel as simplecadapi._cpp.  The Python layers that stay
Python (runtime, build, artifacts, editor, inspect, exporter, translators, scene) run on it unchanged; the
previous Python test suite passes on the new engine.

Layout: src/cpp and src/simplecadapi side by side, one distribution built by scikit-build-core.
simplecadapi.core, .operators.*, .ql, .std.* and the other legacy modules are real forwarding modules; the
Python adapter layer is simplecadapi/_glue.  OCP is no longer a dependency and nothing imports it.

Release: .github/workflows/release.yml builds wheels for Linux x86_64 and aarch64, macOS arm64 and Windows
x86_64 (Python 3.10-3.13) against one static OCCT, runs the test suite on the Linux wheels and publishes to
GitHub and PyPI; ci.yml caches the static OCCT for the test jobs.

Errors: every message the library writes is English; the tag-validation and semantic-query errors render in
full with possible causes and fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 02:55:35 +08:00

109 lines
3.8 KiB
Python

"""Helix handedness tests."""
from __future__ import annotations
import math
import unittest
import simplecadapi as scad
from simplecadapi._cpp.occt.BRepAdaptor import BRepAdaptor_Curve
def _helix_turns_and_rise(edge):
adaptor = BRepAdaptor_Curve(edge.wrapped)
t0, t1 = adaptor.FirstParameter(), adaptor.LastParameter()
samples = 400
prev_theta = None
total = 0.0
for index in range(samples + 1):
point = adaptor.Value(t0 + (t1 - t0) * index / samples)
theta = math.atan2(point.Y(), point.X())
if prev_theta is not None:
delta = theta - prev_theta
while delta > math.pi:
delta -= 2 * math.pi
while delta < -math.pi:
delta += 2 * math.pi
total += delta
prev_theta = theta
z0 = adaptor.Value(t0).Z()
z1 = adaptor.Value(t1).Z()
return total / (2 * math.pi), z1 - z0
class TestHelixHandedness(unittest.TestCase):
def test_right_handed_winds_counterclockwise_ascending(self):
edge = scad.make_helix_redge(pitch=2.0, height=10.0, radius=3.0)
turns, rise = _helix_turns_and_rise(edge)
self.assertAlmostEqual(turns, 5.0, places=3)
self.assertAlmostEqual(rise, 10.0, places=6)
def test_left_handed_winds_clockwise_ascending(self):
edge = scad.make_helix_redge(
pitch=2.0, height=10.0, radius=3.0, handedness="Left"
)
turns, rise = _helix_turns_and_rise(edge)
self.assertAlmostEqual(turns, -5.0, places=3)
self.assertAlmostEqual(rise, 10.0, places=6)
def test_handedness_keeps_radius_and_length(self):
right = scad.make_helix_redge(pitch=2.0, height=10.0, radius=3.0)
left = scad.make_helix_redge(
pitch=2.0, height=10.0, radius=3.0, handedness="Left"
)
self.assertAlmostEqual(
right.get_length(), left.get_length(), places=6
)
def test_invalid_handedness_is_rejected(self):
with self.assertRaises(Exception):
scad.make_helix_redge(
pitch=2.0, height=10.0, radius=3.0, handedness="wrong"
)
def test_left_handed_sweep_builds_a_solid(self):
profile = scad.make_wire_from_edges_rwire(
[scad.make_circle_redge((3.4, 0.0, 0.0), 0.3)]
)
solid = scad.helical_sweep_rsolid(
profile, pitch=2.0, height=6.0, radius=3.0, handedness="Left"
)
self.assertGreater(solid.get_volume(), 0.0)
spring = scad.helical_sweep_rsolid(
profile, pitch=2.0, height=6.0, radius=3.0, handedness="Right"
)
self.assertAlmostEqual(
solid.get_volume(), spring.get_volume(), places=6
)
def test_left_handed_freecad_script_uses_makehelix(self):
from simplecadapi.recording.graph import GraphSession
from simplecadapi.recording.serializer import import_model_json
from simplecadapi.translator.freecad_translator.translator import (
_FreeCADCompiler,
)
with GraphSession() as session:
scad.make_helix_redge(
pitch=2.0, height=10.0, radius=3.0, handedness="Left"
)
payload = scad.export_model_json(session)
script = _FreeCADCompiler().translate_model_payload_to_script(
import_model_json(payload)
)
self.assertIn("Part.makeHelix", script)
self.assertIn("True", script)
with GraphSession() as session:
scad.make_helix_redge(pitch=2.0, height=10.0, radius=3.0)
payload = scad.export_model_json(session)
script = _FreeCADCompiler().translate_model_payload_to_script(
import_model_json(payload)
)
self.assertIn("Part::Helix", script)
if __name__ == "__main__":
unittest.main()