diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 092f79bb4adf..9b1886c73a4f 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -7731,6 +7731,88 @@ bool CmdSketcherToggleDrivingConstraint::isActive(void) return isCreateGeoActive( getActiveGuiDocument() ); } +DEF_STD_CMD_A(CmdSketcherToggleActiveConstraint); + +CmdSketcherToggleActiveConstraint::CmdSketcherToggleActiveConstraint() +:Command("Sketcher_ToggleActiveConstraint") +{ + sAppModule = "Sketcher"; + sGroup = QT_TR_NOOP("Sketcher"); + sMenuText = QT_TR_NOOP("Toggle activate/deactivate constraint"); + sToolTipText = QT_TR_NOOP("Toggles activate/deactivate state for selected constraints"); + sWhatsThis = "Sketcher_ToggleActiveConstraint"; + sStatusTip = sToolTipText; + sPixmap = "Sketcher_ToggleActiveConstraint"; + sAccel = ""; + eType = ForEdit; +} + +void CmdSketcherToggleActiveConstraint::activated(int iMsg) +{ + Q_UNUSED(iMsg); + + std::vector selection; + + if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) > 0){ + // Now we check whether we have a constraint selected or not. + + // get the selection + selection = getSelection().getSelectionEx(); + + // only one sketch with its subelements are allowed to be selected + if (selection.size() != 1 || !selection[0].isObjectTypeOf(Sketcher::SketchObject::getClassTypeId())) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Select constraint(s) from the sketch.")); + return; + } + + Sketcher::SketchObject* Obj = static_cast(selection[0].getObject()); + + // get the needed lists and objects + const std::vector &SubNames = selection[0].getSubNames(); + if (SubNames.empty()) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Select constraint(s) from the sketch.")); + return; + } + + // undo command open + openCommand("Active/Deactivate constraints"); + + int successful=SubNames.size(); + + for (std::vector::const_iterator it=SubNames.begin();it!=SubNames.end();++it){ + + if (it->size() > 10 && it->substr(0,10) == "Constraint") { + int ConstrId = Sketcher::PropertyConstraintList::getIndexFromConstraintName(*it); + try { + // issue the actual commands to toggle + doCommand(Doc,"App.ActiveDocument.%s.toggleActive(%d) ",selection[0].getFeatName(),ConstrId); + } + catch(const Base::Exception&) { + successful--; + } + } + } + + if (successful > 0) + commitCommand(); + else + abortCommand(); + + tryAutoRecompute(Obj); + + // clear the selection (convenience) + getSelection().clearSelection(); + } +} + +bool CmdSketcherToggleActiveConstraint::isActive(void) +{ + return isCreateConstraintActive( getActiveGuiDocument() ); +} + + void CreateSketcherCommandsConstraints(void) { Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager(); @@ -7756,4 +7838,5 @@ void CreateSketcherCommandsConstraints(void) rcCmdMgr.addCommand(new CmdSketcherConstrainSnellsLaw()); rcCmdMgr.addCommand(new CmdSketcherConstrainInternalAlignment()); rcCmdMgr.addCommand(new CmdSketcherToggleDrivingConstraint()); + rcCmdMgr.addCommand(new CmdSketcherToggleActiveConstraint()); }