From 3df6dab640c51d67fdeb776af053ba95cac3a833 Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Mon, 30 Mar 2020 20:02:43 -0600 Subject: [PATCH] Draft: move Wire GuiCommand to gui_lines module --- src/Mod/Draft/DraftTools.py | 43 +------------ src/Mod/Draft/draftguitools/gui_lines.py | 78 +++++++++++++++++++++++- 2 files changed, 78 insertions(+), 43 deletions(-) diff --git a/src/Mod/Draft/DraftTools.py b/src/Mod/Draft/DraftTools.py index 4dcc117f7f..116940ee7f 100644 --- a/src/Mod/Draft/DraftTools.py +++ b/src/Mod/Draft/DraftTools.py @@ -151,49 +151,9 @@ from draftguitools.gui_tool_utils import redraw3DView from draftguitools.gui_base_original import Creator from draftguitools.gui_lines import Line +from draftguitools.gui_lines import Wire -class Wire(Line): - """a FreeCAD command for creating a wire""" - - def __init__(self): - Line.__init__(self,wiremode=True) - - def GetResources(self): - return {'Pixmap' : 'Draft_Wire', - 'Accel' : "P, L", - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_Wire", "Polyline"), - 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_Wire", "Creates a multiple-points line (polyline). CTRL to snap, SHIFT to constrain")} - - def Activated(self): - - # allow to convert several Draft Lines to a Wire - if len(FreeCADGui.Selection.getSelection()) > 1: - edges = [] - for o in FreeCADGui.Selection.getSelection(): - if Draft.getType(o) != "Wire": - edges = [] - break - edges.extend(o.Shape.Edges) - if edges: - try: - import Part - w = Part.Wire(edges) - except: - FreeCAD.Console.PrintError(translate("draft", "Unable to create a Wire from selected objects")+"\n") - else: - pts = ",".join([str(v.Point) for v in w.Vertexes]) - pts = pts.replace("Vector","FreeCAD.Vector") - rems = ["FreeCAD.ActiveDocument.removeObject(\""+o.Name+"\")" for o in FreeCADGui.Selection.getSelection()] - FreeCADGui.addModule("Draft") - ToDo.delayCommit([(translate("draft", "Convert to Wire"), - ['wire = Draft.makeWire(['+pts+'])']+rems+['Draft.autogroup(wire)', - 'FreeCAD.ActiveDocument.recompute()'])]) - return - - Line.Activated(self,name=translate("draft","Polyline")) - - class BSpline(Line): """a FreeCAD command for creating a B-spline""" @@ -4620,7 +4580,6 @@ from draftguitools.gui_snaps import ShowSnapBar #--------------------------------------------------------------------------- # drawing commands -FreeCADGui.addCommand('Draft_Wire',Wire()) FreeCADGui.addCommand('Draft_Circle',Circle()) class CommandArcGroup: def GetCommands(self): diff --git a/src/Mod/Draft/draftguitools/gui_lines.py b/src/Mod/Draft/draftguitools/gui_lines.py index e0220b9e89..9ceef74596 100644 --- a/src/Mod/Draft/draftguitools/gui_lines.py +++ b/src/Mod/Draft/draftguitools/gui_lines.py @@ -42,7 +42,7 @@ import draftutils.gui_utils as gui_utils import draftutils.todo as todo import draftguitools.gui_base_original as gui_base_original import draftguitools.gui_tool_utils as gui_tool_utils -from draftutils.messages import _msg +from draftutils.messages import _msg, _err from draftutils.translate import translate @@ -285,3 +285,79 @@ class Line(gui_base_original.Creator): Gui.addCommand('Draft_Line', Line()) + + +class Wire(Line): + """Gui command for the Wire or Polyline tool. + + It inherits the `Line` class, and calls essentially the same code, + only this time the `wiremode` is set to `True`, + so we are allowed to place more than two points. + """ + + def __init__(self): + super().__init__(wiremode=True) + + def GetResources(self): + """Set icon, menu and tooltip.""" + _tip = ("Creates a multiple-points line (polyline). " + "CTRL to snap, SHIFT to constrain.") + + return {'Pixmap': 'Draft_Wire', + 'Accel': "P, L", + 'MenuText': QT_TRANSLATE_NOOP("Draft_Wire", "Polyline"), + 'ToolTip': QT_TRANSLATE_NOOP("Draft_Wire", _tip)} + + def Activated(self): + """Execute when the command is called.""" + import Part + + # If there is a selection, and this selection contains various + # two-point lines, their shapes are extracted, and we attempt + # to join them into a single Wire (polyline), + # then the old lines are removed. + if len(Gui.Selection.getSelection()) > 1: + edges = [] + for o in Gui.Selection.getSelection(): + if utils.get_type(o) != "Wire": + edges = [] + break + edges.extend(o.Shape.Edges) + if edges: + try: + w = Part.Wire(edges) + except Exception: + _err(translate("draft", + "Unable to create a Wire " + "from selected objects")) + else: + # Points of the new fused Wire in string form + # 'FreeCAD.Vector(x,y,z), FreeCAD.Vector(x1,y1,z1), ...' + pts = ", ".join([str(v.Point) for v in w.Vertexes]) + pts = pts.replace("Vector ", "FreeCAD.Vector") + + # List of commands to remove the old objects + rems = list() + for o in Gui.Selection.getSelection(): + rems.append('FreeCAD.ActiveDocument.' + 'removeObject("' + o.Name + '")') + + Gui.addModule("Draft") + # The command to run is built as a series of text strings + # to be commited through the `draftutils.todo.ToDo` class + _cmd_list = ['wire = Draft.makeWire([' + pts + '])'] + _cmd_list.extend(rems) + _cmd_list.append('Draft.autogroup(wire)') + _cmd_list.append('FreeCAD.ActiveDocument.recompute()') + + _op_name = translate("draft", "Convert to Wire") + todo.ToDo.delayCommit([(_op_name, _cmd_list)]) + return + + # 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().Activated(name=translate("draft", "Polyline")) + + +Gui.addCommand('Draft_Wire', Wire())