From bb4a7d783cb860e74aa80cc32ec3f9958cb6ad8d Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Sat, 16 May 2020 14:52:47 -0500 Subject: [PATCH] Draft: check GUI in downgrade before using viewprovider Also make various improvements in style, PEP8, return value is now a tuple of lists instead of a list of lists. Update the Gui Command as well. --- src/Mod/Draft/draftfunctions/downgrade.py | 224 +++++++++++-------- src/Mod/Draft/draftguitools/gui_downgrade.py | 2 +- 2 files changed, 126 insertions(+), 100 deletions(-) diff --git a/src/Mod/Draft/draftfunctions/downgrade.py b/src/Mod/Draft/draftfunctions/downgrade.py index 5b80716e2f..a66acb3d1d 100644 --- a/src/Mod/Draft/draftfunctions/downgrade.py +++ b/src/Mod/Draft/draftfunctions/downgrade.py @@ -1,7 +1,7 @@ # *************************************************************************** # * Copyright (c) 2009, 2010 Yorik van Havre * # * Copyright (c) 2009, 2010 Ken Cline * -# * Copyright (c) 2020 FreeCAD Developers * +# * Copyright (c) 2020 Eliud Cabrera Castillo * # * * # * This program is free software; you can redistribute it and/or modify * # * it under the terms of the GNU Lesser General Public License (LGPL) * @@ -20,160 +20,181 @@ # * USA * # * * # *************************************************************************** -"""This module provides the code for Draft offset function. +"""Provides the code for Draft downgrade function. + +See also the `upgrade` function. """ -## @package offset +## @package downgrade # \ingroup DRAFT -# \brief This module provides the code for Draft offset function. +# \brief Provides the code for Draft downgrade function. import FreeCAD as App import draftutils.gui_utils as gui_utils import draftutils.utils as utils - +import draftfunctions.cut as cut +from draftutils.messages import _msg from draftutils.translate import _tr -from draftutils.utils import shapify -from draftfunctions.cut import cut - def downgrade(objects, delete=False, force=None): - """downgrade(objects,delete=False,force=None) - - Downgrade the given object(s) (can be an object or a list of objects). + """Downgrade the given objects. + + This is a counterpart to `upgrade`. Parameters ---------- - objects : + objects: Part::Feature or list + A single object to downgrade or a list + containing various such objects. - delete : bool - If delete is True, old objects are deleted. + delete: bool, optional + It defaults to `False`. + If it is `True`, the old objects are deleted, and only the resulting + object is kept. - force : string - The force attribute can be used to force a certain way of downgrading. - It can be: explode, shapify, subtr, splitFaces, cut2, getWire, - splitWires, splitCompounds. - - Return - ---------- - Returns a dictionary containing two lists, a list of new objects and a - list of objects to be deleted + force: str, optional + It defaults to `None`. + Its value can be used to force a certain method of downgrading. + It can be any of: `'explode'`, `'shapify'`, `'subtr'`, `'splitFaces'`, + `'cut2'`, `'getWire'`, `'splitWires'`, or `'splitCompounds'`. + + Returns + ------- + tuple + A tuple containing two lists, a list of new objects + and a list of objects to be deleted. + + None + If there is a problem it will return `None`. + + See Also + -------- + ugrade """ + _name = "downgrade" + utils.print_header(_name, "Downgrade objects") - import Part - import DraftGeomUtils - - if not isinstance(objects,list): + if not isinstance(objects, list): objects = [objects] - global deleteList, addList - deleteList = [] - addList = [] + delete_list = [] + add_list = [] + doc = App.ActiveDocument # actions definitions - def explode(obj): - """explodes a Draft block""" + """Explode a Draft block.""" pl = obj.Placement newobj = [] for o in obj.Components: - o.ViewObject.Visibility = True o.Placement = o.Placement.multiply(pl) + if App.GuiUp: + o.ViewObject.Visibility = True if newobj: - deleteList(obj) + delete_list(obj) return newobj return None def cut2(objects): - """cuts first object from the last one""" - newobj = cut(objects[0],objects[1]) + """Cut first object from the last one.""" + newobj = cut.cut(objects[0], objects[1]) if newobj: - addList.append(newobj) + add_list.append(newobj) return newobj return None def splitCompounds(objects): - """split solids contained in compound objects into new objects""" + """Split solids contained in compound objects into new objects.""" result = False for o in objects: if o.Shape.Solids: for s in o.Shape.Solids: - newobj = App.ActiveDocument.addObject("Part::Feature","Solid") + newobj = doc.addObject("Part::Feature", "Solid") newobj.Shape = s - addList.append(newobj) + add_list.append(newobj) result = True - deleteList.append(o) + delete_list.append(o) return result def splitFaces(objects): - """split faces contained in objects into new objects""" + """Split faces contained in objects into new objects.""" result = False params = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft") - preserveFaceColor = params.GetBool("preserveFaceColor") # True - preserveFaceNames = params.GetBool("preserveFaceNames") # True + preserveFaceColor = params.GetBool("preserveFaceColor") # True + preserveFaceNames = params.GetBool("preserveFaceNames") # True for o in objects: - voDColors = o.ViewObject.DiffuseColor if (preserveFaceColor and hasattr(o,'ViewObject')) else None - oLabel = o.Label if hasattr(o,'Label') else "" + if App.GuiUp and preserveFaceColor and o.ViewObject: + voDColors = o.ViewObject.DiffuseColor + else: + voDColors = None + oLabel = o.Label if hasattr(o, 'Label') else "" if o.Shape.Faces: for ind, f in enumerate(o.Shape.Faces): - newobj = App.ActiveDocument.addObject("Part::Feature","Face") + newobj = doc.addObject("Part::Feature", "Face") newobj.Shape = f if preserveFaceNames: newobj.Label = "{} {}".format(oLabel, newobj.Label) - if preserveFaceColor: - """ At this point, some single-color objects might have - just a single entry in voDColors for all their faces; handle that""" - tcolor = voDColors[ind] if ind 1): + elif (len(objects) == 1 and hasattr(objects[0], 'Shape') + and len(solids) > 1): result = splitCompounds(objects) - #print(result) + # print(result) if result: - App.Console.PrintMessage(_tr("Found 1 multi-solids compound: exploding it")+"\n") + _msg(_tr("Found 1 multi-solids compound: exploding it")) # special case, we have one parametric object: we "de-parametrize" it - elif (len(objects) == 1) and hasattr(objects[0],'Shape') and hasattr(objects[0], 'Base'): - result = shapify(objects[0]) + elif (len(objects) == 1 and hasattr(objects[0], 'Shape') + and hasattr(objects[0], 'Base')): + result = utils.shapify(objects[0]) if result: - App.Console.PrintMessage(_tr("Found 1 parametric object: breaking its dependencies")+"\n") - addList.append(result) - #deleteList.append(objects[0]) + _msg(_tr("Found 1 parametric object: " + "breaking its dependencies")) + add_list.append(result) + # delete_list.append(objects[0]) # we have only 2 objects: cut 2nd from 1st elif len(objects) == 2: result = cut2(objects) if result: - App.Console.PrintMessage(_tr("Found 2 objects: subtracting them")+"\n") - - elif (len(faces) > 1): + _msg(_tr("Found 2 objects: subtracting them")) + elif len(faces) > 1: # one object with several faces: split it if len(objects) == 1: result = splitFaces(objects) if result: - App.Console.PrintMessage(_tr("Found several faces: splitting them")+"\n") - + _msg(_tr("Found several faces: splitting them")) # several objects: remove all the faces from the first one else: result = subtr(objects) if result: - App.Console.PrintMessage(_tr("Found several objects: subtracting them from the first one")+"\n") - + _msg(_tr("Found several objects: " + "subtracting them from the first one")) # only one face: we extract its wires - elif (len(faces) > 0): + elif len(faces) > 0: result = getWire(objects[0]) if result: - App.Console.PrintMessage(_tr("Found 1 face: extracting its wires")+"\n") + _msg(_tr("Found 1 face: extracting its wires")) # no faces: split wire into single edges elif not onlyedges: result = splitWires(objects) if result: - App.Console.PrintMessage(_tr("Found only wires: extracting their edges")+"\n") + _msg(_tr("Found only wires: extracting their edges")) # no result has been obtained if not result: - App.Console.PrintMessage(_tr("No more downgrade possible")+"\n") + _msg(_tr("No more downgrade possible")) if delete: names = [] - for o in deleteList: + for o in delete_list: names.append(o.Name) - deleteList = [] + delete_list = [] for n in names: - App.ActiveDocument.removeObject(n) - gui_utils.select(addList) - return [addList,deleteList] + doc.removeObject(n) + + gui_utils.select(add_list) + return add_list, delete_list diff --git a/src/Mod/Draft/draftguitools/gui_downgrade.py b/src/Mod/Draft/draftguitools/gui_downgrade.py index f6ed23e616..ff9736da27 100644 --- a/src/Mod/Draft/draftguitools/gui_downgrade.py +++ b/src/Mod/Draft/draftguitools/gui_downgrade.py @@ -87,7 +87,7 @@ class Downgrade(gui_base_original.Modifier): _cmd += 'FreeCADGui.Selection.getSelection(), ' _cmd += 'delete=True' _cmd += ')' - _cmd_list = ['d = ' + _cmd, + _cmd_list = ['_objs_ = ' + _cmd, 'FreeCAD.ActiveDocument.recompute()'] self.commit(translate("draft", "Downgrade"), _cmd_list)