Skip to content

Commit d75e74f

Browse files
committed
Expand text vars in title block
+ some formatting changes and lint fixes
1 parent d427b1a commit d75e74f

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

InteractiveHtmlBom/ecad/kicad.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def normalize(point):
4444
return [point[0] * 1e-6, point[1] * 1e-6]
4545

4646
def parse_shape(self, d):
47-
# type: (pcbnew.DRAWSEGMENT) -> dict | None
47+
# type: (pcbnew.PCB_SHAPE) -> dict or None
4848
shape = {
4949
pcbnew.S_SEGMENT: "segment",
5050
pcbnew.S_CIRCLE: "circle",
@@ -249,7 +249,7 @@ def get_all_drawings(self):
249249
return drawings
250250

251251
def parse_pad(self, pad):
252-
# type: (pcbnew.D_PAD) -> dict or None
252+
# type: (pcbnew.PAD) -> dict or None
253253
layers_set = list(pad.GetLayerSet().Seq())
254254
layers = []
255255
if pcbnew.F_Cu in layers_set:
@@ -318,7 +318,7 @@ def parse_pad(self, pad):
318318
return pad_dict
319319

320320
def parse_footprints(self):
321-
# type: (list) -> list
321+
# type: () -> list
322322
footprints = []
323323
for f in self.footprints:
324324
ref = f.GetReference()
@@ -362,11 +362,13 @@ def parse_footprints(self):
362362
if pads:
363363
# Try to guess first pin name.
364364
pads = sorted(pads, key=lambda el: el[0])
365-
pin1_pads = [p for p in pads if p[0] in ['1', 'A', 'A1', 'P1', 'PAD1']]
365+
pin1_pads = [p for p in pads if p[0] in
366+
['1', 'A', 'A1', 'P1', 'PAD1']]
366367
if pin1_pads:
367368
pin1_pad_name = pin1_pads[0][0]
368369
else:
369-
# No pads have common first pin name, pick lexicographically smallest.
370+
# No pads have common first pin name,
371+
# pick lexicographically smallest.
370372
pin1_pad_name = pads[0][0]
371373
for pad_name, pad_dict in pads:
372374
if pad_name == pin1_pad_name:
@@ -398,9 +400,9 @@ def parse_tracks(self, tracks):
398400
"width": track.GetWidth() * 1e-6,
399401
"net": track.GetNetname(),
400402
}
401-
for l in [pcbnew.F_Cu, pcbnew.B_Cu]:
402-
if track.IsOnLayer(l):
403-
result[l].append(track_dict)
403+
for layer in [pcbnew.F_Cu, pcbnew.B_Cu]:
404+
if track.IsOnLayer(layer):
405+
result[layer].append(track_dict)
404406
else:
405407
if track.GetLayer() in [pcbnew.F_Cu, pcbnew.B_Cu]:
406408
track_dict = {
@@ -419,15 +421,16 @@ def parse_tracks(self, tracks):
419421

420422
def parse_zones(self, zones):
421423
result = {pcbnew.F_Cu: [], pcbnew.B_Cu: []}
422-
for zone in zones: # type: pcbnew.ZONE_CONTAINER
424+
for zone in zones: # type: pcbnew.ZONE
423425
if (not zone.IsFilled() or
424426
hasattr(zone, 'GetIsKeepout') and zone.GetIsKeepout() or
425427
hasattr(zone, 'GetIsRuleArea') and zone.GetIsRuleArea()):
426428
continue
427-
layers = [l for l in list(zone.GetLayerSet().Seq())
428-
if l in [pcbnew.F_Cu, pcbnew.B_Cu]]
429+
layers = [layer for layer in list(zone.GetLayerSet().Seq())
430+
if layer in [pcbnew.F_Cu, pcbnew.B_Cu]]
429431
for layer in layers:
430432
try:
433+
# kicad 5.1 and earlier
431434
poly_set = zone.GetFilledPolysList()
432435
except TypeError:
433436
poly_set = zone.GetFilledPolysList(layer)
@@ -483,12 +486,21 @@ def footprint_to_component(footprint):
483486

484487
def parse(self):
485488
title_block = self.board.GetTitleBlock()
489+
title = title_block.GetTitle()
490+
revision = title_block.GetRevision()
491+
company = title_block.GetCompany()
492+
if (hasattr(self.board, "GetProject") and
493+
hasattr(pcbnew, "ExpandTextVars")):
494+
project = self.board.GetProject()
495+
title = pcbnew.ExpandTextVars(title, project)
496+
revision = pcbnew.ExpandTextVars(revision, project)
497+
company = pcbnew.ExpandTextVars(company, project)
498+
486499
file_date = title_block.GetDate()
487500
if not file_date:
488501
file_mtime = os.path.getmtime(self.file_name)
489502
file_date = datetime.fromtimestamp(file_mtime).strftime(
490503
'%Y-%m-%d %H:%M:%S')
491-
title = title_block.GetTitle()
492504
pcb_file_name = os.path.basename(self.file_name)
493505
if not title:
494506
# remove .kicad_pcb extension
@@ -520,8 +532,8 @@ def parse(self):
520532
"footprints": self.parse_footprints(),
521533
"metadata": {
522534
"title": title,
523-
"revision": title_block.GetRevision(),
524-
"company": title_block.GetCompany(),
535+
"revision": revision,
536+
"company": company,
525537
"date": file_date,
526538
},
527539
"bom": {},
@@ -560,8 +572,7 @@ def defaults(self):
560572
def Run(self):
561573
from ..version import version
562574
from ..errors import ParsingException
563-
self.version = version
564-
config = Config(self.version)
575+
config = Config(version)
565576
board = pcbnew.GetBoard()
566577
pcb_file_name = board.GetFileName()
567578

0 commit comments

Comments
 (0)