-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaserproject.py
1723 lines (1195 loc) · 56.2 KB
/
laserproject.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# We will have a class that can be used to covert SVG paths to GCode
# Per path object, we can set the speed and power of the laser
# This means that the class is en essence a list of SVG objects with a speed and power
import datetime
# That would mean that there should be a class for SVG objects
import xml.etree.ElementTree as elementTree
import json
import numpy as np
from shapely import unary_union
from svgelements import SVG
from text import text_to_svg_path
from curvetopoints import quadratic_bezier
import re
MAX_POWER = 1000
# Points contains a list of points and instructions on how those points should be
# Such as if the points should be considered a fill, or a dotted line
class Points:
def __init__(self, points = None, fill = False, dotted = False):
self.points = points
self.fill = fill
self.dotted = dotted
# Need a way to serialize and deserialize this object
def to_dict(self):
return {"points": self.points, "fill": self.fill, "dotted": self.dotted}
def from_dict(self, data):
self.points = data["points"]
self.fill = data["fill"]
self.dotted = data["dotted"]
return self
# LaserProject is a collection of LaserObjects
class LaserProject:
def __init__(self):
self.laser_objects = list()
self.shapes_as_points = None
self.gcode = None
self.gcode_header = None
self.location = (0, 0)
# self.laser_mode = "M3" # M3 is constant power mode, M4 is PWM mode
# Helper function, get a string representation of the SVG
def load_from_svg_file(self, filename):
all_paths_points = extract_points_from_svg_file(filename)
new_points = list()
# the points are in mirror image, lets fix that
for path in all_paths_points:
path = [(x, 512 - y) for x, y in path]
new_points.append(path)
all_paths_points = new_points
factor = 818.677 / 216.60556
n = 1
laser_object_last = LaserObject(600, 850, 16)
lpoints = all_paths_points.pop()
lpoints = [(x / factor, y / factor) for x, y in lpoints]
laser_object_last.add_polygon(lpoints)
for path_index, points in enumerate(all_paths_points, start=1):
laser_object = LaserObject(1200, 150, 1)
# scale down the point by factor 10
points = [(x/factor, y/factor) for x, y in points]
laser_object.add_polygon(points)
laser_object.priority = n
n += 1
self.laser_objects.append(laser_object)
laser_object_last.priority = 0
self.laser_objects.append(laser_object_last)
laser_object_last_t = LaserObject(600, 850, 16)
laser_object_last_t.add_polygon(lpoints)
for path_index, points in enumerate(all_paths_points, start=1):
laser_object = LaserObject(800, 200, 1)
# scale down the point by factor 10
points = [(x/factor, y/factor) for x, y in points]
laser_object.add_polygon(points)
laser_object.priority = n
laser_object.location = (0, 138)
n += 1
self.laser_objects.append(laser_object)
laser_object_last_t.priority = 0
laser_object_last_t.location = (0, 138)
n += 1
self.laser_objects.append(laser_object_last_t)
return
# Draw all the laser objects to a canvas
def draw_laser_objects(self, canvas, scale_factor=1):
# Go through the laser objects
for laser_object in self.laser_objects:
original_location = laser_object.location
# Update the laser_object location to take in mind the location of the project
laser_object.location = (laser_object.location[0] + self.location[0], laser_object.location[1] + self.location[1])
# Get the points for the object
object_points = laser_object.get_process_points()
# Sort the object_points, so that objects that have fill "true" are drawn first
object_points = sorted(object_points, key=lambda x: x["fill"], reverse=True)
# All of these individual lists, contain individual shapes
for shape in object_points:
current_point = shape["points"][0]
for point in shape["points"]:
color = get_color_by_power(laser_object.power) if shape["fill"] else laser_object.color
line = canvas.create_line(current_point[0], current_point[1],
point[0], point[1], fill=color, width=1)
# add a tag to the line
canvas.addtag_withtag("laser_object", line)
canvas.scale(line, 0, 0, scale_factor, scale_factor)
current_point = point
# Flatten the list
object_points = [item for sublist in object_points for item in sublist["points"]]
# Calculate the max and min x and y
max_x = max([x for x, y in object_points]) * scale_factor
min_x = min([x for x, y in object_points]) * scale_factor
max_y = max([y for x, y in object_points]) * scale_factor
min_y = min([y for x, y in object_points]) * scale_factor
# Set the bounding box
laser_object.bounding_box = (min_x, min_y, max_x, max_y)
# And set the location back to what it was.
laser_object.location = original_location
return
def get_svg(self):
# First the header
print('<?xml version="1.0" encoding="UTF-8"?>')
print('<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">')
for laserObject in self.laser_objects:
svg = laserObject.get_svg_element()
if type(svg) is list:
for item in svg:
print(item)
else:
print(svg)
print("</svg>")
def get_gcode_header(self):
# Create empty gcode list
gcode = list()
# Add the header
gcode.append("; gCode created by LumenCat")
gcode.append("; setting up machine basics")
gcode.append("; G17 - XY plane")
gcode.append("; G40 - cutter compensation off")
gcode.append("; G21 - unit to mm")
gcode.append("; G54 - work coordinate system 1")
gcode.append("; G90 - absolute coordinates")
gcode.append("G17 G40 G21 G54 G90")
gcode.append("; air assist on")
gcode.append("M8")
gcode.append("; laser turned off")
gcode.append("M3 S0")
self.gcode_header = gcode
return self.gcode_header
# Convert all the laser objects to points, prior to converting to gcode
def get_all_shapes_as_points(self):
# Create empty list
self.shapes_as_points = []
# Go through the laser objects
for laser_object in self.laser_objects:
# Get the points for the object
object_points = laser_object.get_shape_as_points()
# Add the points to the list
self.shapes_as_points.append(object_points)
# Return the list
return self.shapes_as_points
# This is the original of the function
def get_all_shapes_as_cartesian_points(self):
shapes_as_cartesian_points = []
# Sort the laser_objects, by priority, highest priority first
self.laser_objects = sorted(self.laser_objects, key=lambda x: x.priority, reverse=True)
# Go through the laser objects
for laser_object in self.laser_objects:
speed = laser_object.speed
power = laser_object.power
passes = laser_object.passes
power_mode = laser_object.power_mode
point_lists = laser_object.get_cartesian_points_as_lists()
shapes_as_cartesian_points.append({"speed": speed,
"power": power,
"passes": passes,
"power_mode": power_mode,
"point_lists": point_lists})
return shapes_as_cartesian_points
def get_all_shapes_as_process_points(self):
## NOTE THIS IS STILL IN CARTESIAN COORDINATES
## Update it to use process points later
# Create empty list
shapes_as_process_points = []
# Go through the laser objects
for laser_object in self.laser_objects:
# Get the points for the object
object_points = laser_object.get_process_points()
# Add the points to the list
shapes_as_process_points.append(object_points)
# Return the list
return shapes_as_process_points
def get_gcode(self):
# Get all the shapes as
# No need, we are all cartesian now
#self.get_all_shapes_as_points()
# Account for the fact that the Y axis is inverted
# self.invert_points()
header = self.get_gcode_header()
gcode = []
gcode.extend(header)
# Get all the shapes as cartesian points
shapes_as_cartesian_points = self.get_all_shapes_as_cartesian_points()
# Go through the laser objects
for shape in shapes_as_cartesian_points:
for point_list in shape["point_lists"]:
speed = f"F{shape['speed']}"
power = f"S{shape['power']}"
passes = shape["passes"]
shape_gcode = convert_points_to_gcode(point_list)
start_gcode = shape_gcode.pop(0)
gcode.append("; Shape start")
gcode.append("; power mode")
gcode.append(shape["power_mode"])
# We should iterate the passes here
for i in range(passes):
gcode.append(f"; Pass {i+1} of {passes}")
gcode.append("; Turn laser off, go to start position")
gcode.append("S0")
gcode.append(start_gcode)
# Set the speed and power
gcode.append("; Set speed and power")
gcode.append("; Turn laser on")
on_string = "%s %s %s" % (speed, power, "M3")
gcode.append(on_string)
# Add the gcode to the list
gcode.extend(shape_gcode)
# Add the footer
gcode.append("; All done, turn laser off")
gcode.append("M5")
gcode.append("; air assist off")
gcode.append("M9")
# Got back home
gcode.append("; Go back home")
gcode.append("G0 X0 Y0")
# Return the gcode
return gcode
# Do simple, crude inversion of the Y axis
def invert_points(self):
# Now we have the maximum y and we can invert the points
for shape in self.shapes_as_points:
for points in shape["points"]:
for point_list in points:
for point in point_list:
point[1] = max_y - point[1]
return self.shapes_as_points
# Take some preconfigured paths and text, turn it
def get_max_y(self):
# Add all the points in all the shapes to a list
all_points = []
for shape in self.shapes_as_points:
for point_list in shape["points"]:
for points in point_list:
all_points.extend(points)
# We now have all the points
# Find the maximum y
max_y = 0
for point in all_points:
if point[1] > max_y:
max_y = point[1]
return max_y
def load_test_project(self):
# Clear out the laser objects
self.laser_objects = []
# Lets create a settings list, items of speed, power, passes
settings = [(600, 700, 10),
(200, 800, 4),
(400, 700, 8),
(1200, 400, 20)]
n = 0
for setting in settings:
laser_objects = self.small_material_test(setting[0], setting[1], setting[2])
laser_objects[0].translate(0, n * 20)
laser_objects[1].translate(0, n * 20)
self.laser_objects.append(laser_objects[0])
self.laser_objects.append(laser_objects[1])
n+=1
pass
# Create a small square, and some text
def material_test(self, speed, power):
# Add a simple rectangle
laser_object = LaserObject(speed, power)
laser_object.add_rectangle(2, 2, 21, 21)
# Add a some text
text = (f"Speed {speed}\n"
f"Power {power}\n"
"Passes 1")
# Writing has default speed/power 600-250
laser_text_object = LaserTextObject(text, "../fonts/UbuntuMono-Regular.ttf", 14, 600, 250)
laser_text_object.location = (25, 6)
return laser_object, laser_text_object
def small_material_test(self, speed, power, passes):
# Add a simple rectangle
laser_object = LaserObject(speed, power, passes)
# laser_object.add_rectangle(0, 0, 7, 7)
laser_object.add_rounded_rectangle(10, 10, 10, 10)
laser_object.add_circle(10, 10, 9)
# Add a some text
text = (f"{speed}\n{int(power/10)}.{passes}")
# Writing has default speed/power 600-250
laser_text_object = LaserTextObject(text, "../fonts/UbuntuMono-Regular.ttf", 9, 600, 250, 1)
laser_text_object.location = (6, 7.5)
return laser_object, laser_text_object
# A LaserObject is a points and shapes based object, that can be converted to GCode
class LaserObject:
def __init__(self, speed, power, passes, svg_element = None):
self.svg_element = svg_element
self.speed = speed
self.power = power
self.passes = passes
self.path = None
self.points = None
self.priority = 0
self.shape_gcode = None
self.power_mode = "M3"
self.color = "black"
# These are a list of points, that are in cartesian coordinates
# All other data is derived from this
# An object can have multiple points lists
self.shapes = list()
# This is the origin of the laser object, all other cartesian points are relative to this
self.location = (0, 0)
# This is the bounding box of the object, used to display the item when it is selected
self.bounding_box = (0,0,0,0)
# save the shapes to json file
def save_shapes(self, filename):
shapes = list()
for shape in self.shapes:
shapes.append(shape.to_dict())
# Save the shapes to a file
with open(filename, "w") as file:
json.dump(shapes, file)
def load_shapes(self, filename):
# Load the shapes from a file
with open(filename, "r") as file:
shapes = json.load(file)
for shape in shapes:
new_shape = Points().from_dict(shape)
self.shapes.append(new_shape)
def get_info(self):
# create a string with the information, power, speed and passes
info = f"Speed: {self.speed}, Power: {self.power}, Passes: {self.passes}"
return info
def dot_the_lines(self):
# For all the shapes I have, convert them into dotted lines.
# Make it so that each line is cut up into 2mm segments
# And then add those segments to the shapes list
# Create a whole new shapes list, to replace the old one
new_shapes = list()
for shape in self.shapes:
points = break_lines(shape.points, 2)
for line in points:
new_shape = Points(line, shape.fill, True)
new_shapes.append(new_shape)
self.shapes = new_shapes
def translate(self, x, y ):
x = self.location[0] + x
y = self.location[1] + y
self.location = (x, y)
def center(self):
# Go through all the coordinates in the shapes
# Find the max and min x and y
all_points = []
for shape in self.shapes:
# Get all the points
all_points += [point for point in shape.points]
# Find the max and min x and y
max_x = max([x for x, y in all_points])
min_x = min([x for x, y in all_points])
max_y = max([y for x, y in all_points])
min_y = min([y for x, y in all_points])
# Calculate the center of the bounding box
center_x = (max_x - min_x) / 2
center_y = (max_y - min_y) / 2
# Calculate the offset
offset_x = min_x - center_x
offset_y = min_y - center_y
# self.add_polygon([[min_x, min_y], [max_x, max_y]])
# self.add_polygon([[min_x, max_y], [max_x, min_y]])
# self.add_polygon([[center_x, min_y], [center_x, 20]])
# self.add_rectangle(min_x, min_y, max_x - min_x, max_y - min_y)
# Translate the object
self.translate(offset_x, offset_y)
def get_cartesian_points_as_lists(self):
# This will return a copy of the list, so multiple point lists can be in the same shape
# And it can be mutated without affecting the original
point_lists = list()
for shape in self.shapes:
cartesian_points = list()
for point in shape.points:
x = point[0] + self.location[0]
y = point[1] + self.location[1]
cartesian_points.append((x, y))
point_lists.append(cartesian_points)
return point_lists
# This is a helper function, that will return the points in a proces point format
# Currently without regard for max_y
def get_process_points(self):
## NOTE THIS IS STILL IN CARTESIAN COORDINATES
## Update it to use process points later?
## Or just use cartesian points, and to this somewhere else
# Create empty list
process_points = list()
for shape in self.shapes:
shape_points = []
for point in shape.points:
# Adjust location and flip around the Y-axis in one step
x = point[0] + self.location[0]
y = 400 - (point[1] + self.location[1]) # Assuming 400 is the Y-axis flip value
shape_points.append([x, y])
# make a dict, result with points and fill
result = dict()
result["points"] = shape_points
result["fill"] = shape.fill
process_points.append(result)
return process_points
def get_path(self):
# We have an SVG element, it is already unpacked
# So the root should contain the data
root = elementTree.fromstring(self.svg_element)
try:
path = root.attrib["d"]
return path
except elementTree.ParseError:
# Raise an error maybe
pass
def get_svg_element(self):
return self.svg_element
def convert_to_points(self):
# The whole shape is a list of points
# Each point is tuple of 2 elements, x and y
self.path = self.get_path()
self.points = convert_path_to_points(self.path)
return self.points
def get_shape_as_points(self):
# This will return a list
# Get the points
self.convert_to_points()
points_list = list()
points_list.extend(self.points)
return points_list
def convert_to_gcode(self):
self.shape_gcode = convert_points_to_gcode(self.points)
return self.shape_gcode
# These are functino that add shapes to the laser object
# Considering making separate classes for each shape, but for now they are all just paths
# The rectangle is defined by the bottom left corner, and the width and height
def add_rectangle(self, x, y, width, height):
# Create a points object
points = Points()
# Create a list of points
points.points = list()
# Bottom left corner
points.points.append((x, y))
# Top left corner
points.points.append((x, y + height))
# Top right corner
points.points.append((x + width, y + height))
# Bottom right corner
points.points.append((x + width, y))
# And close the shape
points.points.append((x, y))
# Add this circle to the list of shapes
self.shapes.append(points)
return
def add_rounded_rectangle(self, x_center, y_center, width, height, radius=3):
import numpy as np
"""
Generate points on the boundary of a rounded rectangle.
Parameters:
x_center (float): The x-coordinate of the center of the rectangle.
y_center (float): The y-coordinate of the center of the rectangle.
width (float): The width of the rectangle.
height (float): The height of the rectangle.
radius (float): The radius of the corners.
points_per_corner (int): Number of points to generate on each corner.
Returns:
list of tuples: List of (x, y) coordinates of the points on the boundary.
"""
points_per_corner = 16
# Calculate corner centers
half_width, half_height = width / 2, height / 2
corner_centers = [
(x_center - half_width + radius, y_center - half_height + radius), # top-left
(x_center + half_width - radius, y_center - half_height + radius), # top-right
(x_center + half_width - radius, y_center + half_height - radius), # bottom-right
(x_center - half_width + radius, y_center + half_height - radius), # bottom-left
]
# Generate points for each corner
points = []
for (cx, cy), start_angle in zip(corner_centers, [np.pi, 1.5 * np.pi, 0, 0.5 * np.pi]):
angles = np.linspace(start_angle, start_angle + np.pi / 2, points_per_corner, endpoint=False)
x_points = cx + radius * np.cos(angles)
y_points = cy + radius * np.sin(angles)
points.extend(list(zip(x_points, y_points)))
# Take the last point, and add it to the beginning
points.append(points[0])
points_object = Points(points)
# Add this circle to the list of shapes
self.shapes.append(points_object)
return
def add_polygon(self, points):
points_object = Points(points)
# Add this circle to the list of shapes
self.shapes.append(points_object)
return
def add_circle(self, x_center, y_center, radius):
import numpy as np
"""
Generate points on the circumference of a circle.
Parameters:
x_center (float): The x-coordinate of the center of the circle.
y_center (float): The y-coordinate of the center of the circle.
radius (float): The radius of the circle.
num_points (int): Number of points to generate on the circumference.
Returns:
list of tuples: List of (x, y) coordinates of the points on the circumference.
"""
num_points = 128
angles = np.linspace(0, 2 * np.pi, num_points, endpoint=False)
x_points = x_center + radius * np.cos(angles)
y_points = y_center + radius * np.sin(angles)
points = list(zip(x_points, y_points))
# Take the last point, and add it to the beginning
points.append(points[0])
points_object = Points(points)
# Add this circle to the list of shapes
self.shapes.append(points_object)
return
# Fill the shapes with ilnes
def fill(self):
# Initialize the list to store start and end coordinates of lines
lines = []
step_size = 0.25
step_size = 0.085
# step_size = 0.1
step_size = 0.15
# Get all the points
for shape in self.shapes:
polygon = shape.points.copy()
polygon.pop()
# Extracting the ymin and ymax values from the polygon vertices
y_values = [point[1] for point in polygon]
ymin, ymax = min(y_values), max(y_values)
# Iterate through each y value from ymin to ymax
for y in np.arange(ymin, ymax + step_size, step_size):
intersections = []
# Find intersections with the polygon edges
for i in range(len(polygon)):
start, end = polygon[i], polygon[(i + 1) % len(polygon)]
# Skip horizontal edges
if start[1] == end[1]:
continue
# Ensure that the scanline intersects the edge vertically
if (y >= start[1] and y < end[1]) or (y >= end[1] and y < start[1]):
# Calculate the x value of the intersection using linear interpolation
x = start[0] + (y - start[1]) * (end[0] - start[0]) / (end[1] - start[1])
intersections.append(x)
# Sort the intersections to ensure correct filling
intersections.sort()
# Fill between each pair of intersections
for i in range(0, len(intersections), 2):
if i + 1 < len(intersections): # Ensure there's a pair
start = (intersections[i], y)
stop = (intersections[i + 1], y)
lines.append([start, stop])
lines = greedy_draw(lines)
# Remove the old shapes
self.shapes = []
for line in lines:
points_object = Points(line, fill=True)
self.shapes.append(points_object)
return
# A derived class for Text objects, based on LaserObject
class LaserTextObject(LaserObject):
# This class has a font size, text and font
def __init__(self, text, font, font_size, speed, power, passes):
self.text = str(text)
self.font = font
self.font_size = font_size
# Call the parent constructor
super().__init__(speed, power, passes)
# This is the laser TextObject of the function
def get_cartesian_points_as_lists(self):
# Convert the text to a list of SVG paths
letter_paths = text_to_svg_path(self.text, self.font, self.font_size)
# Make empty process points list
process_points = list()
letter_objects = list()
letter_process_points_list = list()
# Get all the letters
for letter_path in letter_paths:
# Create a LaserObject
letter_object = LaserObject(self.speed, self.power, self.passes, letter_path)
letter_object.location = self.location
# Convert the paths to points
letter_process_points = letter_object.get_shape_as_points()
letter_process_points_list.append(letter_process_points)
letter_objects.append(letter_object)
# Get the max_y over all the letters
max_y = 0
for shape in letter_process_points_list:
for list_of_points in shape:
for point in list_of_points:
if point[1] > max_y:
max_y = point[1]
# A this point, we have cartesian points for each letter, as LaserObjects
# Convert all the letters to cartesian points
for letter_object in letter_objects:
# Get the process points for the letter
letter_process_points = letter_object.get_shape_as_points()
# These are process points, we need to convert them to cartesian points
letter_process_points = convert_process_to_cartesian(letter_process_points, max_y = max_y)
# Add those points to the shapes list
letter_object.shapes.extend(letter_process_points)
# And for all those points, fix location
for shape in letter_process_points:
point_list = list()
for point in shape:
x = point[0] + self.location[0]
y = point[1] + self.location[1]
point_list.append((x, y))
process_points.append(point_list)
return process_points
# This overloaded function takes into account that the text is converted to SVG glyphs first
# Then those pats, are converted to process points
# And finally those process points are converted to cartesian points
def get_process_points(self):
# Convert to a regular laser object
laser_object = self.convert_to_laser_object()
# Return the process points
return laser_object.get_process_points()
def curve_points(self):
center = (0.75*25, 0.75*25) # Circle center
radius = 26 # Circle radius
radius = 18.5 / 2 + 1
# Now convert these polygons to a circle thing
# circle_polygons = distribute_polygons_around_circle(polygons, center,radius)
polygon_groups = list()
for letter in polygons:
group = list()
for polygon in letter:
group.append(Polygon(polygon))
polygon_groups.append(group)
#circle_polygons = distribute_groups_around_circle(polygon_groups, center, radius)
circle_polygons = curve_polygons_around_circle(polygon_groups, center, radius)
#circle_polygons = curve_groups_around_circle(polygon_groups, center, radius)
# laser_object.add_circle(75, 75, radius)
# At this point, I have all the letters, as individual polygons.
for letter in polygons:
for polygon in letter:
pass
laser_object.add_polygon(polygon)
for group in circle_polygons:
for polygon in group:
laser_object.add_polygon(polygon.exterior.coords)
laser_object.location = self.location
self.alt_laser_object = laser_object
# Get the points
return laser_object.get_process_points()
##
def convert_to_laser_object(self):
# Convert the text to a list of SVG paths
letter_paths = text_to_svg_path(self.text, self.font, self.font_size)
# Make empty process points list
process_points = list()
letter_objects = list()
letter_process_points_list = list()
# Get all the letters
for letter_path in letter_paths:
# Create a LaserObject
letter_object = LaserObject(self.speed, self.power, self.passes, letter_path)
letter_object.location = self.location
# Convert the paths to points
letter_process_points = letter_object.get_shape_as_points()
letter_process_points_list.append(letter_process_points)
letter_objects.append(letter_object)
# Get the max_y over all the letters
max_y = 0
for shape in letter_process_points_list:
for list_of_points in shape:
for point in list_of_points:
if point[1] > max_y:
max_y = point[1]
polygons = list()
# Convert all the letters to cartesian points
for letter_object in letter_objects:
# Get the process points for the letter
letter_process_points = letter_object.get_shape_as_points()
# Really confused here!
# These are process points, we need to convert them to cartesian points
letter_process_points = convert_process_to_cartesian(letter_process_points, max_y=max_y)
# Add those to the polygons
polygons.append(letter_process_points)
# Create a laserobject to give all these polygons to!