|
| 1 | +# Basic shape builder tests |
| 2 | + |
| 3 | +# system modules |
| 4 | + |
| 5 | +# my modules |
| 6 | +from cadquery import * |
| 7 | + |
| 8 | +from cqkit import * |
| 9 | + |
| 10 | +from rich import inspect |
| 11 | + |
| 12 | + |
| 13 | +def _almost_same(x, y): |
| 14 | + if not isinstance(x, (list, tuple)): |
| 15 | + return abs(x - y) < 1e-3 |
| 16 | + return all(abs(x[i] - y[i]) < 1e-3 for i in range(len(x))) |
| 17 | + |
| 18 | + |
| 19 | +# def test_drafted_size(): |
| 20 | +# r = drafted_box(2, 3, 5, draft_angle=5) |
| 21 | +# export_step_file(r, "./tests/stepfiles/draft_box.step") |
| 22 | +# rc = drafted_cylinder(1, 5, draft_angle=5) |
| 23 | +# export_step_file(rc, "./tests/stepfiles/draft_cyl.step") |
| 24 | + |
| 25 | + |
| 26 | +def test_drafted_box(): |
| 27 | + r = drafted_box(1, 2, 3) |
| 28 | + assert _almost_same(size_3d(r), (1, 2, 3)) |
| 29 | + r = drafted_box(2, 4, 5, draft_angle=10) |
| 30 | + assert _almost_same(size_3d(r), (2.440, 4.440, 5)) |
| 31 | + assert _almost_same(size_2d(r.faces("<Z")), (2.441, 4.441)) |
| 32 | + assert _almost_same(size_2d(r.faces(">Z")), (1.559, 3.559)) |
| 33 | + r = drafted_box(1, 2, 3, 5, draft_length=False) |
| 34 | + assert _almost_same(size_3d(r), (1, 2.131, 3)) |
| 35 | + r = drafted_box(1, 2, 3, 5, draft_width=False) |
| 36 | + assert _almost_same(size_3d(r), (1.131, 2, 3)) |
| 37 | + |
| 38 | + |
| 39 | +def test_drafted_cylinder(): |
| 40 | + r = drafted_cylinder(1, 4) |
| 41 | + assert _almost_same(size_3d(r), (2, 2, 4)) |
| 42 | + r1 = drafted_cylinder(1.5, height=10) |
| 43 | + assert _almost_same(size_3d(r1), (3, 3, 10)) |
| 44 | + r2 = drafted_cylinder(1, height=7, draft_angle=15) |
| 45 | + assert _almost_same(size_3d(r2), (2.937, 2.937, 7)) |
| 46 | + assert _almost_same(size_2d(r2.faces("<Z")), (2.937, 2.937)) |
| 47 | + assert _almost_same(size_2d(r2.faces(">Z")), (1.062, 1.062)) |
| 48 | + |
| 49 | + |
| 50 | +def test_drafted_slot(): |
| 51 | + r = drafted_slot(10, 1.5, 5) |
| 52 | + assert _almost_same(size_3d(r), (10, 3, 5)) |
| 53 | + r = drafted_slot(10, 1.5, 5, draft_angle=5) |
| 54 | + assert _almost_same(size_3d(r), (10.218, 3.218, 5)) |
| 55 | + assert _almost_same(size_2d(r.faces("<Z")), (10.218, 3.218)) |
| 56 | + assert _almost_same(size_2d(r.faces(">Z")), (9.781, 2.781)) |
| 57 | + |
| 58 | + r = drafted_slot(10, 1.5, 5, draft_angle=5, draft_length=False) |
| 59 | + assert _almost_same(size_3d(r), (10, 3.218, 5)) |
| 60 | + r = drafted_slot(10, 1.5, 5, draft_angle=5, draft_radius=False) |
| 61 | + assert _almost_same(size_3d(r), (10.218, 3, 5)) |
| 62 | + |
| 63 | + |
| 64 | +def test_drafted_hollow_slot(): |
| 65 | + r = drafted_hollow_slot(10, 1.5, 5, 0.5, draft_angle=6) |
| 66 | + assert _almost_same(size_3d(r), (10.262, 3.262, 5)) |
| 67 | + assert _almost_same(size_2d(r.faces("<Z")), (10.262, 3.262)) |
| 68 | + assert _almost_same(size_2d(r.faces(">Z")), (9.737, 2.737)) |
| 69 | + |
| 70 | + |
| 71 | +def test_drafted_hollow_box(): |
| 72 | + r = drafted_hollow_box(10, 15, 20, 1.5) |
| 73 | + assert _almost_same(size_3d(r), (10, 15, 20)) |
| 74 | + w0 = r.faces("<Z").wires().vals() |
| 75 | + assert len(w0) == 2 |
| 76 | + wl = [wire_length(w) for w in w0] |
| 77 | + assert _almost_same(38, wl[0]) or _almost_same(50, wl[0]) |
| 78 | + assert _almost_same(38, wl[1]) or _almost_same(50, wl[1]) |
| 79 | + |
| 80 | + r = drafted_hollow_box(10, 15, 20, 1.5, workplane="ZX") |
| 81 | + assert _almost_same(size_3d(r), (15, 20, 10)) |
| 82 | + |
| 83 | + r1 = drafted_hollow_box(10, 15, 20, 1.5, 5) |
| 84 | + assert _almost_same(size_3d(r1), (10.875, 15.875, 20)) |
| 85 | + assert _almost_same(size_2d(r1.faces("<Z")), (10.875, 15.875)) |
| 86 | + assert _almost_same(size_2d(r1.faces(">Z")), (9.125, 14.125)) |
| 87 | + w1 = r1.faces(">Z").wires().vals() |
| 88 | + assert len(w1) == 1 |
| 89 | + w1 = r1.faces("<Z").wires().vals() |
| 90 | + assert len(w1) == 2 |
| 91 | + wl = [wire_length(w) for w in w1] |
| 92 | + assert _almost_same(41.237, wl[0]) or _almost_same(53.499, wl[0]) |
| 93 | + assert _almost_same(41.237, wl[1]) or _almost_same(53.499, wl[1]) |
| 94 | + re = r1.edges(FlatEdgeSelector(18.5)).vals() |
| 95 | + assert len(re) == 4 |
| 96 | + |
| 97 | + r2 = drafted_hollow_box(10, 15, 20, 1.5, 5, has_floor=True, has_roof=False) |
| 98 | + assert _almost_same(size_3d(r1), (10.875, 15.875, 20)) |
| 99 | + assert _almost_same(size_2d(r1.faces("<Z")), (10.875, 15.875)) |
| 100 | + assert _almost_same(size_2d(r1.faces(">Z")), (9.125, 14.125)) |
| 101 | + w2 = r2.faces("<Z").wires().vals() |
| 102 | + assert len(w2) == 1 |
| 103 | + w2 = r2.faces(">Z").wires().vals() |
| 104 | + assert len(w2) == 2 |
| 105 | + wl = [wire_length(w) for w in w2] |
| 106 | + assert _almost_same(34.763, wl[0]) or _almost_same(46.5, wl[0]) |
| 107 | + assert _almost_same(34.763, wl[1]) or _almost_same(46.5, wl[1]) |
| 108 | + |
| 109 | + r3 = drafted_hollow_box( |
| 110 | + 10, 15, 20, 1.5, 5, has_floor=False, has_roof=True, roof_thickness=1 |
| 111 | + ) |
| 112 | + assert _almost_same(size_3d(r3), (10.875, 15.875, 20)) |
| 113 | + assert _almost_same(size_2d(r3.faces("<Z")), (10.875, 15.875)) |
| 114 | + assert _almost_same(size_2d(r3.faces(">Z")), (9.125, 14.125)) |
| 115 | + w3 = r3.faces("<Z").wires().vals() |
| 116 | + wl = [wire_length(w) for w in w3] |
| 117 | + assert _almost_same(41.324, wl[0]) or _almost_same(53.499, wl[0]) |
| 118 | + assert _almost_same(41.324, wl[1]) or _almost_same(53.499, wl[1]) |
| 119 | + re = r3.wires(FlatWireSelector(19)).vals() |
| 120 | + assert len(re) == 1 |
| 121 | + assert _almost_same(wire_length(re[0]), 34.675) |
| 122 | + |
| 123 | + r4 = drafted_hollow_box(10, 15, 20, 1.5, 5, has_floor=False, has_roof=False) |
| 124 | + assert _almost_same(size_3d(r3), (10.875, 15.875, 20)) |
| 125 | + wb = r4.faces("<Z").wires().vals() |
| 126 | + assert len(wb) == 2 |
| 127 | + wl = [wire_length(w) for w in wb] |
| 128 | + assert _almost_same(41.5, wl[0]) or _almost_same(53.499, wl[0]) |
| 129 | + assert _almost_same(41.5, wl[1]) or _almost_same(53.499, wl[1]) |
| 130 | + wt = r4.faces(">Z").wires().vals() |
| 131 | + assert len(wt) == 2 |
| 132 | + wl = [wire_length(w) for w in wt] |
| 133 | + assert _almost_same(34.5, wl[0]) or _almost_same(46.5, wl[0]) |
| 134 | + assert _almost_same(34.5, wl[1]) or _almost_same(46.5, wl[1]) |
| 135 | + |
| 136 | + |
| 137 | +def test_drafted_hollow_cylinder(): |
| 138 | + r = drafted_hollow_cylinder(5, 15, 0.5) |
| 139 | + assert _almost_same(size_3d(r), (10, 10, 15)) |
| 140 | + w0 = r.faces("<Z").wires().vals() |
| 141 | + assert len(w0) == 2 |
| 142 | + |
| 143 | + r = drafted_hollow_cylinder(5, 15, 0.5, 5) |
| 144 | + assert _almost_same(size_3d(r), (10.656, 10.656, 15)) |
| 145 | + w0 = r.faces("<Z").wires().vals() |
| 146 | + assert len(w0) == 2 |
| 147 | + |
| 148 | + r = drafted_hollow_cylinder( |
| 149 | + 5, 15, 0.5, 5, floor_thickness=2, has_floor=True, has_roof=False |
| 150 | + ) |
| 151 | + assert _almost_same(size_3d(r), (10.656, 10.656, 15)) |
| 152 | + wb = r.faces("<Z").wires().vals() |
| 153 | + assert len(wb) == 1 |
| 154 | + wt = r.faces(">Z").wires().vals() |
| 155 | + assert len(wt) == 2 |
| 156 | + re = r.wires(FlatWireSelector(2)).vals() |
| 157 | + assert len(re) == 1 |
| 158 | + |
| 159 | + r = drafted_hollow_cylinder(4, 10, 1, draft_angle=5, workplane="YZ") |
| 160 | + assert _almost_same(size_3d(r), (10, 8.437, 8.437)) |
0 commit comments