Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / dimension_tests.luc

## Sketch dimensions laid out for drawing (sketch/dimensions.lucb): a
## length's extension lines, dimension line and arrows, its label off the
## geometry and, once put somewhere, kept there as the geometry moves and
## through the sketch's text; a diameter's leader; an angle's arc; a
## point's distance from a line starting on the line.
from luce_cad.sketch import CadSketch
from luce_std import math

## ConstraintKind's order (sketch/entities.lucb).
let distance = 13
let line_distance = 14
let angle = 17
let diameter = 19

func near(a: float, b: float) -> bool:
    return math.abs(a - b) <= 1e-6 * math.max(1.0, math.abs(b))

pub func dimension_tests() -> unit!:
    length()
    round()
    angled()
    point_from_line()
    print("PASS sketch dimension layout: lengths, diameters, angles and point-line distances drawn; labels placed, moving with the geometry and kept as text")

## A 40 mm line along x with its length: the label 26 points off it (a
## point 0.1 mm), outward; extension lines at each end; the dimension line
## between them with arrows pointing out. The label put below stays there
## relative to the line as it moves, and through the text.
func length() -> unit!:
    let s = CadSketch.parse("")
    let a = s.add_point(0.0, 0.0)
    let b = s.add_point(40.0, 0.0)
    let line = s.add_line(a, b)
    let d = s.constrain(distance, a, b, value = 40.0)
    assert(s.lay_out(d, 0.1))
    assert(near(s.label_x(), 20.0) and near(math.abs(s.label_y()), 2.6), f"{s.label_x()}, {s.label_y()}")
    # Two extension lines and the dimension line.
    assert(s.layout_line_count() == 3)
    assert(s.layout_arrow_count() == 2)
    assert(near(s.layout_arrow(0, 0), 0.0) and near(s.layout_arrow(0, 2), -1.0) and near(s.layout_arrow(1, 0), 40.0) and near(s.layout_arrow(1, 2), 1.0))
    # The dimension line runs through the label.
    assert(near(s.layout_line(2, 1), s.label_y()) and near(s.layout_line(2, 3), s.label_y()))
    s.place_label(d, 30.0, -10.0)
    assert(s.label_placed(d))
    assert(s.lay_out(d, 0.1) and near(s.label_x(), 30.0) and near(s.label_y(), -10.0))
    # The line moved up 5: the label with it.
    s.move([line], 0.0, 5.0)
    assert(s.lay_out(d, 0.1) and near(s.label_x(), 30.0) and near(s.label_y(), -5.0))
    let back = CadSketch.parse(s.text())
    assert(back.text() == s.text() and back.label_placed(d))
    assert(back.lay_out(d, 0.5) and near(back.label_x(), 30.0) and near(back.label_y(), -5.0))
    # A label beyond the end: the dimension line goes out to it.
    s.place_label(d, 60.0, -5.0)
    assert(s.lay_out(d, 0.1) and s.layout_line_count() == 4)
    # Close ends on screen: arrows outside, pointing in.
    assert(s.lay_out(d, 10.0) and near(s.layout_arrow(0, 2), 1.0))
    # Not a dimension: nothing.
    assert(not s.lay_out(-1, 0.1))

## A circle's diameter: by default at 45° past the circle; a line across
## it with an arrow at each side; put above, at 90°.
func round() -> unit!:
    let s = CadSketch.parse("")
    let c = s.add_point(10.0, 10.0)
    let circle = s.add_circle(c, 5.0)
    let d = s.constrain(diameter, circle, value = 10.0)
    assert(s.lay_out(d, 0.1))
    let r = 5.0 + 2.6
    assert(near(s.label_x(), 10.0 + r * math.cos(math.pi * 0.25)) and near(s.label_y(), 10.0 + r * math.sin(math.pi * 0.25)))
    assert(s.layout_line_count() == 1 and s.layout_arrow_count() == 2)
    s.place_label(d, 10.0, 40.0)
    assert(s.lay_out(d, 0.1) and near(s.label_x(), 10.0) and near(s.label_y(), 40.0))
    # The arrows on the circle, pointing out along the leader.
    assert(near(s.layout_arrow(0, 1), 15.0) and near(s.layout_arrow(0, 3), 1.0))
    assert(near(s.layout_arrow(1, 1), 5.0) and near(s.layout_arrow(1, 3), -1.0))

## Two lines from the origin at a right angle: an arc between them with
## its label half way round, arrows at its ends along it; put further out,
## extension lines reach it along the rays.
func angled() -> unit!:
    let s = CadSketch.parse("")
    let o = s.add_point(0.0, 0.0)
    let first = s.add_line(o, s.add_point(10.0, 0.0))
    let second = s.add_line(s.add_point(0.0, 0.0), s.add_point(0.0, 10.0))
    let d = s.constrain(angle, first, second, value = math.pi * 0.5)
    assert(s.lay_out(d, 0.1))
    let radius = math.hypot(s.label_x(), s.label_y())
    assert(near(s.label_x(), s.label_y()) and radius > 0.0)
    assert(s.layout_arrow_count() == 2)
    s.place_label(d, 0.0, 20.0)
    assert(s.lay_out(d, 0.1) and near(s.label_x(), 0.0) and near(s.label_y(), 20.0))
    # The arc's pieces and an extension line along each ray.
    assert(s.layout_line_count() == 34, f"{s.layout_line_count()}")

## A point's distance from a line: from its foot on the line to the
## point, the extension line starting on the line where it is nearest.
func point_from_line() -> unit!:
    let s = CadSketch.parse("")
    let line = s.add_line(s.add_point(0.0, 0.0), s.add_point(20.0, 0.0))
    let p = s.add_point(30.0, 10.0)
    let d = s.constrain(line_distance, p, line, value = 10.0)
    assert(s.lay_out(d, 0.1))
    # Beside the line's end: the label past x = 30, the side away from the
    # line's middle.
    assert(s.label_x() > 30.0 and near(s.label_y(), 5.0), f"{s.label_x()}, {s.label_y()}")
    # The first extension starts at the line's end (x = 20 plus the gap).
    assert(near(s.layout_line(0, 0), 20.3) and near(s.layout_line(0, 1), 0.0), f"{s.layout_line(0, 0)}")