forked from kliment/Printrun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pronterface.py
executable file
·1641 lines (1498 loc) · 69.8 KB
/
pronterface.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
#!/usr/bin/env python
# This file is part of the Printrun suite.
#
# Printrun is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Printrun is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Printrun. If not, see <http://www.gnu.org/licenses/>.
import os, Queue, re
from printrun.printrun_utils import install_locale, RemainingTimeEstimator
install_locale('pronterface')
try:
import wx
except:
print _("WX is not installed. This program requires WX to run.")
raise
import sys, glob, time, datetime, threading, traceback, cStringIO, subprocess
import shlex
from printrun.pronterface_widgets import *
from serial import SerialException
StringIO = cStringIO
winsize = (800, 500)
layerindex = 0
if os.name == "nt":
winsize = (800, 530)
try:
import _winreg
except:
pass
import printcore
from printrun.printrun_utils import pixmapfile, configfile
from printrun.gui import MainWindow
import pronsole
from pronsole import dosify, wxSetting, HiddenSetting, StringSetting, SpinSetting, FloatSpinSetting, BooleanSetting
from printrun import gcoder
tempreport_exp = re.compile("([TB]\d*):([-+]?\d*\.?\d*)(?: \/)?([-+]?\d*\.?\d*)")
def parse_temperature_report(report):
matches = tempreport_exp.findall(report)
return dict((m[0], (m[1], m[2])) for m in matches)
def format_time(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime("%H:%M:%S")
def format_duration(delta):
return str(datetime.timedelta(seconds = int(delta)))
class Tee(object):
def __init__(self, target):
self.stdout = sys.stdout
sys.stdout = self
self.target = target
def __del__(self):
sys.stdout = self.stdout
def write(self, data):
try:
self.target(data)
except:
pass
try:
data = data.encode("utf-8")
except:
pass
self.stdout.write(data)
def flush(self):
self.stdout.flush()
def parse_build_dimensions(bdim):
# a string containing up to six numbers delimited by almost anything
# first 0-3 numbers specify the build volume, no sign, always positive
# remaining 0-3 numbers specify the coordinates of the "southwest" corner of the build platform
# "XXX,YYY"
# "XXXxYYY+xxx-yyy"
# "XXX,YYY,ZZZ+xxx+yyy-zzz"
# etc
bdl = re.findall("([-+]?[0-9]*\.?[0-9]*)", bdim)
defaults = [200, 200, 100, 0, 0, 0, 0, 0, 0]
bdl = filter(None, bdl)
bdl_float = [float(value) if value else defaults[i] for i, value in enumerate(bdl)]
if len(bdl_float) < len(defaults):
bdl_float += [defaults[i] for i in range(len(bdl_float), len(defaults))]
for i in range(3): # Check for nonpositive dimensions for build volume
if bdl_float[i] <= 0: bdl_float[i] = 1
return bdl_float
class BuildDimensionsSetting(wxSetting):
widgets = None
def _set_value(self, value):
self._value = value
if self.widgets:
self._set_widgets_values(value)
value = property(wxSetting._get_value, _set_value)
def _set_widgets_values(self, value):
build_dimensions_list = parse_build_dimensions(value)
for i in range(len(self.widgets)):
self.widgets[i].SetValue(build_dimensions_list[i])
def get_widget(self, parent):
from wx.lib.agw.floatspin import FloatSpin
import wx
build_dimensions = parse_build_dimensions(self.value)
self.widgets = []
w = lambda val, m, M: self.widgets.append(FloatSpin(parent, -1, value = val, min_val = m, max_val = M, digits = 2))
addlabel = lambda name, pos: self.widget.Add(wx.StaticText(parent, -1, name), pos = pos, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
addwidget = lambda *pos: self.widget.Add(self.widgets[-1], pos = pos, flag = wx.RIGHT, border = 5)
self.widget = wx.GridBagSizer()
addlabel(_("Width"), (0, 0))
w(build_dimensions[0], 0, 2000)
addwidget(0, 1)
addlabel(_("Depth"), (0, 2))
w(build_dimensions[1], 0, 2000)
addwidget(0, 3)
addlabel(_("Height"), (0, 4))
w(build_dimensions[2], 0, 2000)
addwidget(0, 5)
addlabel(_("X offset"), (1, 0))
w(build_dimensions[3], -2000, 2000)
addwidget(1, 1)
addlabel(_("Y offset"), (1, 2))
w(build_dimensions[4], -2000, 2000)
addwidget(1, 3)
addlabel(_("Z offset"), (1, 4))
w(build_dimensions[5], -2000, 2000)
addwidget(1, 5)
addlabel(_("X home pos."), (2, 0))
w(build_dimensions[6], -2000, 2000)
self.widget.Add(self.widgets[-1], pos = (2, 1))
addlabel(_("Y home pos."), (2, 2))
w(build_dimensions[7], -2000, 2000)
self.widget.Add(self.widgets[-1], pos = (2, 3))
addlabel(_("Z home pos."), (2, 4))
w(build_dimensions[8], -2000, 2000)
self.widget.Add(self.widgets[-1], pos = (2, 5))
return self.widget
def update(self):
values = [float(w.GetValue()) for w in self.widgets]
self.value = "%.02fx%.02fx%.02f%+.02f%+.02f%+.02f%+.02f%+.02f%+.02f" % tuple(values)
class StringSetting(wxSetting):
def get_specific_widget(self, parent):
import wx
self.widget = wx.TextCtrl(parent, -1, str(self.value))
return self.widget
class ComboSetting(wxSetting):
def __init__(self, name, default, choices, label = None, help = None, group = None):
super(ComboSetting, self).__init__(name, default, label, help, group)
self.choices = choices
def get_specific_widget(self, parent):
import wx
self.widget = wx.ComboBox(parent, -1, str(self.value), choices = self.choices, style = wx.CB_DROPDOWN)
return self.widget
class PronterWindow(MainWindow, pronsole.pronsole):
def __init__(self, filename = None, size = winsize):
pronsole.pronsole.__init__(self)
#default build dimensions are 200x200x100 with 0, 0, 0 in the corner of the bed and endstops at 0, 0 and 0
monitorsetting = BooleanSetting("monitor", False)
monitorsetting.hidden = True
self.settings._add(monitorsetting)
self.settings._add(BuildDimensionsSetting("build_dimensions", "200x200x100+0+0+0+0+0+0", _("Build dimensions"), _("Dimensions of Build Platform\n & optional offset of origin\n & optional switch position\n\nExamples:\n XXXxYYY\n XXX,YYY,ZZZ\n XXXxYYYxZZZ+OffX+OffY+OffZ\nXXXxYYYxZZZ+OffX+OffY+OffZ+HomeX+HomeY+HomeZ"), "Printer"))
self.settings._add(StringSetting("bgcolor", "#FFFFFF", _("Background color"), _("Pronterface background color (default: #FFFFFF)"), "UI"))
self.settings._add(ComboSetting("uimode", "Standard", ["Standard", "Compact", "Tabbed"], _("Interface mode"), _("Standard interface is a one-page, three columns layout with controls/visualization/log\nCompact mode is a one-page, two columns layout with controls + log/visualization\nTabbed mode is a two-pages mode, where the first page shows controls and the second one shows visualization and log."), "UI"))
self.settings._add(BooleanSetting("viz3d", False, _("Enable 3D viewer (requires restarting)"), _("Use 3D visualization instead of 2D layered visualization"), "UI"))
self.settings._add(ComboSetting("mainviz", "2D", ["2D", "3D", "None"], _("Main visualization"), _("Select visualization for main window."), "UI"))
self.settings._add(BooleanSetting("tempgauges", False, _("Display temperature gauges"), _("Display graphical gauges for temperatures visualization"), "UI"))
self.settings._add(HiddenSetting("last_bed_temperature", 0.0))
self.settings._add(HiddenSetting("last_file_path", ""))
self.settings._add(HiddenSetting("last_temperature", 0.0))
self.settings._add(FloatSpinSetting("preview_extrusion_width", 0.5, 0, 10, _("Preview extrusion width"), _("Width of Extrusion in Preview (default: 0.5)"), "UI"))
self.settings._add(SpinSetting("preview_grid_step1", 10., 0, 200, _("Fine grid spacing"), _("Fine Grid Spacing (default: 10)"), "UI"))
self.settings._add(SpinSetting("preview_grid_step2", 50., 0, 200, _("Coarse grid spacing"), _("Coarse Grid Spacing (default: 50)"), "UI"))
self.pauseScript = "pause.gcode"
self.endScript = "end.gcode"
self.filename = filename
os.putenv("UBUNTU_MENUPROXY", "0")
MainWindow.__init__(self, None, title = _("Printer Interface"), size = size);
if hasattr(sys,"frozen") and sys.frozen=="windows_exe":
self.SetIcon(wx.Icon(sys.executable, wx.BITMAP_TYPE_ICO))
else:
self.SetIcon(wx.Icon(pixmapfile("P-face.ico"), wx.BITMAP_TYPE_ICO))
self.panel = wx.Panel(self,-1, size = size)
self.statuscheck = False
self.status_thread = None
self.capture_skip = {}
self.capture_skip_newline = False
self.tempreport = ""
self.userm114 = 0
self.userm105 = 0
self.monitor = 0
self.fgcode = None
self.skeinp = None
self.monitor_interval = 3
self.current_pos = [0, 0, 0]
self.paused = False
self.sentlines = Queue.Queue(0)
self.cpbuttons = [
SpecialButton(_("Motors off"), ("M84"), (250, 250, 250), None, 0, _("Switch all motors off")),
SpecialButton(_("Check temp"), ("M105"), (225, 200, 200), (2, 5), (1, 1), _("Check current hotend temperature")),
SpecialButton(_("Extrude"), ("extrude"), (225, 200, 200), (4, 0), (1, 2), _("Advance extruder by set length")),
SpecialButton(_("Reverse"), ("reverse"), (225, 200, 200), (5, 0), (1, 2), _("Reverse extruder by set length")),
]
self.custombuttons = []
self.btndict = {}
self.autoconnect = False
self.parse_cmdline(sys.argv[1:])
self.build_dimensions_list = parse_build_dimensions(self.settings.build_dimensions)
self.display_gauges = self.settings.tempgauges
#initialize the code analyzer with the correct sizes. There must be a more general way to do so
# minimum = offset
self.p.analyzer.minX = self.build_dimensions_list[3]
self.p.analyzer.minY = self.build_dimensions_list[4]
self.p.analyzer.minZ = self.build_dimensions_list[5]
#max = offset + bedsize
self.p.analyzer.maxX = self.build_dimensions_list[3] + self.build_dimensions_list[0]
self.p.analyzer.maxY = self.build_dimensions_list[4] + self.build_dimensions_list[1]
self.p.analyzer.maxZ = self.build_dimensions_list[5] + self.build_dimensions_list[2]
self.p.analyzer.homeX = self.build_dimensions_list[6]
self.p.analyzer.homeY = self.build_dimensions_list[7]
self.p.analyzer.homeZ = self.build_dimensions_list[8]
#set feedrates in printcore for pause/resume
self.p.xy_feedrate = self.settings.xy_feedrate
self.p.z_feedrate = self.settings.z_feedrate
#make printcore aware of me
self.p.pronterface = self
self.panel.SetBackgroundColour(self.settings.bgcolor)
customdict = {}
try:
execfile(configfile("custombtn.txt"), customdict)
if len(customdict["btns"]):
if not len(self.custombuttons):
try:
self.custombuttons = customdict["btns"]
for n in xrange(len(self.custombuttons)):
self.cbutton_save(n, self.custombuttons[n])
os.rename("custombtn.txt", "custombtn.old")
rco = open("custombtn.txt", "w")
rco.write(_("# I moved all your custom buttons into .pronsolerc.\n# Please don't add them here any more.\n# Backup of your old buttons is in custombtn.old\n"))
rco.close()
except IOError, x:
print str(x)
else:
print _("Note!!! You have specified custom buttons in both custombtn.txt and .pronsolerc")
print _("Ignoring custombtn.txt. Remove all current buttons to revert to custombtn.txt")
except:
pass
self.popmenu()
if self.settings.uimode == "Tabbed":
self.createTabbedGui()
else:
self.createGui(self.settings.uimode == "Compact")
self.t = Tee(self.catchprint)
self.stdout = sys.stdout
self.skeining = 0
self.mini = False
self.p.sendcb = self.sentcb
self.p.printsendcb = self.printsentcb
self.p.layerchangecb = self.layer_change_cb
self.p.startcb = self.startcb
self.p.endcb = self.endcb
self.compute_eta = None
self.starttime = 0
self.extra_print_time = 0
self.curlayer = 0
self.cur_button = None
self.predisconnect_mainqueue = None
self.predisconnect_queueindex = None
self.predisconnect_layer = None
self.hsetpoint = 0.0
self.bsetpoint = 0.0
if self.autoconnect:
self.connect()
if self.filename is not None:
self.do_load(self.filename)
if self.settings.monitor:
self.setmonitor(None)
def add_cmdline_arguments(self, parser):
pronsole.pronsole.add_cmdline_arguments(self, parser)
parser.add_argument('-a','--autoconnect', help = _("automatically try to connect to printer on startup"), action = "store_true")
def process_cmdline_arguments(self, args):
pronsole.pronsole.process_cmdline_arguments(self, args)
self.autoconnect = args.autoconnect
def startcb(self, resuming = False):
self.starttime = time.time()
if resuming:
print _("Print resumed at: %s") % format_time(self.starttime)
else:
print _("Print started at: %s") % format_time(self.starttime)
self.compute_eta = RemainingTimeEstimator(self.fgcode)
def endcb(self):
if self.p.queueindex == 0:
print_duration = int(time.time () - self.starttime + self.extra_print_time)
print _("Print ended at: %(end_time)s and took %(duration)s") % {"end_time": format_time(time.time()),
"duration": format_duration(print_duration)}
wx.CallAfter(self.pausebtn.Disable)
wx.CallAfter(self.printbtn.SetLabel, _("Print"))
self.p.runSmallScript(self.endScript)
param = self.settings.final_command
if not param:
return
pararray = [i.replace("$s", str(self.filename)).replace("$t", format_duration(print_duration)).encode() for i in shlex.split(param.replace("\\", "\\\\").encode())]
self.finalp = subprocess.Popen(pararray, stderr = subprocess.STDOUT, stdout = subprocess.PIPE)
def online(self):
print _("Printer is now online.")
self.connectbtn.SetLabel(_("Disconnect"))
self.connectbtn.SetToolTip(wx.ToolTip("Disconnect from the printer"))
self.connectbtn.Bind(wx.EVT_BUTTON, self.disconnect)
for i in self.printerControls:
wx.CallAfter(i.Enable)
# Enable XYButtons and ZButtons
wx.CallAfter(self.xyb.enable)
wx.CallAfter(self.zb.enable)
if self.filename:
wx.CallAfter(self.printbtn.Enable)
def layer_change_cb(self, newlayer):
if self.compute_eta:
secondselapsed = int(time.time() - self.starttime + self.extra_print_time)
self.compute_eta.update_layer(newlayer, secondselapsed)
def sentcb(self, line):
gline = gcoder.Line(line)
gline.parse_coordinates(imperial = False)
if gline.is_move:
if gline.z != None:
layer = gline.z
if layer != self.curlayer:
self.curlayer = layer
self.gviz.clearhilights()
wx.CallAfter(self.gviz.setlayer, layer)
elif gline.command in ["M104", "M109"]:
gline.parse_coordinates(imperial = False, force = True)
if gline.s != None:
temp = gline.s
if self.display_gauges: wx.CallAfter(self.hottgauge.SetTarget, temp)
wx.CallAfter(self.graph.SetExtruder0TargetTemperature, temp)
elif gline.command == "M140":
gline.parse_coordinates(imperial = False, force = True)
if gline.s != None:
temp = gline.s
if self.display_gauges: wx.CallAfter(self.bedtgauge.SetTarget, temp)
wx.CallAfter(self.graph.SetBedTargetTemperature, temp)
else:
return
self.sentlines.put_nowait(line)
def printsentcb(self, gline):
if gline.is_move and hasattr(self.gwindow, "set_current_gline"):
wx.CallAfter(self.gwindow.set_current_gline, gline)
if gline.is_move and hasattr(self.gviz, "set_current_gline"):
wx.CallAfter(self.gviz.set_current_gline, gline)
def do_extrude(self, l = ""):
try:
if not l.__class__ in (str, unicode) or not len(l):
l = str(self.edist.GetValue())
pronsole.pronsole.do_extrude(self, l)
except:
raise
def do_reverse(self, l = ""):
try:
if not l.__class__ in (str, unicode) or not len(l):
l = str(- float(self.edist.GetValue()))
pronsole.pronsole.do_extrude(self, l)
except:
pass
def setbedgui(self, f):
self.bsetpoint = f
if self.display_gauges: self.bedtgauge.SetTarget(int(f))
wx.CallAfter(self.graph.SetBedTargetTemperature, int(f))
if f>0:
wx.CallAfter(self.btemp.SetValue, str(f))
self.set("last_bed_temperature", str(f))
wx.CallAfter(self.setboff.SetBackgroundColour, None)
wx.CallAfter(self.setboff.SetForegroundColour, None)
wx.CallAfter(self.setbbtn.SetBackgroundColour, "#FFAA66")
wx.CallAfter(self.setbbtn.SetForegroundColour, "#660000")
wx.CallAfter(self.btemp.SetBackgroundColour, "#FFDABB")
else:
wx.CallAfter(self.setboff.SetBackgroundColour, "#0044CC")
wx.CallAfter(self.setboff.SetForegroundColour, "white")
wx.CallAfter(self.setbbtn.SetBackgroundColour, None)
wx.CallAfter(self.setbbtn.SetForegroundColour, None)
wx.CallAfter(self.btemp.SetBackgroundColour, "white")
wx.CallAfter(self.btemp.Refresh)
def sethotendgui(self, f):
self.hsetpoint = f
if self.display_gauges: self.hottgauge.SetTarget(int(f))
wx.CallAfter(self.graph.SetExtruder0TargetTemperature, int(f))
if f > 0:
wx.CallAfter(self.htemp.SetValue, str(f))
self.set("last_temperature", str(f))
wx.CallAfter(self.settoff.SetBackgroundColour, None)
wx.CallAfter(self.settoff.SetForegroundColour, None)
wx.CallAfter(self.settbtn.SetBackgroundColour, "#FFAA66")
wx.CallAfter(self.settbtn.SetForegroundColour, "#660000")
wx.CallAfter(self.htemp.SetBackgroundColour, "#FFDABB")
else:
wx.CallAfter(self.settoff.SetBackgroundColour, "#0044CC")
wx.CallAfter(self.settoff.SetForegroundColour, "white")
wx.CallAfter(self.settbtn.SetBackgroundColour, None)
wx.CallAfter(self.settbtn.SetForegroundColour, None)
wx.CallAfter(self.htemp.SetBackgroundColour, "white")
wx.CallAfter(self.htemp.Refresh)
def do_settemp(self, l = ""):
try:
if not l.__class__ in (str, unicode) or not len(l):
l = str(self.htemp.GetValue().split()[0])
l = l.lower().replace(", ", ".")
for i in self.temps.keys():
l = l.replace(i, self.temps[i])
f = float(l)
if f >= 0:
if self.p.online:
self.p.send_now("M104 S"+l)
print _("Setting hotend temperature to %f degrees Celsius.") % f
self.sethotendgui(f)
else:
print _("Printer is not online.")
else:
print _("You cannot set negative temperatures. To turn the hotend off entirely, set its temperature to 0.")
except Exception, x:
print _("You must enter a temperature. (%s)") % (repr(x),)
def do_bedtemp(self, l = ""):
try:
if not l.__class__ in (str, unicode) or not len(l):
l = str(self.btemp.GetValue().split()[0])
l = l.lower().replace(", ", ".")
for i in self.bedtemps.keys():
l = l.replace(i, self.bedtemps[i])
f = float(l)
if f >= 0:
if self.p.online:
self.p.send_now("M140 S"+l)
print _("Setting bed temperature to %f degrees Celsius.") % f
self.setbedgui(f)
else:
print _("Printer is not online.")
else:
print _("You cannot set negative temperatures. To turn the bed off entirely, set its temperature to 0.")
except Exception, x:
print _("You must enter a temperature. (%s)") % (repr(x),)
def end_macro(self):
pronsole.pronsole.end_macro(self)
self.update_macros_menu()
def delete_macro(self, macro_name):
pronsole.pronsole.delete_macro(self, macro_name)
self.update_macros_menu()
def start_macro(self, macro_name, old_macro_definition = ""):
if not self.processing_rc:
def cb(definition):
if len(definition.strip()) == 0:
if old_macro_definition != "":
dialog = wx.MessageDialog(self, _("Do you want to erase the macro?"), style = wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
if dialog.ShowModal() == wx.ID_YES:
self.delete_macro(macro_name)
return
print _("Cancelled.")
return
self.cur_macro_name = macro_name
self.cur_macro_def = definition
self.end_macro()
MacroEditor(macro_name, old_macro_definition, cb)
else:
pronsole.pronsole.start_macro(self, macro_name, old_macro_definition)
def catchprint(self, l):
if self.capture_skip_newline and len(l) and not len(l.strip("\n\r")):
self.capture_skip_newline = False
return
for pat in self.capture_skip.keys():
if self.capture_skip[pat] > 0 and pat.match(l):
self.capture_skip[pat] -= 1
self.capture_skip_newline = True
return
wx.CallAfter(self.addtexttolog,l);
def scanserial(self):
"""scan for available ports. return a list of device names."""
baselist = []
if os.name == "nt":
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM")
i = 0
while True:
baselist += [_winreg.EnumValue(key, i)[1]]
i += 1
except:
pass
return baselist+glob.glob('/dev/ttyUSB*') + glob.glob('/dev/ttyACM*') + glob.glob("/dev/tty.*") + glob.glob("/dev/cu.*") + glob.glob("/dev/rfcomm*")
def project(self,event):
from printrun import projectlayer
projectlayer.SettingsFrame(self, self.p).Show()
def popmenu(self):
self.menustrip = wx.MenuBar()
# File menu
m = wx.Menu()
self.Bind(wx.EVT_MENU, self.loadfile, m.Append(-1, _("&Open..."), _(" Opens file")))
self.Bind(wx.EVT_MENU, self.do_editgcode, m.Append(-1, _("&Edit..."), _(" Edit open file")))
self.Bind(wx.EVT_MENU, self.clearOutput, m.Append(-1, _("Clear console"), _(" Clear output console")))
self.Bind(wx.EVT_MENU, self.project, m.Append(-1, _("Projector"), _(" Project slices")))
self.Bind(wx.EVT_MENU, self.OnExit, m.Append(wx.ID_EXIT, _("E&xit"), _(" Closes the Window")))
self.menustrip.Append(m, _("&File"))
# Settings menu
m = wx.Menu()
self.macros_menu = wx.Menu()
m.AppendSubMenu(self.macros_menu, _("&Macros"))
self.Bind(wx.EVT_MENU, self.new_macro, self.macros_menu.Append(-1, _("<&New...>")))
self.Bind(wx.EVT_MENU, lambda *e: PronterOptions(self), m.Append(-1, _("&Options"), _(" Options dialog")))
self.Bind(wx.EVT_MENU, lambda x: threading.Thread(target = lambda:self.do_skein("set")).start(), m.Append(-1, _("Slicing Settings"), _(" Adjust slicing settings")))
mItem = m.AppendCheckItem(-1, _("Debug G-code"),
_("Print all G-code sent to and received from the printer."))
m.Check(mItem.GetId(), self.p.loud)
self.Bind(wx.EVT_MENU, self.setloud, mItem)
#try:
# from SkeinforgeQuickEditDialog import SkeinforgeQuickEditDialog
# self.Bind(wx.EVT_MENU, lambda *e:SkeinforgeQuickEditDialog(self), m.Append(-1,_("SFACT Quick Settings"),_(" Quickly adjust SFACT settings for active profile")))
#except:
# pass
self.menustrip.Append(m, _("&Settings"))
self.update_macros_menu()
self.SetMenuBar(self.menustrip)
def doneediting(self, gcode):
open(self.filename, "w").write("\n".join(gcode))
wx.CallAfter(self.loadfile, None, self.filename)
def do_editgcode(self, e = None):
if self.filename is not None:
MacroEditor(self.filename, [line.raw for line in self.fgcode], self.doneediting, 1)
def new_macro(self, e = None):
dialog = wx.Dialog(self, -1, _("Enter macro name"), size = (260, 85))
panel = wx.Panel(dialog, -1)
vbox = wx.BoxSizer(wx.VERTICAL)
wx.StaticText(panel, -1, _("Macro name:"), (8, 14))
dialog.namectrl = wx.TextCtrl(panel, -1, '', (110, 8), size = (130, 24), style = wx.TE_PROCESS_ENTER)
hbox = wx.BoxSizer(wx.HORIZONTAL)
okb = wx.Button(dialog, wx.ID_OK, _("Ok"), size = (60, 24))
dialog.Bind(wx.EVT_TEXT_ENTER, lambda e:dialog.EndModal(wx.ID_OK), dialog.namectrl)
#dialog.Bind(wx.EVT_BUTTON, lambda e:self.new_macro_named(dialog, e), okb)
hbox.Add(okb)
hbox.Add(wx.Button(dialog, wx.ID_CANCEL, _("Cancel"), size = (60, 24)))
vbox.Add(panel)
vbox.Add(hbox, 1, wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, 10)
dialog.SetSizer(vbox)
dialog.Centre()
macro = ""
if dialog.ShowModal() == wx.ID_OK:
macro = dialog.namectrl.GetValue()
if macro != "":
wx.CallAfter(self.edit_macro, macro)
dialog.Destroy()
return macro
def edit_macro(self, macro):
if macro == "": return self.new_macro()
if self.macros.has_key(macro):
old_def = self.macros[macro]
elif len([c for c in macro.encode("ascii", "replace") if not c.isalnum() and c != "_"]):
print _("Macro name may contain only ASCII alphanumeric symbols and underscores")
return
elif hasattr(self.__class__, "do_"+macro):
print _("Name '%s' is being used by built-in command") % macro
return
else:
old_def = ""
self.start_macro(macro, old_def)
return macro
def update_macros_menu(self):
if not hasattr(self, "macros_menu"):
return # too early, menu not yet built
try:
while True:
item = self.macros_menu.FindItemByPosition(1)
if item is None: break
self.macros_menu.DeleteItem(item)
except:
pass
for macro in self.macros.keys():
self.Bind(wx.EVT_MENU, lambda x, m = macro: self.start_macro(m, self.macros[m]), self.macros_menu.Append(-1, macro))
def OnExit(self, event):
self.Close()
def rescanports(self, event = None):
scan = self.scanserial()
portslist = list(scan)
if self.settings.port != "" and self.settings.port not in portslist:
portslist += [self.settings.port]
self.serialport.Clear()
self.serialport.AppendItems(portslist)
try:
if os.path.exists(self.settings.port) or self.settings.port in scan:
self.serialport.SetValue(self.settings.port)
elif len(portslist) > 0:
self.serialport.SetValue(portslist[0])
except:
pass
def cbkey(self, e):
if e.GetKeyCode() == wx.WXK_UP:
if self.commandbox.histindex == len(self.commandbox.history):
self.commandbox.history+=[self.commandbox.GetValue()] #save current command
if len(self.commandbox.history):
self.commandbox.histindex = (self.commandbox.histindex-1)%len(self.commandbox.history)
self.commandbox.SetValue(self.commandbox.history[self.commandbox.histindex])
self.commandbox.SetSelection(0, len(self.commandbox.history[self.commandbox.histindex]))
elif e.GetKeyCode() == wx.WXK_DOWN:
if self.commandbox.histindex == len(self.commandbox.history):
self.commandbox.history+=[self.commandbox.GetValue()] #save current command
if len(self.commandbox.history):
self.commandbox.histindex = (self.commandbox.histindex+1)%len(self.commandbox.history)
self.commandbox.SetValue(self.commandbox.history[self.commandbox.histindex])
self.commandbox.SetSelection(0, len(self.commandbox.history[self.commandbox.histindex]))
else:
e.Skip()
def plate(self, e):
import plater
print "plate function activated"
plater.stlwin(size = (800, 580), callback = self.platecb, parent = self).Show()
def platecb(self, name):
print "plated: "+name
self.loadfile(None, name)
def sdmenu(self, e):
obj = e.GetEventObject()
popupmenu = wx.Menu()
item = popupmenu.Append(-1, _("SD Upload"))
if not self.fgcode:
item.Enable(False)
self.Bind(wx.EVT_MENU, self.upload, id = item.GetId())
item = popupmenu.Append(-1, _("SD Print"))
self.Bind(wx.EVT_MENU, self.sdprintfile, id = item.GetId())
self.panel.PopupMenu(popupmenu, obj.GetPosition())
def htemp_change(self, event):
if self.hsetpoint > 0:
self.do_settemp("")
wx.CallAfter(self.htemp.SetInsertionPoint, 0)
def btemp_change(self, event):
if self.bsetpoint > 0:
self.do_bedtemp("")
wx.CallAfter(self.btemp.SetInsertionPoint, 0)
def showwin(self, event):
if self.fgcode:
self.gwindow.Show(True)
self.gwindow.SetToolTip(wx.ToolTip("Mousewheel zooms the display\nShift / Mousewheel scrolls layers"))
self.gwindow.Raise()
def setfeeds(self, e):
self.feedrates_changed = True
try:
self.settings._set("e_feedrate", self.efeedc.GetValue())
except:
pass
try:
self.settings._set("z_feedrate", self.zfeedc.GetValue())
except:
pass
try:
self.settings._set("xy_feedrate", self.xyfeedc.GetValue())
except:
pass
def cbuttons_reload(self):
allcbs = getattr(self, "custombuttonbuttons", [])
for button in allcbs:
self.centersizer.Detach(button)
button.Destroy()
self.custombuttonbuttons = []
custombuttons = self.custombuttons[:] + [None]
for i, btndef in enumerate(custombuttons):
try:
b = wx.Button(self.cbuttons_panel, -1, btndef.label, style = wx.BU_EXACTFIT)
b.SetToolTip(wx.ToolTip(_("Execute command: ")+btndef.command))
if btndef.background:
b.SetBackgroundColour(btndef.background)
rr, gg, bb = b.GetBackgroundColour().Get()
if 0.3*rr+0.59*gg+0.11*bb < 60:
b.SetForegroundColour("#ffffff")
except:
if i == len(custombuttons) - 1:
self.newbuttonbutton = b = wx.Button(self.cbuttons_panel, -1, "+", size = (19, 18), style = wx.BU_EXACTFIT)
#b.SetFont(wx.Font(12, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
b.SetForegroundColour("#4444ff")
b.SetToolTip(wx.ToolTip(_("click to add new custom button")))
b.Bind(wx.EVT_BUTTON, self.cbutton_edit)
else:
b = wx.Button(self.cbuttons_panel,-1, ".", size = (1, 1))
#b = wx.StaticText(self.panel,-1, "", size = (72, 22), style = wx.ALIGN_CENTRE+wx.ST_NO_AUTORESIZE) #+wx.SIMPLE_BORDER
b.Disable()
#continue
b.custombutton = i
b.properties = btndef
if btndef is not None:
b.Bind(wx.EVT_BUTTON, self.procbutton)
b.Bind(wx.EVT_MOUSE_EVENTS, self.editbutton)
self.custombuttonbuttons.append(b)
self.centersizer.Add(b, pos = (i // 4, i % 4))
self.centersizer.Layout()
def help_button(self):
print _('Defines custom button. Usage: button <num> "title" [/c "colour"] command')
def do_button(self, argstr):
def nextarg(rest):
rest = rest.lstrip()
if rest.startswith('"'):
return rest[1:].split('"',1)
else:
return rest.split(None, 1)
#try:
num, argstr = nextarg(argstr)
num = int(num)
title, argstr = nextarg(argstr)
colour = None
try:
c1, c2 = nextarg(argstr)
if c1 == "/c":
colour, argstr = nextarg(c2)
except:
pass
command = argstr.strip()
if num<0 or num>=64:
print _("Custom button number should be between 0 and 63")
return
while num >= len(self.custombuttons):
self.custombuttons.append(None)
self.custombuttons[num] = SpecialButton(title, command)
if colour is not None:
self.custombuttons[num].background = colour
if not self.processing_rc:
self.cbuttons_reload()
#except Exception, x:
# print "Bad syntax for button definition, see 'help button'"
# print x
def cbutton_save(self, n, bdef, new_n = None):
if new_n is None: new_n = n
if bdef is None or bdef == "":
self.save_in_rc(("button %d" % n),'')
elif bdef.background:
colour = bdef.background
if type(colour) not in (str, unicode):
#print type(colour), map(type, colour)
if type(colour) == tuple and tuple(map(type, colour)) == (int, int, int):
colour = map(lambda x:x%256, colour)
colour = wx.Colour(*colour).GetAsString(wx.C2S_NAME|wx.C2S_HTML_SYNTAX)
else:
colour = wx.Colour(colour).GetAsString(wx.C2S_NAME|wx.C2S_HTML_SYNTAX)
self.save_in_rc(("button %d" % n),'button %d "%s" /c "%s" %s' % (new_n, bdef.label, colour, bdef.command))
else:
self.save_in_rc(("button %d" % n),'button %d "%s" %s' % (new_n, bdef.label, bdef.command))
def cbutton_edit(self, e, button = None):
bedit = ButtonEdit(self)
if button is not None:
n = button.custombutton
bedit.name.SetValue(button.properties.label)
bedit.command.SetValue(button.properties.command)
if button.properties.background:
colour = button.properties.background
if type(colour) not in (str, unicode):
#print type(colour)
if type(colour) == tuple and tuple(map(type, colour)) == (int, int, int):
colour = map(lambda x:x%256, colour)
colour = wx.Colour(*colour).GetAsString(wx.C2S_NAME|wx.C2S_HTML_SYNTAX)
else:
colour = wx.Colour(colour).GetAsString(wx.C2S_NAME|wx.C2S_HTML_SYNTAX)
bedit.color.SetValue(colour)
else:
n = len(self.custombuttons)
while n>0 and self.custombuttons[n-1] is None:
n -= 1
if bedit.ShowModal() == wx.ID_OK:
if n == len(self.custombuttons):
self.custombuttons+=[None]
self.custombuttons[n]=SpecialButton(bedit.name.GetValue().strip(), bedit.command.GetValue().strip(), custom = True)
if bedit.color.GetValue().strip()!="":
self.custombuttons[n].background = bedit.color.GetValue()
self.cbutton_save(n, self.custombuttons[n])
wx.CallAfter(bedit.Destroy)
wx.CallAfter(self.cbuttons_reload)
def cbutton_remove(self, e, button):
n = button.custombutton
self.cbutton_save(n, None)
del self.custombuttons[n]
for i in range(n, len(self.custombuttons)):
self.cbutton_save(i, self.custombuttons[i])
wx.CallAfter(self.cbuttons_reload)
def cbutton_order(self, e, button, dir):
n = button.custombutton
if dir<0:
n = n-1
if n+1 >= len(self.custombuttons):
self.custombuttons+=[None] # pad
# swap
self.custombuttons[n], self.custombuttons[n+1] = self.custombuttons[n+1], self.custombuttons[n]
self.cbutton_save(n, self.custombuttons[n])
self.cbutton_save(n+1, self.custombuttons[n+1])
#if self.custombuttons[-1] is None:
# del self.custombuttons[-1]
wx.CallAfter(self.cbuttons_reload)
def editbutton(self, e):
if e.IsCommandEvent() or e.ButtonUp(wx.MOUSE_BTN_RIGHT):
if e.IsCommandEvent():
pos = (0, 0)
else:
pos = e.GetPosition()
popupmenu = wx.Menu()
obj = e.GetEventObject()
if hasattr(obj, "custombutton"):
item = popupmenu.Append(-1, _("Edit custom button '%s'") % e.GetEventObject().GetLabelText())
self.Bind(wx.EVT_MENU, lambda e, button = e.GetEventObject():self.cbutton_edit(e, button), item)
item = popupmenu.Append(-1, _("Move left <<"))
self.Bind(wx.EVT_MENU, lambda e, button = e.GetEventObject():self.cbutton_order(e, button,-1), item)
if obj.custombutton == 0: item.Enable(False)
item = popupmenu.Append(-1, _("Move right >>"))
self.Bind(wx.EVT_MENU, lambda e, button = e.GetEventObject():self.cbutton_order(e, button, 1), item)
if obj.custombutton == 63: item.Enable(False)
pos = self.panel.ScreenToClient(e.GetEventObject().ClientToScreen(pos))
item = popupmenu.Append(-1, _("Remove custom button '%s'") % e.GetEventObject().GetLabelText())
self.Bind(wx.EVT_MENU, lambda e, button = e.GetEventObject():self.cbutton_remove(e, button), item)
else:
item = popupmenu.Append(-1, _("Add custom button"))
self.Bind(wx.EVT_MENU, self.cbutton_edit, item)
self.panel.PopupMenu(popupmenu, pos)
elif e.Dragging() and e.ButtonIsDown(wx.MOUSE_BTN_LEFT):
obj = e.GetEventObject()
scrpos = obj.ClientToScreen(e.GetPosition())
if not hasattr(self, "dragpos"):
self.dragpos = scrpos
e.Skip()
return
else:
dx, dy = self.dragpos[0]-scrpos[0], self.dragpos[1]-scrpos[1]
if dx*dx+dy*dy < 5*5: # threshold to detect dragging for jittery mice
e.Skip()
return
if not hasattr(self, "dragging"):
# init dragging of the custom button
if hasattr(obj, "custombutton") and obj.properties is not None:
#self.newbuttonbutton.SetLabel("")
#self.newbuttonbutton.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
#self.newbuttonbutton.SetForegroundColour("black")
#self.newbuttonbutton.SetSize(obj.GetSize())
#if self.uppersizer.GetItem(self.newbuttonbutton) is not None:
# self.uppersizer.SetItemMinSize(self.newbuttonbutton, obj.GetSize())
# self.mainsizer.Layout()
for b in self.custombuttonbuttons:
#if b.IsFrozen(): b.Thaw()
if b.properties is None:
b.Enable()
b.SetLabel("")
b.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
b.SetForegroundColour("black")
b.SetSize(obj.GetSize())
if self.uppersizer.GetItem(b) is not None:
self.uppersizer.SetItemMinSize(b, obj.GetSize())
self.mainsizer.Layout()
# b.SetStyle(wx.ALIGN_CENTRE+wx.ST_NO_AUTORESIZE+wx.SIMPLE_BORDER)
self.dragging = wx.Button(self.panel,-1, obj.GetLabel(), style = wx.BU_EXACTFIT)
self.dragging.SetBackgroundColour(obj.GetBackgroundColour())
self.dragging.SetForegroundColour(obj.GetForegroundColour())
self.dragging.sourcebutton = obj
self.dragging.Raise()
self.dragging.Disable()
self.dragging.SetPosition(self.panel.ScreenToClient(scrpos))
self.last_drag_dest = obj
self.dragging.label = obj.s_label = obj.GetLabel()
self.dragging.bgc = obj.s_bgc = obj.GetBackgroundColour()
self.dragging.fgc = obj.s_fgc = obj.GetForegroundColour()
else:
# dragging in progress
self.dragging.SetPosition(self.panel.ScreenToClient(scrpos))
wx.CallAfter(self.dragging.Refresh)
btns = self.custombuttonbuttons
dst = None
src = self.dragging.sourcebutton
drg = self.dragging
for b in self.custombuttonbuttons:
if b.GetScreenRect().Contains(scrpos):
dst = b
break
#if dst is None and self.panel.GetScreenRect().Contains(scrpos):
# # try to check if it is after buttons at the end
# tspos = self.panel.ClientToScreen(self.uppersizer.GetPosition())
# bspos = self.panel.ClientToScreen(self.centersizer.GetPosition())
# tsrect = wx.Rect(*(tspos.Get()+self.uppersizer.GetSize().Get()))
# bsrect = wx.Rect(*(bspos.Get()+self.centersizer.GetSize().Get()))
# lbrect = btns[-1].GetScreenRect()
# p = scrpos.Get()
# if len(btns)<4 and tsrect.Contains(scrpos):
# if lbrect.GetRight() < p[0]:
# print "Right of last button on upper cb sizer"
# if bsrect.Contains(scrpos):
# if lbrect.GetBottom() < p[1]:
# print "Below last button on lower cb sizer"
# if lbrect.GetRight() < p[0] and lbrect.GetTop() <= p[1] and lbrect.GetBottom() >= p[1]:
# print "Right to last button on lower cb sizer"
if dst is not self.last_drag_dest:
if self.last_drag_dest is not None:
self.last_drag_dest.SetBackgroundColour(self.last_drag_dest.s_bgc)
self.last_drag_dest.SetForegroundColour(self.last_drag_dest.s_fgc)
self.last_drag_dest.SetLabel(self.last_drag_dest.s_label)
if dst is not None and dst is not src:
dst.s_bgc = dst.GetBackgroundColour()
dst.s_fgc = dst.GetForegroundColour()
dst.s_label = dst.GetLabel()
src.SetBackgroundColour(dst.GetBackgroundColour())
src.SetForegroundColour(dst.GetForegroundColour())
src.SetLabel(dst.GetLabel())
dst.SetBackgroundColour(drg.bgc)
dst.SetForegroundColour(drg.fgc)
dst.SetLabel(drg.label)
else:
src.SetBackgroundColour(drg.bgc)
src.SetForegroundColour(drg.fgc)
src.SetLabel(drg.label)
self.last_drag_dest = dst
elif hasattr(self, "dragging") and not e.ButtonIsDown(wx.MOUSE_BTN_LEFT):
# dragging finished
obj = e.GetEventObject()
scrpos = obj.ClientToScreen(e.GetPosition())
dst = None
src = self.dragging.sourcebutton
drg = self.dragging
for b in self.custombuttonbuttons:
if b.GetScreenRect().Contains(scrpos):
dst = b
break
if dst is not None: