gen_pattern.py 3.95 KB
Newer Older
1
#!/usr/bin/env python
2

3
"""gen_pattern.py
4 5 6 7 8 9 10 11 12 13 14 15 16
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)
-H, --help - show help
17 18 19 20 21 22 23 24
"""

from svgfig import *

import sys
import getopt

class PatternMaker:
25
  def __init__(self, cols,rows,output,units,square_size,radius_rate,page_width,page_height):
26 27 28 29 30
    self.cols = cols
    self.rows = rows
    self.output = output
    self.units = units
    self.square_size = square_size
31
    self.radius_rate = radius_rate
32 33 34
    self.width = page_width
    self.height = page_height
    self.g = SVG("g") # the svg group container
35

36 37
  def makeCirclesPattern(self):
    spacing = self.square_size
38
    r = spacing / self.radius_rate
39 40 41 42
    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)
43

44 45
  def makeACirclesPattern(self):
    spacing = self.square_size
46
    r = spacing / self.radius_rate
47
    for i in range(0,self.rows):
48
      for j in range(0,self.cols):
49
        dot = SVG("circle", cx= ((j*2 + i%2)*spacing) + spacing, cy=self.height - (i * spacing + spacing), r=r, fill="black")
50
        self.g.append(dot)
51

52 53 54 55
  def makeCheckerboardPattern(self):
    spacing = self.square_size
    for x in range(1,self.cols+1):
      for y in range(1,self.rows+1):
56 57 58 59
        if x%2 == y%2:
          dot = SVG("rect", x=x * spacing, y=y * spacing, width=spacing, height=spacing, stroke_width="0", fill="black")
          self.g.append(dot)

60 61 62
  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))
    c.inkview(self.output)
63 64


65 66 67
def main():
    # parse command line options, TODO use argparse for better doc
    try:
68 69 70
        opts, args = getopt.getopt(sys.argv[1:], "Ho:c:r:T:u:s:R:w:h:", ["help","output=","columns=","rows=",
                                                                      "type=","units=","square_size=","radius_rate=",
                                                                      "page_width=","page_height="])
71 72 73 74 75 76 77 78 79 80
    except getopt.error, msg:
        print msg
        print "for help use --help"
        sys.exit(2)
    output = "out.svg"
    columns = 8
    rows = 11
    p_type = "circles"
    units = "mm"
    square_size = 20.0
81
    radius_rate = 5.0
82 83 84 85
    page_width = 216    #8.5 inches
    page_height = 279   #11 inches
    # process options
    for o, a in opts:
86
        if o in ("-H", "--help"):
87 88 89 90 91 92 93 94 95 96 97 98 99 100
            print __doc__
            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)
101 102
        elif o in ("-R", "--radius_rate"):
            radius_rate = float(a)
103 104 105 106
        elif o in ("-w", "--page_width"):
            page_width = float(a)
        elif o in ("-h", "--page_height"):
            page_height = float(a)
107
    pm = PatternMaker(columns,rows,output,units,square_size,radius_rate,page_width,page_height)
108 109 110 111 112 113 114 115
    #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()