Browse Source

Added code to initialize pages when loading a document (fix for balloons visibility and image file path)

master
Youen 2 years ago
parent
commit
13ff11293a
  1. 20
      ahb_document_observer.py
  2. 4
      ahb_raster_view.py
  3. 41
      ahb_techdraw_extensions.py

20
ahb_document_observer.py

@ -43,12 +43,29 @@ class DocLinkObserver:
class DocObserver:
changed_object_by_type = {}
selection_by_type = {}
doc_callbacks = {}
was_selected = []
def __init__(self):
Gui.Selection.addObserver(self)
def slotActivateDocument(self, doc):
#print('slotActivateDocument', doc.Name)
self._triggerDocumentEvent(doc, 'activate')
def slotCreatedDocument(self, doc):
#print('slotCreatedDocument', doc.Name)
self._triggerDocumentEvent(doc, 'created')
def slotDeletedDocument(self, doc):
#print('slotDeletedDocument', doc.Name)
self._triggerDocumentEvent(doc, 'deleted')
def _triggerDocumentEvent(self, doc, event):
for callback in self.doc_callbacks.values():
callback(doc, event)
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)
@ -95,3 +112,6 @@ class DocObserver:
callbacks = {}
self.selection_by_type[type_id] = callbacks
callbacks[callback_id] = callback
def onDocumentEvent(self, callback_id: str, callback):
self.doc_callbacks[callback_id] = callback

4
ahb_raster_view.py

@ -207,7 +207,7 @@ class RasterView:
self.image_view.Assembly_handbook_ViewVolumeHeight = viewVolume.getHeight()
self.image_view.Assembly_handbook_ViewVolumeDepth = viewVolume.getDepth()
max_res = 3200 # todo: keep aspect ratio when we limit max image dimensions
max_res = 3200
#max_res = 1500
resolution = [
int(viewVolume.getWidth() * view.Scale * 10),
@ -291,7 +291,7 @@ class RasterView:
image.Scale = image_scale
image.X = view.X
image.Y = view.Y
image.ImageFile = self.image_file_name # TODO: see if it's possible to set a relative path
image.ImageFile = self.image_file_name
image.ViewObject.Crop = True
image.Width = composite_img.size[0] * image_scale / 10.0 * 1.01
image.Height = composite_img.size[1] * image_scale / 10.0 * 1.01

41
ahb_techdraw_extensions.py

@ -77,10 +77,16 @@ class TechDrawExtensions:
enable_selected_part_highlight = False # disable for now, for performance reasons
initialized_documents = []
def __init__(self):
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
workbench.docObserver.onObjectTypeChanged('balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop))
workbench.docObserver.onObjectTypeSelected('balloon_selected', 'TechDraw::DrawViewBalloon', lambda operation, obj, sub, point: self.onBalloonSelected(operation, obj, sub, point))
workbench.docObserver.onDocumentEvent('techdrawext_doc_event', lambda doc, event: self.onDocumentEvent(doc, event))
if App.ActiveDocument is not None:
self.onDocumentEvent(App.ActiveDocument, 'activate')
def repaint(self, view, fast_render = True):
self.views_to_repaint[view] = fast_render
@ -646,7 +652,19 @@ class TechDrawExtensions:
QTimer.singleShot(10, restoreKeepUpdated)
def refreshOverlays(self, page, callback = None):
import os
doc = page.Document
for image in page.Views:
if image.TypeId == 'TechDraw::DrawViewImage':
folder_name = '/' + os.path.basename(doc.FileName).replace('.FCStd', '') + '_raster/'
if folder_name in image.ImageFile:
full_path = doc.FileName.replace('.FCStd', '') + '_raster/' + image.ImageFile.split(folder_name)[1]
if image.ImageFile != full_path:
image.ImageFile = full_path
if page.KeepUpdated:
if callback:
callback()
else:
page.KeepUpdated = True
@ -772,3 +790,26 @@ class TechDrawExtensions:
cache = ViewCache()
self.view_cache[view] = cache
return cache
def onDocumentEvent(self, doc, event):
if event == 'activate':
if doc not in self.initialized_documents:
self.initialized_documents.append(doc)
self.initializeDocument(doc)
elif event == 'deleted':
if doc in self.initialized_documents:
self.initialized_documents.remove(doc)
def initializeDocument(self, doc):
def doInit():
try:
for obj in doc.Objects:
if obj.TypeId == 'TechDraw::DrawPage':
self.onPageLoaded(obj)
except:
pass
QTimer.singleShot(0, doInit)
def onPageLoaded(self, page):
self.refreshOverlays(page)

Loading…
Cancel
Save