summaryrefslogtreecommitdiff
path: root/gerbonara/cad/kicad/pcb.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2023-06-29 19:47:31 +0200
committerjaseg <git@jaseg.de>2023-06-29 19:47:31 +0200
commit56d55fda5d5424644e5914c73cfee392f8739bbb (patch)
tree38dd0a4d976b6d61fd876e0ebc8f541df8c1de45 /gerbonara/cad/kicad/pcb.py
parent467e482bf435fce83900fb446761dded4ffd7b31 (diff)
downloadgerbonara-56d55fda5d5424644e5914c73cfee392f8739bbb.tar.gz
gerbonara-56d55fda5d5424644e5914c73cfee392f8739bbb.tar.bz2
gerbonara-56d55fda5d5424644e5914c73cfee392f8739bbb.zip
kicad: Extend query API
Diffstat (limited to 'gerbonara/cad/kicad/pcb.py')
-rw-r--r--gerbonara/cad/kicad/pcb.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/gerbonara/cad/kicad/pcb.py b/gerbonara/cad/kicad/pcb.py
index 8a90a7e..22ad23e 100644
--- a/gerbonara/cad/kicad/pcb.py
+++ b/gerbonara/cad/kicad/pcb.py
@@ -5,6 +5,7 @@ Library for handling KiCad's PCB files (`*.kicad_mod`).
from pathlib import Path
from dataclasses import field
from itertools import chain
+import re
import fnmatch
from .sexp import *
@@ -23,6 +24,12 @@ from ...newstroke import Newstroke
from ...utils import MM
+def match_filter(f, value):
+ if isinstance(f, str) and re.fullmatch(f, value):
+ return True
+ return value in f
+
+
@sexp_type('general')
class GeneralSection:
thickness: Named(float) = 1.60
@@ -234,6 +241,39 @@ class Board:
original_filename: str = None
_bounding_box: tuple = None
+
+ def __after_parse__(self, parent):
+ self.properties = {prop.key: prop.value for prop in self.properties}
+
+ for fp in self.footprints:
+ fp.board = self
+
+ def __before_sexp__(self):
+ self.properties = [Property(key, value) for key, value in self.properties.items()]
+
+ def find_pads(self, net=None):
+ for fp in self.footprints:
+ for pad in fp.pads:
+ if net and not match_filter(net, pad.net.name):
+ continue
+ yield pad
+
+ def find_footprints(self, value=None, reference=None, name=None, net=None, sheetname=None, sheetfile=None):
+ for fp in self.footprints:
+ if name and not match_filter(name, fp.name):
+ continue
+ if value and not match_filter(value, fp.properties.get('value', '')):
+ continue
+ if reference and not match_filter(reference, fp.properties.get('reference', '')):
+ continue
+ if net and not any(match_filter(net, pad.net.name) for pad in fp.pads):
+ continue
+ if sheetname and not match_filter(sheetname, fp.sheetname):
+ continue
+ if sheetfile and not match_filter(sheetfile, fp.sheetfile):
+ continue
+ yield fp
+
@property
def version(self):
return self._version