Files
vibemake-pcb-studio/backend/tests/test_pcb_layout_rules.py
T
2026-11-10 19:04:27 +08:00

104 lines
6.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Mechanical constraints survive rotation search and provisional size recovery."""
from copy import deepcopy
from pathlib import Path
import sys,unittest
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'easyeda_backend'))
from design_runtime.layout import place,variants,placement_moves,audit_placement
from design_runtime.board_policy import bind_size_policy,can_grow,intake_schema
from design_runtime.connector_geometry import constraints
def part(name,w,h,**extra):
return {'id':name,'designator':name,'w':w,'h':h,'rotation':0,'nets':['GND'],'pins':2,'dx':0,'dy':0,**extra}
class LayoutRules(unittest.TestCase):
def test_long_headers_rotate_to_long_board_edges(self):
parts=[part('H1',51.054,2.754,pins=20),part('H2',51.054,2.754,pins=20),part('U1',18,25,pins=40)]
result=place(parts,{'width_mm':30,'height_mm':70,'size_origin':'user_requirement'})
self.assertTrue(result['passed'],result);self.assertFalse(result['provisional_resized'])
headers=[p for p in result['placements'] if p['id'].startswith('H')]
self.assertTrue(all(p['rotation'] in (90,270) for p in headers))
self.assertEqual({p['edge'] for p in headers},{'left','right'})
def test_transformed_origin_and_terminal_offsets_follow_rotation(self):
p=part('H1',51,3,dx=4,dy=2,pad_offsets=[{'net':'GND','x':7,'y':3}])
v=list(variants(p))[1];self.assertAlmostEqual(v['pad_offsets'][0]['x'],-3);self.assertAlmostEqual(v['pad_offsets'][0]['y'],7)
moves=placement_moves([p],{'placements':[{'id':'H1','cx':10,'cy':30,'rotation':90}]})
self.assertAlmostEqual(moves[0]['x']*.0254,12);self.assertAlmostEqual(moves[0]['y']*.0254,26)
self.assertEqual(moves[0]['rotation'],90)
def test_provisional_board_grows_without_mutating_contract_or_parts(self):
parts=[part('U1',16,16)];board={'width_mm':10,'height_mm':10,'size_origin':'ai_provisional'};before=deepcopy((parts,board))
result=place(parts,board);self.assertTrue(result['passed']);self.assertTrue(result['provisional_resized']);self.assertGreater(len(result['size_attempts']),1)
self.assertEqual((parts,board),before)
def test_explicit_or_unknown_maximum_never_grows(self):
for source in ('user_requirement','unknown',None):
with self.subTest(source=source):
result=place([part('H1',51,3)],{'width_mm':20,'height_mm':20,'size_mode':'maximum','size_origin':source})
self.assertFalse(result['passed']);self.assertEqual(len(result['size_attempts']),1)
def test_no_growth_for_mechanical_outline_or_mounting(self):
board={'size_origin':'ai_provisional','shape':'rectangle'}
self.assertFalse(can_grow({**board,'outline_asset_id':'enclosure'}))
self.assertFalse(can_grow({**board,'shape':'custom'}))
self.assertFalse(can_grow(board,[{'id':'mount'}]))
def test_rotation_lock_and_opening_vector_are_respected(self):
p=part('J1',15,2,allowed_rotations=[0],mating_vector=[0,-1])
result=place([p],{'width_mm':20,'height_mm':30})
self.assertTrue(result['passed']);self.assertEqual(result['placements'][0]['rotation'],0);self.assertEqual(result['placements'][0]['edge'],'bottom')
result=place([part('H1',51,3,allowed_rotations=[0])],{'width_mm':30,'height_mm':70})
self.assertFalse(result['passed'])
def test_alternate_usb_opening_uses_actual_drawing_and_pad_signature(self):
pins=('B1A12','B4A9','B5','A8','B6','A7','A6','B7','A5','B8','A4B9','A1B12','1','2','3','4')
actual={'rotation':0,'layer':1,'footprint':{'name':'USB-C_SMD-TYPE-C-31-M-12_1'},'pads':[{'n':n} for n in pins]}
c=constraints({'Supplier Part':'C165948','Manufacturer Part':'TYPE-C-31-M-12'},actual)
self.assertEqual(c['mating_vector'],(0,-1))
result=place([part('J1',9,8,**c)],{'width_mm':30,'height_mm':20})
pos=result['placements'][0]
from design_runtime.layout import rotate
normal={'left':(-1,0),'right':(1,0),'bottom':(0,-1),'top':(0,1)}[pos['edge']]
vector=rotate(*c['mating_vector'],pos['rotation'])
self.assertAlmostEqual(sum(a*b for a,b in zip(vector,normal)),1)
actual['pads'].pop()
self.assertNotIn('mating_vector',constraints({'DeviceName':'USB-C','Supplier Part':'C165948','Manufacturer Part':'TYPE-C-31-M-12'},actual))
def test_unknown_side_entry_port_does_not_gain_unverified_rotations(self):
constraint=constraints({'DeviceName':'new USB-C port'},{'rotation':180,'pads':[]})
self.assertEqual(constraint['allowed_rotations'],[180])
# This is optional geometry evidence, never a part registration gate.
self.assertNotIn('blocked',constraint)
def test_readback_detects_native_rotation_not_applied(self):
layout={'width_mm':30,'height_mm':70,'placements':[{'id':'H1','cx':2.1,'cy':35,'w':3,'h':51,'rotation':90}]}
actual=[{'id':'H1','designator':'H1','rotation':0,'bbox':{'minX':.6/.0254,'minY':9.5/.0254,'maxX':3.6/.0254,'maxY':60.5/.0254}}]
self.assertFalse(audit_placement(actual,layout)['passed']);actual[0]['rotation']=90;self.assertTrue(audit_placement(actual,layout)['passed'])
def test_new_intake_requires_provenance_without_breaking_legacy_schema(self):
schema={'properties':{'board':{'required':['width_mm','height_mm']}}};original=deepcopy(schema)
self.assertEqual(intake_schema(schema)['properties']['board']['required'],['width_mm','height_mm','size_origin','size_evidence'])
self.assertEqual(schema,original)
def test_user_dimensions_override_incorrect_model_provenance(self):
for text in ('做一块30×70mm的开发板','板宽30mm,高度70mm','30毫米宽的板子'):
r={'board':{'size_origin':'ai_provisional'}}
bind_size_policy(r,[{'role':'user','text':text}]);self.assertEqual(r['board']['size_origin'],'user_requirement')
r={'board':{'size_origin':'ai_provisional'}};bind_size_policy(r,[{'role':'user','text':'板子大小你自己决定'}]);self.assertTrue(can_grow(r['board']))
def test_reference_photo_does_not_freeze_model_default_dimensions(self):
r={'board':{'size_origin':'ai_provisional'}}
bind_size_policy(r,[{'role':'user','text':'参考这个图片做开发板'}],[{'kind':'mechanical','description':'我想抄的板子.png','path':'inputs/mechanical/photo.png'}])
self.assertTrue(can_grow(r['board']))
bind_size_policy(r,[],[{'kind':'mechanical','path':'inputs/mechanical/enclosure.step'}])
self.assertFalse(can_grow(r['board']))
def test_structured_user_board_and_mechanical_attachment_win(self):
r={'board':{'size_origin':'ai_provisional'}}
bind_size_policy(r,[{'role':'user','request':{'board':{'width_mm':30}}}]);self.assertFalse(can_grow(r['board']))
r={'board':{'size_origin':'ai_provisional'}};bind_size_policy(r,[],[{'kind':'mechanical','description':'mounting drawing'}]);self.assertFalse(can_grow(r['board']))
if __name__=='__main__':unittest.main()