This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/maze/maze.py
2021-10-13 16:25:49 +02:00

65 lines
2.3 KiB
Python

#! /usr/bin/env python3
# this module is under licence MIT @ Tiemen DUVILLARD 2020
# for all questions, comments, bugs: duvillard.tiemen@gmail.com
import inkex
from lxml import etree
from maze_lib import *
def points_to_svgd(p, close=False):
""" convert list of points (x,y) pairs
into a SVG path list
"""
f = p[0]
p = p[1:]
svgd = 'M%.4f,%.4f' % f
for x in p:
svgd += 'L%.4f,%.4f' % x
if close:
svgd += 'z'
return svgd
class Maze(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument("--verti", type=int, default=20, help="Height (cells)")
pars.add_argument("--horiz", type=int, default=20, help="Length (cells)")
pars.add_argument("--size", type=float, default=10.0, help="Cell size")
pars.add_argument("--cell_units", default="mm", help="Units")
pars.add_argument("--algo", default=1, help="Algorithm")
pars.add_argument("--width", type=float, default=10.0, help="Line width")
def effect(self):
# basic style
style = { 'stroke': "black", "fill":"none", 'stroke-width': self.options.width }
# my group of paths
topgroup = etree.SubElement(self.svg.get_current_layer(), 'g' )
lc = self.svg.unittouu (str(self.options.size) + self.options.cell_units)
X = self.options.verti
Y = self.options.horiz
L = MazeLib(X,Y,self.options.algo)
for i,j,door in L.verticalDoors():
if door:
path = points_to_svgd([(lc*(j+1), lc*(i)), (lc*(j+1), lc*(i+1))])
mypath_attribs = { 'style': str(inkex.Style(style)), 'd': path }
squiggle = etree.SubElement(topgroup, inkex.addNS('path','svg'), mypath_attribs )
for i,j,door in L.horizontalDoors():
if door:
path = points_to_svgd([(lc*(j), lc*(i+1)), (lc*(j+1), lc*(i+1))])
mypath_attribs = { 'style': str(inkex.Style(style)), 'd': path }
squiggle = etree.SubElement(topgroup, inkex.addNS('path','svg'), mypath_attribs )
path = points_to_svgd([(0,0),(0,lc*Y),(lc*X,lc*Y),(lc*X,0)], True)
mypath_attribs = { 'style': str(inkex.Style(style)), 'd': path }
squiggle = etree.SubElement(topgroup, inkex.addNS('path','svg'), mypath_attribs )
if __name__ == '__main__':
Maze().run()