test_002_board_class.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import code
  2. import unittest
  3. import os
  4. import pcbnew
  5. import pdb
  6. import tempfile
  7. from kicommand import run as kc
  8. from pcbnew import *
  9. BACK_COPPER = 'Back_Copper'
  10. B_CU = 'B.Cu'
  11. NEW_NAME = 'My_Fancy_Layer_Name'
  12. KICAD_INSTALL = r'C:\Program Files\KiCad'
  13. class TestBoardClass(unittest.TestCase):
  14. def setUp(self):
  15. file = os.path.join(KICAD_INSTALL,r'share\kicad\demos\complex_hierarchy\complex_hierarchy.kicad_pcb')
  16. self.pcb = kc(
  17. 'clear pcbnew list "%s" list list LoadBoard callargs '
  18. 'delist Board spush board'%file)
  19. #self.pcb = LoadBoard("data/complex_hierarchy.kicad_pcb")
  20. self.TITLE="Test Board"
  21. self.COMMENT1="For load/save test"
  22. self.FILENAME=tempfile.mktemp()+".kicad_pcb"
  23. def test_pcb_find_module(self):
  24. reference = kc('board list P1 list list FindModule callargs GetReference call delist')
  25. self.assertEqual(reference,'P1')
  26. # module = self.pcb.FindModule('P1')
  27. # self.assertEqual(module.GetReference(),'P1')
  28. def test_pcb_get_track_count(self):
  29. # :persist newboard "Elements Create a new empty board and make it the current board." pcbnew list BOARD call delist Board spush ;
  30. # : newtrack "Elements Create bare, undefined track on current board" pcbnew list board list list TRACK callargs board list swap list Add callargs ;
  31. # new board
  32. pcb = kc('clear pcbnew list BOARD call delist Board spush board')
  33. num = kc('list GetNumSegmTrack call delist')
  34. self.assertEqual(num,0)
  35. # Creates a new track
  36. kc('clear pcbnew list board list list TRACK callargs')
  37. # Adds track to Board
  38. kc('Board scopy list swap list Add callargs')
  39. # Obtain the number of tracks on this board
  40. num = kc('board list GetNumSegmTrack call delist')
  41. self.assertEqual(num,1)
  42. #kc(':persist newadd "Elements [TYPE PARENT] Add create a element of TYPE (TRACK, PAD) and add it to the PARENT." pcbnew list swap board list list swap stack')
  43. # Creates a new track
  44. # Adds track to Board
  45. kc('clear pcbnew list board list list TRACK callargs')
  46. kc('board list swap list Add callargs')
  47. num = kc('board list GetNumSegmTrack call delist')
  48. self.assertEqual(num,2)
  49. # pcb = BOARD()
  50. # self.assertEqual(pcb.GetNumSegmTrack(),0)
  51. # track0 = TRACK(pcb)
  52. # pcb.Add(track0)
  53. # self.assertEqual(pcb.GetNumSegmTrack(),1)
  54. # track1 = TRACK(pcb)
  55. # pcb.Add(track1)
  56. # self.assertEqual(pcb.GetNumSegmTrack(),2)
  57. def test_pcb_bounding_box(self):
  58. kc(':persist newboard "Elements Create a new empty board and make it the current board." pcbnew list BOARD call delist Board spush ;')
  59. pcb = kc('clear newboard board')
  60. track = kc('clear 0.5 mm t param F.Cu l param 10,10,20,30 mm '
  61. 'drawpoly delist delist')
  62. wval, hval = kc('list GetClearance call '
  63. 'copy append copy 30,-10,0.5 mm append sum swap '
  64. '20,-10,0.5 mm append sum',returnval=-1)
  65. # float is because integer doesn't compare to float in assertAlmostEqual----+
  66. # ilist is to create a list from the wxRect---------------------------+ |
  67. # copy is to keep bb reference in memory----------v v v
  68. height, width = kc('board list ComputeBoundingBox call GetSize call delist ilist float')
  69. #height, width = bounding_box.GetSize()
  70. self.assertAlmostEqual(ToMM(height), ToMM(hval), 2)
  71. self.assertAlmostEqual(ToMM(width ), ToMM(wval), 2)
  72. # pcb = BOARD()
  73. # track = TRACK(pcb)
  74. # pcb.Add(track)
  75. # track.SetStartEnd(wxPointMM(10.0, 10.0),
  76. # wxPointMM(20.0, 30.0))
  77. # track.SetStart(wxPointMM(10.0, 10.0))
  78. # track.SetEnd (wxPointMM(20.0, 30.0))
  79. # track.SetWidth(FromMM(0.5))
  80. #!!! THIS FAILS? == 0.0 x 0.0 ??
  81. height, width = ToMM(pcb.ComputeBoundingBox().GetSize())
  82. # bounding_box = pcb.ComputeBoundingBox()
  83. # height, width = ToMM(bounding_box.GetSize())
  84. clearance = ToMM(track.GetClearance()*2)
  85. self.assertAlmostEqual(width, (30-10) + 0.5 + clearance, 2)
  86. self.assertAlmostEqual(height, (20-10) + 0.5 + clearance, 2)
  87. def test_pcb_get_pad(self):
  88. pcb = kc('newboard board')
  89. # pcb = BOARD()
  90. module = kc('MODULE swap newadd')
  91. #MODULE board list copy pcbnew list swap list 3 pick callargs 1 pick 1 pick list Add callargs pop swap pop swap pop delist
  92. # module = MODULE(pcb)
  93. # pcb.Add(module)
  94. pad = kc('D_PAD swap newadd')
  95. #pad = D_PAD(module)
  96. #kc('list list Add callargs pop pop')
  97. #module.Add(pad)
  98. # newboard board MODULE swap newadd delist D_PAD swap newadd delist
  99. try:
  100. pad.SetShape(PAD_SHAPE_OVAL) # 5.x nightly
  101. except:
  102. pad.SetShape(PAD_OVAL) # 4.0.7
  103. pad.SetSize(wxSizeMM(2.0, 3.0))
  104. pad.SetPosition(wxPointMM(0,0))
  105. # easy case
  106. p1 = pcb.GetPad(wxPointMM(0,0))
  107. # top side
  108. p2 = pcb.GetPad(wxPointMM(0.9,0.0))
  109. # bottom side
  110. p3 = pcb.GetPad(wxPointMM(0,1.4))
  111. # TODO: get pad == p1 evaluated as true instead
  112. # of relying in the internal C++ object pointer
  113. self.assertEqual(pad.this, p1.this)
  114. self.assertEqual(pad.this, p2.this)
  115. self.assertEqual(pad.this, p3.this)
  116. def test_pcb_save_and_load(self):
  117. pcb = BOARD()
  118. pcb.GetTitleBlock().SetTitle(self.TITLE)
  119. pcb.GetTitleBlock().SetComment1(self.COMMENT1)
  120. result = SaveBoard(self.FILENAME,pcb)
  121. self.assertTrue(result)
  122. pcb2 = LoadBoard(self.FILENAME)
  123. self.assertNotEqual(pcb2,None)
  124. tb = pcb2.GetTitleBlock()
  125. self.assertEqual(tb.GetTitle(),self.TITLE)
  126. self.assertEqual(tb.GetComment1(),self.COMMENT1)
  127. os.remove(self.FILENAME)
  128. def test_pcb_layer_name_set_get(self):
  129. pcb = kc('newboard board list 31 int list %s list append list SetLayerName callargs board'%BACK_COPPER)
  130. # pcb = BOARD()
  131. # pcb.SetLayerName(31, BACK_COPPER)
  132. self.assertEqual(pcb.GetLayerName(31), BACK_COPPER)
  133. def test_pcb_layer_name_set_get2(self):
  134. pcb = BOARD()
  135. pcb.SetLayerName(31, BACK_COPPER)
  136. self.assertEqual(pcb.GetLayerName(31), BACK_COPPER)
  137. def test_pcb_layer_id_get(self):
  138. # pcb = BOARD()
  139. pcb = kc('newboard board')
  140. b_cu_id = kc('%s layernums delist'%B_CU)
  141. #b_cu_id = pcb.GetLayerID(B_CU)
  142. #pcb.SetLayerName(b_cu_id, NEW_NAME)
  143. kc('%d,%s'%(b_cu_id,NEW_NAME))
  144. kc('board list swap int list SetLayerName callargs pop')
  145. # ensure we can get the ID for the new name
  146. self.assertEqual(pcb.GetLayerID(NEW_NAME), b_cu_id)
  147. # ensure we can get to the ID via the STD name too
  148. self.assertEqual(pcb.GetLayerID(B_CU), b_cu_id)
  149. #def test_interactive(self):
  150. # code.interact(local=locals())
  151. if __name__ == '__main__':
  152. unittest.main()