51 lines
3.6 KiB
Python
51 lines
3.6 KiB
Python
import unittest,math,copy,tempfile,importlib.util
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
from build123d import Plane,Pos,Rot,Cylinder,Box
|
|
from capabilities.impl import bevel_gear as bevel
|
|
from engine.radial_sections import measure_radial_sections
|
|
from capabilities.impl import sketch_segment_profile as profile
|
|
from engine.model_revision import patched_operations
|
|
|
|
class Prototypes(unittest.TestCase):
|
|
def test_conical_sections_after_transform(self):
|
|
for large,small,height,teeth,twist in [(22,15,7,18,0),(18,12,5,16,6),(24,17,8,22,-8)]:
|
|
p=dict(large_tip_diameter=large,small_tip_diameter=small,height=height,tooth_count=teeth,twist_angle=twist)
|
|
s=bevel.run({},None,p)
|
|
self.assertTrue(s.is_valid);self.assertEqual(len(s.solids()),1)
|
|
tr=Pos(3,-7,4)*Rot(23,17,41);plane=Plane.XY.moved(tr)
|
|
offsets=[height*.05,height*.5,height*.95]
|
|
rows=measure_radial_sections(tr*s,plane.origin,plane.z_dir,offsets)['sections']
|
|
for row,z in zip(rows,offsets):
|
|
self.assertEqual(row['outer_lobe_count'],teeth);self.assertTrue(row['equally_spaced'])
|
|
self.assertAlmostEqual(row['outer_radius_range_mm'][1],(large-(large-small)*z/height)/2,delta=.003)
|
|
phase=rows[-1]['phase_modulo_pitch_deg']-rows[0]['phase_modulo_pitch_deg']
|
|
phase=(phase+180/teeth)%(360/teeth)-180/teeth
|
|
self.assertAlmostEqual(phase,.9*twist,delta=.06)
|
|
def test_circle_is_not_teeth_and_empty_section_unavailable(self):
|
|
rows=measure_radial_sections(Cylinder(6,4),[0,0,0],[0,0,1],[0,10])['sections']
|
|
self.assertEqual(rows[0]['outer_lobe_count'],0);self.assertFalse(rows[0]['equally_spaced'])
|
|
self.assertIn('unavailable',rows[1])
|
|
def test_bad_bevel_parameters_rejected(self):
|
|
good=dict(large_tip_diameter=22,small_tip_diameter=15,height=7,tooth_count=18)
|
|
for patch in ({'height':0},{'small_tip_diameter':23},{'tooth_count':True},{'pressure_angle':90},{'twist_angle':float('nan')}):
|
|
self.assertTrue(bevel.validate({},None,{**good,**patch}))
|
|
def test_tangent_controls_and_failure_before_solid_creation(self):
|
|
segments=[dict(kind='line',points=[[-10,2],[0,2]],smooth_to_next=True),dict(kind='bezier',points=[[0,2],[2,2],[3,5],[5,5]],smooth_to_next=True),dict(kind='line',points=[[5,5],[10,5]]),dict(kind='line',points=[[10,5],[10,-5]]),dict(kind='line',points=[[10,-5],[-10,-5]]),dict(kind='line',points=[[-10,-5],[-10,2]])]
|
|
self.assertTrue(profile.run({},None,{'segments':segments}).is_valid)
|
|
spline=copy.deepcopy(segments);spline[1].update(kind='spline',points=[[0,2],[2.5,3.5],[5,5]],tangents=[[1,0],[1,0]])
|
|
self.assertTrue(profile.run({},None,{'segments':spline}).is_valid)
|
|
segments[1]['points'][1]=[2,3]
|
|
with self.assertRaisesRegex(ValueError,'PROFILE_TANGENCY'):profile.run({},None,{'segments':segments})
|
|
spline[1]['tangents'][0]=[0,0]
|
|
self.assertTrue(profile.validate({},None,{'segments':spline}))
|
|
def test_parameter_patch_is_nondestructive(self):
|
|
ops=[{'id':'a','op':'extrude','params':{'amount':3,'direction':[0,0,1]},'target':'s'}]
|
|
result=patched_operations(ops,[{'id':'a','params':{'amount':5}}])
|
|
self.assertEqual(ops[0]['params']['amount'],3);self.assertEqual(result[0]['params']['amount'],5)
|
|
self.assertEqual(result[0]['params']['direction'],[0,0,1])
|
|
for update in ([],[{'id':'unknown','params':{'amount':3}}],[{'id':'a','params':{} }]):
|
|
with self.assertRaises(ValueError):patched_operations(ops,update)
|
|
|
|
if __name__=='__main__':unittest.main()
|