Code: Select all
"""Stripped down from MyDialog.FCMacro by openBrain
https://github.com/0penBrain/FreeCAD-macros/blob/MyDialog/Examples/MyDialog.FCMacro
Demonstrate the selection and reporting of edges.
"""
import re
from PySide2 import QtCore, QtWidgets
import FreeCADGui as Gui
MSG = App.Console.PrintMessage
class MyDialog(QtWidgets.QDialog):
class MySelObs(QtCore.QObject):
"""Selection Observer"""
selection = QtCore.Signal(str)
clear = QtCore.Signal(str)
def __init__(self):
super().__init__()
def addSelection(self, doc, obj, sub, pnt):
print(sub, sub[:4])
if sub[:4] == "Edge": # Is sub-element an Edge? Check head of string.
x = sub[4:] # get numerical value of sub-element from tail of string.
self.selection.emit(f'Edge {x[0]}')
else:
print("\nNot an Edge. Deselect and try again.\n")
def clearSelection(self,doc):
self.clear.emit(f'Selection cleared in document {doc}')
def __init__(self, parent=Gui.getMainWindow()):
super().__init__(parent, QtCore.Qt.Tool)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
self.setAttribute(QtCore.Qt.WA_WindowPropagation, True)
self.initUI()
if Gui.activeView() and repr(Gui.activeView()) == 'View3DInventor':
self.view = Gui.activeView()
else:
pass # Only running this in Gui active anyway
self.sel_observer = self.MySelObs()
self.sel_observer.selection.connect(self.writeSel)
self.sel_observer.clear.connect(self.writeSel)
Gui.Selection.addObserver(self.sel_observer)
def initUI(self):
self.setWindowTitle('Edge Gatherer Test')
my_lay = QtWidgets.QGridLayout(self)
self.setLayout(my_lay)
my_sel_ob_w = QtWidgets.QWidget(self)
my_lay.addWidget(my_sel_ob_w, 0, 1)
my_button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel, self)
my_lay.addWidget(my_button_box, 2, 0, 1, -1)
my_sel_ob_lay = QtWidgets.QVBoxLayout(my_sel_ob_w)
my_sel_ob_w.setLayout(my_sel_ob_lay)
my_sel_ob_lay.addWidget(QtWidgets.QLabel('Selection observer:', my_sel_ob_w))
self.my_sel_ob = QtWidgets.QPlainTextEdit(my_sel_ob_w)
self.my_sel_ob.setReadOnly(True)
self.my_sel_ob.setMaximumBlockCount(100)
my_sel_ob_lay.addWidget(self.my_sel_ob)
my_button_box.accepted.connect(self.accept)
my_button_box.rejected.connect(self.reject)
def writeSel(self, text):
self.my_sel_ob.appendPlainText(text)
def accept(self):
MSG('Good bye\n')
super().accept()
def reject(self):
print('\nSorry. [Cancel] doesn't work yet.\n')
def hideEvent(self, event):
if hasattr(self, 'view_callback'):
try:
self.view.removeEventCallback('SoEvent', self.view_callback)
except RuntimeError:
pass
Gui.Selection.removeObserver(self.sel_observer)
super().hideEvent(event)
MyDialog().show()
Code: Select all
def addSelection(self, doc, obj, sub, pnt):
Code: Select all
"Preselected: <doc>.<obj>.<sub> (pnt)
and also in the python console when clicked on as
Code: Select all
# Gui.Selection.addSelection(doc,obj,sub,pnt.x,pnt.y,pnt.z)
Code: Select all
self.sel_observer = self.MySelObs() # <<<<------<<<<
self.sel_observer.selection.connect(self.writeSel)
self.sel_observer.clear.connect(self.writeSel)
Gui.Selection.addObserver(self.sel_observer) # <<<<------<<<<