101 lines
6.5 KiB
Python
101 lines
6.5 KiB
Python
"""Mechanical data must survive export, transforms, routing and chamfers."""
|
|
from pathlib import Path
|
|
import json,sys,unittest,math
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'easyeda_backend'))
|
|
from design_runtime.mechanical import from_sources,audit,path_points
|
|
from design_runtime.dsn import export,add_mechanical
|
|
from design_runtime.router import parse,seeded_dsn
|
|
from design_runtime.state import DesignError
|
|
|
|
|
|
def record(kind,identity,body):return json.dumps({'type':kind,'id':identity})+'||'+json.dumps(body)+'|\n'
|
|
|
|
|
|
def fixture():
|
|
source=record('PAD','p1',{'num':'1','centerX':0,'centerY':0})+record('POLY','slot',{'layerId':11,'path':[20,20,'L',20,50,50,50,50,20]})
|
|
parts=[{'id':'C','designator':'J1','x':100,'y':200,'rotation':90,'layer':1,'footprint':{'libraryUuid':'lib','uuid':'fp'},'pads':[{'n':'1','net':'GND','x':100,'y':200,'layer':1,'shape':['RECT',10,10,0],'rotation':90}]}]
|
|
return parts,source
|
|
|
|
|
|
class MechanicalTests(unittest.TestCase):
|
|
def test_footprint_cutout_follows_actual_rotation_and_translation(self):
|
|
parts,source=fixture();m=from_sources(parts,'',{'lib:fp':source})
|
|
self.assertEqual(len(m['obstacles']),1);self.assertEqual(m['obstacles'][0]['id'],'Cslot')
|
|
self.assertAlmostEqual(m['obstacles'][0]['points'][0][0],80);self.assertAlmostEqual(m['obstacles'][0]['points'][0][1],220)
|
|
|
|
def test_missing_or_mismatched_source_cannot_erase_cutout(self):
|
|
parts,source=fixture()
|
|
with self.assertRaises(DesignError):from_sources(parts,'',{})
|
|
parts[0]['pads'][0]['x']=101
|
|
with self.assertRaises(DesignError):from_sources(parts,'',{'lib:fp':source})
|
|
|
|
def test_native_dsn_also_retains_nonplated_locating_holes(self):
|
|
parts,source=fixture()
|
|
source+=record('FILL','peg',{'layerId':12,'netName':'','path':[['CIRCLE',80,80,10]]})
|
|
model=from_sources(parts,'',{'lib:fp':source})
|
|
self.assertEqual(len(model['obstacles']),2)
|
|
text,count=add_mechanical('(PCB "b" (resolution mil 1000) (structure (rule (clearance 6))))',model)
|
|
self.assertGreater(count,4)
|
|
self.assertIn('keepout',text)
|
|
hole=model['obstacles'][1]
|
|
self.assertTrue(all(abs(((x-20)**2+(y-280)**2)**.5-10/math.cos(math.pi/32))<1e-6 for x,y in hole['points']))
|
|
|
|
def test_board_outline_spacing_is_in_native_dsn_and_postcheck(self):
|
|
source=record('POLY','outline',{'layerId':11,'width':4,'path':[0,0,'L',400,0,400,400,0,400,0,0]})
|
|
geo=from_sources([],source,{})
|
|
self.assertEqual(len(geo['board_edges']),4)
|
|
text,count=add_mechanical('(PCB "b" (resolution mil 1000) (structure (rule (clearance 6))))',geo)
|
|
self.assertEqual(count,8)
|
|
t={'id':'t','net':'N','layer':1,'width':8,'x1':100,'y1':16,'x2':300,'y2':16}
|
|
self.assertFalse(audit([],[t],[],geo)['passed'])
|
|
self.assertTrue(audit([],[{**t,'y1':20,'y2':20}],[],geo)['passed'])
|
|
|
|
def test_single_layer_plated_drill_is_kept_as_opposite_layer_obstacle(self):
|
|
rules={'config':{'Physics':{'Via Size':{'viaSize':{'form':{'viaOuterdiameterDefault':.6,'viaInnerdiameterDefault':.3}}}},
|
|
'Spacing':{'Safe Spacing':{'copperThickness1oz':{'tables':{'1':{'content':[[.15]*12 for _ in range(12)]}}}}}}}
|
|
pad={'id':'thermal','net':'GND','x':100,'y':100,'rotation':0,'shape':['ELLIPSE',30,30,0],
|
|
'layer':1,'hole':['ROUND',12],'metallization':True}
|
|
text,r=export([pad],20,15,rules)
|
|
self.assertEqual(r['single_layer_drill_obstacles'],2)
|
|
self.assertIn('via_keepout',text)
|
|
self.assertEqual(pad['hole'],['ROUND',12])
|
|
pad['metallization']=False
|
|
with self.assertRaises(DesignError):export([pad],20,15,rules)
|
|
|
|
def test_false_polygon_hole_getter_requires_explicit_zero_source(self):
|
|
from design_runtime.mechanical import canonical_pad_holes
|
|
pads=[{'id':'smd','hole':['ROUND',14]},{'id':'pth','hole':['ROUND',40]}]
|
|
result,changes=canonical_pad_holes(pads,{'source_pad_holes':{'smd':{'holeType':'ROUND','width':0,'height':0},'pth':{'holeType':'ROUND','width':40,'height':40}}})
|
|
self.assertIsNone(result[0]['hole']);self.assertEqual(result[1]['hole'],['ROUND',40]);self.assertEqual(len(changes),1)
|
|
self.assertEqual(pads[0]['hole'],['ROUND',14])
|
|
with self.assertRaises(DesignError):canonical_pad_holes([{'id':'pth','hole':None}],{'source_pad_holes':{'pth':{'width':40,'height':40}}})
|
|
|
|
def test_unsupported_curves_fail_explicitly(self):
|
|
with self.assertRaises(DesignError):path_points([0,0,'ARC',90,10,10])
|
|
|
|
def test_copper_cannot_cross_cavity_or_its_clearance(self):
|
|
parts,source=fixture();m=from_sources(parts,'',{'lib:fp':source})
|
|
bad={'id':'track','net':'GND','layer':1,'width':4,'x1':40,'y1':235,'x2':100,'y2':235}
|
|
self.assertTrue(audit([], [bad],[],m)['routing_errors'])
|
|
good={**bad,'y1':280,'y2':280};self.assertTrue(audit([],[good],[],m)['passed'])
|
|
via={'id':'via','net':'GND','x':65,'y':235,'diameter':12}
|
|
self.assertTrue(audit([],[],[via],m)['routing_errors'])
|
|
|
|
def test_intrinsic_pad_clearance_is_not_relaxed(self):
|
|
parts,source=fixture();m=from_sources(parts,'',{'lib:fp':source});parts[0]['pads'][0].update(x=85,y=235)
|
|
result=audit(parts,[],[],m);self.assertFalse(result['passed']);self.assertEqual(len(result['pad_conflicts']),1)
|
|
|
|
def test_via_only_region_does_not_block_wires(self):
|
|
geo={'obstacles':[{'id':'via-only','kind':'keepout','layers':[1,2],'wire':False,'via':True,'points':[(0,0),(30,0),(30,30),(0,30)]}]}
|
|
track={'id':'track','net':'GND','layer':1,'width':4,'x1':0,'y1':15,'x2':30,'y2':15}
|
|
self.assertTrue(audit([],[track],[],geo)['passed'])
|
|
self.assertFalse(audit([],[],[{'id':'via','net':'GND','x':15,'y':15,'diameter':10}],geo)['passed'])
|
|
|
|
def test_continuation_preserves_nets_and_uses_movable_wiring(self):
|
|
dsn='(PCB "p" (library (padstack via0 (shape (circle TopLayer 24)) (shape (circle BottomLayer 24)))) (wiring))'
|
|
ses='(session "p" (routes (resolution mil 1000) (library_out (padstack via0 (shape (circle TopLayer 24000)) (shape (circle BottomLayer 24000)))) (network_out (net "GND" (wire (path TopLayer 8000 1000 2000 3000 2000)) (via via0 3000 2000)))))'
|
|
tree=parse(seeded_dsn(dsn,ses));wiring=next(n for n in tree if isinstance(n,list) and n[0]=='wiring')
|
|
self.assertEqual(len(wiring),3);self.assertIn(['net','"GND"'],wiring[1]);self.assertNotIn('protect',str(wiring));self.assertNotIn('fix',str(wiring))
|
|
|
|
if __name__=='__main__':unittest.main()
|