2023-05-06 00:13:08 +00:00
|
|
|
import pytest
|
2014-02-08 23:21:47 +00:00
|
|
|
import os
|
2014-02-04 21:16:32 +00:00
|
|
|
import pcbnew
|
2014-02-08 23:21:47 +00:00
|
|
|
import tempfile
|
|
|
|
|
2014-02-04 21:16:32 +00:00
|
|
|
|
2014-02-08 00:02:29 +00:00
|
|
|
from pcbnew import *
|
2014-02-04 21:16:32 +00:00
|
|
|
|
2015-02-15 13:31:47 +00:00
|
|
|
|
|
|
|
BACK_COPPER = 'Back_Copper'
|
|
|
|
B_CU = 'B.Cu'
|
|
|
|
NEW_NAME = 'My_Fancy_Layer_Name'
|
|
|
|
|
|
|
|
|
2023-05-06 00:13:08 +00:00
|
|
|
class TestBoardClass:
|
|
|
|
pcb : pcbnew.BOARD = None
|
2014-02-04 21:16:32 +00:00
|
|
|
|
2023-05-06 00:13:08 +00:00
|
|
|
def setup_method(self):
|
2023-04-25 12:29:47 +00:00
|
|
|
self.pcb = LoadBoard("../data/pcbnew/complex_hierarchy.kicad_pcb")
|
2014-02-08 23:21:47 +00:00
|
|
|
self.TITLE="Test Board"
|
|
|
|
self.COMMENT1="For load/save test"
|
|
|
|
self.FILENAME=tempfile.mktemp()+".kicad_pcb"
|
2014-02-08 00:02:29 +00:00
|
|
|
|
2014-02-04 21:16:32 +00:00
|
|
|
def test_pcb_find_module(self):
|
2020-11-13 23:06:31 +00:00
|
|
|
module = self.pcb.FindFootprintByReference('P1')
|
2023-05-06 00:13:08 +00:00
|
|
|
assert module.GetReference() =='P1'
|
2014-02-04 21:16:32 +00:00
|
|
|
|
2014-02-08 00:02:29 +00:00
|
|
|
def test_pcb_get_track_count(self):
|
|
|
|
pcb = BOARD()
|
|
|
|
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb.Tracks().size() == 0
|
2014-02-08 00:02:29 +00:00
|
|
|
|
2021-06-13 15:19:19 +00:00
|
|
|
track0 = PCB_TRACK(pcb)
|
2014-02-08 00:02:29 +00:00
|
|
|
pcb.Add(track0)
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb.Tracks().size() == 1
|
2014-02-08 00:02:29 +00:00
|
|
|
|
2021-06-13 15:19:19 +00:00
|
|
|
track1 = PCB_TRACK(pcb)
|
2014-02-08 00:02:29 +00:00
|
|
|
pcb.Add(track1)
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb.Tracks().size() == 2
|
2014-02-08 00:02:29 +00:00
|
|
|
|
2014-02-04 21:16:32 +00:00
|
|
|
def test_pcb_bounding_box(self):
|
2014-02-08 00:02:29 +00:00
|
|
|
pcb = BOARD()
|
2021-06-13 15:19:19 +00:00
|
|
|
track = PCB_TRACK(pcb)
|
2014-02-08 00:02:29 +00:00
|
|
|
pcb.Add(track)
|
|
|
|
|
2022-01-02 20:23:17 +00:00
|
|
|
track.SetStart(VECTOR2I_MM(10.0, 10.0))
|
|
|
|
track.SetEnd(VECTOR2I_MM(20.0, 30.0))
|
2014-02-08 00:02:29 +00:00
|
|
|
|
|
|
|
track.SetWidth(FromMM(0.5))
|
|
|
|
|
|
|
|
#!!! THIS FAILS? == 0.0 x 0.0 ??
|
|
|
|
#height, width = ToMM(pcb.ComputeBoundingBox().GetSize())
|
|
|
|
bounding_box = pcb.ComputeBoundingBox()
|
|
|
|
height, width = ToMM(bounding_box.GetSize())
|
|
|
|
|
2018-06-12 15:21:03 +00:00
|
|
|
margin = 0 # margin around bounding boxes (currently 0)
|
2023-05-06 00:13:08 +00:00
|
|
|
assert width == pytest.approx((30-10) + 0.5 + margin, 2)
|
|
|
|
assert height == pytest.approx((20-10) + 0.5 + margin, 2)
|
2014-02-08 00:02:29 +00:00
|
|
|
|
|
|
|
def test_pcb_get_pad(self):
|
|
|
|
pcb = BOARD()
|
2020-11-13 23:10:45 +00:00
|
|
|
module = FOOTPRINT(pcb)
|
2014-02-08 00:02:29 +00:00
|
|
|
pcb.Add(module)
|
2020-11-14 01:06:05 +00:00
|
|
|
pad = PAD(module)
|
2014-02-08 00:02:29 +00:00
|
|
|
module.Add(pad)
|
|
|
|
|
2016-11-25 13:52:34 +00:00
|
|
|
pad.SetShape(PAD_SHAPE_OVAL)
|
2022-01-05 04:23:23 +00:00
|
|
|
pad.SetSize(VECTOR2I_MM(2.0, 3.0))
|
2022-01-02 20:23:17 +00:00
|
|
|
pad.SetPosition(VECTOR2I_MM(0,0))
|
2016-11-25 13:52:34 +00:00
|
|
|
|
2014-02-08 00:02:29 +00:00
|
|
|
# easy case
|
2022-01-02 20:23:17 +00:00
|
|
|
p1 = pcb.GetPad(VECTOR2I_MM(0,0))
|
2014-02-08 00:02:29 +00:00
|
|
|
|
|
|
|
# top side
|
2022-01-02 20:23:17 +00:00
|
|
|
p2 = pcb.GetPad(VECTOR2I_MM(0.9,0.0))
|
2014-02-04 21:16:32 +00:00
|
|
|
|
2014-02-08 00:02:29 +00:00
|
|
|
# bottom side
|
2022-01-02 20:23:17 +00:00
|
|
|
p3 = pcb.GetPad(VECTOR2I_MM(0,1.4))
|
2014-02-04 21:16:32 +00:00
|
|
|
|
2014-02-08 00:02:29 +00:00
|
|
|
# TODO: get pad == p1 evaluated as true instead
|
|
|
|
# of relying in the internal C++ object pointer
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pad.this == p1.this
|
|
|
|
assert pad.this == p2.this
|
|
|
|
assert pad.this == p3.this
|
2014-02-04 21:16:32 +00:00
|
|
|
|
2014-02-08 23:21:47 +00:00
|
|
|
def test_pcb_save_and_load(self):
|
|
|
|
pcb = BOARD()
|
|
|
|
pcb.GetTitleBlock().SetTitle(self.TITLE)
|
2019-08-22 23:55:39 +00:00
|
|
|
pcb.GetTitleBlock().SetComment(0,self.COMMENT1)
|
2014-02-08 23:21:47 +00:00
|
|
|
result = SaveBoard(self.FILENAME,pcb)
|
2023-05-06 00:13:08 +00:00
|
|
|
assert result
|
2016-11-25 13:52:34 +00:00
|
|
|
|
2014-02-08 23:21:47 +00:00
|
|
|
pcb2 = LoadBoard(self.FILENAME)
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb2 is not None
|
2014-02-08 23:21:47 +00:00
|
|
|
|
|
|
|
tb = pcb2.GetTitleBlock()
|
2023-05-06 00:13:08 +00:00
|
|
|
assert tb.GetTitle() == self.TITLE
|
|
|
|
assert tb.GetComment(0) == self.COMMENT1
|
2014-02-08 23:21:47 +00:00
|
|
|
|
|
|
|
os.remove(self.FILENAME)
|
|
|
|
|
2015-02-15 13:31:47 +00:00
|
|
|
def test_pcb_layer_name_set_get(self):
|
|
|
|
pcb = BOARD()
|
|
|
|
pcb.SetLayerName(31, BACK_COPPER)
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb.GetLayerName(31) == BACK_COPPER
|
2015-02-15 13:31:47 +00:00
|
|
|
|
|
|
|
def test_pcb_layer_name_set_get(self):
|
|
|
|
pcb = BOARD()
|
|
|
|
pcb.SetLayerName(31, BACK_COPPER)
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb.GetLayerName(31) == BACK_COPPER
|
2015-02-15 13:31:47 +00:00
|
|
|
|
|
|
|
def test_pcb_layer_id_get(self):
|
|
|
|
pcb = BOARD()
|
|
|
|
b_cu_id = pcb.GetLayerID(B_CU)
|
|
|
|
pcb.SetLayerName(b_cu_id, NEW_NAME)
|
|
|
|
|
|
|
|
# ensure we can get the ID for the new name
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb.GetLayerID(NEW_NAME) == b_cu_id
|
2015-02-15 13:31:47 +00:00
|
|
|
|
|
|
|
# ensure we can get to the ID via the STD name too
|
2023-05-06 00:13:08 +00:00
|
|
|
assert pcb.GetLayerID(B_CU) == b_cu_id
|
2015-02-15 13:31:47 +00:00
|
|
|
|
2021-01-25 05:51:44 +00:00
|
|
|
def test_footprint_properties(self):
|
2023-04-25 12:29:47 +00:00
|
|
|
pcb = LoadBoard("../data/pcbnew/custom_fields.kicad_pcb")
|
2021-01-25 05:51:44 +00:00
|
|
|
footprint = pcb.FindFootprintByReference('J1')
|
2023-06-19 18:40:54 +00:00
|
|
|
expected_fields = {
|
2021-01-25 05:51:44 +00:00
|
|
|
'myfield': 'myvalue'
|
|
|
|
}
|
2023-06-20 14:31:13 +00:00
|
|
|
assert footprint.GetSheetfile() == 'custom_fields.kicad_sch'
|
|
|
|
assert footprint.GetSheetname() == ''
|
2023-06-23 17:44:26 +00:00
|
|
|
assert footprint.GetFieldText('myfield') == 'myvalue'
|
|
|
|
assert footprint.HasField('myfield')
|
|
|
|
assert not footprint.HasField('abcd')
|
2023-06-19 18:40:54 +00:00
|
|
|
footprint.SetField('abcd', 'efgh')
|
2023-06-23 17:44:26 +00:00
|
|
|
assert footprint.HasField('abcd')
|
|
|
|
assert footprint.GetFieldText('abcd') == 'efgh'
|