From 4eb4aaf00ac23b4ce6a95f8a8dc6286e2a205487 Mon Sep 17 00:00:00 2001 From: Bernd Hahnebach Date: Thu, 9 Feb 2017 19:35:21 +0100 Subject: [PATCH] Part: CompoundFilter, - add CompoundTools menue enty - add command to FreeCAD GUI --- src/Mod/Part/App/AppPart.cpp | 13 +++ src/Mod/Part/CompoundTools/CompoundFilter.py | 30 +++++++ src/Mod/Part/CompoundTools/__init__.py | 24 ++++++ src/Mod/Part/Gui/Command.cpp | 83 ++++++++++++++++++++ src/Mod/Part/Gui/Workbench.cpp | 10 ++- src/Mod/Part/InitGui.py | 8 +- 6 files changed, 163 insertions(+), 5 deletions(-) diff --git a/src/Mod/Part/App/AppPart.cpp b/src/Mod/Part/App/AppPart.cpp index c771af7d69670..10a69b86cce2f 100644 --- a/src/Mod/Part/App/AppPart.cpp +++ b/src/Mod/Part/App/AppPart.cpp @@ -380,6 +380,19 @@ PyMODINIT_FUNC initPart() err.ReportException(); } + try { + //import all submodules of CompoundTools, to make them easy to browse in Py console. + //It's done in this weird manner instead of bt.caMemberFunction("importAll"), + //because the latter crashed when importAll failed with exception. + Base::Interpreter().runString("__import__('CompoundTools').importAll()"); + + Py::Object bt = Base::Interpreter().runStringObject("__import__('CompoundTools')"); + module.setAttr(std::string("CompoundTools"),bt); + } catch (Base::PyException &err){ + Base::Console().Error("Failed to import CompoundTools package:\n"); + err.ReportException(); + } + Part::TopoShape ::init(); Part::PropertyPartShape ::init(); Part::PropertyGeometryList ::init(); diff --git a/src/Mod/Part/CompoundTools/CompoundFilter.py b/src/Mod/Part/CompoundTools/CompoundFilter.py index e48620c8ece60..68b780cfcb845 100644 --- a/src/Mod/Part/CompoundTools/CompoundFilter.py +++ b/src/Mod/Part/CompoundTools/CompoundFilter.py @@ -32,6 +32,7 @@ if FreeCAD.GuiUp: import FreeCADGui from PySide import QtGui + from PySide import QtCore #-------------------------- translation-related code ---------------------------------------- @@ -271,9 +272,38 @@ def cmdCreateCompoundFilter(name): FreeCADGui.doCommand("f = None") FreeCAD.ActiveDocument.commitTransaction() + + +class _CommandCompoundFilter: + "Command to create CompoundFilter feature" + def GetResources(self): + return {'Pixmap': ":/icons/Part_CompoundFilter.svg", + 'MenuText': QtCore.QT_TRANSLATE_NOOP("Part_CompoundFilter", "Compound Filter"), + 'Accel': "", + 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Part_CompoundFilter", "Compound Filter: remove some childs from a compound")} + + def Activated(self): + if len(FreeCADGui.Selection.getSelection()) == 1 or len(FreeCADGui.Selection.getSelection()) == 2: + cmdCreateCompoundFilter(name="CompoundFilter") + else: + mb = QtGui.QMessageBox() + mb.setIcon(mb.Icon.Warning) + mb.setText(_translate("Part_CompoundFilter", "Select a shape that is a compound, first! Second selected item (optional) will be treated as a stencil.", None)) + mb.setWindowTitle(_translate("Part_CompoundFilter", "Bad selection", None)) + mb.exec_() + + def IsActive(self): + if FreeCAD.ActiveDocument: + return True + else: + return False # -------------------------- /GUI command -------------------------------------------------- +def addCommands(): + FreeCADGui.addCommand('Part_CompoundFilter', _CommandCompoundFilter()) + + # helper def getNullShapeShape(scale=1.0): """obtains a shape intended ad a placeholder in case null shape was produced by an operation""" diff --git a/src/Mod/Part/CompoundTools/__init__.py b/src/Mod/Part/CompoundTools/__init__.py index 615dce3a5e8e3..24cc71bf2e67c 100644 --- a/src/Mod/Part/CompoundTools/__init__.py +++ b/src/Mod/Part/CompoundTools/__init__.py @@ -27,3 +27,27 @@ ## @package CompoundTools # \ingroup PART + + +__all__ = [ +"CompoundFilter" +] + + +def importAll(): + "importAll(): imports all modules of CompoundTools package" + from . import CompoundFilter + + +def reloadAll(): + "reloadAll(): reloads all modules of CompoundTools package. Useful for debugging." + for modstr in __all__: + reload(globals()[modstr]) + import FreeCAD + if FreeCAD.GuiUp: + addCommands() + + +def addCommands(): + "addCommands(): add all GUI commands of CompoundTools package to FreeCAD command manager." + CompoundFilter.addCommands() diff --git a/src/Mod/Part/Gui/Command.cpp b/src/Mod/Part/Gui/Command.cpp index ff3083313a713..7729944bdab56 100644 --- a/src/Mod/Part/Gui/Command.cpp +++ b/src/Mod/Part/Gui/Command.cpp @@ -755,6 +755,88 @@ bool CmdPartCompSplitFeatures::isActive(void) return false; } +//=========================================================================== +// Part_CompoundFilter (dropdown toolbar button for CompoundTools, CompoundFilter) +//=========================================================================== + +DEF_STD_CMD_ACL(CmdPartCompFilterFeatures); + +CmdPartCompFilterFeatures::CmdPartCompFilterFeatures() + : Command("Part_CompoundFilter") +{ + sAppModule = "Part"; + sGroup = QT_TR_NOOP("Part"); + sMenuText = QT_TR_NOOP("Compound Filter"); + sToolTipText = QT_TR_NOOP("Compound Filter: remove some childs from a compound"); + sWhatsThis = "Part_CompoundFilter"; + sStatusTip = sToolTipText; +} + +void CmdPartCompFilterFeatures::activated(int iMsg) +{ + Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager(); + if (iMsg==0) + rcCmdMgr.runCommandByName("Part_CompoundFilter"); + else + return; + + // Since the default icon is reset when enabing/disabling the command we have + // to explicitly set the icon of the used command. + Gui::ActionGroup* pcAction = qobject_cast(_pcAction); + QList a = pcAction->actions(); + + assert(iMsg < a.size()); + pcAction->setIcon(a[iMsg]->icon()); +} + +Gui::Action * CmdPartCompFilterFeatures::createAction(void) +{ + Gui::ActionGroup* pcAction = new Gui::ActionGroup(this, Gui::getMainWindow()); + pcAction->setDropDownMenu(true); + applyCommandData(this->className(), pcAction); + + QAction* cmd0 = pcAction->addAction(QString()); + cmd0->setIcon(Gui::BitmapFactory().pixmap("Part_CompoundFilter")); + + _pcAction = pcAction; + languageChange(); + + pcAction->setIcon(cmd0->icon()); + int defaultId = 0; + pcAction->setProperty("defaultAction", QVariant(defaultId)); + + return pcAction; +} + +void CmdPartCompFilterFeatures::languageChange() +{ + Command::languageChange(); + + if (!_pcAction) + return; + + Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager(); + + Gui::ActionGroup* pcAction = qobject_cast(_pcAction); + QList a = pcAction->actions(); + + Gui::Command* makeCompoundFilter = rcCmdMgr.getCommandByName("Part_CompoundFilter"); + if (makeCompoundFilter) { + QAction* cmd0 = a[0]; + cmd0->setText(QApplication::translate("Part_CompoundFilter", makeCompoundFilter->getMenuText())); + cmd0->setToolTip(QApplication::translate("Part_CompoundFilter", makeCompoundFilter->getToolTipText())); + cmd0->setStatusTip(QApplication::translate("Part_CompoundFilter", makeCompoundFilter->getStatusTip())); + } +} + +bool CmdPartCompFilterFeatures::isActive(void) +{ + if (getActiveGuiDocument()) + return true; + else + return false; +} + //=========================================================================== // Part_Compound //=========================================================================== @@ -2238,6 +2320,7 @@ void CreatePartCommands(void) rcCmdMgr.addCommand(new CmdPartFuse()); rcCmdMgr.addCommand(new CmdPartCompJoinFeatures()); rcCmdMgr.addCommand(new CmdPartCompSplitFeatures()); + rcCmdMgr.addCommand(new CmdPartCompFilterFeatures()); rcCmdMgr.addCommand(new CmdPartCompound()); rcCmdMgr.addCommand(new CmdPartSection()); //rcCmdMgr.addCommand(new CmdPartBox2()); diff --git a/src/Mod/Part/Gui/Workbench.cpp b/src/Mod/Part/Gui/Workbench.cpp index 0e8969f699ddd..1a79f17425e63 100644 --- a/src/Mod/Part/Gui/Workbench.cpp +++ b/src/Mod/Part/Gui/Workbench.cpp @@ -75,6 +75,10 @@ Gui::MenuItem* Workbench::setupMenuBar() const split->setCommand("Split"); *split << "Part_BooleanFragments" << "Part_Slice" << "Part_XOR"; + Gui::MenuItem* compound = new Gui::MenuItem; + compound->setCommand("Compound"); + *compound << "Part_Compound" << "Part_CompoundFilter"; + Gui::MenuItem* part = new Gui::MenuItem; root->insertItem(item, part); part->setCommand("&Part"); @@ -82,8 +86,8 @@ Gui::MenuItem* Workbench::setupMenuBar() const *part << prim << "Part_Primitives" << "Part_Builder" << "Separator" << "Part_ShapeFromMesh" << "Part_MakeSolid" << "Part_ReverseShape" << "Part_SimpleCopy" << "Part_RefineShape" << "Part_CheckGeometry" - << "Separator" << bop << join << split << "Separator" - << "Part_CrossSections" << "Part_Compound" << "Part_MakeFace" << "Part_Extrude" + << "Separator" << bop << join << split << compound << "Separator" + << "Part_CrossSections" << "Part_MakeFace" << "Part_Extrude" << "Part_Revolve" << "Part_Mirror" << "Part_Fillet" << "Part_Chamfer" << "Part_RuledSurface" << "Part_Loft" << "Part_Sweep" << "Part_Offset" << "Part_Offset2D" << "Part_Thickness" << "Separator" << "Part_EditAttachment"; @@ -129,7 +133,7 @@ Gui::ToolBarItem* Workbench::setupToolBars() const *boolop << "Part_Boolean" << "Part_Cut" << "Part_Fuse" << "Part_Common" << "Part_CompJoinFeatures" << "Part_CompSplitFeatures" << "Part_CheckGeometry" << "Part_Section" << "Part_CrossSections"; - + Gui::ToolBarItem* measure = new Gui::ToolBarItem(root); measure->setCommand("Measure"); *measure << "Part_Measure_Linear" << "Part_Measure_Angular" << "Separator" << "Part_Measure_Clear_All" << "Part_Measure_Toggle_All" diff --git a/src/Mod/Part/InitGui.py b/src/Mod/Part/InitGui.py index 4a99040b64fbe..a890c1690a979 100644 --- a/src/Mod/Part/InitGui.py +++ b/src/Mod/Part/InitGui.py @@ -1,4 +1,4 @@ -# Part gui init module +# Part gui init module # (c) 2003 Juergen Riegel # # Gathering all the information to start FreeCAD @@ -45,7 +45,11 @@ def Initialize(self): try: Part.BOPTools.addCommands() except Exception as err: - FreeCAD.Console.PrintError("Features from BOPTools package cannot be loaded. {err}\n".format(err= err.message)) + FreeCAD.Console.PrintError("Features from BOPTools package cannot be loaded. {err}\n".format(err= err.message)) + try: + Part.CompoundTools.addCommands() + except Exception as err: + FreeCAD.Console.PrintError("Features from CompoundTools package cannot be loaded. {err}\n".format(err= err.message)) def GetClassName(self): return "PartGui::Workbench"