File: offset_tool_button.py

package info (click to toggle)
linuxcnc 1%3A2.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 285,604 kB
  • sloc: python: 202,568; ansic: 109,036; cpp: 99,239; tcl: 16,054; xml: 10,631; sh: 10,303; makefile: 1,255; javascript: 138; sql: 72; asm: 15
file content (210 lines) | stat: -rw-r--r-- 8,786 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
#
# QTVcp Widget
# Copyright (c) 2019 Chris Morley
#
# 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.
###############################################################################

from PyQt5.QtWidgets import QToolButton, QMenu, QAction
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtGui import QIcon

from qtvcp.widgets.widget_baseclass import _HalWidgetBase
from qtvcp.core import Status, Action, Info
from qtvcp import logger

# Instiniate the libraries with global reference
# STATUS gives us status messages from LinuxCNC
# AUX_PRGM holds helper program loader
# INI holds INI details
# ACTION gives commands to LinuxCNC
# LOG is for running code logging
STATUS = Status()
INFO = Info()
ACTION = Action()
LOG = logger.getLogger(__name__)
# Force the log level for this module
#LOG.setLevel(logger.DEBUG) # One of DEBUG, INFO, WARNING, ERROR, CRITICAL

class OffsetToolButton(QToolButton, _HalWidgetBase):
    def __init__(self, parent=None):
        super(OffsetToolButton, self).__init__(parent)
        self._axis = ''
        self._last = 0
        self._block_signal = False
        self.dialog_code = 'CALCULATOR'
        self.display_units_mm = 0

        self.settingMenu = QMenu(self)
        self.setMenu(self.settingMenu)

        Button = QAction(QIcon('exit24.png'), 'Set Current Tool Position', self)
        Button.triggered.connect(self.setOffset)
        self.settingMenu.addAction(Button)

        Button = QAction(QIcon('exit24.png'), 'Adjust Current Tool Position', self)
        Button.triggered.connect(self.adjustOffset)
        self.settingMenu.addAction(Button)

        Button = QAction(QIcon('exit24.png'), 'Zero Current Tool Position', self)
        Button.triggered.connect(self.zeroOffset)
        self.settingMenu.addAction(Button)

        Button = QAction(QIcon('exit24.png'), 'Set Tool Offset Directly', self)
        Button.triggered.connect(self.setDirectOffset)
        self.settingMenu.addAction(Button)

        Button = QAction(QIcon('exit24.png'), 'Reset To Last', self)
        Button.triggered.connect(self.last)
        self.settingMenu.addAction(Button)

    def _hal_init(self):
        def homed_on_test():
            return (STATUS.machine_is_on()
                    and (STATUS.get_current_tool() not in(0,-1))
                    and (STATUS.is_all_homed()
                    or INFO.NO_HOME_REQUIRED))
        STATUS.connect('metric-mode-changed', self._switch_units)
        STATUS.connect('state-off', lambda w: self.settingMenu.setEnabled(False))
        STATUS.connect('state-estop', lambda w: self.settingMenu.setEnabled(False))
        STATUS.connect('interp-idle', lambda w: self.settingMenu.setEnabled(homed_on_test()))
        STATUS.connect('interp-run', lambda w: self.settingMenu.setEnabled(False))
        STATUS.connect('all-homed', lambda w: self.settingMenu.setEnabled(homed_on_test()))
        STATUS.connect('not-all-homed', lambda w, data: self.settingMenu.setEnabled(False))
        STATUS.connect('interp-paused', lambda w: self.settingMenu.setEnabled(homed_on_test()))
        STATUS.connect('tool-in-spindle-changed', lambda w,t:self.settingMenu.setEnabled(homed_on_test()))
        STATUS.connect('general',self.return_value)

    # process the STATUS return message
    def return_value(self, w, message):
        num = message['RETURN']
        code = bool(message.get('ID') == '%s__'% self.objectName())
        name = bool(message.get('NAME') == self.dialog_code)
        if code and name and num is not None:
            LOG.debug('message return:{}'.format (message))
            self._last = message.get('CURRENT')
            axis = message.get('AXIS')
            fixture = message.get('FIXTURE') or False
            direct = message.get('DIRECT') or False
            adjust = message.get('ADJUST') or False
            if direct:
                if adjust:
                    num = self._last - num
                ACTION.SET_DIRECT_TOOL_OFFSET(axis,num)
                STATUS.emit('update-machine-log', 'Set Direct tool offset of Axis {}: {} to {}'.format(axis, self._last, num), 'TIME')
            else:
                ACTION.SET_TOOL_OFFSET(axis,num,fixture)
                STATUS.emit('update-machine-log', 'Adjust Tool offset of Axis {}, {} to {}'.format(axis, self._last, num), 'TIME')

    def zeroOffset(self):
        axis, tool, now = self._get_current_info(self._axis)
        if axis:
            self._last = now
            fixture = False
            ACTION.SET_TOOL_OFFSET(axis, 0, fixture)
            STATUS.emit('update-machine-log', 'Zeroed Tool Offset {} of Tool {}'.format(axis, tool), 'TIME')

    def setOffset(self):
        axis, tool, now = self._get_current_info(self._axis)
        if axis:
            mess = {'NAME':self.dialog_code,'ID':'%s__' % self.objectName(),
                    'AXIS':axis,'CURRENT':now,
                    'TITLE':'Set Axis {} Current Position by setting Offset of Tool {}'.format(axis, tool)}
            STATUS.emit('dialog-request', mess)
            LOG.debug('message sent:{}'.format (mess))

    def adjustOffset(self):
        axis, tool, now = self._get_current_info(self._axis)
        if axis:
            mess = {'NAME':self.dialog_code,'ID':'%s__' % self.objectName(),
                    'AXIS':axis,'CURRENT':now,'ADJUST':True, 'DIRECT':True,
                    'TITLE':'Adjust Axis {} Current Position by Offsetting Tool {} by Entered Amount'.format(axis, tool, now)}
            STATUS.emit('dialog-request', mess)
            LOG.debug('message sent:{}'.format (mess))

    def setDirectOffset(self):
        axis, tool, now = self._get_current_info(self._axis)
        if axis:
            mess = {'NAME':self.dialog_code,'ID':'%s__' % self.objectName(),
                    'AXIS':axis,'CURRENT':now,'DIRECT':True,
                     'TITLE':'Set Axis {} Offset of Tool {}'.format(axis, tool)}
            STATUS.emit('dialog-request', mess)
            LOG.debug('message sent:{}'.format (mess))

    def last(self):
        axis, tool, now = self._get_current_info(self._axis)
        if axis:
            fixture = False
            ACTION.SET_DIRECT_TOOL_OFFSET(axis, self._last)
            STATUS.emit('update-machine-log', 'Reset Tool {}  Offset To Last {}'.format(tool, self._last), 'TIME')
            self._last = now

    def _get_current_info(self, axis):
        tool = STATUS.get_current_tool()
        if STATUS.is_joint_mode() or not tool:
            return None, None, None
        if axis == '':
            axis = STATUS.get_selected_axis()
        j = "XYZABCUVW"
        try:
            jnum = j.find(axis)
        except:
            return None, None, None
        offset = STATUS.stat.tool_offset[jnum]
        return axis, tool, offset

    def _switch_units(self, widget, data):
        self.display_units_mm = data
        if self.display_units_mm != INFO.MACHINE_IS_METRIC:
            self._last = INFO.convert_units(self._last)

    #########################################################################
    # This is how designer can interact with our widget properties.
    # designer will show the pyqtProperty properties in the editor
    # it will use the get set and reset calls to do those actions
    #
    ########################################################################

    def set_axis(self, data):
        if data.upper() in('X','Y','Z','A','B','C','U','V','W'):
            self._axis = str(data.upper())
    def get_axis(self):
        return self._axis
    def reset_axis(self):
        self._axis = ''
    axis_letter = pyqtProperty(str, get_axis, set_axis, reset_axis)


    def set_dialog_code(self, data):
        self.dialog_code = data
    def get_dialog_code(self):
        return self.dialog_code
    def reset_dialog_code(self):
        self.dialog_code = 'CALCULATOR'
    dialog_code_string = pyqtProperty(str, get_dialog_code, set_dialog_code, reset_dialog_code)

    def __getitem__(self, item):
        return getattr(self, item)
    def __setitem__(self, item, value):
        return setattr(self, item, value)

# for testing without editor:
def main():
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    widget = OffsetToolButton()
    widget.show()

    sys.exit(app.exec_())
if __name__ == "__main__":
    main()