-
Notifications
You must be signed in to change notification settings - Fork 70
/
CD_OneButton.py
208 lines (170 loc) · 7.23 KB
/
CD_OneButton.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
# ***************************************************************************
# * *
# * Copyright (c) 2020 Dan Miel *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""
This is to be used in conjunction with A2plus Assembly Workbench.
Enables two features to be selected without using the control key.
"""
import os
from PySide import QtGui, QtCore
import FreeCAD
import FreeCADGui
translate = FreeCAD.Qt.translate
class globaluseclass:
def __init__(self, name):
self.sONOFF = 'off'
self.feat1 = ''
self.buttonenabled = False
self.obj1 = ''
self.partselected = False
g = globaluseclass("g")
class onebutton:
def readselect(self, doc, obj, sub):
if g.partselected:
g.partselected = False
return
sels = len(FreeCADGui.Selection.getSelectionEx())
if sub == "":
pass
elif sels == 1:
if g.obj1 == '' and g.feat1 == '' or obj == g.obj1:
g.obj1 = obj
g.feat1 = sub
obj = ''
sub = ''
elif g.obj1 != '' and g.feat1 != '':
if obj != '' and sub != '':
try:
obj1 = FreeCAD.ActiveDocument.getObject(g.obj1)
obj2 = FreeCAD.ActiveDocument.getObject(obj)
FreeCADGui.Selection.addSelection(obj1, g.feat1)
FreeCADGui.Selection.addSelection(obj2, sub)
g.partselected = True
except:
pass
g.feat1 = ''
g.obj1 = ''
obj = ''
sub = ''
class SelObserver:
def __init__(self):
pass
def SelObserverON(self):
if g.sONOFF != 'on':
FreeCADGui.Selection.addObserver(selObv)
g.sONOFF = 'on'
# print('SelObserverON')
def SelObserverOFF(self):
try:
FreeCADGui.Selection.removeObserver(selObv)
g.sONOFF = 'off'
# print('SelObserverOFF')
except:
FreeCAD.Console.PrintMessage(translate("A2plus", "SelObserverOFF by except") + "\n")
def addSelection(self, doc, obj, sub, pnt): # Selection object
onebutton.readselect(onebutton, doc, obj, sub)
def removeSelection(self, doc, obj, sub): # Delete the selected object
pass
def setSelection(self, doc):
pass
selObv = SelObserver()
# This class looks for mouse clicks in space to unselect parts.
class ViewObserver:
def __init__(self):
self.view = None
self.o = None
self.c = None
def vostart(self):
self.view = FreeCADGui.activeDocument().activeView()
self.o = ViewObserver()
self.c = self.view.addEventCallback("SoMouseButtonEvent", self.o.logPosition)
def vooff(self):
try:
self.view.removeEventCallback("SoMouseButtonEvent", self.c)
except Exception as e:
print(str(e))
def logPosition(self, myinfo):
down = (myinfo["State"] == "DOWN")
up = (myinfo["State"] == "UP")
pos = myinfo["Position"]
if up:
pass
if (down):
if myinfo['Button'] == 'BUTTON1':
pos = FreeCADGui.ActiveDocument.ActiveView.getCursorPos()
partinfo = FreeCADGui.activeDocument().activeView().getObjectInfo(pos)
if partinfo is None:
g.feat1 = ''
g.obj1 = ''
FreeCADGui.Selection.clearSelection()
else:
pass
viewob = ViewObserver()
class rnp_OneButton:
def GetResources(self):
mypath = os.path.dirname(__file__)
return {
'Pixmap': mypath + "/icons/CD_OneButton.svg",
'MenuText': translate("A2plus", "Use one mouse button to select features"),
'ToolTip': translate("A2plus", "Use left mouse button to select two features.\nDo not use the control key."),
'Checkable': self.IsChecked()
}
def Activated(self, placeholder=None):
if FreeCAD.activeDocument() is None:
mApp(translate("A2plus", "No file is opened.\nYou must open an assembly file first."))
return
FreeCADGui.Selection.clearSelection()
if g.buttonenabled is False:
selObv.SelObserverON() # Checks for part and entity click
viewob.vostart() # Checks for click in background
g.buttonenabled = True
FreeCAD.Console.PrintMessage(translate("A2plus", "OneButton is ON") + "\n")
else:
selObv.SelObserverOFF()
viewob.vooff()
g.buttonenabled = False
FreeCAD.Console.PrintMessage(translate("A2plus", "OneButton is OFF") + "\n")
def Deactivated(self):
"""This function is executed when the workbench is deactivated."""
selObv.SelObserverOFF()
viewob.vooff()
def IsChecked(self):
return(g.buttonenabled)
def IsActive(self):
return(True)
FreeCADGui.addCommand('rnp_OneButton', rnp_OneButton())
class mApp(QtGui.QWidget):
"""This message box was added to make this file a standalone file"""
# for error messages
def __init__(self, msg, msgtype='ok'):
super().__init__()
self.title = translate("A2plus", "Warning")
self.left = 100
self.top = 100
self.width = 400
self.height = 300
self.initUI(msg)
def initUI(self, msg):
# self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
QtGui.QMessageBox.question(self, translate("A2plus", "Warning"), msg, QtGui.QMessageBox.Ok | QtGui.QMessageBox.Ok)
self.show()