From 08d23cf9518a802b0eb127631167b8ebe1fe2a46 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Thu, 21 Dec 2017 00:42:17 +0100 Subject: [PATCH] Sketcher: Blocked Constraint toolbar commands --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 186 ++++++++++++++++++++ src/Mod/Sketcher/Gui/Workbench.cpp | 2 + 2 files changed, 188 insertions(+) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 1b5b9a8d46..21a94aa939 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -1743,6 +1743,191 @@ void CmdSketcherConstrainLock::updateAction(int mode) } } +// ====================================================================================== + +/* XPM */ +static const char *cursor_createblocked[]={ + "32 32 3 1", + "+ c white", + "# c red", + ". c None", + "......+.........................", + "......+.........................", + "......+.........................", + "......+.........................", + "......+.........................", + "................................", + "+++++...+++++...................", + "................................", + "......+.........................", + "......+.........................", + "......+.........................", + "......+.........................", + "......+..........###............", + "....................##..........", + ".....................##.........", + "......................##........", + "......................##........", + ".............############.......", + ".............###########........", + ".............##########.........", + ".............########...........", + ".............#######............", + ".............########...........", + ".............##########.........", + ".............###########........", + ".............############.......", + "......................##........", + "......................##........", + ".....................##.........", + "....................##..........", + ".................###............", + "................................"}; + +class CmdSketcherConstrainBlocked : public CmdSketcherConstraint +{ +public: + CmdSketcherConstrainBlocked(); + virtual ~CmdSketcherConstrainBlocked(){} + virtual const char* className() const + { return "CmdSketcherConstrainBlocked"; } +protected: + virtual void activated(int iMsg); + virtual void applyConstraint(std::vector &selSeq, int seqIndex); +}; + +CmdSketcherConstrainBlocked::CmdSketcherConstrainBlocked() +:CmdSketcherConstraint("Sketcher_ConstrainBlocked") +{ + sAppModule = "Sketcher"; + sGroup = QT_TR_NOOP("Sketcher"); + sMenuText = QT_TR_NOOP("Constrain Blocked"); + sToolTipText = QT_TR_NOOP("Create a blocked constraint on the selected item"); + sWhatsThis = "Sketcher_ConstrainBlocked"; + sStatusTip = sToolTipText; + sPixmap = "Sketcher_ConstrainBlocked"; + eType = ForEdit; + + allowedSelSequences = {{SelEdge}}; + constraintCursor = cursor_createblocked; +} + +void CmdSketcherConstrainBlocked::activated(int iMsg) +{ + Q_UNUSED(iMsg); + + // get the selection + std::vector selection = getSelection().getSelectionEx(); + + // only one sketch with its subelements are allowed to be selected + if (selection.size() != 1) { + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); + bool constraintMode = hGrp->GetBool("ContinuousConstraintMode", true); + + if (constraintMode) { + ActivateHandler(getActiveGuiDocument(), + new DrawSketchHandlerGenConstraint(constraintCursor, this)); + getSelection().clearSelection(); + } else { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Select vertices from the sketch.")); + } + return; + } + + // get the needed lists and objects + const std::vector &SubNames = selection[0].getSubNames(); + Sketcher::SketchObject* Obj = static_cast(selection[0].getObject()); + + std::vector GeoId; + + for (std::vector::const_iterator it = SubNames.begin(); it != SubNames.end(); ++it) { + int GeoIdt; + Sketcher::PointPos PosIdt; + getIdsFromName((*it), Obj, GeoIdt, PosIdt); + GeoId.push_back(GeoIdt); + + if ( isVertex(GeoIdt,PosIdt) || GeoIdt < 0 ) { + if(selection.size() == 1) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Select one edge from the sketch.")); + } + else { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Select only edges from the sketch.")); + } + // clear the selection + getSelection().clearSelection(); + return; + } + } + + for (std::vector::iterator itg = GeoId.begin(); itg != GeoId.end(); ++itg) { + // undo command open + openCommand("add blocked constraint"); + + try { + + Gui::Command::doCommand( + Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Blocked',%d)) ", + selection[0].getFeatName(),(*itg)); + + } catch (const Base::Exception& e) { + Base::Console().Error("%s\n", e.what()); + QMessageBox::warning(Gui::getMainWindow(), + QObject::tr("Error"), + QString::fromLatin1(e.what())); + + Gui::Command::abortCommand(); + + tryAutoRecompute(); + return; + } + + commitCommand(); + tryAutoRecompute(); + } + + // clear the selection (convenience) + getSelection().clearSelection(); +} + +void CmdSketcherConstrainBlocked::applyConstraint(std::vector &selSeq, int seqIndex) +{ + switch (seqIndex) { + case 0: // {Edge} + // Create the constraints + SketcherGui::ViewProviderSketch* sketchgui = static_cast(getActiveGuiDocument()->getInEdit()); + + // undo command open + openCommand("add blocked constraint"); + + try { + + Gui::Command::doCommand( + Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Blocked',%d)) ", + sketchgui->getObject()->getNameInDocument(),selSeq.front().GeoId); + + } catch (const Base::Exception& e) { + Base::Console().Error("%s\n", e.what()); + QMessageBox::warning(Gui::getMainWindow(), + QObject::tr("Error"), + QString::fromLatin1(e.what())); + + Gui::Command::abortCommand(); + + tryAutoRecompute(); + return; + } + + commitCommand(); + tryAutoRecompute(); + + break; + } +} + + // ====================================================================================== /* XPM */ @@ -6601,6 +6786,7 @@ void CreateSketcherCommandsConstraints(void) rcCmdMgr.addCommand(new CmdSketcherConstrainHorizontal()); rcCmdMgr.addCommand(new CmdSketcherConstrainVertical()); rcCmdMgr.addCommand(new CmdSketcherConstrainLock()); + rcCmdMgr.addCommand(new CmdSketcherConstrainBlocked()); rcCmdMgr.addCommand(new CmdSketcherConstrainCoincident()); rcCmdMgr.addCommand(new CmdSketcherConstrainParallel()); rcCmdMgr.addCommand(new CmdSketcherConstrainPerpendicular()); diff --git a/src/Mod/Sketcher/Gui/Workbench.cpp b/src/Mod/Sketcher/Gui/Workbench.cpp index d11823b17d..cf686a1c2c 100644 --- a/src/Mod/Sketcher/Gui/Workbench.cpp +++ b/src/Mod/Sketcher/Gui/Workbench.cpp @@ -212,6 +212,7 @@ inline void SketcherAddWorkbenchConstraints(Gui::MenuItem& cons){ << "Sketcher_ConstrainTangent" << "Sketcher_ConstrainEqual" << "Sketcher_ConstrainSymmetric" + << "Sketcher_ConstrainBlocked" << "Separator" << "Sketcher_ConstrainLock" << "Sketcher_ConstrainDistanceX" @@ -236,6 +237,7 @@ inline void SketcherAddWorkbenchConstraints(Gui::ToolBarItem& << "Sketcher_ConstrainTangent" << "Sketcher_ConstrainEqual" << "Sketcher_ConstrainSymmetric" + << "Sketcher_ConstrainBlocked" << "Separator" << "Sketcher_ConstrainLock" << "Sketcher_ConstrainDistanceX"