From 979d83a22b59f88e2f6ddefad094180cd971a0d0 Mon Sep 17 00:00:00 2001 From: Roy-043 <70520633+Roy-043@users.noreply.github.com> Date: Fri, 25 Aug 2023 03:47:39 +0200 Subject: [PATCH] Draft: Housekeeping and minor improvements for some draftguitools (#10339) --- src/Mod/Draft/draftguitools/gui_base.py | 2 - .../Draft/draftguitools/gui_base_original.py | 74 +++++++------------ src/Mod/Draft/draftguitools/gui_lines.py | 59 ++++++--------- 3 files changed, 51 insertions(+), 84 deletions(-) diff --git a/src/Mod/Draft/draftguitools/gui_base.py b/src/Mod/Draft/draftguitools/gui_base.py index fd1b3ab9ee..abacacfbdf 100644 --- a/src/Mod/Draft/draftguitools/gui_base.py +++ b/src/Mod/Draft/draftguitools/gui_base.py @@ -35,8 +35,6 @@ import draftutils.todo as todo from draftutils.messages import _msg, _log -__metaclass__ = type # to support Python 2 use of `super()` - class GuiCommandSimplest: """Simplest base class for GuiCommands. diff --git a/src/Mod/Draft/draftguitools/gui_base_original.py b/src/Mod/Draft/draftguitools/gui_base_original.py index 1b58a33d9b..31e8dce90e 100644 --- a/src/Mod/Draft/draftguitools/gui_base_original.py +++ b/src/Mod/Draft/draftguitools/gui_base_original.py @@ -44,16 +44,12 @@ import draftguitools.gui_tool_utils as gui_tool_utils from draftutils.messages import _msg, _log -__metaclass__ = type # to support Python 2 use of `super()` - class DraftTool: """The base class of all Draft Tools. This is the original class that was defined in `DraftTools.py` before any re-organization of the code. - It must be preserved exactly like this to keep the original tools - running without problems. This class is subclassed by `Creator` and `Modifier` to set up a few additional properties of these two types. @@ -84,7 +80,7 @@ class DraftTool: else: return False - def Activated(self, name="None", noplanesetup=False, is_subtool=False): + def Activated(self, name="None", is_subtool=False): """Execute when the command is called. If an active Gui Command exists, it will call the `finish` method @@ -100,11 +96,6 @@ class DraftTool: It defaults to `'None'`. It is the `featureName` of the object, to know what is being run. - noplanesetup: bool, optional - It defaults to `False`. - If it is `False` it will set up the working plane - by running `App.DraftWorkingPlane.setup()`. - is_subtool: bool, optional It defaults to `False`. This is set to `True` when we want to modify an object @@ -113,8 +104,13 @@ class DraftTool: That is, first we run `Draft_SubelementHighlight` then we can use `Draft_Move` while setting `is_subtool` to `True`. """ + self.doc = App.ActiveDocument + if not self.doc: + return + if App.activeDraftCommand and not is_subtool: App.activeDraftCommand.finish() + App.activeDraftCommand = self # The Part module is first initialized when using any Gui Command # for the first time. @@ -122,28 +118,22 @@ class DraftTool: import Part import DraftGeomUtils - self.ui = None self.call = None - self.support = None - self.point = None self.commitList = [] - self.doc = App.ActiveDocument - if not self.doc: - self.finish() - return - - App.activeDraftCommand = self - self.view = gui_utils.get_3d_view() - self.ui = Gui.draftToolBar - self.featureName = name - self.ui.sourceCmd = self - if not noplanesetup: - App.DraftWorkingPlane.setup() - self.node = [] - self.pos = [] self.constrain = None - self.obj = None self.extendedCopy = False + self.featureName = name + self.node = [] + self.obj = None + self.point = None + self.pos = [] + self.support = None + self.ui = Gui.draftToolBar + self.ui.sourceCmd = self + self.view = gui_utils.get_3d_view() + + App.DraftWorkingPlane.setup() + self.planetrack = None if utils.get_param("showPlaneTracker", False): self.planetrack = trackers.PlaneTracker() @@ -264,18 +254,11 @@ class DraftTool: class Creator(DraftTool): """A generic Creator tool, used by creation tools such as line or arc. - It runs the Activated method from the parent class. - If `noplanesetup` is `False`, it sets the appropriate `support` attribute - and sets the working plane with `gui_tool_utils.get_support`. - It inherits `DraftTool`, which sets up the majority of the behavior of this class. """ - def __init__(self): - super(Creator, self).__init__() - - def Activated(self, name="None", noplanesetup=False): + def Activated(self, name="None"): """Execute when the command is called. Parameters @@ -283,15 +266,12 @@ class Creator(DraftTool): name: str, optional It defaults to `'None'`. It is the `featureName` of the object, to know what is being run. - - noplanesetup: bool, optional - It defaults to `False`. - If it is `False` it will set up the working plane - by running `App.DraftWorkingPlane.setup()`. """ - super(Creator, self).Activated(name, noplanesetup) - if not noplanesetup: - self.support = gui_tool_utils.get_support() + super().Activated(name) + # call DraftWorkingPlane.save to sync with + # DraftWorkingPlane.restore called in finish method + App.DraftWorkingPlane.save() + self.support = gui_tool_utils.get_support() class Modifier(DraftTool): @@ -305,11 +285,11 @@ class Modifier(DraftTool): """ def __init__(self): - super(Modifier, self).__init__() + super().__init__() self.copymode = False - def Activated(self, name="None", noplanesetup=False, is_subtool=False): - super(Modifier, self).Activated(name, noplanesetup, is_subtool) + def Activated(self, name="None", is_subtool=False): + super().Activated(name, is_subtool) # call DraftWorkingPlane.save to sync with # DraftWorkingPlane.restore called in finish method App.DraftWorkingPlane.save() diff --git a/src/Mod/Draft/draftguitools/gui_lines.py b/src/Mod/Draft/draftguitools/gui_lines.py index e0584c2c16..3c8a4885b3 100644 --- a/src/Mod/Draft/draftguitools/gui_lines.py +++ b/src/Mod/Draft/draftguitools/gui_lines.py @@ -52,7 +52,7 @@ class Line(gui_base_original.Creator): """Gui command for the Line tool.""" def __init__(self, wiremode=False): - super(Line, self).__init__() + super().__init__() self.isWire = wiremode def GetResources(self): @@ -63,17 +63,13 @@ class Line(gui_base_original.Creator): 'MenuText': QT_TRANSLATE_NOOP("Draft_Line", "Line"), 'ToolTip': QT_TRANSLATE_NOOP("Draft_Line", "Creates a 2-point line. CTRL to snap, SHIFT to constrain.")} - def Activated(self, name=QT_TRANSLATE_NOOP("draft","Line"), icon="Draft_Line", task_title=None): + def Activated(self, name=QT_TRANSLATE_NOOP("draft", "Line"), icon="Draft_Line", task_title=None): """Execute when the command is called.""" - super(Line, self).Activated(name) + super().Activated(name) if task_title is None: title = translate("draft", name) else: title = task_title - if not self.doc: - return - self.obj = None # stores the temp shape - self.oldWP = None # stores the WP if we modify it if self.isWire: self.ui.wireUi(title=title, icon=icon) else: @@ -139,12 +135,6 @@ class Line(gui_base_original.Creator): Close the line if `True`. """ self.removeTemporaryObject() - if self.oldWP: - App.DraftWorkingPlane.setFromParameters(self.oldWP) - if hasattr(Gui, "Snapper"): - Gui.Snapper.setGrid(tool=True) - Gui.Snapper.restack() - self.oldWP = None if len(self.node) > 1: Gui.addModule("Draft") @@ -191,7 +181,10 @@ class Line(gui_base_original.Creator): 'FreeCAD.ActiveDocument.recompute()'] self.commit(translate("draft", "Create Wire"), _cmd_list) - super(Line, self).finish() + super().finish() + if hasattr(Gui, "Snapper"): + Gui.Snapper.setGrid(tool=True) + Gui.Snapper.restack() if cont or (cont is None and self.ui and self.ui.continueMode): self.Activated() @@ -259,23 +252,19 @@ class Line(gui_base_original.Creator): def orientWP(self): """Orient the working plane.""" - import DraftGeomUtils - if hasattr(App, "DraftWorkingPlane"): - if len(self.node) > 1 and self.obj: - n = DraftGeomUtils.getNormal(self.obj.Shape) - if not n: - n = App.DraftWorkingPlane.axis - p = self.node[-1] - v = self.node[-2].sub(self.node[-1]) - v = v.negative() - if not self.oldWP: - self.oldWP = App.DraftWorkingPlane.getParameters() - App.DraftWorkingPlane.alignToPointAndAxis(p, n, upvec=v) - if hasattr(Gui, "Snapper"): - Gui.Snapper.setGrid() - Gui.Snapper.restack() - if self.planetrack: - self.planetrack.set(self.node[-1]) + if len(self.node) > 1 and self.obj: + import DraftGeomUtils + n = DraftGeomUtils.getNormal(self.obj.Shape) + if not n: + n = App.DraftWorkingPlane.axis + p = self.node[-1] + v = self.node[-1].sub(self.node[-2]) + App.DraftWorkingPlane.alignToPointAndAxis(p, n, upvec=v) + if hasattr(Gui, "Snapper"): + Gui.Snapper.setGrid() + Gui.Snapper.restack() + if self.planetrack: + self.planetrack.set(self.node[-1]) def numericInput(self, numx, numy, numz): """Validate the entry fields in the user interface. @@ -303,7 +292,7 @@ class Wire(Line): """ def __init__(self): - super(Wire, self).__init__(wiremode=True) + super().__init__(wiremode=True) def GetResources(self): """Set icon, menu and tooltip.""" @@ -362,9 +351,9 @@ class Wire(Line): # If there was no selection or the selection was just one object # then we proceed with the normal line creation functions, # only this time we will be able to input more than two points - super(Wire, self).Activated(name="Polyline", - icon="Draft_Wire", - task_title=translate("draft","Polyline")) + super().Activated(name="Polyline", + icon="Draft_Wire", + task_title=translate("draft", "Polyline")) Gui.addCommand('Draft_Wire', Wire())