-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathCsvWriter.py
More file actions
222 lines (192 loc) · 6.29 KB
/
CsvWriter.py
File metadata and controls
222 lines (192 loc) · 6.29 KB
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
import math
from .EmbFunctions import *
from .PecGraphics import get_graphic_as_string
from .WriteHelper import write_string_utf8
ENCODE = False
WRITES_SPEEDS = True
SEQUIN_CONTINGENCY = CONTINGENCY_SEQUIN_UTILIZE
def csv(f, values):
string = ""
for v in values:
if len(string) > 0:
string += ","
string += '"%s"' % v
write_string_utf8(f, string + "\n")
def distance(dx, dy):
dx *= dx
dy *= dy
return math.sqrt(dx + dy)
def angle(dx, dy):
tau = math.pi * 2
angle = math.atan2(dy, dx)
angle += tau / float(2)
angle /= tau
return angle
def write_data(pattern, f):
names = get_common_name_dictionary()
extends = pattern.bounds()
width = extends[2] - extends[0]
height = extends[3] - extends[1]
csv(f, ("#", "[VAR_NAME]", "[VAR_VALUE]"))
count_stitches = pattern.count_stitches()
csv(f, (">", "STITCH_COUNT:", str(count_stitches)))
count_threads = pattern.count_color_changes()
csv(f, (">", "THREAD_COUNT:", str(count_threads)))
count_set_needles = pattern.count_needle_sets()
csv(f, (">", "NEEDLE_COUNT:", str(count_set_needles)))
csv(f, (">", "EXTENTS_LEFT:", str(extends[0])))
csv(f, (">", "EXTENTS_TOP:", str(extends[1])))
csv(f, (">", "EXTENTS_RIGHT:", str(extends[2])))
csv(f, (">", "EXTENTS_BOTTOM:", str(extends[3])))
csv(f, (">", "EXTENTS_WIDTH:", str(width)))
csv(f, (">", "EXTENTS_HEIGHT:", str(height)))
stitch_counts = {}
for s in pattern.stitches:
command = s[2] & COMMAND_MASK
if command in stitch_counts:
stitch_counts[command] += 1
else:
stitch_counts[command] = 1
if len(stitch_counts) != 0:
for the_key, the_value in stitch_counts.items():
try:
the_key &= COMMAND_MASK
name = "COMMAND_" + names[the_key]
except (IndexError, KeyError):
name = "COMMAND_UNKNOWN_" + str(the_key)
csv(f, (">", name, str(the_value)))
write_string_utf8(f, "\n")
def write_metadata(pattern, f):
if len(pattern.extras) > 0:
csv(f, ("#", "[METADATA_NAME]", "[METADATA]"))
for the_key, the_value in pattern.extras.items():
if isinstance(the_value, tuple):
the_value = "\n" + get_graphic_as_string(the_value)
csv(f, ("@", str(the_key), str(the_value)))
write_string_utf8(f, "\n")
def write_threads(pattern, f):
if len(pattern.threadlist) > 0:
csv(
f,
(
"#",
"[THREAD_NUMBER]",
"[HEX_COLOR]",
"[DESCRIPTION]",
"[BRAND]",
"[CATALOG_NUMBER]",
"[DETAILS]",
"[WEIGHT]",
),
)
for i, thread in enumerate(pattern.threadlist):
csv(
f,
(
"$",
str(i),
thread.hex_color(),
thread.description,
thread.brand,
thread.catalog_number,
thread.details,
thread.weight,
),
)
write_string_utf8(f, "\n")
def decoded_name(names, data):
command = decode_embroidery_command(data)
try:
name = names[command[0]]
if command[1] is not None:
name = name + " t" + str(command[1])
if command[2] is not None:
name = name + " n" + str(command[2])
if command[3] is not None:
name = name + " o" + str(command[3])
except (IndexError, KeyError):
name = "UNKNOWN " + str(data)
return name
def write_stitches_displacement(pattern, f):
names = get_common_name_dictionary()
csv(
f,
(
"#",
"[STITCH_INDEX]",
"[STITCH_TYPE]",
"[X]",
"[Y]",
"[DX]",
"[DY]",
"[R]",
"[ANGLE]",
),
)
current_x = 0
current_y = 0
for i, stitch in enumerate(pattern.stitches):
name = decoded_name(names, stitch[2])
dx = stitch[0] - current_x
dy = stitch[1] - current_y
csv(
f,
(
"*",
str(i),
name,
str(stitch[0]),
str(stitch[1]),
str(dx),
str(dy),
str(distance(dx, dy)),
str(angle(dx, dy)),
),
)
current_x = stitch[0]
current_y = stitch[1]
def write_stitches_deltas(pattern, f):
names = get_common_name_dictionary()
csv(f, ("#", "[STITCH_INDEX]", "[STITCH_TYPE]", "[X]", "[Y]", "[DX]", "[DY]"))
current_x = 0
current_y = 0
for i, stitch in enumerate(pattern.stitches):
name = decoded_name(names, stitch[2])
dx = stitch[0] - current_x
dy = stitch[1] - current_y
csv(f, ("*", str(i), name, str(stitch[0]), str(stitch[1]), str(dx), str(dy)))
current_x = stitch[0]
current_y = stitch[1]
def write_stitches(pattern, f):
names = get_common_name_dictionary()
csv(f, ("#", "[STITCH_INDEX]", "[STITCH_TYPE]", "[X]", "[Y]"))
for i, stitch in enumerate(pattern.stitches):
name = decoded_name(names, stitch[2])
csv(
f,
(
"*",
str(i),
name,
str(stitch[0]),
str(stitch[1]),
),
)
def write(pattern, f, settings=None):
version = "default"
if settings is not None:
if "deltas" in settings:
version = "delta"
elif "displacement" in settings:
version = "full"
version = settings.get("version", version)
write_data(pattern, f)
write_metadata(pattern, f)
write_threads(pattern, f)
if len(pattern.stitches) > 0:
if version == "full":
write_stitches_displacement(pattern, f)
elif version == "delta":
write_stitches_deltas(pattern, f)
else:
write_stitches(pattern, f)