HoneyCombExtention/honeycomb.py
Jesse Millwood 28f126df71 Initial Commit
This includes a working plugin
2023-09-12 17:27:03 -04:00

86 lines
3.2 KiB
Python

#!/usr/bin/env python3
import inkex
class HoneyCombExtension(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument("--spacing", type=float, default=0.0)
pars.add_argument("--count_x", type=int, default=0)
pars.add_argument("--count_y", type=int, default=0)
def effect(self):
if len(self.svg.selection) != 2:
inkex.errormsg("Must select a bounding box and a circle to honeycomb")
return
target = self.svg.selection.popitem()[1]
bounding_box = self.svg.selection.popitem()[1]
# Validate bounding box
if bounding_box is None:
inkex.errormsg("Must have a bounding box selected")
return
bounding_box_h = float(bounding_box.get("height"))
bounding_box_w = float(bounding_box.get("width"))
bounding_box_x = float(bounding_box.get("x"))
bounding_box_y = float(bounding_box.get("y"))
bounding_box_style = bounding_box.get("style")
if (bounding_box_h and bounding_box_w and bounding_box_x and bounding_box_y) \
is None:
inkex.errormsg("Bounding Box must be the first selection")
return
# Validate honeycomb target
if target is None:
inkex.errormsg("Must make a selection of an object to honycomb")
if target.get("r"):
# Selection is a circle
target_rad = float(target.get("r"))
target_dia = 2 * float(target_rad)
target_style = target.get("style")
difference = bounding_box_w - (target_dia * self.options.count_x)
target_spacing = difference/(self.options.count_x)
shift_row = True
for y_cnt in range(0, self.options.count_y):
y_coord = (bounding_box_y + target_rad)
y_coord += y_cnt * target_dia
if y_cnt % 2 != 0:
shift_row = True
num_x = self.options.count_x + 1
else:
shift_row = False
num_x = self.options.count_x
for x_cnt in range(0, num_x):
x_coord = bounding_box_x + target_rad
x_coord += x_cnt * (target_spacing + target_dia)
if shift_row:
x_coord -= target_rad
else:
x_coord += target_spacing / 2
circ = inkex.Circle(attrib={"cx": f"{x_coord}",
"cy": f"{y_coord}",
"r": f"{target_rad}",
"style": target_style})
layer = self.svg.get_current_layer()
layer.append(circ)
elif target.get("rx"):
# Selection is an elipse
inkex.errormsg("Elipses are not supported\n"
"Selection must have 'r' attribute")
else:
inkex.errormsg("Other shapes not supported\n"
"Selection must have 'r' attribute")
return
if __name__ == '__main__':
HoneyCombExtension().run()