cosplay: Touhou/Houjuu Nue #4
17
nhf/test.py
17
nhf/test.py
|
@ -163,5 +163,22 @@ class TestUtils(unittest.TestCase):
|
||||||
self.assertAlmostEqual(bbox.ylen, 15)
|
self.assertAlmostEqual(bbox.ylen, 15)
|
||||||
self.assertAlmostEqual(bbox.zlen, 5)
|
self.assertAlmostEqual(bbox.zlen, 5)
|
||||||
|
|
||||||
|
def test_abs_location(self):
|
||||||
|
box = Cq.Solid.makeBox(1, 1, 1)
|
||||||
|
assembly = (
|
||||||
|
Cq.Assembly()
|
||||||
|
.add(box, name="b1")
|
||||||
|
.add(box, name="b2", loc=Cq.Location((0,0,1)))
|
||||||
|
.add(box, name="b3", loc=Cq.Location((0,0,2)))
|
||||||
|
)
|
||||||
|
(x, y, z), _ = assembly.get_abs_location("b2@faces@>Y").toTuple()
|
||||||
|
self.assertAlmostEqual(x, 0.5)
|
||||||
|
self.assertAlmostEqual(y, 1)
|
||||||
|
self.assertAlmostEqual(z, 1.5)
|
||||||
|
(rx, ry, rz), _ = assembly.get_abs_direction("b2@faces@>Y").toTuple()
|
||||||
|
self.assertAlmostEqual(rx, 0)
|
||||||
|
self.assertAlmostEqual(ry, 1)
|
||||||
|
self.assertAlmostEqual(rz, 0)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -30,7 +30,7 @@ class TestJoints(unittest.TestCase):
|
||||||
Tests the arm radius
|
Tests the arm radius
|
||||||
"""
|
"""
|
||||||
j = MJ.ShoulderJoint()
|
j = MJ.ShoulderJoint()
|
||||||
for deflection in [0, 40, 95, 120]:
|
for deflection in [0, 40, j.angle_max_deflection]:
|
||||||
with self.subTest(deflection=deflection):
|
with self.subTest(deflection=deflection):
|
||||||
a = j.assembly(deflection=deflection)
|
a = j.assembly(deflection=deflection)
|
||||||
# Axle
|
# Axle
|
||||||
|
|
31
nhf/utils.py
31
nhf/utils.py
|
@ -1,9 +1,10 @@
|
||||||
"""
|
"""
|
||||||
Utility functions for cadquery objects
|
Utility functions for cadquery objects
|
||||||
"""
|
"""
|
||||||
import math
|
|
||||||
import functools
|
import functools
|
||||||
|
import math
|
||||||
import cadquery as Cq
|
import cadquery as Cq
|
||||||
|
from cadquery.occ_impl.solver import ConstraintSpec
|
||||||
from nhf import Role
|
from nhf import Role
|
||||||
from typing import Union, Tuple, cast
|
from typing import Union, Tuple, cast
|
||||||
from nhf.materials import KEY_ITEM, KEY_MATERIAL
|
from nhf.materials import KEY_ITEM, KEY_MATERIAL
|
||||||
|
@ -198,14 +199,40 @@ Cq.Assembly.markPlane = mark_plane
|
||||||
|
|
||||||
def get_abs_location(self: Cq.Assembly,
|
def get_abs_location(self: Cq.Assembly,
|
||||||
tag: str) -> Cq.Location:
|
tag: str) -> Cq.Location:
|
||||||
|
"""
|
||||||
|
Gets the location of a tag
|
||||||
|
|
||||||
|
BUG: Currently bugged. See `nhf/test.py` for example
|
||||||
|
"""
|
||||||
name, shape = self._query(tag)
|
name, shape = self._query(tag)
|
||||||
loc_self = shape.location()
|
loc_self = Cq.Location(shape.Center())
|
||||||
loc_parent, _ = self._subloc(name)
|
loc_parent, _ = self._subloc(name)
|
||||||
loc = loc_parent * loc_self
|
loc = loc_parent * loc_self
|
||||||
return loc
|
return loc
|
||||||
|
|
||||||
Cq.Assembly.get_abs_location = get_abs_location
|
Cq.Assembly.get_abs_location = get_abs_location
|
||||||
|
|
||||||
|
def get_abs_direction(self: Cq.Assembly,
|
||||||
|
tag: str) -> Cq.Location:
|
||||||
|
"""
|
||||||
|
Gets the location of a tag
|
||||||
|
"""
|
||||||
|
name, shape = self._query(tag)
|
||||||
|
# Must match `cadquery.occ_impl.solver.ConstraintSpec._getAxis`
|
||||||
|
if isinstance(shape, Cq.Face):
|
||||||
|
vec_dir = shape.normalAt()
|
||||||
|
elif isinstance(shape, Cq.Edge) and shape.geomType() != "CIRCLE":
|
||||||
|
vec_dir = shape.tangentAt()
|
||||||
|
elif isinstance(shape, Cq.Edge) and shape.geomType() == "CIRCLE":
|
||||||
|
vec_dir = shape.normal()
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Cannot construct Axis for {shape}")
|
||||||
|
loc_self = Cq.Location(vec_dir)
|
||||||
|
loc_parent, _ = self._subloc(name)
|
||||||
|
loc = loc_parent * loc_self
|
||||||
|
return loc
|
||||||
|
Cq.Assembly.get_abs_direction = get_abs_direction
|
||||||
|
|
||||||
|
|
||||||
# Tallying functions
|
# Tallying functions
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue