-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
gmoccapy.py
5789 lines (5021 loc) · 261 KB
/
gmoccapy.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 python3
# -*- coding:UTF-8 -*-
"""
A GUI for LinuxCNC based on gladevcp and Python
Based on the design of moccagui from Tom
and with a lot of code from gscreen from Chris Morley
and with the help from Michael Haberler
and Chris Morley and some more
Copyright 2012 / 2017 Norbert Schechner
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from gi.repository import GLib
import traceback # needed to launch traceback errors
import hal # base hal class to react to hal signals
import hal_glib # needed to make our own hal pins
import sys # handle system calls
import os # needed to get the paths and directories
import atexit # needed to register child's to be closed on closing the GUI
import subprocess # to launch onboard and other processes
import tempfile # needed only if the user click new in edit mode to open a new empty file
import linuxcnc # to get our own error system
import locale # for setting the language of the GUI
import gettext # to extract the strings to be translated
from collections import OrderedDict # needed for proper jog button arrangement
from time import strftime # needed for the clock in the GUI
#from Gtk._Gtk import main_quit
# Throws up a dialog with debug info when an error is encountered
def excepthook(exc_type, exc_obj, exc_tb):
try:
w = app.widgets.window1
except KeyboardInterrupt:
sys.exit()
except NameError:
w = None
lines = traceback.format_exception(exc_type, exc_obj, exc_tb)
message ="Found an error!\nThe following information may be useful in troubleshooting:\n\n" + "".join(lines)
LOG.error(message)
m = Gtk.MessageDialog(parent = w,
modal = True ,
destroy_with_parent = True,
message_type = Gtk.MessageType.ERROR,
text = message,
buttons = Gtk.ButtonsType.OK,)
m.show()
m.run()
m.destroy()
sys.excepthook = excepthook
# constants
# # gmoccapy #"
_RELEASE = " 3.4.3"
_INCH = 0 # imperial units are active
_MM = 1 # metric units are active
# set names for the tab numbers, its easier to understand the code
# Bottom Button Tabs
_BB_MANUAL = 0
_BB_MDI = 1
_BB_AUTO = 2
_BB_HOME = 3
_BB_TOUCH_OFF = 4
_BB_SETUP = 5
_BB_EDIT = 6
_BB_TOOL = 7
_BB_LOAD_FILE = 8
#_BB_HOME_JOINTS will not be used, we will reorder the notebooks to get the correct page shown
# Default button size for bottom buttons
_DEFAULT_BB_SIZE = (90, 56)
_TEMPDIR = tempfile.gettempdir() # Now we know where the tempdir is, usually /tmp
# set up paths to files
BASE = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))
LIBDIR = os.path.join(BASE, "lib", "python")
sys.path.insert(0, LIBDIR)
# set up paths to files, part two
CONFIGPATH = os.environ['CONFIG_DIR']
DATADIR = os.path.join(BASE, "share", "gmoccapy")
IMAGEDIR = os.path.join(DATADIR, "images")
XMLNAME = os.path.join(DATADIR, "gmoccapy.glade")
THEMEDIR = "/usr/share/themes"
USERTHEMEDIR = os.path.join(os.path.expanduser("~"), ".themes")
LOCALEDIR = os.path.join(BASE, "share", "locale")
ICON_THEME_DIR = os.path.join(DATADIR, "icons")
USER_ICON_THEME_DIR = os.path.join(os.path.expanduser("~"), ".icons")
DEFAULT_ICON_THEME = "classic"
# path to TCL for external programs eg. halshow
TCLPATH = os.environ['LINUXCNC_TCL_DIR']
# the ICONS should must exist in the icon theme
ALERT_ICON = "dialog_warning"
INFO_ICON = "dialog_information"
class gmoccapy(object):
def __init__(self, argv):
# prepare for translation / internationalisation
locale.setlocale(locale.LC_ALL, '')
locale.bindtextdomain("gmoccapy", LOCALEDIR)
gettext.install("gmoccapy", localedir=LOCALEDIR)
# CSS styling
css = b"""
button {
padding: 0;
}
#gcode_edit {
padding: 3px;
margin: 1px;
}
/* #__jog_incr_buttons *:checked {
background: rgba(230,230,50,0.8);
} */
#jog_incr_buttons *:active, #jog_buttons *:active {
background: rgba(230,230,50,0.8);
}
#eb_program_label, #eb_blockheight_label {
background: rgba(0,0,0,1);
}
"""
screen = Gdk.Screen.get_default()
provider = Gtk.CssProvider()
provider.load_from_data(css)
style_context = Gtk.StyleContext()
style_context.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
# needed components to communicate with hal and linuxcnc
self.halcomp = hal.component("gmoccapy")
self.command = linuxcnc.command()
self.stat = linuxcnc.stat()
self.error_channel = linuxcnc.error_channel()
# initial poll, so all is up to date
self.stat.poll()
self.error_channel.poll()
self.builder = Gtk.Builder()
# translation of the glade file will be done with
self.builder.set_translation_domain("gmoccapy")
self.builder.add_from_file(XMLNAME)
self.widgets = widgets.Widgets(self.builder)
self.initialized = False # will be set True after the window has been shown and all
# basic settings has been finished, so we avoid some actions
# because we cause click or toggle events when initializing
# widget states.
self.start_line = 0 # needed for start from line
self.active_gcodes = [] # this are the formatted G-code values
self.active_mcodes = [] # this are the formatted M-code values
self.gcodes = [] # this are the unformatted G-code values to check if an update is required
self.mcodes = [] # this are the unformatted M-code values to check if an update is required
self.distance = 0 # This global will hold the jog distance
self.tool_change = False # this is needed to get back to manual mode after a tool change
self.load_tool = False # We use this avoid mode switching on reloading the tool on start up of the GUI
self.macrobuttons = [] # The list of all macros defined in the INI file
self.fo_counts = 0 # need to calculate difference in counts to change the feed override slider
self.so_counts = 0 # need to calculate difference in counts to change the spindle override slider
self.jv_counts = 0 # need to calculate difference in counts to change the jog_vel slider
self.ro_counts = 0 # need to calculate difference in counts to change the rapid override slider
self.spindle_override = 1 # holds the feed override value and is needed to be able to react to halui pin
self.feed_override = 1 # holds the spindle override value and is needed to be able to react to halui pin
self.rapidrate = 1 # holds the rapid override value and is needed to be able to react to halui pin
self.incr_rbt_list = [] # we use this list to add hal pin to the button later
self.jog_increments = [] # This holds the increment values
self.unlock = False # this value will be set using the hal pin unlock settings
# needed to display the labels
self.system_list = ("0", "G54", "G55", "G56", "G57", "G58", "G59", "G59.1", "G59.2", "G59.3")
self.dro_size = 28 # The size of the DRO, user may want them bigger on bigger screen
self.axisnumber_four = "" # we use this to get the number of the 4-th axis
self.axisletter_four = None # we use this to get the letter of the 4-th axis
self.axisnumber_five = "" # we use this to get the number of the 5-th axis
self.axisletter_five = None # we use this to get the letter of the 5-th axis
self.notification = notification.Notification() # Our own message system
self.notification.connect("message_deleted", self._on_message_deleted)
self.last_key_event = None, 0 # needed to avoid the auto repeat function of the keyboard
self.all_homed = False # will hold True if all axis are homed
self.faktor = 1.0 # needed to calculate velocities
self.xpos = 40 # The X Position of the main Window
self.ypos = 30 # The Y Position of the main Window
self.width = 979 # The width of the main Window
self.height = 750 # The height of the main Window
self.gcodeerror = "" # we need this to avoid multiple messages of the same error
self.file_changed = False
self.widgets.hal_action_saveas.connect("saved-as", self.saved_as)
self.lathe_mode = None # we need this to check if we have a lathe config
self.backtool_lathe = False
self.diameter_mode = False
# the default theme = System Theme we store here to be able to go back to that one later
self.default_theme = Gtk.Settings.get_default().get_property("gtk-theme-name")
self.icon_theme = Gtk.IconTheme()
self.icon_theme.append_search_path(ICON_THEME_DIR)
self.icon_theme.append_search_path(USER_ICON_THEME_DIR)
self.dialogs = dialogs.Dialogs()
self.dialogs.connect("play_sound", self._on_play_sound)
# check the arguments given from the command line (Ini file)
self.user_mode = False
self.logofile = None
for index, arg in enumerate(argv):
LOG.debug("{0} = {1}".format(index, arg))
if arg == "-user_mode":
self.user_mode = True
self.widgets.tbtn_setup.set_sensitive(False)
LOG.debug(_("user mode selected"))
if arg == "-logo":
self.logofile = str(argv[ index + 1 ])
LOG.debug(_("logo entry found = {0}").format(self.logofile))
self.logofile = self.logofile.strip("\"\'")
if not os.path.isfile(self.logofile):
self.logofile = None
message = _("Logofile entry found, but could not be converted to path.")
message += "\n" + _("The file path should not contain any spaces")
LOG.warning(message)
# check if the user want a Logo (given as command line argument)
if self.logofile:
self.widgets.img_logo.set_from_file(self.logofile)
self.widgets.img_logo.show()
page2 = self.widgets.ntb_jog_JA.get_nth_page(2)
self.widgets.ntb_jog_JA.reorder_child(page2, 0)
page1 = self.widgets.ntb_jog_JA.get_nth_page(1)
self.widgets.ntb_jog_JA.reorder_child(page1, -1)
# Our own class to get information from INI the file we use this way, to be sure
# to get a valid result, as the checks are done in that module
self._get_ini_data()
self._get_pref_data()
self.tool_measure_OK = self._check_toolmeasurement()
# make all widgets we create dynamically
self._make_DRO()
self._make_ref_axis_button()
self._make_touch_button()
self._make_jog_increments()
self._make_jog_button()
if not self.trivial_kinematics:
# we need joint jogging button
self._make_joints_button()
self._arrange_joint_button()
self._make_macro_button()
# if we have a lathe, we need to rearrange some stuff
# we will do that in a separate function
if self.lathe_mode:
self._make_lathe()
else:
self.widgets.rbt_view_y2.hide()
# X Offset is not necessary on a mill
self.widgets.lbl_tool_offset_x.hide()
self.widgets.lbl_offset_x.hide()
self.widgets.btn_tool_touchoff_x.hide()
self.widgets.lbl_hide_tto_x.show()
self._arrange_dro()
self._arrange_jog_button()
self._make_hal_pins()
self._init_user_messages()
# set the title of the window, to show the release
self.widgets.window1.set_title("gmoccapy for LinuxCNC {0}".format(_RELEASE))
self.widgets.lbl_version.set_label("<b>gmoccapy\n{0}</b>".format(_RELEASE))
panel = gladevcp.makepins.GladePanel(self.halcomp, XMLNAME, self.builder, None)
self.halcomp.ready()
self.builder.connect_signals(self)
# this are settings to be done before window show
self._init_preferences()
# finally show the window
self.widgets.window1.show()
self._init_dynamic_tabs()
self._init_tooleditor()
self._init_themes()
self._init_icon_themes()
self._init_audio()
self._init_gremlin()
self._init_kinematics_type()
self._init_hide_cursor()
self._init_hide_tooltips()
self._init_offsetpage()
self._init_keybindings()
self._init_IconFileSelection()
self._init_keyboard()
# now we initialize the file to load widget
self._init_file_to_load()
self._show_offset_tab(False)
self._show_tooledit_tab(False)
self._show_iconview_tab(False)
# the velocity settings
self.widgets.adj_spindle_bar_min.set_value(self.min_spindle_rev)
self.widgets.adj_spindle_bar_max.set_value(self.max_spindle_rev)
self.widgets.spindle_feedback_bar.set_property("min", float(self.min_spindle_rev))
self.widgets.spindle_feedback_bar.set_property("max", float(self.max_spindle_rev))
# Popup Messages position and size
self.widgets.adj_x_pos_popup.set_value(self.prefs.getpref("x_pos_popup", 45, float))
self.widgets.adj_y_pos_popup.set_value(self.prefs.getpref("y_pos_popup", 55, float))
self.widgets.adj_width_popup.set_value(self.prefs.getpref("width_popup", 250, float))
self.widgets.adj_max_messages.set_value(self.prefs.getpref("max_messages", 10, float))
self.widgets.fontbutton_popup.set_font(self.prefs.getpref("message_font", "sans 10", str))
self.widgets.chk_use_frames.set_active(self.prefs.getpref("use_frames", True, bool))
# this sets the background colors of several buttons
# the colors are different for the states of the button
# self.widgets.tbtn_on.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_estop.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FF0000"))
# self.widgets.tbtn_estop.modify_bg(Gtk.StateFlags.NORMAL, Gdk.color_parse("#00FF00"))
# self.widgets.rbt_manual.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_mdi.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_auto.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_setup.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_forward.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#00FF00"))
# self.widgets.rbt_reverse.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#00FF00"))
# self.widgets.rbt_stop.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_view_p.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_view_x.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_view_y.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_view_y2.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.rbt_view_z.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_flood.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#00FF00"))
# self.widgets.tbtn_fullsize_preview0.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_fullsize_preview1.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_mist.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#00FF00"))
# self.widgets.tbtn_optional_blocks.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_user_tabs.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_view_dimension.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_view_tool_path.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# self.widgets.tbtn_switch_mode.modify_bg(Gtk.StateFlags.ACTIVE, Gdk.color_parse("#FFFF00"))
# should the tool in spindle be reloaded on startup?
self.widgets.chk_reload_tool.set_active(self.prefs.getpref("reload_tool", True, bool))
# and the rest of the widgets
self.widgets.rbt_manual.set_active(True)
self.widgets.ntb_jog.set_current_page(0)
opt_blocks = self.prefs.getpref("blockdel", False, bool)
self.widgets.tbtn_optional_blocks.set_active(opt_blocks)
self.command.set_block_delete(opt_blocks)
#optional_stops = self.prefs.getpref( "opstop", False, bool )
#self.widgets.tbtn_optional_stops.set_active( optional_stops )
#self.command.set_optional_stop( optional_stops )
self.widgets.chk_show_dro.set_active(self.prefs.getpref("enable_dro", False, bool))
self.widgets.chk_show_offsets.set_active(self.prefs.getpref("show_offsets", False, bool))
self.widgets.chk_show_dtg.set_active(self.prefs.getpref("show_dtg", False, bool))
self.widgets.chk_show_offsets.set_sensitive(self.widgets.chk_show_dro.get_active())
self.widgets.chk_show_dtg.set_sensitive(self.widgets.chk_show_dro.get_active())
self.widgets.cmb_mouse_button_mode.set_active(self.prefs.getpref("mouse_btn_mode", 4, int))
self.widgets.tbtn_view_tool_path.set_active(self.prefs.getpref("view_tool_path", True, bool))
self.widgets.tbtn_view_dimension.set_active(self.prefs.getpref("view_dimension", True, bool))
view = view = self.prefs.getpref("view", "p", str)
self.widgets["rbt_view_{0}".format(view)].set_active(True)
# get if run from line should be used
rfl = self.prefs.getpref("run_from_line", "no_run", str)
# and set the corresponding button active
self.widgets["rbtn_{0}_from_line".format(rfl)].set_active(True)
if rfl == "no_run":
self.widgets.btn_from_line.set_sensitive(False)
else:
self.widgets.btn_from_line.set_sensitive(True)
# get the way to unlock the setting
unlock = self.prefs.getpref("unlock_way", "use", str)
# and set the corresponding button active
self.widgets["rbt_{0}_unlock".format(unlock)].set_active(True)
# if Hal pin should be used, only set the button active, if the pin is high
if unlock == "hal" and not self.halcomp["unlock-settings"]:
self.widgets.tbtn_setup.set_sensitive(False)
# check if the user want to display preview window instead of offsetpage widget
state = self.prefs.getpref("show_preview_on_offset", False, bool)
if state:
self.widgets.rbtn_show_preview.set_active(True)
else:
self.widgets.rbtn_show_offsets.set_active(True)
# check if keyboard shortcuts should be used and set the chkbox widget
self.widgets.chk_use_kb_shortcuts.set_active(self.prefs.getpref("use_keyboard_shortcuts",
False, bool))
# check the highlighting type
# the following would load the python language
# self.widgets.gcode_view.set_language("python")
LANGDIR = os.path.join(BASE, "share", "Gtksourceview-2.0", "language-specs")
file_path = os.path.join(LANGDIR, "gcode.lang")
if os.path.isfile(file_path):
LOG.info("Gcode.lang found")
self.widgets.gcode_view.set_language("gcode", LANGDIR)
# set the user colors and digits of the DRO
self.widgets.abs_colorbutton.set_rgba(self._get_RGBA_color(self.abs_color))
self.widgets.rel_colorbutton.set_rgba(self._get_RGBA_color(self.rel_color))
self.widgets.dtg_colorbutton.set_rgba(self._get_RGBA_color(self.dtg_color))
self.widgets.homed_colorbtn.set_rgba(self._get_RGBA_color(self.homed_color))
self.widgets.unhomed_colorbtn.set_rgba(self._get_RGBA_color(self.unhomed_color))
self.widgets.adj_dro_digits.set_value(self.dro_digits)
# the adjustment change signal will set the dro_digits correct, so no extra need here.
self.widgets.chk_toggle_readout.set_active(self.toggle_readout)
self.widgets.adj_start_spindle_RPM.set_value(self.spindle_start_rpm)
self.widgets.gcode_view.set_sensitive(False)
self.widgets.ntb_user_tabs.remove_page(0)
# call the function to change the button status
# so every thing is ready to start
widgetlist = ["rbt_manual", "rbt_mdi", "rbt_auto", "btn_homing", "btn_touch", "btn_tool",
"ntb_jog", "ntb_jog_JA", "vbtb_jog_incr", "hbox_jog_vel",
"spc_feed", "btn_feed_100", "rbt_forward", "btn_index_tool",
"rbt_reverse", "rbt_stop", "tbtn_flood", "tbtn_mist", "btn_change_tool",
"btn_select_tool_by_no", "btn_spindle_100", "spc_rapid", "spc_spindle",
"btn_tool_touchoff_x", "btn_tool_touchoff_z"
]
#
self._sensitize_widgets(widgetlist, False)
# if limit switch active, activate ignore-checkbox
if any(self.stat.limit):
self.widgets.ntb_jog.set_sensitive(True)
# this must be done last, otherwise we will get wrong values
# because the window is not fully realized
self._init_notification()
# since the main loop is needed to handle the UI and its events, blocking calls like sleep()
# will block the UI as well, so everything goes through event handlers (aka callbacks)
# The GLib.timeout_add() function sets a function to be called at regular intervals
# the time between calls to the function, in milliseconds
# CYCLE_TIME = time, in milliseconds, that display will sleep between polls
cycle_time = self.get_ini_info.get_cycle_time()
GLib.timeout_add( cycle_time, self._periodic ) # time between calls to the function, in milliseconds
# This allows sourcing an user defined file
rcfile = "~/.gmoccapyrc"
user_command_file = self.get_ini_info.get_user_command_file()
if user_command_file:
rcfile = user_command_file
rcfile = os.path.expanduser(rcfile)
if os.path.exists(rcfile):
try:
exec(compile(open(rcfile, "rb").read(), rcfile, 'exec'))
except:
tb = traceback.format_exc()
LOG.error(tb)
self.notification.add_message(_("Error in ") + rcfile + "\n" \
+ _("Please check the console output."), ALERT_ICON)
# Custom css file, e.g.:
# button:checked {
# background: rgba(230,230,50,0.8);
# }
css_file = "~/.gmoccapy_css"
user_css_file = self.get_ini_info.get_user_css_file()
if user_css_file:
css_file = user_css_file
css_file = os.path.expanduser(css_file)
if os.path.exists(css_file):
provider_custom = Gtk.CssProvider()
try:
provider_custom.load_from_path(css_file)
style_context.add_provider_for_screen(screen, provider_custom, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
except:
tb = traceback.format_exc()
LOG.error(tb)
self.notification.add_message(_("Error in ") + css_file + "\n" \
+ _("Please check the console output."), ALERT_ICON)
def _get_ini_data(self):
self.get_ini_info = getiniinfo.GetIniInfo()
# get the axis list from INI
self.axis_list = self.get_ini_info.get_axis_list()
# get the joint axis relation from INI
self.joint_axis_dic, self.double_axis_letter = self.get_ini_info.get_joint_axis_relation()
# if it's a lathe config, set the tool editor style
self.lathe_mode = self.get_ini_info.get_lathe()
if self.lathe_mode:
# we do need to know also if we have a backtool lathe
self.backtool_lathe = self.get_ini_info.get_backtool_lathe()
# check if the user want actual or commanded for the DRO
self.dro_actual = self.get_ini_info.get_position_feedback_actual()
# the given Jog Increments
self.jog_increments = self.get_ini_info.get_increments()
# check if NO_FORCE_HOMING is used in INI
self.no_force_homing = self.get_ini_info.get_no_force_homing()
# do we use a identity kinematics or do we have to distinguish
# JOINT and Axis modes?
self.trivial_kinematics = self.get_ini_info.get_trivial_kinematics()
units = self.get_ini_info.get_machine_units()
if units == "mm" or units == "cm":
self.metric = True
else:
self.metric = False
self.no_force_homing = self.get_ini_info.get_no_force_homing()
# get the values for the sliders
self.rabbit_jog = self.get_ini_info.get_jog_vel()
self.jog_rate_max = self.get_ini_info.get_max_jog_vel()
self.min_ang_vel = self.get_ini_info.get_min_ang_jog_vel()
self.default_ang_vel = self.get_ini_info.get_default_ang_jog_vel()
self.max_ang_vel = self.get_ini_info.get_max_ang_jog_vel()
self.spindle_override_max = self.get_ini_info.get_max_spindle_override()
self.spindle_override_min = self.get_ini_info.get_min_spindle_override()
self.feed_override_max = self.get_ini_info.get_max_feed_override()
self.dro_actual = self.get_ini_info.get_position_feedback_actual()
def _get_pref_data(self):
self.prefs = preferences.preferences(self.get_ini_info.get_preference_file_path())
# the size and digits of the DRO
# set default values according to the machine units
digits = 3
if self.stat.linear_units != _MM:
digits = 4
self.dro_digits = self.prefs.getpref("dro_digits", digits, int)
self.dro_size = self.prefs.getpref("dro_size", 28, int)
# the colors of the DRO
self.abs_color = self.prefs.getpref("abs_color", "#0000FF", str) # blue
self.rel_color = self.prefs.getpref("rel_color", "#000000", str) # black
self.dtg_color = self.prefs.getpref("dtg_color", "#FFFF00", str) # yellow
self.homed_color = self.prefs.getpref("homed_color", "#00FF00", str) # green
self.unhomed_color = self.prefs.getpref("unhomed_color", "#FF0000", str) # red
# do we want gremlin dro ?
self.enable_gremlin_dro = self.prefs.getpref("enable_dro", False, bool)
# the scale to be applied to the counts of the hardware mpg wheel, to avoid to much turning
self.scale_jog_vel = self.prefs.getpref("scale_jog_vel", self.jog_rate_max / 100, float)
self.scale_spindle_override = self.prefs.getpref("scale_spindle_override", 1, float)
self.scale_feed_override = self.prefs.getpref("scale_feed_override", 1, float)
self.scale_rapid_override = self.prefs.getpref("scale_rapid_override", 1, float)
# the velocity settings
self.min_spindle_rev = self.prefs.getpref("spindle_bar_min", 0.0, float)
self.max_spindle_rev = self.prefs.getpref("spindle_bar_max", 6000.0, float)
self.turtle_jog_factor = self.prefs.getpref('turtle_jog_factor', 20, int)
self.hide_turtle_jog_button = self.prefs.getpref("hide_turtle_jog_button", False, bool)
self.unlock_code = self.prefs.getpref("unlock_code", "123", str) # get unlock code
self.toggle_readout = self.prefs.getpref("toggle_readout", True, bool)
# if there is a INI Entry for default spindle speed, we will use that one as default
# but if there is a setting in our preference file, that one will beet the INI entry
default_spindle_speed = self.get_ini_info.get_default_spindle_speed()
self.spindle_start_rpm = self.prefs.getpref( 'spindle_start_rpm', default_spindle_speed, float )
self.kbd_height = self.prefs.getpref("kbd_height", 250, int)
###############################################################################
## create widgets dynamically ##
###############################################################################
def _make_DRO(self):
LOG.debug("Entering make_DRO")
LOG.debug("axis_list = {0}".format(self.axis_list))
# we build one DRO for each axis
self.dro_dic = {}
for pos, axis in enumerate(self.axis_list):
joint = self._get_joint_from_joint_axis_dic(axis)
dro = Combi_DRO()
dro.set_joint_no(joint)
dro.set_axis(axis)
dro.change_axisletter(axis.upper())
dro.show()
dro.set_property("name", "Combi_DRO_{0}".format(pos))
dro.set_property("abs_color", self._get_RGBA_color(self.abs_color))
dro.set_property("rel_color", self._get_RGBA_color(self.rel_color))
dro.set_property("dtg_color", self._get_RGBA_color(self.dtg_color))
dro.set_property("homed_color", self._get_RGBA_color(self.homed_color))
dro.set_property("unhomed_color", self._get_RGBA_color(self.unhomed_color))
dro.set_property("actual", self.dro_actual)
dro.connect("clicked", self._on_DRO_clicked)
dro.connect('axis_clicked', self._on_DRO_axis_clicked)
self.dro_dic[dro.get_property("name")] = dro
# print dro.name
def _get_RGBA_color(self, color_str):
color = Gdk.RGBA()
color.parse(color_str)
return Gdk.RGBA(color.red, color.green, color.blue, color.alpha)
def _get_joint_from_joint_axis_dic(self, value):
# if the selected axis is a double axis we will get the joint from the
# master axis, which should end with 0
if value in self.double_axis_letter:
value = value + "0"
return list(self.joint_axis_dic.keys())[list(self.joint_axis_dic.values()).index(value)]
def _make_ref_axis_button(self):
LOG.debug("Entering make ref axis button")
# check if we need axis or joint homing button
if self.trivial_kinematics:
# lets find out, how many axis we got
dic = self.axis_list
name_prefix = "axis"
name_prefix_sg = _("axis")
name_prefix_pl = _("axes")
else:
# lets find out, how many joints we got
dic = self.joint_axis_dic
name_prefix = "joint"
name_prefix_sg = _("joint")
name_prefix_pl = _("joints")
num_elements = len(dic)
# as long as the number of axis is less 6 we can use the standard layout
# we can display 6 axis without the second space label
# and 7 axis if we do not display the first space label either
# if we have more than 7 axis, we need arrows to switch the visible ones
if num_elements < 7:
lbl = self._get_space_label("lbl_space_0")
self.widgets.hbtb_ref.pack_start(lbl,True,True,0)
btn = self._new_button_with_predefined_image(
name="ref_all",
size=_DEFAULT_BB_SIZE,
image=self.widgets.img_ref_all
)
btn.set_property("tooltip-text", _("Press to home all {0}").format(name_prefix_pl))
btn.connect("clicked", self._on_btn_home_clicked)
# we use pack_start, so the widgets will be moved from right to left
# and are displayed the way we want
self.widgets.hbtb_ref.pack_start(btn,True,True,0)
if num_elements > 7:
# show the previous arrow to switch visible homing button)
btn = self._new_button_with_predefined_image(
name="previous_button",
size=_DEFAULT_BB_SIZE,
image=self.widgets.img_ref_paginate_prev
)
btn.set_property("tooltip-text", _("Press to display previous homing button"))
btn.connect("clicked", self._on_btn_previous_clicked)
self.widgets.hbtb_ref.pack_start(btn,True,True,0)
btn.hide()
# do not use this label, to allow one more axis
if num_elements < 6:
lbl = self._get_space_label("lbl_space_2")
self.widgets.hbtb_ref.pack_start(lbl,True,True,0)
for pos, elem in enumerate(dic):
btn = self._new_button_with_predefined_image(
name=f"home_{name_prefix}_{elem}",
size=_DEFAULT_BB_SIZE,
image_name=f"img_ref_{elem}"
)
btn.set_property("tooltip-text", _("Press to home {0} {1}").format(name_prefix_sg, str(elem).upper()))
btn.connect("clicked", self._on_btn_home_clicked)
self.widgets.hbtb_ref.pack_start(btn,True,True,0)
# if we have more than 7 axis we need to hide some button
if num_elements > 7:
if pos > 5:
btn.hide()
if num_elements > 7:
# show the next arrow to switch visible homing button)
btn = self._new_button_with_predefined_image(
name="next_button",
size=_DEFAULT_BB_SIZE,
image=self.widgets.img_ref_paginate_next
)
btn.set_property("tooltip-text", _("Press to display next homing button"))
btn.connect("clicked", self._on_btn_next_clicked)
self.widgets.hbtb_ref.pack_start(btn,True,True,0)
# if there is space left, fill it with space labels
start = self.widgets.hbtb_ref.child_get_property(btn,"position")
for count in range(start + 1 , 8):
lbl = self._get_space_label("lbl_space_{0}".format(count))
self.widgets.hbtb_ref.pack_start(lbl,True,True,0)
btn = self._new_button_with_predefined_image(
name="unref_all",
size=_DEFAULT_BB_SIZE,
image=self.widgets.img_unref_all
)
btn.set_property("tooltip-text", _("Press to unhome all {0}").format(name_prefix_pl))
btn.connect("clicked", self._on_btn_unhome_clicked)
self.widgets.hbtb_ref.pack_start(btn,True,True,0)
btn = self._new_button_with_predefined_image(
name="home_back",
size=_DEFAULT_BB_SIZE,
image=self.widgets.img_ref_menu_close
)
btn.set_property("tooltip-text", _("Press to return to main button list"))
btn.connect("clicked", self._on_btn_home_back_clicked)
self.widgets.hbtb_ref.pack_start(btn,True,True,0)
self.ref_button_dic = {}
children = self.widgets.hbtb_ref.get_children()
for child in children:
self.ref_button_dic[child.get_property("name")] = child
self.widgets.hbtb_ref.show_all()
def _get_space_label(self, name):
lbl = Gtk.Label.new("")
lbl.set_property("name", name)
lbl.set_size_request(*_DEFAULT_BB_SIZE)
lbl.show()
return lbl
def _new_button_with_predefined_image(self, name, size, image = None, image_name = None):
btn = Gtk.Button()
btn.set_size_request(*size)
btn.set_halign(Gtk.Align.CENTER)
btn.set_valign(Gtk.Align.CENTER)
btn.set_property("name", name)
try:
if image:
btn.set_image(image)
elif image_name:
btn.set_image(self.widgets[image_name])
else:
raise ValueError("Either image or image_name must not be None")
except Exception as e:
LOG.warning(f"Error creating button with predefined image: {e}")
missing_image = Gtk.Image()
# TODO: Deprecated
missing_image.set_from_stock(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.BUTTON)
btn.set_image(missing_image)
btn.show_all()
return btn
def _get_button_with_image(self, name, filepath, icon_name):
LOG.debug("get button with image")
image = Gtk.Image()
image.set_size_request(72,48)
btn = Gtk.Button.new()
btn.set_size_request(*_DEFAULT_BB_SIZE)
btn.set_halign(Gtk.Align.CENTER)
btn.set_valign(Gtk.Align.CENTER)
btn.set_property("name", name)
try:
if filepath:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(filepath, 48, 48)
image.set_from_pixbuf(pixbuf)
else:
image.set_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
btn.add(image)
except Exception as e:
LOG.error(e)
message = _("could not resolv the image path '{0}' given for button '{1}'".format(filepath, name))
LOG.error(message)
image.set_from_icon_name("image-missing", Gtk.IconSize.DIALOG)
btn.add(image)
btn.show_all()
return btn
def _remove_button(self, dic, box):
for child in dic:
box.remove(dic[child])
def _on_btn_next_clicked(self, widget):
# remove all buttons from container
self._remove_button(self.ref_button_dic, self.widgets.hbtb_ref)
self.widgets.hbtb_ref.pack_start(self.ref_button_dic["ref_all"], True, True, 0)
self.ref_button_dic["ref_all"].show()
self.widgets.hbtb_ref.pack_start(self.ref_button_dic["previous_button"], True, True, 0)
self.ref_button_dic["previous_button"].show()
start = len(self.axis_list) - 6
end = len(self.axis_list)
# now put the needed widgets in the container
for axis in self.axis_list[start : end]:
name = "home_axis_{0}".format(axis.lower())
self.ref_button_dic[name].show()
self.widgets.hbtb_ref.pack_start(self.ref_button_dic[name], True, True, 0)
self._put_unref_and_back()
def _on_btn_next_touch_clicked(self, widget):
self._remove_button(self.touch_button_dic, self.widgets.hbtb_touch_off)
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["edit_offsets"],True,True,0)
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["previous_button"],True,True,0)
self.touch_button_dic["previous_button"].show()
start = len(self.axis_list) - 5
end = len(self.axis_list)
# now put the needed widgets in the container
for axis in self.axis_list[start : end]:
name = "touch_{0}".format(axis.lower())
self.touch_button_dic[name].show()
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic[name], True, True, 0)
self._put_set_active_and_back()
def _on_btn_next_macro_clicked(self, widget):
# remove all buttons from container
self._remove_button(self.macro_dic, self.widgets.hbtb_MDI)
self.widgets.hbtb_MDI.pack_start(self.macro_dic["previous_button"],True,True,0)
self.macro_dic["previous_button"].show()
end = len(self.macro_dic) - 3 # reduced by next, previous and keyboard
start = end - 8
# now put the needed widgets in the container
for pos in range(start, end):
name = "macro_{0}".format(pos)
self.widgets.hbtb_MDI.pack_start(self.macro_dic[name], True, True, 0)
self.macro_dic[name].show()
self.widgets.hbtb_MDI.pack_start(self.macro_dic["keyboard"],True,True,0)
self.macro_dic["keyboard"].show()
def _on_btn_previous_clicked(self, widget):
LOG.debug("previous")
self._remove_button(self.ref_button_dic, self.widgets.hbtb_ref)
self.widgets.hbtb_ref.pack_start(self.ref_button_dic["ref_all"], True, True, 0)
self.ref_button_dic["ref_all"].show()
start = 0
end = 6
# now put the needed widgets in the container
for axis in self.axis_list[start : end]:
name = "home_axis_{0}".format(axis.lower())
self.ref_button_dic[name].show()
self.widgets.hbtb_ref.pack_start(self.ref_button_dic[name], True, True, 0)
self.widgets.hbtb_ref.pack_start(self.ref_button_dic["next_button"], True, True, 0)
self.ref_button_dic["next_button"].show()
self._put_unref_and_back()
def _on_btn_previous_touch_clicked(self, widget):
self._remove_button(self.touch_button_dic, self.widgets.hbtb_touch_off)
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["edit_offsets"],True,True,0)
if self.tool_measure_OK:
end = 4
else:
end = 5
start = 0
# now put the needed widgets in the container
for axis in self.axis_list[start : end]:
name = "touch_{0}".format(axis.lower())
self.touch_button_dic[name].show()
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic[name], True, True, 0)
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["next_button"],True,True,0)
self.touch_button_dic["next_button"].show()
if self.tool_measure_OK:
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["block_height"],True,True,0)
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["zero_offsets"],True,True,0)
self._put_set_active_and_back()
def _on_btn_previous_macro_clicked(self, widget):
# remove all buttons from container
self._remove_button(self.macro_dic, self.widgets.hbtb_MDI)
start = 0
end = 8
# now put the needed widgets in the container
for pos in range(start, end):
name = "macro_{0}".format(pos)
self.widgets.hbtb_MDI.pack_start(self.macro_dic[name], True, True, 0)
self.macro_dic[name].show()
self.widgets.hbtb_MDI.pack_start(self.macro_dic["next_button"],True,True,0)
self.macro_dic["next_button"].show()
self.widgets.hbtb_MDI.pack_start(self.macro_dic["keyboard"],True,True,0)
self.macro_dic["keyboard"].show()
def _put_set_active_and_back(self):
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["zero_offsets"], True, True, 0)
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["set_active"], True, True, 0)
self.widgets.hbtb_touch_off.pack_start(self.touch_button_dic["touch_back"], True, True, 0)
def _put_unref_and_back(self):
self.widgets.hbtb_ref.pack_start(self.ref_button_dic["unref_all"], True, True, 0)
self.widgets.hbtb_ref.pack_start(self.ref_button_dic["home_back"], True, True, 0)
def _make_touch_button(self):
LOG.debug("Entering make touch button")
dic = self.axis_list
num_elements = len(dic)
end = 7
if self.tool_measure_OK:
# we will have 3 buttons on the right side
end -= 1
lbl = Gtk.Label.new(_("edit\noffsets"))
lbl.set_visible(True)
lbl.set_justify(Gtk.Justification.CENTER)
btn = Gtk.ToggleButton.new()
btn.set_size_request(*_DEFAULT_BB_SIZE)
btn.set_halign(Gtk.Align.CENTER)
btn.set_valign(Gtk.Align.CENTER)
btn.add(lbl)
btn.connect("toggled", self.on_tbtn_edit_offsets_toggled)
btn.set_property("tooltip-text", _("Press to edit the offsets"))
btn.set_property("name", "edit_offsets")
# TODO: Use CSS for this if still needed
# btn.override_background_color(Gtk.StateFlags.ACTIVE, Gdk.RGBA(1.0, 1.0, 0.0, 1.0))
self.widgets.hbtb_touch_off.pack_start(btn,True,True,0)
btn.show()
if num_elements > 6:
# show the previous arrow to switch visible touch button)
btn = self._new_button_with_predefined_image(
name="previous_button",
size=_DEFAULT_BB_SIZE,
image=self.widgets.img_touch_paginate_prev
)
btn.set_property("tooltip-text", _("Press to display previous homing button"))
btn.connect("clicked", self._on_btn_previous_touch_clicked)