From 368f0f0ccd18cb064ffcd7e26e341893bb53076e Mon Sep 17 00:00:00 2001 From: DeepSOIC Date: Mon, 24 Dec 2018 18:05:27 +0300 Subject: [PATCH] Part: "Explode compound" tool --- src/Mod/Part/CMakeLists.txt | 2 + src/Mod/Part/CompoundTools/CompoundFilter.py | 9 +- src/Mod/Part/CompoundTools/Explode.py | 25 +++++ .../CompoundTools/_CommandExplodeCompound.py | 101 ++++++++++++++++++ src/Mod/Part/InitGui.py | 1 + 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 src/Mod/Part/CompoundTools/Explode.py create mode 100644 src/Mod/Part/CompoundTools/_CommandExplodeCompound.py diff --git a/src/Mod/Part/CMakeLists.txt b/src/Mod/Part/CMakeLists.txt index 9f4658d422..c482107278 100644 --- a/src/Mod/Part/CMakeLists.txt +++ b/src/Mod/Part/CMakeLists.txt @@ -40,7 +40,9 @@ set(BOPTools_Scripts set(CompoundTools_Scripts CompoundTools/__init__.py CompoundTools/_CommandCompoundFilter.py + CompoundTools/_CommandExplodeCompound.py CompoundTools/CompoundFilter.py + CompoundTools/Explode.py ) add_custom_target(PartScripts ALL SOURCES diff --git a/src/Mod/Part/CompoundTools/CompoundFilter.py b/src/Mod/Part/CompoundTools/CompoundFilter.py index b385c3137e..03b1f356cf 100644 --- a/src/Mod/Part/CompoundTools/CompoundFilter.py +++ b/src/Mod/Part/CompoundTools/CompoundFilter.py @@ -40,9 +40,14 @@ DistConfusion = 1e-7 ParaConfusion = 1e-8 -def makeCompoundFilter(name): +def makeCompoundFilter(name, into_group = None): '''makeCompoundFilter(name): makes a CompoundFilter object.''' - obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name) + if into_group is None: + into_group = FreeCAD.ActiveDocument + if into_group.isDerivedFrom('App::Document'): + obj = into_group.addObject("Part::FeaturePython", name) + else: + obj = into_group.newObject("Part::FeaturePython", name) _CompoundFilter(obj) if obj.ViewObject: _ViewProviderCompoundFilter(obj.ViewObject) diff --git a/src/Mod/Part/CompoundTools/Explode.py b/src/Mod/Part/CompoundTools/Explode.py new file mode 100644 index 0000000000..c0e51878e5 --- /dev/null +++ b/src/Mod/Part/CompoundTools/Explode.py @@ -0,0 +1,25 @@ +from .CompoundFilter import makeCompoundFilter + +def explodeCompound(compound_obj, b_group = None): + """explodeCompound(compound_obj, b_group = None): creates a bunch of compound filters, to extract every child of a compount into a separate object. + group: if True, Group is always made. If False, group is never made. If None, group is made if there is more than one child. + returns: (group_object, list_of_child_objects)""" + sh = compound_obj.Shape + n = len(sh.childShapes(False,False)) + if b_group is None: + b_group = n > 1 + if b_group: + group = compound_obj.Document.addObject('App::DocumentObjectGroup','GrExplode_'+compound_obj.Name) + group.Label = 'Exploded {obj.Label}'.format(obj = compound_obj) + else: + group = compound_obj.Document + features_created = [] + for i in range(0, n): + cf = makeCompoundFilter('{obj.Name}_child{child_num}'.format(obj = compound_obj, child_num = i), group) + cf.Label = '{obj.Label}.{child_num}'.format(obj = compound_obj, child_num = i) + cf.Base = compound_obj + cf.FilterType = 'specific items' + cf.items = str(i) + cf.ViewObject.DontUnhideOnDelete = True + features_created.append(cf) + return (group, features_created) diff --git a/src/Mod/Part/CompoundTools/_CommandExplodeCompound.py b/src/Mod/Part/CompoundTools/_CommandExplodeCompound.py new file mode 100644 index 0000000000..bd8804a60f --- /dev/null +++ b/src/Mod/Part/CompoundTools/_CommandExplodeCompound.py @@ -0,0 +1,101 @@ +# *************************************************************************** +# * * +# * Copyright (c) 2016 - Victor Titov (DeepSOIC) * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * This program is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# * GNU Library General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with this program; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** + +__title__ = "CompoundTools._CommandExplodeCompound" +__author__ = "DeepSOIC" +__url__ = "http://www.freecadweb.org" +__doc__ = "ExplodeCompound: create a bunch of CompoundFilter objects to split a compound into pieces." + +from .Explode import explodeCompound + +import FreeCAD +if FreeCAD.GuiUp: + import FreeCADGui + from PySide import QtGui + from PySide import QtCore + + +# translation-related code + try: + _fromUtf8 = QtCore.QString.fromUtf8 + except Exception: + def _fromUtf8(s): + return s + try: + _encoding = QtGui.QApplication.UnicodeUTF8 + def _translate(context, text, disambig): + return QtGui.QApplication.translate(context, text, disambig, _encoding) + except AttributeError: + def _translate(context, text, disambig): + return QtGui.QApplication.translate(context, text, disambig) + + +# command class +class _CommandExplodeCompound: + "Command to explode a compound" + def GetResources(self): + return {'Pixmap': ":/icons/Part_ExplodeCompound.svg", + 'MenuText': QtCore.QT_TRANSLATE_NOOP("Part_ExplodeCompound", "Explode compound"), + 'Accel': "", + 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Part_ExplodeCompound", "Explode compound: split up a list of shapes into separate objects")} + + def Activated(self): + if len(FreeCADGui.Selection.getSelection()) == 1: + cmdExplode() + else: + mb = QtGui.QMessageBox() + mb.setIcon(mb.Icon.Warning) + mb.setText(_translate("Part_ExplodeCompound", "Select a shape that is a compound, first!", None)) + mb.setWindowTitle(_translate("Part_ExplodeCompound", "Bad selection", None)) + mb.exec_() + + def IsActive(self): + if FreeCAD.ActiveDocument: + return True + else: + return False + + +if FreeCAD.GuiUp: + FreeCADGui.addCommand('Part_ExplodeCompound', _CommandExplodeCompound()) + + + + +def cmdExplode(): + FreeCAD.ActiveDocument.openTransaction("Explode") + try: + sel = FreeCADGui.Selection.getSelectionEx() + if len(sel) != 1: + raise ValueError("Bad selection","More than one object selected. You have selected {num} objects.".format(num= len(sel))) + obj = sel[0].Object + FreeCADGui.addModule("CompoundTools.Explode") + FreeCADGui.doCommand("input_obj = App.ActiveDocument."+obj.Name) + FreeCADGui.doCommand("CompoundTools.Explode.explodeCompound(input_obj)") + FreeCADGui.doCommand("input_obj.ViewObject.hide()") + except Exception: + FreeCAD.ActiveDocument.abortTransaction() + raise + + FreeCAD.ActiveDocument.commitTransaction() + FreeCADGui.doCommand("App.ActiveDocument.recompute()") + diff --git a/src/Mod/Part/InitGui.py b/src/Mod/Part/InitGui.py index c0f2d835f7..c6b8f2a6fc 100644 --- a/src/Mod/Part/InitGui.py +++ b/src/Mod/Part/InitGui.py @@ -44,6 +44,7 @@ class PartWorkbench ( Workbench ): try: import CompoundTools._CommandCompoundFilter + import CompoundTools._CommandExplodeCompound except ImportError as err: FreeCAD.Console.PrintError("Features from CompoundTools package cannot be loaded. {err}\n".format(err= str(err)))