From 9dbaa141f1a06696d49f8f21a7b9adae25841067 Mon Sep 17 00:00:00 2001 From: Youen Date: Sat, 22 Oct 2022 17:35:50 +0200 Subject: [PATCH] multiple small improvements - improved naming of balloons (part name appears more clearly, balloon numbering is at the end) - balloons for parts that were already in the previous step are hidden by default - disabled highlighting of part drawing related to the selected balloon (for performance reasons) - added possibility to explicitely set the display name of a part (instead of using the file name) - added workaround for balloons that are not rendered until selected --- ahb_cmd_view_annotate.py | 7 ++- ahb_techdraw_extensions.py | 94 ++++++++++++++++++++++++++++---------- 2 files changed, 76 insertions(+), 25 deletions(-) diff --git a/ahb_cmd_view_annotate.py b/ahb_cmd_view_annotate.py index 1b678d5..b61ebec 100644 --- a/ahb_cmd_view_annotate.py +++ b/ahb_cmd_view_annotate.py @@ -49,7 +49,9 @@ class AHB_View_Annotate: if balloon is None: partName = partLink.Name - balloon = doc.addObject("TechDraw::DrawViewBalloon", "Balloon" + partName) + balloonName = partName + "_Balloon" + + balloon = doc.addObject("TechDraw::DrawViewBalloon", balloonName) balloon.SourceView = view balloon.addProperty("App::PropertyString", "Assembly_handbook_PartName", "Assembly_handbook") @@ -64,6 +66,9 @@ class AHB_View_Annotate: balloon.X = int(balloon.OriginX) + 20 balloon.Y = int(balloon.OriginY) + 20 + + if not workbench.techDrawExtensions.isNewPartInView(view, partLink): + balloon.ViewObject.Visibility = False else: workbench.techDrawExtensions.updateBalloon(balloon) diff --git a/ahb_techdraw_extensions.py b/ahb_techdraw_extensions.py index 70259d8..2f54ff5 100644 --- a/ahb_techdraw_extensions.py +++ b/ahb_techdraw_extensions.py @@ -69,6 +69,8 @@ class TechDrawExtensions: edited_view = None + enable_selected_part_highlight = False # disable for now, for performance reasons + def __init__(self): workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench workbench.docObserver.onObjectTypeChanged('balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop)) @@ -101,10 +103,11 @@ class TechDrawExtensions: parts_to_paint = [] # repaint parts that are highlighted by selection - for balloon in selected_balloons: - part = doc.getObject(balloon.Assembly_handbook_PartName) - if part in view.XSource: - parts_to_paint.append(part) + if self.enable_selected_part_highlight: + for balloon in selected_balloons: + part = doc.getObject(balloon.Assembly_handbook_PartName) + if part in view.XSource: + parts_to_paint.append(part) # repaint parts that are new in this step (thick line) if 'Assembly_handbook_PreviousStepView' in view.PropertiesList: @@ -122,7 +125,7 @@ class TechDrawExtensions: default_line_thickness = 0.05 line_thickness = default_line_thickness - default_color = (0, 0, 0) + default_color = (0.65, 0.65, 0.65) color = default_color if part is not None: @@ -134,11 +137,13 @@ class TechDrawExtensions: prev_view = doc.getObject(view.Assembly_handbook_PreviousStepView) if prev_view is not None: if not part in prev_view.XSource: - line_thickness = 0.3 - - for balloon in selected_balloons: - if part.Name == balloon.Assembly_handbook_PartName: - color = (0.0, 0.85, 0.0) # selection highlighting + line_thickness = 0.2 + color = (0, 0, 0) + + if self.enable_selected_part_highlight: + for balloon in selected_balloons: + if part.Name == balloon.Assembly_handbook_PartName: + color = (0.0, 0.85, 0.0) # selection highlighting # iterate edges of actual view and highlight matching edges for edgeIdx in range(10000): @@ -181,18 +186,24 @@ class TechDrawExtensions: view.requestPaint() - cursor = self.view_cursors.get(view, None) - if cursor is None: - cursor = CursorItem(view = view) - TDG.addQGIToView(view, cursor); - cursor.onViewPosChange(lambda new_pos: self.onCursorMoved(view, new_pos)) - self.view_cursors[view] = cursor - - if len(selected_balloons) == 0: - cursor.setVisible(False) - else: - cursor.setViewPos(App.Vector(selected_balloons[0].OriginX, selected_balloons[0].OriginY)) - cursor.setVisible(True) + def updateBalloonCursor(self, view): + selected_balloons = [] + for obj in Gui.Selection.getSelection(): + if obj.TypeId == 'TechDraw::DrawViewBalloon' and obj.SourceView == view and 'Assembly_handbook_PartName' in obj.PropertiesList: + selected_balloons.append(obj) + + cursor = self.view_cursors.get(view, None) + if cursor is None: + cursor = CursorItem(view = view) + TDG.addQGIToView(view, cursor); + cursor.onViewPosChange(lambda new_pos: self.onCursorMoved(view, new_pos)) + self.view_cursors[view] = cursor + + if len(selected_balloons) == 0: + cursor.setVisible(False) + else: + cursor.setViewPos(App.Vector(selected_balloons[0].OriginX, selected_balloons[0].OriginY)) + cursor.setVisible(True) def setCurrentViewDirection(self, view): from pivy import coin @@ -299,7 +310,9 @@ class TechDrawExtensions: if "Assembly_handbook_PartName" in balloon.PropertiesList: #print(operation + " " + balloon.Name) view = balloon.SourceView - self.repaint(view) + self.updateBalloonCursor(view) + if self.enable_selected_part_highlight: + self.repaint(view) # disabled for now, for performance reasons def onBalloonChanged(self, obj, prop): # Avoid reentry @@ -319,17 +332,37 @@ class TechDrawExtensions: doc = view.Document obj = doc.getObject(balloon.Assembly_handbook_PartName) + partDisplayName = self.getPartDisplayName(obj) + objectCenterView = workbench.techDrawExtensions.computePartCenter(view, obj) balloon.OriginX = objectCenterView.x + balloon.Assembly_handbook_OriginOffsetX balloon.OriginY = objectCenterView.y + balloon.Assembly_handbook_OriginOffsetY - balloon.Text = obj.LinkedObject.Document.Name if obj.TypeId == 'App::Link' else obj.Name + balloon.Text = partDisplayName balloon.ViewObject.Font = 'DejaVu Sans' balloon.ViewObject.Fontsize = 4 balloon.BubbleShape = 'Inspection' balloon.EndTypeScale = 4 + def getPartDisplayName(self, obj): + if obj.TypeId == 'App::Link': + linked_obj = obj.LinkedObject + if 'Assembly_handbook_PartDisplayName' in linked_obj.PropertiesList: + return linked_obj.Assembly_handbook_PartDisplayName + else: + return linked_obj.Document.Name + return obj.Name + + def isNewPartInView(self, view, obj): + doc = view.Document + if 'Assembly_handbook_PreviousStepView' in view.PropertiesList: + prev_view = doc.getObject(view.Assembly_handbook_PreviousStepView) + if prev_view is not None: + if not obj in prev_view.XSource: + return True + return False + def getActivePage(self): activeView = Gui.activeView() if activeView is None: return None @@ -347,6 +380,19 @@ class TechDrawExtensions: for view in page.Views: if view.TypeId == 'TechDraw::DrawViewPart' and 'Assembly_handbook_PreviousStepView' in view.PropertiesList: self.refreshView(view) + elif view.TypeId == 'TechDraw::DrawViewBalloon': + if view.ViewObject.Visibility: + # workaround for a TechDraw bug: sometimes the balloon should be visible but doesn't appear, showing it again fixes the issue + view.ViewObject.Visibility = False + def makeRedrawCallback(view): + def redrawBalloon(): + view.ViewObject.Visibility = True + return redrawBalloon + QTimer.singleShot(0, makeRedrawCallback(view)) + else: + # workaround for a TechDraw bug: sometimes the balloon text is visible even if the balloon is hidden, hiding it again fixes the issue + view.ViewObject.Visibility = True + view.ViewObject.Visibility = False if page.KeepUpdated: for view in page.Views: