From 339ebba88eb4830b38ce5b4351f4683a2244f81b Mon Sep 17 00:00:00 2001 From: Youen Date: Sat, 15 Oct 2022 12:28:58 +0200 Subject: [PATCH] Added button to insert a new step --- InitGui.py | 3 ++ ahb_cmd_new_step.py | 99 ++++++++++++++++++++++++++++++++++++++ ahb_parts_cache.py | 2 +- ahb_techdraw_extensions.py | 2 +- 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 ahb_cmd_new_step.py diff --git a/InitGui.py b/InitGui.py index ae6f840..826d8ac 100644 --- a/InitGui.py +++ b/InitGui.py @@ -88,6 +88,9 @@ class AssemblyHandbookWorkbench(Gui.Workbench): #self.importModule('ahb_cmd_animate') #toolbox.append("AHB_animate") + self.importModule('ahb_cmd_new_step') + toolbox.append("AHB_new_step") + self.importModule('ahb_cmd_view_annotate') toolbox.append("AHB_view_annotate") diff --git a/ahb_cmd_new_step.py b/ahb_cmd_new_step.py new file mode 100644 index 0000000..5a192e8 --- /dev/null +++ b/ahb_cmd_new_step.py @@ -0,0 +1,99 @@ +import FreeCADGui as Gui +import FreeCAD as App + +class AHB_New_Step: + def GetResources(self): + return {"MenuText": "New Step", + "ToolTip": "Creates a page for the next step (first, select the view of the preceding step, if any)", + "Pixmap": "" + } + + def IsActive(self): + return True + + def Activated(self): + import re + import os + import sys + from PySide.QtCore import QTimer + + workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench + + if len(Gui.Selection.getSelection()) > 1: + raise Exception("Please either select exactly one TechDraw view, or nothing at all") + + + prev_view = None if len(Gui.Selection.getSelection()) == 0 else Gui.Selection.getSelection()[0] + if prev_view is not None and prev_view.TypeId != 'TechDraw::DrawViewPart': + raise Exception("Selected object is not a TechDraw view") + + doc = App.ActiveDocument if prev_view is None else prev_view.Document + + freecad_path = os.path.normpath(os.path.dirname(sys.executable) + '/..') # 'usr' absolute path + + if prev_view is None: + step_number = 1 + keep_updated = False + template_file_name = freecad_path + '/share/Mod/TechDraw/Templates/A4_Landscape_blank.svg' + parent_group = None + else: + prev_page = workbench.techDrawExtensions.getViewPage(prev_view) + prev_page_group = prev_page.getParentGroup() + if prev_page_group is not None: + parent_group = prev_page_group.getParentGroup() + keep_updated = prev_page.KeepUpdated + template_file_name = prev_page.Template.Template + numbers = re.findall(r'\d+', prev_page.Label) + if len(numbers) == 0: prev_number = 0 + else: prev_number = max(1, int(numbers[-1])) + step_number = prev_number + 1 + + step_num_str = str(step_number).zfill(3) + + group = doc.addObject('App::DocumentObjectGroup', 'Etape' + step_num_str) + if parent_group is not None: + parent_group.addObject(group) + + page_name = 'Etape' + step_num_str + '_Page' + page = doc.addObject('TechDraw::DrawPage', page_name) + group.addObject(page) + if page.KeepUpdated != keep_updated: page.KeepUpdated = keep_updated + template = doc.addObject('TechDraw::DrawSVGTemplate', 'Template') + template.Template = template_file_name + page.Template = template + + view = doc.addObject('TechDraw::DrawViewPart', 'View') + view.Perspective = False + view.addProperty("App::PropertyString", "AssemblyHandbook_PreviousStepView", "AssemblyHandbook") + if prev_view is not None: + view.AssemblyHandbook_PreviousStepView = prev_view.Name + view.X = prev_view.X + view.Y = prev_view.Y + view.Direction = prev_view.Direction + view.XDirection = prev_view.XDirection + view.ScaleType = prev_view.ScaleType + view.Scale = prev_view.Scale + view.XSource = prev_view.XSource + + page.addView(view) + view.recompute() + + # search for views after the prev view to relink them after the new view (i.e. we insert the new view as an intermediate step) + # TODO: re-number next steps if needed + if prev_view is not None: + for obj in doc.Objects: + if obj != view and obj.TypeId == 'TechDraw::DrawViewPart' and 'AssemblyHandbook_PreviousStepView' in obj.PropertiesList and obj.AssemblyHandbook_PreviousStepView == prev_view.Name: + obj.AssemblyHandbook_PreviousStepView = view.Name + print(obj.Label + ' has been moved after the new step') + + if not page.KeepUpdated and len(view.XSource) > 0: + page.KeepUpdated = True + def restoreKeepUpdated(): + page.KeepUpdated = False + QTimer.singleShot(100, restoreKeepUpdated) + + Gui.Selection.clearSelection() + Gui.Selection.addSelection(page) + +from ahb_command import AHB_CommandWrapper +AHB_CommandWrapper.addGuiCommand('AHB_new_step', AHB_New_Step()) diff --git a/ahb_parts_cache.py b/ahb_parts_cache.py index 4abcbf0..d23c182 100644 --- a/ahb_parts_cache.py +++ b/ahb_parts_cache.py @@ -51,7 +51,7 @@ class PartCachedView: self.cached_lines = np.empty([numLines, 4]) lineIdx = 0 for edge in tmpEdges: - if not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1: + if (not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1) and len(edge.Vertexes) == 2: sx = 1.0 sy = -1.0 self.cached_lines[lineIdx] = [ diff --git a/ahb_techdraw_extensions.py b/ahb_techdraw_extensions.py index 0d6c55e..2880467 100644 --- a/ahb_techdraw_extensions.py +++ b/ahb_techdraw_extensions.py @@ -115,7 +115,7 @@ class TechDrawExtensions: if is_first_part: view.formatGeometricEdge(edgeIdx,1,0.25,0,True) - if balloon is not None and (not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1): + if balloon is not None and (not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1) and len(edge.Vertexes) == 2: edgeData = [ edge.Vertexes[0].X - center.x, edge.Vertexes[0].Y - center.y,