-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsvgtogcode.py
615 lines (445 loc) · 19.7 KB
/
svgtogcode.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
from typing import Any
import lxml.etree as ET
import math
from datetime import datetime
import sys
import argparse
import shapely
import numpy as np
from shapely import LineString, Point
DEFAULT_INPUT_FILENAME = "world.svg"
DEFAULT_MAX_LENGTH_SEGMENT = 30 # in m
# FILTER_BY_LAYER = ["coastlines"]
# FILTER_BY_LAYER = ["coastlines_hatching"]
# FILTER_BY_LAYER = ["places"]
# FILTER_BY_LAYER = ["places_circles"]
# FILTER_BY_LAYER = ["bathymetry"]
# FILTER_BY_LAYER = ["terrain"]
# FILTER_BY_LAYER = ["meta"]
OFFSET = [0, 0] #[-1425, -000]
# Rotate by 90 degrees
ROTATE_90 = False
TRAVEL_SPEED = 6000
WRITE_SPEED = 5000
PEN_LIFT_SPEED = 2000
COMP_TOLERANCE = 0.9
MIN_LINE_LENGTH = 0.75 # in mm
PEN_UP_DISTANCE = 2
CMD_MOVE = "G1 X{0:.3f} Y{1:.3f}\n"
CMD_PEN_UP = "G1 Z{} F{}\n".format(PEN_UP_DISTANCE, PEN_LIFT_SPEED)
OPTIMIZE_ORDER = True
# np.set_printoptions(precision=4,
# threshold=10000,
# linewidth=150)
np.set_printoptions(suppress=True)
def process_count(e: Any, default_namespace: str) -> int:
if e.tag == default_namespace + "rect":
return 4
if e.tag == default_namespace + "line":
return 1
if e.tag == default_namespace + "path":
d = e.attrib["d"]
d = d[1:] # cut off the M
return len(d.split("L")) - 1
return 0
def process(e: Any, default_namespace: str) -> list[tuple[float, float, float, float]]:
lines = []
if e.tag == default_namespace + "rect":
x1 = float(e.attrib["x"])
y1 = float(e.attrib["y"])
x2 = x1 + float(e.attrib["width"])
y2 = y1 + float(e.attrib["height"])
lines.append([x1, y1, x1, y2]) # left
lines.append([x1, y1, x2, y1]) # top
lines.append([x1, y2, x2, y2]) # bottom
lines.append([x2, y1, x2, y2]) # right
return lines
if e.tag == default_namespace + "line":
lines.append([float(e.attrib["x1"]), float(e.attrib["y1"]), float(e.attrib["x2"]), float(e.attrib["y2"])])
return lines
if e.tag == default_namespace + "path":
d = e.attrib["d"]
d = d[1:] # cut off the M
segments = d.split("L")
l = []
for s in segments:
pairs = s.split(" ")
l.append([float(pairs[0]), float(pairs[1])])
for i in range(1, len(l)):
lines.append([l[i-1][0], l[i-1][1], l[i][0], l[i][1]])
return lines
if e.tag == default_namespace + "circle":
print("invalid element: {}".format(e.tag))
return lines
if e.tag == default_namespace + "image":
print("invalid element: {}".format(e.tag))
return lines
print("unknown element: {}".format(e.tag))
return lines
def compare_equal(e0, e1):
if math.isclose(e0[0], e1[0], abs_tol=COMP_TOLERANCE):
if math.isclose(e0[1], e1[1], abs_tol=COMP_TOLERANCE):
return True
return False
# from: https://stackoverflow.com/a/34325723
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"input_filename",
default=DEFAULT_INPUT_FILENAME,
)
parser.add_argument(
"--crop",
nargs="*",
type=int,
help="crop: center-X center-Y width height"
)
parser.add_argument(
"--max-length-segment",
type=int,
default=DEFAULT_MAX_LENGTH_SEGMENT,
help="maximum length of segment [m]"
)
parser.add_argument(
"--filter-layer",
type=str,
default=None,
help="filter layers by name"
)
parser.add_argument(
"--high-precision",
action="store_true",
help="high precision mode"
)
parser.add_argument(
"--limit",
type=int,
default=0,
help="process only the first n lines"
)
args = parser.parse_args()
crop_region = None
if args.crop is not None:
if len(args.crop) != 4:
print(f"invalid crop arguments: {args.crop}. Must be exactly four int values: [center-X, center-Y, widht, height]")
sys.exit(2)
crop_region = shapely.box(
args.crop[0]-args.crop[2]//2,
args.crop[1]-args.crop[3]//2,
args.crop[0]+args.crop[2]//2,
args.crop[1]+args.crop[3]//2,
)
args.max_length_segment *= 100*10 # m to mm
tree = ET.parse(args.input_filename)
root = tree.getroot()
svg_default_namespace = "{" + root.nsmap[None] + "}"
svg_inkscape_namespace = "{" + root.nsmap["inkscape"] + "}"
output_filename = f"map_layer_{args.filter_layer}"
size = [root.get("width"), root.get("height")]
for i, dim in enumerate(size):
if dim.endswith("px") or dim.endswith("mm"):
dim = dim[:-2]
dim = int(dim)
if dim is None or dim <= 0:
print(f"SVG size attributes not correct ({size})")
exit(-1)
size[i] = dim
if args.high_precision:
print("set to high precision mode")
COMP_TOLRANCE = 0.001
MIN_LINE_LENGTH = 0.1
all_lines = []
for layer in root.findall("g", root.nsmap):
if args.filter_layer is not None:
if layer.attrib["id"] != args.filter_layer:
print(f"skip layer {layer.attrib['id']}")
continue
line_count = 0
for child in layer:
line_count += process_count(child, svg_default_namespace)
all_lines = np.zeros([line_count, 4], dtype=np.float64)
fill_index = 0
for i, child in enumerate(layer):
if i%100 == 0:
printProgressBar(i, len(layer), prefix=f"process layer: {layer.attrib['id']:<25}")
lines = process(child, svg_default_namespace)
for line in lines:
all_lines[fill_index, :] = line
fill_index += 1
all_lines = all_lines.tolist()
print("")
if args.limit > 0:
limit = min(len(all_lines), args.limit)
print(f"processing limited to {limit} lines")
all_lines = all_lines[0:limit]
if crop_region is not None:
cropped_lines = []
crop_translation = crop_region.bounds[0:2]
for i, line in enumerate(all_lines):
if i%100 == 0:
printProgressBar(i, len(all_lines), prefix="cropping")
ls = LineString([line[0:2], line[2:4]])
result = crop_region.intersection(ls)
if result.is_empty:
continue
match result:
case Point():
continue
case LineString():
result = shapely.affinity.translate(
result,
xoff=-crop_translation[0],
yoff=-crop_translation[1]
)
cropped_lines.append([
result.coords[0][0],
result.coords[0][1],
result.coords[1][0],
result.coords[1][1],
])
case _:
print(f"cropping: unexpected shapely geometry: {type(result)}")
print("")
all_lines = cropped_lines
print(" ")
print("--------------------------------------------------")
print(" ")
number_of_lines = len(all_lines)
print("number of lines: {}".format(number_of_lines))
if number_of_lines == 0:
exit(0)
# ------------------------------------------------------------------------------------
# filter duplicates
nplines = np.array(all_lines, dtype=float)
unique = np.unique(nplines, axis=0)
number_duplicates = len(all_lines) - unique.shape[0]
print("cleaned duplicates: {0} | duplicate ratio: {1:.2f}%".format(number_duplicates, (number_duplicates/len(all_lines))*100))
nplines = unique
# ------------------------------------------------------------------------------------
# filter tiny lines
# distances = np.sqrt(np.add(np.power(np.subtract(nplines[:, 0], nplines[:, 2]), 2), np.power(np.subtract(nplines[:, 1], nplines[:, 3]), 2)))
# indices_shortlines = np.where(distances < MIN_LINE_LENGTH)[0]
# nplines = np.delete(nplines, indices_shortlines, axis=0)
# print("cleaned short lines: {0} | short line ratio: {1:.2f}%".format(indices_shortlines.shape[0], (indices_shortlines.shape[0]/len(all_lines))*100))
# evil ... breaks paths without pen up/down events in two and creates gaps
# ------------------------------------------------------------------------------------
# mirror along X-axis to transfer SVG coordinate system (0 top left) to gcode (0 bottom left)
maxy = size[1] #np.max([np.max(nplines[:, 1]), np.max(nplines[:, 3])])
if crop_region is not None:
maxy = crop_region.bounds[3] - crop_region.bounds[1]
print(maxy)
nplines[:, 1] = np.multiply(nplines[:, 1], -1)
nplines[:, 3] = np.multiply(nplines[:, 3], -1)
nplines[:, 1] = np.add(nplines[:, 1], maxy)
nplines[:, 3] = np.add(nplines[:, 3], maxy)
# ------------------------------------------------------------------------------------
# optimize drawing order. greedy (and inefficient)
ordered_lines = None
if OPTIMIZE_ORDER:
timer = datetime.now()
indices_done = [0]
# indices_done_mask = np.zeros(nplines.shape, dtype=bool)
# indices_done_mask[indices_done, :] = True
indices_done_mask = np.zeros(nplines.shape[0], dtype=bool)
indices_done_mask[indices_done] = True
ordered_lines = [nplines[0, :]]
# nplines_masked = np.ma.masked_array(nplines, mask=indices_done_mask)
for i in range(0, nplines.shape[0]):
if i%100 == 0:
# print("{0:.2f}".format((len(ordered_lines)/nplines.shape[0])*100.0), end="\r")
printProgressBar(len(ordered_lines), nplines.shape[0], prefix="optimize order")
last = ordered_lines[-1]
indices_done_mask[indices_done] = True
# indices_done_mask[indices_done, :] = True
# pythagorean distance
# distance_forw = np.sqrt(np.add(np.power(np.subtract(nplines[:, 0], last[2]), 2), np.power(np.subtract(nplines[:, 1], last[3]), 2)))
# distance_forw_masked = np.ma.masked_array(distance_forw, mask=indices_done_mask)
# distance_forw_min = np.argmin(distance_forw_masked)
# distance_back = np.sqrt(np.add(np.power(np.subtract(nplines[:, 2], last[2]), 2), np.power(np.subtract(nplines[:, 3], last[3]), 2)))
# distance_back_masked = np.ma.masked_array(distance_back, mask=indices_done_mask)
# distance_back_min = np.argmin(distance_back_masked)
# if distance_forw[distance_forw_min] < distance_back[distance_back_min]:
# indices_done.append(distance_forw_min)
# ordered_lines.append(nplines[distance_forw_min, :])
# else:
# indices_done.append(distance_back_min)
# flip = nplines[distance_back_min, :]
# ordered_lines.append(np.array([flip[2], flip[3], flip[0], flip[1]]))
# manhattan distance
# mnplines = np.ma.masked_array(nplines, mask=indices_done_mask, axis=0)
distance_forw = np.add(np.abs(np.subtract(nplines[:, 0], last[2])), np.abs(np.subtract(nplines[:, 1], last[3])))
distance_forw = np.ma.masked_array(distance_forw, mask=indices_done_mask)
distance_forw_min = np.argmin(distance_forw)
distance_back = np.add(np.abs(np.subtract(nplines[:, 2], last[2])), np.abs(np.subtract(nplines[:, 3], last[3])))
distance_back = np.ma.masked_array(distance_back, mask=indices_done_mask)
distance_back_min = np.argmin(distance_back)
if distance_forw[distance_forw_min] < distance_back[distance_back_min]:
indices_done.append(distance_forw_min)
ordered_lines.append(nplines[distance_forw_min, :])
else:
indices_done.append(distance_back_min)
flip = nplines[distance_back_min, :]
ordered_lines.append(np.array([flip[2], flip[3], flip[0], flip[1]]))
print("")
print("optimization done. time: {0:.2f}s".format((datetime.now()-timer).total_seconds()))
else:
ordered_lines = nplines
# ------------------------------------------------------------------------------------
# filter tiny edges/leaves/whatever (small lines which are not connected)
nplines = np.array(ordered_lines, dtype=float)
distances = np.sqrt(np.add(np.power(np.subtract(nplines[:, 0], nplines[:, 2]), 2), np.power(np.subtract(nplines[:, 1], nplines[:, 3]), 2)))
indices_shortlines = np.where(distances < MIN_LINE_LENGTH)[0]
unconnected_indices = []
for i in range(1, nplines.shape[0]-1):
prv = nplines[i-1, :]
cur = nplines[i , :]
nxt = nplines[i+1, :]
if not prv[2] == cur[0] or not prv[3] == cur[1] or not cur[2] == nxt[0] or not cur[3] == nxt[1]:
if i in indices_shortlines:
unconnected_indices.append(i)
nplines = np.delete(nplines, unconnected_indices, axis=0)
ordered_lines = nplines
print("cleaned unconnected short lines: {0} | short line ratio: {1:.2f}%".format(len(unconnected_indices), (len(unconnected_indices)/len(all_lines))*100))
# ------------------------------------------------------------------------------------
# ordered_lines = []
# ordered_lines.append(all_lines[0])
# all_lines.remove(ordered_lines[0])
# while(len(all_lines) > 0):
# print("{0:.2f}".format((len(ordered_lines)/number_of_lines)*100.0))
# src = ordered_lines[-1][1]
# dst = all_lines[0]
# candidate = dst
# candidate_distance = distance(src, candidate[0])
# candidateFlip = False
# candidate_i = 0
# for i in range(0, len(all_lines)):
# dst = all_lines[i]
# distance0 = distance(src, dst[0])
# distance1 = distance(src, dst[1])
# if distance0 < 0.001:
# candidate = dst
# candidateFlip = False
# candidate_i = i
# break
# if distance1 < 0.001:
# candidate = dst
# candidateFlip = True
# candidate_i = i
# break
# if distance0 < candidate_distance:
# candidate = dst
# candidate_distance = distance0
# candidateFlip = False
# candidate_i = i
# if distance1 < candidate_distance:
# candidate = dst
# candidate_distance = distance1
# candidateFlip = True
# candidate_i = i
# # print("{}|{} {}".format(candidate_i, len(all_lines), candidateFlip))
# if candidateFlip:
# ordered_lines.append([candidate[1], candidate[0]])
# else:
# ordered_lines.append(candidate)
# all_lines.pop(candidate_i)
# print("number of ordered_lines: {}".format(len(ordered_lines)))
# print(order_index)
segments = [[]]
number_lines = len(ordered_lines)
total_length_segment = 0
for i in range(0, number_lines):
dist = math.sqrt(
(ordered_lines[i][2]-ordered_lines[i][0])**2 + (ordered_lines[i][3]-ordered_lines[i][1])**2
)
if (dist + total_length_segment) > args.max_length_segment:
segments.append([])
print("new segment [{:5.2f}m]".format(total_length_segment/1000))
total_length_segment = 0
else:
total_length_segment += dist
segments[-1].append(ordered_lines[i])
print("last segment [{:5.2f}m]".format(total_length_segment/1000))
count_pen_up = 0
count_pen_down = 0
count_draw_moves = 0
count_travel_moves = 0
state_pen_up = True
for s in range(0, len(segments)):
segment = segments[s]
filename = output_filename + f"_{s+1}of{len(segments)}.nc"
with open(filename, "w") as out:
out.write("G90\n") # absolute positioning
out.write("G21\n") # Set Units to Millimeters
out.write(CMD_PEN_UP) # move pen up
out.write(f"G1 F{TRAVEL_SPEED}\n") # Set feedrate to TRAVEL_SPEED mm/min
state_pen_up = True
out.write("\n")
count_pen_up += 1
number_lines = len(segment)
for i in range(0, number_lines):
line = segment[i]
line_next = None
if (i + 1) < number_lines:
line_next = segment[i+1]
if ROTATE_90:
out.write(CMD_MOVE.format(line[1]+OFFSET[1], (line[0]+OFFSET[0]) * -1 + size[0]))
else:
out.write(CMD_MOVE.format(line[0]+OFFSET[0], line[1]+OFFSET[1]))
# pen down
if (state_pen_up):
out.write(f"G1 Z0 F{PEN_LIFT_SPEED}\n")
out.write(f"G1 F{WRITE_SPEED}\n")
state_pen_up = False
count_travel_moves += 1
count_pen_down += 1
else:
count_draw_moves += 1
if ROTATE_90:
out.write(CMD_MOVE.format(line[3]+OFFSET[1], (line[2]+OFFSET[0]) * -1 + size[0]))
else:
out.write(CMD_MOVE.format(line[2]+OFFSET[0], line[3]+OFFSET[1]))
count_draw_moves += 1
move_pen_up = True
if line_next is not None:
if math.isclose(line[2], line_next[0], abs_tol=COMP_TOLERANCE):
if math.isclose(line[3], line_next[1], abs_tol=COMP_TOLERANCE):
move_pen_up = False
if move_pen_up:
out.write(CMD_PEN_UP)
out.write(f"G1 F{TRAVEL_SPEED}\n")
out.write("\n")
state_pen_up = True
count_pen_up += 1
out.write(CMD_PEN_UP)
out.write(f"G1 F{TRAVEL_SPEED}\n")
out.write("G1 X0 Y0\n")
count_pen_up += 1
# Lower pen (will fall down anyway when motor is turned off)
# out.write("G1 Z0 F{}\n".format(PEN_LIFT_SPEED))
# count_pen_down += 1
print(f"write segment {s+1}/{len(segments)}: {filename}")
print(f"count_pen_up: {count_pen_up}")
print(f"count_pen_down: {count_pen_down}")
print(f"count_draw_moves: {count_draw_moves}")
print(f"count_travel_moves: {count_travel_moves}")
print(f"ratio draw/travel: {float(count_draw_moves)/float(count_travel_moves):6.3f}")