2013-04-11 22:34:04 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2011-02-24 02:11:25 +08:00
|
|
|
"""gen_pattern.py
|
2015-04-12 01:03:17 +08:00
|
|
|
Usage example:
|
|
|
|
python gen_pattern.py -o out.svg -r 11 -c 8 -T circles -s 20.0 -R 5.0 -u mm -w 216 -h 279
|
|
|
|
-o, --output - output file (default out.svg)
|
|
|
|
-r, --rows - pattern rows (default 11)
|
|
|
|
-c, --columns - pattern columns (default 8)
|
|
|
|
-T, --type - type of pattern, circles, acircles, checkerboard (default circles)
|
|
|
|
-s, --square_size - size of squares in pattern (default 20.0)
|
|
|
|
-R, --radius_rate - circles_radius = square_size/radius_rate (default 5.0)
|
|
|
|
-u, --units - mm, inches, px, m (default mm)
|
|
|
|
-w, --page_width - page width in units (default 216)
|
|
|
|
-h, --page_height - page height in units (default 279)
|
2015-06-09 17:35:48 +08:00
|
|
|
-a, --page_size - page size (default A4), supercedes -h -w arguments
|
2015-04-12 01:03:17 +08:00
|
|
|
-H, --help - show help
|
2011-02-24 02:11:25 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
from svgfig import *
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
|
|
|
|
class PatternMaker:
|
2015-04-12 01:03:17 +08:00
|
|
|
def __init__(self, cols,rows,output,units,square_size,radius_rate,page_width,page_height):
|
2011-02-24 02:11:25 +08:00
|
|
|
self.cols = cols
|
|
|
|
self.rows = rows
|
|
|
|
self.output = output
|
|
|
|
self.units = units
|
|
|
|
self.square_size = square_size
|
2015-04-12 01:03:17 +08:00
|
|
|
self.radius_rate = radius_rate
|
2011-02-24 02:11:25 +08:00
|
|
|
self.width = page_width
|
|
|
|
self.height = page_height
|
|
|
|
self.g = SVG("g") # the svg group container
|
2015-04-12 01:03:17 +08:00
|
|
|
|
2011-02-24 02:11:25 +08:00
|
|
|
def makeCirclesPattern(self):
|
|
|
|
spacing = self.square_size
|
2015-04-12 01:03:17 +08:00
|
|
|
r = spacing / self.radius_rate
|
2011-02-24 02:11:25 +08:00
|
|
|
for x in range(1,self.cols+1):
|
|
|
|
for y in range(1,self.rows+1):
|
|
|
|
dot = SVG("circle", cx=x * spacing, cy=y * spacing, r=r, fill="black")
|
|
|
|
self.g.append(dot)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-02-24 02:11:25 +08:00
|
|
|
def makeACirclesPattern(self):
|
|
|
|
spacing = self.square_size
|
2015-04-12 01:03:17 +08:00
|
|
|
r = spacing / self.radius_rate
|
2011-03-23 07:12:33 +08:00
|
|
|
for i in range(0,self.rows):
|
2012-10-17 07:18:30 +08:00
|
|
|
for j in range(0,self.cols):
|
2011-03-23 07:12:33 +08:00
|
|
|
dot = SVG("circle", cx= ((j*2 + i%2)*spacing) + spacing, cy=self.height - (i * spacing + spacing), r=r, fill="black")
|
2011-02-24 02:11:25 +08:00
|
|
|
self.g.append(dot)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-02-24 02:11:25 +08:00
|
|
|
def makeCheckerboardPattern(self):
|
|
|
|
spacing = self.square_size
|
2015-06-09 17:35:48 +08:00
|
|
|
xspacing = (self.width - self.cols * self.square_size) / 2.0
|
|
|
|
yspacing = (self.height - self.rows * self.square_size) / 2.0
|
|
|
|
for x in range(0,self.cols):
|
|
|
|
for y in range(0,self.rows):
|
2015-04-12 01:03:17 +08:00
|
|
|
if x%2 == y%2:
|
2015-06-09 17:35:48 +08:00
|
|
|
square = SVG("rect", x=x * spacing + xspacing, y=y * spacing + yspacing, width=spacing, height=spacing, fill="black")
|
|
|
|
self.g.append(square)
|
2015-04-12 01:03:17 +08:00
|
|
|
|
2011-02-24 02:11:25 +08:00
|
|
|
def save(self):
|
|
|
|
c = canvas(self.g,width="%d%s"%(self.width,self.units),height="%d%s"%(self.height,self.units),viewBox="0 0 %d %d"%(self.width,self.height))
|
2016-05-30 16:47:59 +08:00
|
|
|
c.save(self.output)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
|
2011-02-24 02:11:25 +08:00
|
|
|
def main():
|
|
|
|
# parse command line options, TODO use argparse for better doc
|
|
|
|
try:
|
2015-06-09 17:35:48 +08:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "Ho:c:r:T:u:s:R:w:h:a:", ["help","output=","columns=","rows=",
|
2015-04-12 01:03:17 +08:00
|
|
|
"type=","units=","square_size=","radius_rate=",
|
2015-06-09 17:35:48 +08:00
|
|
|
"page_width=","page_height=", "page_size="])
|
2017-01-16 18:03:49 +08:00
|
|
|
except getopt.error as msg:
|
|
|
|
print(msg)
|
|
|
|
print("for help use --help")
|
2011-02-24 02:11:25 +08:00
|
|
|
sys.exit(2)
|
|
|
|
output = "out.svg"
|
|
|
|
columns = 8
|
|
|
|
rows = 11
|
|
|
|
p_type = "circles"
|
|
|
|
units = "mm"
|
|
|
|
square_size = 20.0
|
2015-04-12 01:03:17 +08:00
|
|
|
radius_rate = 5.0
|
2015-06-09 17:35:48 +08:00
|
|
|
page_size = "A4"
|
|
|
|
# page size dict (ISO standard, mm) for easy lookup. format - size: [width, height]
|
|
|
|
page_sizes = {"A0": [840, 1188], "A1": [594, 840], "A2": [420, 594], "A3": [297, 420], "A4": [210, 297], "A5": [148, 210]}
|
|
|
|
page_width = page_sizes[page_size.upper()][0]
|
|
|
|
page_height = page_sizes[page_size.upper()][1]
|
2011-02-24 02:11:25 +08:00
|
|
|
# process options
|
|
|
|
for o, a in opts:
|
2015-04-12 01:03:17 +08:00
|
|
|
if o in ("-H", "--help"):
|
2017-01-16 18:03:49 +08:00
|
|
|
print(__doc__)
|
2011-02-24 02:11:25 +08:00
|
|
|
sys.exit(0)
|
|
|
|
elif o in ("-r", "--rows"):
|
|
|
|
rows = int(a)
|
|
|
|
elif o in ("-c", "--columns"):
|
|
|
|
columns = int(a)
|
|
|
|
elif o in ("-o", "--output"):
|
|
|
|
output = a
|
|
|
|
elif o in ("-T", "--type"):
|
|
|
|
p_type = a
|
|
|
|
elif o in ("-u", "--units"):
|
|
|
|
units = a
|
|
|
|
elif o in ("-s", "--square_size"):
|
|
|
|
square_size = float(a)
|
2015-04-12 01:03:17 +08:00
|
|
|
elif o in ("-R", "--radius_rate"):
|
|
|
|
radius_rate = float(a)
|
2011-02-24 02:11:25 +08:00
|
|
|
elif o in ("-w", "--page_width"):
|
|
|
|
page_width = float(a)
|
|
|
|
elif o in ("-h", "--page_height"):
|
|
|
|
page_height = float(a)
|
2015-06-09 17:35:48 +08:00
|
|
|
elif o in ("-a", "--page_size"):
|
|
|
|
units = "mm"
|
|
|
|
page_size = a.upper()
|
|
|
|
page_width = page_sizes[page_size][0]
|
|
|
|
page_height = page_sizes[page_size][1]
|
2015-04-12 01:03:17 +08:00
|
|
|
pm = PatternMaker(columns,rows,output,units,square_size,radius_rate,page_width,page_height)
|
2011-02-24 02:11:25 +08:00
|
|
|
#dict for easy lookup of pattern type
|
|
|
|
mp = {"circles":pm.makeCirclesPattern,"acircles":pm.makeACirclesPattern,"checkerboard":pm.makeCheckerboardPattern}
|
|
|
|
mp[p_type]()
|
|
|
|
#this should save pattern to output
|
|
|
|
pm.save()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|