From 5f416063be76fb98c574e1f72da52d3814ec0d04 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Mon, 27 Mar 2023 10:53:33 -0300 Subject: [PATCH] [PcbDraw][Fixed] Support for 7.0.1 polygons - Now KiCad generates polygons in the SVG Fixes the problem reported in upstream: yaqwsx/PcbDraw#142 --- CHANGELOG.md | 2 ++ kibot/PcbDraw/README.md | 32 ++++++++++++++++++++++++++++++++ kibot/PcbDraw/plot.py | 17 ++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50830a1e0..35905c66b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - KiKit present: problems when no board was specified. (#402) - Datasheet download: - Avoid interruptions when too many redirections is detected (#408) +- PcbDraw: + - KiCad 7.0.1 polygons used as board edge. (yaqwsx/PcbDraw#142) ## [1.6.1] - 2023-03-16 diff --git a/kibot/PcbDraw/README.md b/kibot/PcbDraw/README.md index 3b1cba681..68425dfe8 100644 --- a/kibot/PcbDraw/README.md +++ b/kibot/PcbDraw/README.md @@ -289,3 +289,35 @@ index 8ca660e6..9dc45ba9 100644 center = footprint.GetPosition() orient = math.radians(footprint.GetOrientation().AsDegrees()) + +## 2023-03-27 Fixe for KiCad 7.0.1 polygons + +diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py +index 9dc45ba9..8df84469 100644 +--- a/kibot/PcbDraw/plot.py ++++ b/kibot/PcbDraw/plot.py +@@ -408,7 +408,22 @@ def get_board_polygon(svg_elements: etree.Element) -> etree.Element: + for group in svg_elements: + for svg_element in group: + if svg_element.tag == "path": +- elements.append(SvgPathItem(svg_element.attrib["d"])) ++ path = svg_element.attrib["d"] ++ # Check if this is a closed polygon (KiCad 7.0.1+) ++ polygon = re.fullmatch(r"M ((\d+\.\d+),(\d+\.\d+) )+Z", path) ++ if polygon: ++ # Yes, decompose it in lines ++ polygon = re.findall(r"(\d+\.\d+),(\d+\.\d+) ", path) ++ start = polygon[0] ++ # Close it ++ polygon.append(polygon[0]) ++ # Add the lines ++ for end in polygon[1:]: ++ path = 'M'+start[0]+' '+start[1]+' L'+end[0]+' '+end[1] ++ elements.append(SvgPathItem(path)) ++ start = end ++ else: ++ elements.append(SvgPathItem(path)) + elif svg_element.tag == "circle": + # Convert circle to path + att = svg_element.attrib + diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py index 9dc45ba96..8df844697 100644 --- a/kibot/PcbDraw/plot.py +++ b/kibot/PcbDraw/plot.py @@ -408,7 +408,22 @@ def get_board_polygon(svg_elements: etree.Element) -> etree.Element: for group in svg_elements: for svg_element in group: if svg_element.tag == "path": - elements.append(SvgPathItem(svg_element.attrib["d"])) + path = svg_element.attrib["d"] + # Check if this is a closed polygon (KiCad 7.0.1+) + polygon = re.fullmatch(r"M ((\d+\.\d+),(\d+\.\d+) )+Z", path) + if polygon: + # Yes, decompose it in lines + polygon = re.findall(r"(\d+\.\d+),(\d+\.\d+) ", path) + start = polygon[0] + # Close it + polygon.append(polygon[0]) + # Add the lines + for end in polygon[1:]: + path = 'M'+start[0]+' '+start[1]+' L'+end[0]+' '+end[1] + elements.append(SvgPathItem(path)) + start = end + else: + elements.append(SvgPathItem(path)) elif svg_element.tag == "circle": # Convert circle to path att = svg_element.attrib