-
Notifications
You must be signed in to change notification settings - Fork 6
/
lace_ground.py
260 lines (227 loc) · 10.5 KB
/
lace_ground.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
# Copyright (c) 2017, Veronika Irvine
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import os
from math import sin,cos,radians, ceil
from lxml import etree
from random import random
import inkex
__author__ = 'Veronika Irvine'
__credits__ = ['Ben Connors', 'Veronika Irvine', 'Mark Shafer']
__license__ = 'Simplified BSD'
class LaceGround(inkex.Effect):
"""
Create a ground for lace from a text file descriptor
using specified angle and spacing
"""
def unitToUu(self,param):
""" Convert units.
Converts a number in some units into the units used internally by
Inkscape.
param is a string representing a number with units attached. An
example would be '3.8mm'. Any units supported by Inkscape
are supported by this function.
This wrapper function catches changes made to the location
of the function between Inkscape versions.
"""
try:
return self.svg.unittouu(param)
except:
return inkex.unittouu(param)
def loadFile(self):
# Ensure that file exists and has the proper extension
if not self.options.file:
inkex.errormsg('You must specify a template file.')
exit()
self.options.file = self.options.file.strip()
if self.options.file == '':
inkex.errormsg('You must specify a template file.')
exit()
if not os.path.isfile(self.options.file):
inkex.errormsg('You have not specified a valid path for the template file.\n\nYour entry: '+self.options.file)
exit()
extension = os.path.splitext(self.options.file)[1]
if extension != '.txt':
inkex.errormsg('The file name must end with .txt.\n\nYour entry: '+self.options.file)
exit()
data = []
rowCount = 0
colCount = 0
with open(self.options.file,'r') as f:
first = True
for line in f:
if first:
# first line of file gives row count and column count
first = False
line = line.strip()
temp = line.split('\t')
type = temp[0]
rowCount = int(temp[1])
colCount = int(temp[-1])
else:
line = line.strip()
line = line.lstrip('[')
line = line.rstrip(']')
rowData = line.split(']\t[')
data.append([])
for cell in rowData:
cell = cell.strip()
data[-1].append([float(num) for num in cell.split(',')])
return {'type':type, 'rowCount':rowCount, 'colCount':colCount, 'data':data}
def line(self, x1, y1, x2, y2):
"""
Draw a line from point at (x1, y1) to point at (x2, y2).
Style of line is hard coded and specified by 's'.
"""
# define the motions
path = 'M %s,%s L %s,%s' %(x1,y1,x2,y2)
# define the stroke style
s = {'stroke-linejoin': 'miter',
'stroke-width': self.options.linewidth,
'stroke-opacity': '1.0',
'fill-opacity': '1.0',
'stroke': self.options.linecolor,
'stroke-linecap': 'butt',
'stroke-linejoin': 'miter',
'fill': 'none'
}
# create attributes from style and path
attribs = {'style':str(inkex.Style(s)), 'd':path}
# insert path object into current layer
etree.SubElement(self.svg.get_current_layer(), inkex.addNS('path', 'svg'), attribs)
def jitter(self, nodeJitter, x, y):
if self.options.xrand == 0 and self.options.yrand == 0:
return [x,y]
id = "%.3f" % x +",%.3f" % y
value = nodeJitter.get(id)
if not value:
jitx = x + self.options.xrand*(2.0*random()-1.0)*self.options.spacing/100
jity = y + self.options.yrand*(2.0*random()-1.0)*self.options.spacing/100
value = [jitx,jity]
nodeJitter[id] = value
return value
def draw(self, data, rowCount, colCount):
a = self.options.spacing
theta = self.options.angle
deltaX = a*sin(theta)
deltaY = a*cos(theta)
maxRows = ceil(self.options.height / deltaY)
maxCols = ceil(self.options.width / deltaX)
x = 0.0
y = 0.0
repeatY = 0
repeatX = 0
# Random jitter of nodes
nodeJitter = {}
while repeatY * rowCount < maxRows:
x = 0.0
repeatX = 0
while repeatX * colCount < maxCols:
for row in data:
for coords in row:
x1 = x + coords[0]*deltaX
y1 = y + coords[1]*deltaY
x2 = x + coords[2]*deltaX
y2 = y + coords[3]*deltaY
x3 = x + coords[4]*deltaX
y3 = y + coords[5]*deltaY
x1,y1 = self.jitter(nodeJitter,x1,y1)
x2,y2 = self.jitter(nodeJitter,x2,y2)
x3,y3 = self.jitter(nodeJitter,x3,y3)
self.line(x1,y1,x2,y2)
self.line(x1,y1,x3,y3)
repeatX += 1
x += deltaX * colCount
repeatY += 1
y += deltaY * rowCount
def __init__(self):
"""
Constructor.
Defines the '--centerx' option of the script.
"""
# Call the base class constructor.
inkex.Effect.__init__(self)
# file
self.arg_parser.add_argument('-f', '--file',
type=str,
dest='file',
help='File containing lace ground description')
# Grid description
self.arg_parser.add_argument('--angle',
type=float,
dest='angle')
self.arg_parser.add_argument('--distance',
type=float,
dest='spacing')
self.arg_parser.add_argument('--pinunits',
type=str,
dest='pinunits')
self.arg_parser.add_argument('--width',
type=float,
dest='width')
self.arg_parser.add_argument('--patchunits',
type=str,
dest='patchunits')
self.arg_parser.add_argument('--height',
type=float,
dest='height')
self.arg_parser.add_argument('--linewidth',
type=float,
dest='linewidth')
self.arg_parser.add_argument('--lineunits',
type=str,
dest='lineunits')
self.arg_parser.add_argument('--linecolor',
type=inkex.Color,
dest='linecolor')
self.arg_parser.add_argument('--xrand',
type=int,
dest='xrand')
self.arg_parser.add_argument('--yrand',
type=int,
dest='yrand')
def effect(self):
"""
Effect behaviour.
Overrides base class' method and draws something.
"""
result = self.loadFile()
# Convert input to universal units
self.options.width = self.unitToUu(str(self.options.width)+self.options.patchunits)
self.options.height = self.unitToUu(str(self.options.height)+self.options.patchunits)
self.options.linewidth = self.unitToUu(str(self.options.linewidth)+self.options.lineunits)
self.options.spacing = self.unitToUu(str(self.options.spacing)+self.options.pinunits)
# Users expect spacing to be the vertical distance between footside pins
# (vertical distance between every other row) but in the script we use it
# as the diagonal distance between grid points
# therefore convert spacing based on the angle chosen
self.options.angle = radians(self.options.angle)
self.options.spacing = self.options.spacing/(2.0*cos(self.options.angle))
# Draw a ground based on file description and user inputs
self.options.linecolor = self.options.linecolor.to_rgb()
# For now, assume style is Checker but could change in future
self.draw(result['data'],result['rowCount'],result['colCount'])
# Create effect instance and apply it.
effect = LaceGround()
effect.run()