From 754c9557e52a5ba9704c8fb3af4175a36d37110e Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 19 Jul 2024 19:17:46 +0200 Subject: kicad: Make point lists more ergonomic This commit removes the unnecessary "PointList" wrapper class, and just puts regular python lists in its place. --- gerbonara/cad/kicad/base_types.py | 9 ++++++++- gerbonara/cad/kicad/footprints.py | 14 +++++++------- gerbonara/cad/kicad/graphical_primitives.py | 14 +++++++------- gerbonara/cad/kicad/primitives.py | 8 ++++---- gerbonara/cad/kicad/schematic.py | 6 +++--- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/gerbonara/cad/kicad/base_types.py b/gerbonara/cad/kicad/base_types.py index 6e9fe72..0060980 100644 --- a/gerbonara/cad/kicad/base_types.py +++ b/gerbonara/cad/kicad/base_types.py @@ -270,7 +270,14 @@ class XYCoord: @sexp_type('pts') class PointList: - xy : List(XYCoord) = field(default_factory=list) + @classmethod + def __map__(kls, obj, parent=None): + _tag, *values = obj + return [map_sexp(XYCoord, elem, parent=parent) for elem in values] + + @classmethod + def __sexp__(kls, value): + yield [kls.name_atom, *(e for elem in value for e in elem.__sexp__(elem))] @sexp_type('arc') diff --git a/gerbonara/cad/kicad/footprints.py b/gerbonara/cad/kicad/footprints.py index ce9e86c..c7bc973 100644 --- a/gerbonara/cad/kicad/footprints.py +++ b/gerbonara/cad/kicad/footprints.py @@ -243,7 +243,7 @@ class Arc: @sexp_type('fp_poly') class Polygon: - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) layer: Named(str) = None uuid: UUID = field(default_factory=UUID) width: Named(float) = None @@ -253,13 +253,13 @@ class Polygon: tstamp: Timestamp = None def render(self, variables=None, cache=None): - if len(self.pts.xy) < 2: + if len(self.pts) < 2: return dasher = Dasher(self) - start = self.pts.xy[0] + start = self.pts[0] dasher.move(start.x, start.y) - for point in self.pts.xy[1:]: + for point in self.pts[1:]: dasher.line(point.x, point.y) if dasher.width > 0: @@ -268,12 +268,12 @@ class Polygon: yield go.Line(x1, -y1, x2, -y2, aperture=aperture, unit=MM) if self.fill == Atom.solid: - yield go.Region([(pt.x, -pt.y) for pt in self.pts.xy], unit=MM) + yield go.Region([(pt.x, -pt.y) for pt in self.pts], unit=MM) @sexp_type('fp_curve') class Curve: - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) layer: Named(str) = None uuid: UUID = field(default_factory=UUID) width: Named(float) = None @@ -314,7 +314,7 @@ class Dimension: layer: Named(str) = None uuid: UUID = field(default_factory=UUID) tstamp: Timestamp = None - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) height: Named(float) = None orientation: Named(int) = 0 leader_length: Named(float) = None diff --git a/gerbonara/cad/kicad/graphical_primitives.py b/gerbonara/cad/kicad/graphical_primitives.py index e068d23..8911639 100644 --- a/gerbonara/cad/kicad/graphical_primitives.py +++ b/gerbonara/cad/kicad/graphical_primitives.py @@ -37,7 +37,7 @@ class TextBox(BBoxMixin): text: str = '' start: Named(XYCoord) = None end: Named(XYCoord) = None - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) angle: OmitDefault(Named(float)) = 0.0 layer: Named(str) = "" uuid: UUID = field(default_factory=UUID) @@ -55,7 +55,7 @@ class TextBox(BBoxMixin): raise ValueError('Vector font text with empty render cache') for poly in render_cache.polygons: - reg = go.Region([(p.x, -p.y) for p in poly.pts.xy], unit=MM) + reg = go.Region([(p.x, -p.y) for p in poly.pts], unit=MM) if self.stroke: if self.stroke.type not in (None, Atom.default, Atom.solid): @@ -263,12 +263,12 @@ class Polygon(BBoxMixin): yield reg def offset(self, x=0, y=0): - self.pts = PointList([pt.with_offset(x, y) for pt in self.pts]) + self.pts = [pt.with_offset(x, y) for pt in self.pts] @sexp_type('gr_curve') class Curve(BBoxMixin): - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) layer: Named(str) = None width: Named(float) = None uuid: UUID = field(default_factory=UUID) @@ -278,7 +278,7 @@ class Curve(BBoxMixin): raise NotImplementedError('Bezier rendering is not yet supported. Please raise an issue and provide an example file.') def offset(self, x=0, y=0): - self.pts = PointList([pt.with_offset(x, y) for pt in self.pts]) + self.pts =[pt.with_offset(x, y) for pt in self.pts] @sexp_type('gr_bbox') @@ -335,7 +335,7 @@ class Dimension: layer: Named(str) = 'Dwgs.User' uuid: UUID = field(default_factory=UUID) tstamp: Timestamp = field(default_factory=Timestamp) - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) height: Named(float) = None orientation: Named(int) = None leader_length: Named(float) = None @@ -347,5 +347,5 @@ class Dimension: raise NotImplementedError('Dimension rendering is not yet supported. Please raise an issue.') def offset(self, x=0, y=0): - self.pts = PointList([pt.with_offset(x, y) for pt in self.pts]) + self.pts = [pt.with_offset(x, y) for pt in self.pts] diff --git a/gerbonara/cad/kicad/primitives.py b/gerbonara/cad/kicad/primitives.py index 5be0422..11da6c6 100644 --- a/gerbonara/cad/kicad/primitives.py +++ b/gerbonara/cad/kicad/primitives.py @@ -135,18 +135,18 @@ class ZoneFill: class FillPolygon: layer: Named(str) = "" island: Wrap(Flag()) = False - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) @sexp_type('fill_segments') class FillSegment: layer: Named(str) = "" - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) @sexp_type('polygon') class ZonePolygon: - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) @sexp_type('zone') @@ -199,7 +199,7 @@ class Zone: @sexp_type('polygon') class RenderCachePolygon: - pts: PointList = field(default_factory=PointList) + pts: PointList = field(default_factory=list) @sexp_type('render_cache') diff --git a/gerbonara/cad/kicad/schematic.py b/gerbonara/cad/kicad/schematic.py index 0a2f4de..bf32d11 100644 --- a/gerbonara/cad/kicad/schematic.py +++ b/gerbonara/cad/kicad/schematic.py @@ -132,7 +132,7 @@ def _polyline_bounds(self): @sexp_type('wire') class Wire: - points: PointList = field(default_factory=PointList) + points: PointList = field(default_factory=list) stroke: Stroke = field(default_factory=Stroke) uuid: UUID = field(default_factory=UUID) @@ -145,7 +145,7 @@ class Wire: @sexp_type('bus') class Bus: - points: PointList = field(default_factory=PointList) + points: PointList = field(default_factory=list) stroke: Stroke = field(default_factory=Stroke) uuid: UUID = field(default_factory=UUID) @@ -158,7 +158,7 @@ class Bus: @sexp_type('polyline') class Polyline: - points: PointList = field(default_factory=PointList) + points: PointList = field(default_factory=list) stroke: Stroke = field(default_factory=Stroke) uuid: UUID = field(default_factory=UUID) -- cgit