forked from youen/assembly_handbook
Youen
2 years ago
2 changed files with 142 additions and 14 deletions
@ -0,0 +1,58 @@ |
|||||||
|
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 |
Loading…
Reference in new issue