summaryrefslogtreecommitdiff
path: root/test2.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2023-09-22 18:50:58 +0200
committerjaseg <git@jaseg.de>2023-09-22 18:54:11 +0200
commit61e591b5b811438ff36e4347f94c6c89f222b7b7 (patch)
tree81f23fda65b935d187702243a706c07a8455e613 /test2.py
parent95da4820337911f20dcc54c42820805fbc35ee13 (diff)
downloadgerbonara-61e591b5b811438ff36e4347f94c6c89f222b7b7.tar.gz
gerbonara-61e591b5b811438ff36e4347f94c6c89f222b7b7.tar.bz2
gerbonara-61e591b5b811438ff36e4347f94c6c89f222b7b7.zip
WiP
Diffstat (limited to 'test2.py')
-rw-r--r--test2.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/test2.py b/test2.py
new file mode 100644
index 0000000..72626db
--- /dev/null
+++ b/test2.py
@@ -0,0 +1,53 @@
+from gerbonara import *
+from shapely import *
+
+stack = layers.LayerStack.open('/home/jaseg/proj/ihsm-strain-gage-controller-hw/pcb/gerber')
+# Let's work in mm here. Gerbonara will take care to convert units when the file is in US customary units.
+(x1, y1), (x2, y2) = stack.bounding_box(unit=utils.MM)
+
+for l in [stack['bottom mask'], stack['top mask']]:
+
+ # The solder mask gerber layer by convention is "negative". That is, a "dark" polarity (drawn) Gerber primitive
+ # will result in an opening in the solder mask. Conversely, an empty gerber file would lead to the entire board
+ # being covered in solder mask.
+ #
+ # Here, we add a rectangle covering the entire board so the entire board is *free* of solder mask.
+
+ new = [graphic_objects.Region(
+ [(x1, y1), (x1, y2), (x2, y2), (x2, y1), (x1, y1)],
+ unit=utils.MM,
+ polarity_dark=True)]
+
+ # Iterate through all objects on the solder mask layer. In later KiCad versions, everything on the solder mask
+ # layer is exported as a Gerber region, which is a really bad idea, but makes things easy for us here.
+ for obj in l.objects:
+ if not obj.polarity_dark:
+ continue
+
+ if isinstance(obj, graphic_objects.Region):
+ regions = []
+ else:
+ regions = [graphic_objects.Region.from_arc_poly(prim.to_arc_poly())
+ for prim in obj.to_primitives(unit=utils.MM)]
+
+ for obj in regions:
+ # Convert the region to a shapely line string
+ ls = LineString(obj.outline).normalize()
+
+ # Ask shapely to offset the line string by 1 mm
+ out = ls.offset_curve(obj.unit(1, 'mm'))
+
+ # For negative offsets, this operation can result in an object being split up into multiple parts, so we
+ # might get back a MultiLineString instead of a LineString.
+ for ls in (out.geoms if hasattr(out, 'geoms') else [out]):
+
+ # Convert the resulting shapely object back to a Gerber region.
+ new.append(graphic_objects.Region(
+ unit=obj.unit,
+ polarity_dark=not obj.polarity_dark,
+ outline=list(ls.coords)))
+
+ # Append the new objects to the original layer data
+ l.objects = new + l.objects
+# Write the modified layer stack to a new Gerber directory
+stack.save_to_directory('/tmp/out')