From 90019d071bd9c711a15357c06f971c8b3088a40d Mon Sep 17 00:00:00 2001 From: shai Date: Sat, 21 Dec 2024 12:54:43 +0200 Subject: [PATCH] Reinstate unfold unattended command. Issue #426 --- InitGui.py | 6 ++++ README.md | 7 ++-- SheetMetalTools.py | 24 +++++++------ SheetMetalUnfoldCmd.py | 82 +++++++++++++++++++++++++++++++++--------- package.xml | 2 +- 5 files changed, 91 insertions(+), 30 deletions(-) diff --git a/InitGui.py b/InitGui.py index 17efccd..f12b59a 100644 --- a/InitGui.py +++ b/InitGui.py @@ -27,6 +27,7 @@ import FreeCAD from FreeCAD import Gui import SheetMetalTools +from engineering_mode import engineering_mode_enabled # add translations path SMWBPath = SheetMetalTools.mod_path @@ -41,6 +42,7 @@ class SMWorkbench(Workbench): global SMIconPath global SMWBPath global SHEETMETALWB_VERSION + global engineering_mode_enabled MenuText = FreeCAD.Qt.translate("SheetMetal", "Sheet Metal") ToolTip = FreeCAD.Qt.translate( @@ -83,6 +85,10 @@ def Initialize(self): "SheetMetal_Forming", "SheetMetal_BaseShape", ] # A list of command names created in the line above + if engineering_mode_enabled(): + self.list.insert( + self.list.index("SheetMetal_Unfold") + 1, "SheetMetal_UnattendedUnfold" + ) self.appendToolbar( FreeCAD.Qt.translate("SheetMetal", "Sheet Metal"), self.list ) # creates a new toolbar with your commands diff --git a/README.md b/README.md index bed7dbd..40bb603 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,10 @@ Starting from FreeCAD 0.17 it can be installed via the [Addon Manager](https://g * FreeCAD Forum announcement/discussion [thread](https://forum.freecadweb.org/viewtopic.php?f=3&t=60818) #### Release notes: -* V0.6.10 20 Dec 2024: Make unfold object parametric. -* V0.6.02 14 Dec 2024: Fix Base Shape UI error when using different language. -* V0.6.01 06 Dec 2024: Add GUI to BaseBend command. +* V0.6.11 20 Dec 2024: Reinstate unattended unfold command. +* V0.6.10 20 Dec 2024: Make unfold object parametric. +* V0.6.02 14 Dec 2024: Fix Base Shape UI error when using different language. +* V0.6.01 06 Dec 2024: Add GUI to BaseBend command. - Fix detection of basebend belonging to body or not. - Update translations by [@hasecilu][hasecilu]. - Fix python compatibility by [@Syres916][Syres916]. diff --git a/SheetMetalTools.py b/SheetMetalTools.py index 9e43f39..77732c6 100644 --- a/SheetMetalTools.py +++ b/SheetMetalTools.py @@ -357,20 +357,23 @@ def taskLoadUI(*args): forms.append(Gui.PySideUic.loadUi(path)) return forms - def smGuiExportSketch(sketches, fileType, fileName): - filePath, _ = QtGui.QFileDialog.getSaveFileName( - Gui.getMainWindow(), - translate("SheetMetal","Export unfold sketch"), - fileName, # Default file path - f"Vector Files (*.{fileType})" # File type filters - ) + def smGuiExportSketch(sketches, fileType, fileName, useDialog = True): + if useDialog: + filePath, _ = QtGui.QFileDialog.getSaveFileName( + Gui.getMainWindow(), + translate("SheetMetal","Export unfold sketch"), + fileName, # Default file path + f"Vector Files (*.{fileType})" # File type filters + ) + else: + filePath = fileName if filePath: if fileType == "dxf": importDXF.export(sketches, filePath) else: importSVG.export(sketches, filePath) - def smAddNewObject(baseObj, newObj, activeBody, taskPanel): + def smAddNewObject(baseObj, newObj, activeBody, taskPanel = None): if activeBody is not None: activeBody.addObject(newObj) viewConf = GetViewConfig(baseObj) @@ -379,8 +382,9 @@ def smAddNewObject(baseObj, newObj, activeBody, taskPanel): #newObj.baseObject[0].ViewObject.Visibility = False baseObj.ViewObject.Visibility = False FreeCAD.ActiveDocument.recompute() - dialog = taskPanel(newObj) - Gui.Control.showDialog(dialog) + if taskPanel is not None: + dialog = taskPanel(newObj) + Gui.Control.showDialog(dialog) return def smCreateNewObject(baseObj, name, allowPartDesign = True): diff --git a/SheetMetalUnfoldCmd.py b/SheetMetalUnfoldCmd.py index ed37a1f..723a3a0 100644 --- a/SheetMetalUnfoldCmd.py +++ b/SheetMetalUnfoldCmd.py @@ -62,6 +62,27 @@ KFACTOR = 0.40 +########################################################################################################## +# Helper functions +########################################################################################################## +def smUnfoldExportSketches(obj, useDialog = True): + if len(obj.UnfoldSketches) == 0: + return + sketches = [] + if len(obj.UnfoldSketches) == 1: + sketchNames = [obj.UnfoldSketches[0]] + else: + sketchNames = obj.UnfoldSketches[1:] + for name in sketchNames: + sketch = obj.Document.getObject(name) + if sketch is None: + return + sketches.append(sketch) + exptype = obj.Proxy.ExportType + filename = f"{FreeCAD.ActiveDocument.FileName[0:-6]}-{obj.Name}.{exptype}" + SheetMetalTools.smGuiExportSketch(sketches, exptype, filename, useDialog) + + ########################################################################################################## # Object class ########################################################################################################## @@ -368,22 +389,7 @@ def reject(self): FreeCAD.ActiveDocument.recompute() def doExport(self): - obj = self.obj - if len(obj.UnfoldSketches) == 0: - return - sketches = [] - if len(obj.UnfoldSketches) == 1: - sketchNames = [obj.UnfoldSketches[0]] - else: - sketchNames = obj.UnfoldSketches[1:] - for name in sketchNames: - sketch = obj.Document.getObject(name) - if sketch is None: - return - sketches.append(sketch) - exptype = self.obj.Proxy.ExportType - filename = f"{FreeCAD.ActiveDocument.FileName[0:-6]}-{obj.Name}.{exptype}" - SheetMetalTools.smGuiExportSketch(sketches, exptype, filename) + smUnfoldExportSketches(self.obj) def populateMdsList(self): sheetnames = SheetMetalKfactor.getSpreadSheetNames() @@ -514,5 +520,49 @@ def Activated(self): def IsActive(self): return len(SheetMetalTools.smObjectsToRecompute) > 0 + class SMUnfoldUnattendedCommandClass: + """Unfold object""" + + def GetResources(self): + __dir__ = os.path.dirname(__file__) + iconPath = os.path.join(__dir__, "Resources", "icons") + return { + "Pixmap": os.path.join( + iconPath, "SheetMetal_UnfoldUnattended.svg" + ), # the name of a svg file available in the resources + "MenuText": FreeCAD.Qt.translate("SheetMetal", "Unattended Unfold"), + "Accel": "U", + "ToolTip": FreeCAD.Qt.translate( + "SheetMetal", + "Flatten folded sheet metal object with default options\n" + "1. Select flat face on sheetmetal shape.\n" + "2. Click this command to unfold the object with last used parameters.", + ), + } + + def Activated(self): + sel = Gui.Selection.getSelectionEx()[0] + selobj = sel.Object + newObj, activeBody = SheetMetalTools.smCreateNewObject(selobj, "Unfold", False) + if newObj is None: + return + SMUnfold(newObj, selobj, sel.SubElementNames) + SMUnfoldViewProvider(newObj.ViewObject) + SheetMetalTools.smAddNewObject(selobj, newObj, activeBody) + smUnfoldExportSketches(newObj, False) + return + + def IsActive(self): + if ( + len(Gui.Selection.getSelection()) != 1 + or len(Gui.Selection.getSelectionEx()[0].SubElementNames) != 1 + ): + return False + selFace = Gui.Selection.getSelectionEx()[0].SubObjects[0] + + return isinstance(selFace.Surface, Part.Plane) + + + Gui.addCommand("SheetMetal_UnattendedUnfold", SMUnfoldUnattendedCommandClass()) Gui.addCommand("SheetMetal_Unfold", SMUnfoldCommandClass()) Gui.addCommand("SheetMetal_UnfoldUpdate", SMRecomputeUnfoldsCommandClass()) \ No newline at end of file diff --git a/package.xml b/package.xml index 36df17c..524baab 100644 --- a/package.xml +++ b/package.xml @@ -2,7 +2,7 @@ SheetMetal Workbench A simple sheet metal tools workbench for FreeCAD. - 0.6.10 + 0.6.11 2024-12-20 Shai Seger LGPL-2.1-or-later