-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaser.py
More file actions
559 lines (457 loc) · 20.5 KB
/
laser.py
File metadata and controls
559 lines (457 loc) · 20.5 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
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
# Class to handle laser objects
# These are cuts or etches
# Typically cuts will be lines which cut fully through the material
# whereas etches are shapes (rect / polygons)
# Child classes are created for both Cut and Etch to make it easier
# to create
# all dimensions are in mm (convert using scale if required)
# internal_offset if used is for relative to wall
# eg position of top left of feature
# Standard methods uses scale class for scaling
# Methods with _screen return using vs (zoom level)
from viewscale import ViewScale
class Laser():
# Scale convertor set as a class variable
# Set once during app startup and then can use for all subclasses
# Alternative to setting up a singleton
sc = None
# Also has view scale
vs = ViewScale()
def __init__(self, type, internal_offset):
self.type = type
self.io = internal_offset
def get_type(self):
return self.type
# Call this once to pass the scale object
def set_scale_object(self, sc):
Laser.sc = sc
def set_internal_offset(self, internal_offset):
self.io = internal_offset
# If want to change scale use this - not the set_scale_object
# Provide as the scale name (eg. OO)
# Returns scale if success or None
def set_scale(self, scale):
return self.sc.set_scale(scale)
class Cut(Laser):
def __init__(self, type, internal_offset):
super().__init__(type, internal_offset)
# Start and end are tuples
class CutLine(Cut):
def __init__(self, start, end, internal_offset=(0,0)):
self.start = start
self.end = end
super().__init__("line", internal_offset)
# unformat returns (type, [list_of_values])
def unformat(self):
return (("line", (self.start, self.end)))
def __str__(self):
return f'Cut {self.type} from {self.start} to {self.end} io {self.io}'
# Get start value converted by scale and into pixels
# If supplied offset is in pixels relative to start of object
def get_start_pixels(self, offset=(0,0)):
# Add internal offset to offset
start_io = (self.start[0]+self.io[0], self.start[1]+self.io[1])
start_pixels = Laser.sc.convert(start_io)
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
def get_start_pixels_screen(self, offset=(0,0)):
# Add internal offset to offset
start_io = (self.start[0]+self.io[0], self.start[1]+self.io[1])
start_pixels = Laser.vs.convert(start_io)
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
# Get end value converted by scale and into pixels
# If supplied offset is in pixels relative to start of object
def get_end_pixels(self, offset=(0,0)):
end_io = (self.end[0]+self.io[0], self.end[1]+self.io[1])
end_pixels = Laser.sc.convert(end_io)
# Add offset
return ([end_pixels[0]+offset[0], end_pixels[1]+offset[1]])
def get_end_pixels_screen(self, offset=(0,0)):
end_io = (self.end[0]+self.io[0], self.end[1]+self.io[1])
end_pixels = Laser.vs.convert(end_io)
# Add offset
return ([end_pixels[0]+offset[0], end_pixels[1]+offset[1]])
class CutRect(Cut):
def __init__(self, start, size, internal_offset=(0,0)):
self.start = start
self.size = size
super().__init__("rect", internal_offset)
# uformat returns (type, [list_of_values])
def unformat(self):
return (("rect", (self.start, self.size)))
def __str__(self):
return f'Cut {self.type} from {self.start} size {self.size} io {self.io}'
def get_start_pixels(self, offset=(0,0)):
start_io = ([self.start[0]+self.io[0], self.start[1]+self.io[1]])
start_pixels = Laser.sc.convert(start_io)
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
def get_start_pixels_screen(self, offset=(0,0)):
start_io = ([self.start[0]+self.io[0], self.start[1]+self.io[1]])
start_pixels = Laser.vs.convert(start_io)
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
# Note that size does not need offset
def get_size_pixels(self):
return Laser.sc.convert(self.size)
def get_size_pixels_screen(self):
return Laser.vs.convert(self.size)
class CutPolygon(Cut):
def __init__(self, points, internal_offset=(0,0)):
self.points = points
super().__init__("polygon", internal_offset)
# unformat returns (type, [list_of_values])
def unformat(self):
return (("polygon", self.points))
# Add internal offset to points
def get_points(self):
new_points = []
for point in self.points:
new_points.append((point[0]+self.io[0], point[1]+self.io[1]))
return new_points
# Offset is applied to all points
def get_points_pixels(self, offset=(0,0)):
new_points = []
for point in self.points:
sc_point = Laser.sc.convert((point[0]+self.io[0], point[1]+self.io[1]))
new_points.append([(offset[0]+sc_point[0]),(offset[1]+sc_point[1])])
return new_points
def get_points_pixels_screen(self, offset=(0,0)):
new_points = []
for point in self.points:
sc_point = Laser.vs.convert((point[0]+self.io[0], point[1]+self.io[1]))
new_points.append([(offset[0]+sc_point[0]),(offset[1]+sc_point[1])])
return new_points
# type could be "line" / "rect" etc.
class Etch(Laser):
def __init__(self, type, internal_offset):
super().__init__(type, internal_offset)
# Strength is added by the subclass
strength = None
def get_strength(self):
return self.strength
# Start and end are tuples
class EtchLine(Etch):
# Default etch width if none others set on individual class
# Used by etch lines only, but can be accessed by all instances
global_etch_width = 10
def __init__(self, start, end, internal_offset=(0,0), strength=5, etch_width=None):
self.strength = strength
self.start = start
self.end = end
# how wide to cut (cannot have line as lightburn doesn't like it)
# If set to None (default) then look at class variable global_etch_width
self.etch_width = etch_width
super().__init__("line", internal_offset)
# Returns as [(x,y) (x,y)]
def get_line(self):
return ([self.get_start(), self.get_end()])
# unformat returns (type, [list_of_values])
def unformat(self):
return (("line", (self.start, self.end)))
def __str__(self):
return f'Etch {self.type} from {self.start} to {self.end} io {self.io}'
def get_start(self):
return (self.start[0]+self.io[0], self.start[1]+self.io[1])
# Get start value converted by scale and into pixels
# If supplied offset is in pixels relative to start of object
def get_start_pixels(self, offset=(0,0)):
# Add internal offset to offset
start_pixels = self.sc.convert(self.get_start())
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
def get_start_pixels_screen(self, offset=(0,0)):
# Add internal offset to offset
start_pixels = self.vs.convert(self.get_start())
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
# Get end value converted by scale and into pixels
# If supplied offset is in pixels relative to start of object
def get_end_pixels(self, offset=(0,0)):
end_pixels = self.sc.convert(self.get_end())
# Add offset
return ([end_pixels[0]+offset[0], end_pixels[1]+offset[1]])
def get_end_pixels_screen(self, offset=(0,0)):
end_pixels = self.vs.convert(self.get_end())
# Add offset
return ([end_pixels[0]+offset[0], end_pixels[1]+offset[1]])
def get_end(self):
return (self.end[0]+self.io[0], self.end[1]+self.io[1])
def set_global_width(self, line_width):
EtchLine.global_etch_width = line_width
# Gets a line as a polygon instead
# Required for etches which don't allow lines
def get_polygon_pixels(self, offset=(0,0)):
# half the width - first check local (to this line) - otherwise default to global
if self.etch_width != None:
hw = self.etch_width / 2
else:
hw = EtchLine.global_etch_width / 2
# Create an approxmation by only widening along thinest part (dx vs dy)
# For a horizontal / vertical line this this is accurate
# If not then it's an approximation
# As this is for a laser cutter the approximation will not be noticed when etched
# although may be able to see if using vector editor
# Need to know which is min and max x determine if increase
# or decrease appropriate values
dx = abs(self.end[0] - self.start[0])
dy = abs(self.end[1] - self.start[1])
# If more vertical than horizontal
if (dy > dx):
points = [
(self.start[0]-hw, self.start[1]),
(self.start[0]+hw, self.start[1]),
(self.end[0]+hw, self.end[1]),
(self.end[0]-hw, self.end[1]),
(self.start[0]-hw, self.start[1])
]
# More horizontal than vertical
else:
points = [
(self.start[0], self.start[1]-hw),
(self.end[0], self.end[1]-hw),
(self.end[0], self.end[1]+hw),
(self.start[0], self.start[1]+hw),
(self.start[0], self.start[1]-hw)
]
# Now convert to scale and add offsets
new_points = []
for point in points:
point_io = (point[0]+self.io[0], point[1]+self.io[1])
sc_point = Laser.sc.convert(point_io)
new_points.append(((offset[0]+sc_point[0]),(offset[1]+sc_point[1])))
return new_points
def get_polygon_pixels_screen(self, offset=(0,0)):
# half the width - first check local (to this line) - otherwise default to global
if self.etch_width != None:
hw = self.etch_width / 2
else:
hw = EtchLine.global_etch_width / 2
# Create an approxmation by only widening along thinest part (dx vs dy)
# For a horizontal / vertical line this this is accurate
# If not then it's an approximation
# As this is for a laser cutter the approximation will not be noticed when etched
# although may be able to see if using vector editor
# Need to know which is min and max x determine if increase
# or decrease appropriate values
dx = abs(self.end[0] - self.start[0])
dy = abs(self.end[1] - self.start[1])
# If more vertical than horizontal
if (dy > dx):
points = [
(self.start[0]-hw, self.start[1]),
(self.start[0]+hw, self.start[1]),
(self.end[0]+hw, self.end[1]),
(self.end[0]-hw, self.end[1]),
(self.start[0]-hw, self.start[1])
]
# More horizontal than vertical
else:
points = [
(self.start[0], self.start[1]-hw),
(self.end[0], self.end[1]-hw),
(self.end[0], self.end[1]+hw),
(self.start[0], self.start[1]+hw),
(self.start[0], self.start[1]-hw)
]
# Now convert to scale and add offsets
new_points = []
for point in points:
point_io = (point[0]+self.io[0], point[1]+self.io[1])
sc_point = Laser.vs.convert(point_io)
new_points.append(((offset[0]+sc_point[0]),(offset[1]+sc_point[1])))
return new_points
class EtchRect(Etch):
def __init__(self, start, size, internal_offset=(0,0), strength=5):
self.strength = strength
self.start = start
self.size = size
super().__init__("rect", internal_offset)
# uformat returns (type, [list_of_values])
def unformat(self):
return (("rect", (self.start, self.size)))
def get_start(self):
return (self.start[0]+self.io[0], self.start[1]+self.io[1])
def get_size(self):
return self.size
def get_start_pixels(self, offset=(0,0)):
start_pixels = Laser.sc.convert(self.get_start())
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
def get_start_pixels_screen(self, offset=(0,0)):
start_pixels = Laser.vs.convert(self.get_start())
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
# Note that size does not need offset
def get_size_pixels(self):
return Laser.sc.convert(self.size)
def get_size_pixels_screen(self):
return Laser.vs.convert(self.size)
class EtchPolygon(Etch):
def __init__(self, points, internal_offset=(0,0), strength=5):
self.strength = strength
self.points = points
super().__init__("polygon", internal_offset)
# unformat returns (type, [list_of_values])
def unformat(self):
return (("polygon", self.points))
def get_points(self):
new_points = []
for point in self.points:
new_points.append((point[0]+self.io[0], point[1]+self.io[1]))
return new_points
def get_points_offset(self, offset):
new_points = []
for point in self.get_points():
new_points.append([(offset[0]+point[0]),(offset[1]+point[1])])
return new_points
# Offset is applied to all points
def get_points_pixels(self, offset=(0,0)):
new_points = []
for point in self.get_points():
sc_point = Laser.sc.convert(point)
new_points.append([(offset[0]+sc_point[0]),(offset[1]+sc_point[1])])
return new_points
def get_points_pixels_screen(self, offset=(0,0)):
new_points = []
for point in self.get_points():
sc_point = Laser.vs.convert(point)
new_points.append([(offset[0]+sc_point[0]),(offset[1]+sc_point[1])])
return new_points
# Outer can be either cut or edge
# Determined when generating, so convert to appropriate type when required
# Outer must have a get_args method that allows creation of cut / edge
# TODO - If etch current default to 5, need to make configurable
class Outer(Laser):
def __init__(self, type, internal_offset):
super().__init__(type, internal_offset)
# Unable to use laserfactory due to circular imports
# implement get_cut / get_etch in each of the child classes
class OuterLine(Outer):
def __init__(self, start, end, internal_offset=(0,0), strength=5):
self.strength = strength
self.start = start
self.end = end
super().__init__("line", internal_offset)
# unformat returns (type, [list_of_values])
def unformat(self):
return (("line", (self.start, self.end)))
def get_args(self):
#print ("Outer line get arg")
#print (f"Start {self.start}")
#print (f"End {self.end}")
return [self.start, self.end]
def get_start(self):
return (self.start[0]+self.io[0], self.start[1]+self.io[1])
def get_end(self):
return (self.end[0]+self.io[0], self.end[1]+self.io[1])
#def get_size(self):
# return self.size
# Returns as a cut object
def get_cut(self):
args = self.get_args()
return CutLine(args[0], args[1], self.io)
def get_etch(self):
#print ("Getting etch from outerline")
args = self.get_args()
#print ("Getting etch line")
#print (f"args {args}")
#print (f"Self.io {self.io}")
#print (f"Strength {self.strength}")
return EtchLine(args[0], args[1], self.io, self.strength)
def get_start_pixels_screen(self, offset=(0,0)):
# Add internal offset to offset
start_pixels = self.vs.convert(self.get_start())
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
def get_end_pixels_screen(self, offset=(0,0)):
end_pixels = self.vs.convert(self.get_end())
# Add offset
return ([end_pixels[0]+offset[0], end_pixels[1]+offset[1]])
class OuterRect(Outer):
def __init__(self, start, size, internal_offset=(0,0), strength=5):
self.strength = strength
self.start = start
self.size = size
super().__init__("rect", internal_offset)
# uformat returns (type, [list_of_values])
def unformat(self):
return (("rect", (self.start, self.size)))
def get_args(self):
return [self.start, self.size]
def get_start(self):
return (self.start[0]+self.io[0], self.start[1]+self.io[1])
# Returns as a cut object
def get_cut(self):
args = self.get_args()
return CutRect(args[0], args[1], self.io)
def get_etch(self):
args = self.get_args()
return EtchRect(args[0], args[1], self.io)
def get_start_pixels_screen(self, offset=(0,0)):
start_pixels = Laser.vs.convert(self.get_start())
# Add offset
return ([start_pixels[0]+offset[0], start_pixels[1]+offset[1]])
def get_size_pixels_screen(self):
return Laser.vs.convert(self.size)
class OuterPolygon(Outer):
def __init__(self, points, internal_offset=(0,0), strength=5):
self.strength = strength
self.points = points
super().__init__("polygon", internal_offset)
# unformat returns (type, [list_of_values])
def unformat(self):
return (("polygon", self.points))
def get_args(self):
#print ("Get args")
#print (f"Points {self.points}")
return self.points
# Returns as a cut object
def get_cut(self):
return CutPolygon(self.get_args(), self.io)
def get_etch(self):
return EtchPolygon(self.get_args(), self.io)
def get_points_pixels_screen(self, offset=(0,0)):
new_points = []
#for point in self.get_points():
for point in self.points:
sc_point = Laser.vs.convert(point)
new_points.append([(offset[0]+sc_point[0]),(offset[1]+sc_point[1])])
return new_points
# Exclude is not used by the laser, but is used as part of objview
# Typically polygons to draw as white areas
class Exclude(Laser):
def __init__(self, type, internal_offset):
super().__init__(type, internal_offset)
class ExcludePolygon(Etch):
def __init__(self, points, internal_offset=(0,0), strength=5):
self.strength = strength # not used by exclude
self.points = points
super().__init__("polygon", internal_offset)
# unformat returns (type, [list_of_values])
def unformat(self):
return (("polygon", self.points))
def get_points(self):
new_points = []
for point in self.points:
new_points.append((point[0]+self.io[0], point[1]+self.io[1]))
return new_points
def get_points_offset(self, offset):
new_points = []
for point in self.get_points():
new_points.append([(offset[0]+point[0]),(offset[1]+point[1])])
return new_points
# Offset is applied to all points
def get_points_pixels(self, offset=(0,0)):
new_points = []
for point in self.get_points():
sc_point = Laser.sc.convert(point)
new_points.append([(offset[0]+sc_point[0]),(offset[1]+sc_point[1])])
return new_points
def get_points_pixels_screen(self, offset=(0,0)):
new_points = []
for point in self.get_points():
sc_point = Laser.vs.convert(point)
new_points.append([(offset[0]+sc_point[0]),(offset[1]+sc_point[1])])
return new_points