You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.3 KiB
58 lines
2.3 KiB
import FreeCAD as App |
|
import FreeCADGui as Gui |
|
|
|
class DocObserver: |
|
changed_object_by_type = {} |
|
selection_by_type = {} |
|
|
|
was_selected = [] |
|
|
|
def __init__(self): |
|
Gui.Selection.addObserver(self) |
|
|
|
def slotChangedObject(self, obj, prop): |
|
#print("object changed: " + str(obj).replace('<', '').replace(' object>', '') + " " + obj.Name + " : " + str(prop)) |
|
callbacks = self.changed_object_by_type.get(obj.TypeId, None) |
|
if callbacks is not None: |
|
for callback in callbacks.values(): |
|
callback(obj, prop) |
|
|
|
def addSelection(self, doc, obj_name, sub, pnt): |
|
self.was_selected = Gui.Selection.getSelection() |
|
obj = App.getDocument(doc).getObject(obj_name) |
|
callbacks = self.selection_by_type.get(obj.TypeId, None) |
|
if callbacks is not None: |
|
for callback in callbacks.values(): |
|
callback('added', obj, sub, pnt) |
|
|
|
def removeSelection(self, doc, obj_name, sub): |
|
self.was_selected = Gui.Selection.getSelection() |
|
obj = App.getDocument(doc).getObject(obj_name) |
|
callbacks = self.selection_by_type.get(obj.TypeId, None) |
|
if callbacks is not None: |
|
for callback in callbacks.values(): |
|
callback('removed', obj, sub, None) |
|
|
|
def clearSelection(self, p): |
|
was_selected = self.was_selected |
|
self.was_selected = [] |
|
|
|
for obj in was_selected: |
|
callbacks = self.selection_by_type.get(obj.TypeId, None) |
|
if callbacks is not None: |
|
for callback in callbacks.values(): |
|
callback('removed', obj, None, None) |
|
|
|
def onObjectTypeChanged(self, callback_id: str, type_id: str, callback): |
|
callbacks = self.changed_object_by_type.get(type_id, None) |
|
if callbacks is None: |
|
callbacks = {} |
|
self.changed_object_by_type[type_id] = callbacks |
|
callbacks[callback_id] = callback |
|
|
|
def onObjectTypeSelected(self, callback_id: str, type_id: str, callback): |
|
callbacks = self.selection_by_type.get(type_id, None) |
|
if callbacks is None: |
|
callbacks = {} |
|
self.selection_by_type[type_id] = callbacks |
|
callbacks[callback_id] = callback |