From 57f3d26d68009dc66a057306294da22c2ac9fbd2 Mon Sep 17 00:00:00 2001 From: DeepSOIC Date: Thu, 14 Jun 2018 01:03:17 +0300 Subject: [PATCH] add partdesign toolbar --- InitGui.py | 5 +++-- Lattice2PartDesignFeatures.py | 1 + lattice2InjectedToolbars.py | 35 +++++++++++++++++++++++++++++ lattice2PDPattern.py | 36 +----------------------------- lattice2PDPatternCommand.py | 42 +++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 37 deletions(-) create mode 100644 lattice2InjectedToolbars.py create mode 100644 lattice2PDPatternCommand.py diff --git a/InitGui.py b/InitGui.py index 83bd26b..9ecc15b 100644 --- a/InitGui.py +++ b/InitGui.py @@ -32,7 +32,8 @@ __Status__ = 'alpha' __Requires__ = 'freecad 0.16.5155' __Communication__ = 'vv.titov@gmail.com; DeepSOIC on FreeCAD forum' - +import lattice2InjectedToolbars as TB +TB.registerPDToolbar() class Lattice2Workbench (Workbench): MenuText = 'Lattice2' @@ -85,7 +86,7 @@ class Lattice2Workbench (Workbench): self.appendMenu('Lattice2', cmdsCompoundTools) cmdsPDTools = ([] - + Lattice2.PartDesignFeatures.PDPattern.exportedCommands + + Lattice2.PartDesignFeatures.PDPatternCommand.exportedCommands ) self.appendToolbar('Lattice2PartDesignFeatres', cmdsPDTools) self.appendMenu('Lattice2', cmdsPDTools) diff --git a/Lattice2PartDesignFeatures.py b/Lattice2PartDesignFeatures.py index ccffce1..c95b596 100644 --- a/Lattice2PartDesignFeatures.py +++ b/Lattice2PartDesignFeatures.py @@ -1 +1,2 @@ import lattice2PDPattern as PDPattern +import lattice2PDPatternCommand as PDPatternCommand diff --git a/lattice2InjectedToolbars.py b/lattice2InjectedToolbars.py new file mode 100644 index 0000000..3765238 --- /dev/null +++ b/lattice2InjectedToolbars.py @@ -0,0 +1,35 @@ +import FreeCAD as App + +def findToolbar(name, label, workbench, create = False): + """findToolbar(name, label, workbench, create= False): returns tuple "User parameter:BaseApp/Workbench/Global/Toolbar", "toolbar_group_name".""" + tb_root = "User parameter:BaseApp/Workbench/{workbench}/Toolbar".format(workbench= workbench) + pp = App.ParamGet(tb_root) + if pp.HasGroup(name): + return [tb_root, name] + + for i in range(10): + g = 'Custom_'+str(i) + if pp.HasGroup(g) and pp.GetGroup(g).GetString('Name') == label: + return [tb_root, g] + if create: + return [tb_root, name] + return None + +def findGlobalToolbar(name, label, create = False): + return findToolbar(name, label, 'Global', create) + +def findPDToolbar(name, label, create = False): + return findToolbar(name, label, 'PartDesignWorkbench', create) + +def registerPDToolbar(): + creating_anew = not isPDRegistered() + p = App.ParamGet('/'.join(findPDToolbar('Lattice2',"Lattice2 PartDesign", create= True))) + p.SetString("Name", "Lattice2 PartDesign") + import lattice2PDPatternCommand as PDPattern + for cmd in PDPattern.exportedCommands: + p.SetString(cmd, "FreeCAD") + if creating_anew: + p.SetBool("Active", 1) + +def isPDRegistered(): + return findPDToolbar('Lattice2',"Lattice2 PartDesign") diff --git a/lattice2PDPattern.py b/lattice2PDPattern.py index 1e4643a..92383bb 100644 --- a/lattice2PDPattern.py +++ b/lattice2PDPattern.py @@ -357,38 +357,4 @@ def cmdPDPattern(): " a template body and two placements/arrays, one from selected body and one from active body." ) - -class CommandLatticePDPattern: - "Command to create Lattice PartDesign Pattern feature" - def GetResources(self): - return {'Pixmap' : getIconPath("Lattice2_PDPattern.svg"), - 'MenuText': "Lattice PartDesign Pattern", - 'Accel': "", - 'ToolTip': "Lattice PartDesign Pattern command. Replicates partdesign features at every placement in array."} - - def Activated(self): - try: - if len(FreeCADGui.Selection.getSelection())==0: - infoMessage("Lattice PartDesign Pattern", - "Lattice PartDesign Pattern command. Replicates partdesign features at every placement in array.\n\n" - "Please select features to repeat, reference placement (optional), and target placement/array. \n\n" - "You can use features from another body. Then, reference placement is required. You can also select a body (a \"template body\"), then all features from that body will be replicated.\n\n" - "Please observe scope restrictions. Reference placement must be in same body the original features are in; target placement/array must be in active body. You can create Lattice Arrays " - "right in PartDesign bodies, but you can't drag them in after the fact. You can import arrays of placements from elsewhere using a Shapebinder, or Part-o-Magic Ghost.") - return - if activeBody() is None: - infoMessage("Lattice PartDesign Pattern", "No active body. Please, activate a body, first.") - cmdPDPattern() - except Exception as err: - msgError(err) - - def IsActive(self): - if FreeCAD.ActiveDocument: - return True - else: - return False - -if FreeCAD.GuiUp: - FreeCADGui.addCommand('Lattice2_PDPattern', CommandLatticePDPattern()) - -exportedCommands = ['Lattice2_PDPattern'] +# command defined in lattice2PDPatternCommand.py \ No newline at end of file diff --git a/lattice2PDPatternCommand.py b/lattice2PDPatternCommand.py new file mode 100644 index 0000000..e192d43 --- /dev/null +++ b/lattice2PDPatternCommand.py @@ -0,0 +1,42 @@ +# all imports are in command's activated(), for to speed up FC startup +import FreeCAD +if FreeCAD.GuiUp: + import FreeCADGui + +class CommandLatticePDPattern: + "Command to create Lattice PartDesign Pattern feature" + def GetResources(self): + import lattice2_rc + return {'Pixmap' : ":/icons/Lattice2_PDPattern.svg", + 'MenuText': "Lattice PartDesign Pattern", + 'Accel': "", + 'ToolTip': "Lattice PartDesign Pattern command. Replicates partdesign features at every placement in array."} + + def Activated(self): + from lattice2Common import infoMessage, msgError, activeBody + from lattice2PDPattern import cmdPDPattern + try: + if len(FreeCADGui.Selection.getSelection())==0: + infoMessage("Lattice PartDesign Pattern", + "Lattice PartDesign Pattern command. Replicates partdesign features at every placement in array.\n\n" + "Please select features to repeat, reference placement (optional), and target placement/array. \n\n" + "You can use features from another body. Then, reference placement is required. You can also select a body (a \"template body\"), then all features from that body will be replicated.\n\n" + "Please observe scope restrictions. Reference placement must be in same body the original features are in; target placement/array must be in active body. You can create Lattice Arrays " + "right in PartDesign bodies, but you can't drag them in after the fact. You can import arrays of placements from elsewhere using a Shapebinder, or Part-o-Magic Ghost.") + return + if activeBody() is None: + infoMessage("Lattice PartDesign Pattern", "No active body. Please, activate a body, first.") + cmdPDPattern() + except Exception as err: + msgError(err) + + def IsActive(self): + if FreeCAD.ActiveDocument: + return True + else: + return False + +if FreeCAD.GuiUp: + FreeCADGui.addCommand('Lattice2_PDPattern', CommandLatticePDPattern()) + +exportedCommands = ['Lattice2_PDPattern']