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.CoarseView = True view.addProperty("App::PropertyString", "Assembly_handbook_PreviousStepView", "Assembly_handbook") if prev_view is None: try: workbench.techDrawExtensions.setCurrentViewDirection(view) except: pass else: view.Assembly_handbook_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 'Assembly_handbook_PreviousStepView' in obj.PropertiesList and obj.Assembly_handbook_PreviousStepView == prev_view.Name: obj.Assembly_handbook_PreviousStepView = view.Name print(obj.Label + ' has been moved after the new step') if len(view.XSource) > 0: workbench.techDrawExtensions.forceRedrawPage(page) Gui.Selection.clearSelection() Gui.Selection.addSelection(page) from ahb_command import AHB_CommandWrapper AHB_CommandWrapper.addGuiCommand('AHB_new_step', AHB_New_Step())