From 99f93f357b286d6d7fd311a30b462fc05851fb12 Mon Sep 17 00:00:00 2001 From: AjinkyaDahale Date: Sun, 22 Jan 2017 02:27:21 +0530 Subject: [PATCH] CmdSketcherConstraint::applyConstraint() is now used in DrawSketchHandlerGenConstraint --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 373 ++++++++++++-------- 1 file changed, 229 insertions(+), 144 deletions(-) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index ab05080a89..03f8480ad4 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -66,7 +66,7 @@ namespace SketcherGui ConstraintCreationMode constraintCreationMode=Driving; -void ActivateHandler(Gui::Document *doc,DrawSketchHandler *handler); +void ActivateHandler(Gui::Document *doc, DrawSketchHandler *handler); bool isCreateGeoActive(Gui::Document *doc); @@ -735,8 +735,6 @@ int SketchSelection::setUp(void) } // namespace SketcherGui - - /* Constrain commands =======================================================*/ namespace SketcherGui { @@ -797,85 +795,6 @@ namespace SketcherGui { }; } -class DrawSketchHandlerGenConstraint: public DrawSketchHandler -{ -public: - DrawSketchHandlerGenConstraint(const char* cursor[]) : constraintCursor(cursor) {} - virtual ~DrawSketchHandlerGenConstraint() - { - Gui::Selection().rmvSelectionGate(); - } - - virtual void activated(ViewProviderSketch *) - { - Gui::Selection().rmvSelectionGate(); - GenericConstraintSelection* selFilterGate = new GenericConstraintSelection(sketchgui->getObject()); - selFilterGate->setAllowedSelTypes(SelEdge); - Gui::Selection().addSelectionGate(selFilterGate); - setCursor(QPixmap(constraintCursor), 7, 7); - } - - virtual void mouseMove(Base::Vector2d onSketchPos) {Q_UNUSED(onSketchPos);} - - virtual bool pressButton(Base::Vector2d onSketchPos) - { - Q_UNUSED(onSketchPos); - return false; - } - - virtual bool releaseButton(Base::Vector2d onSketchPos) - { - Q_UNUSED(onSketchPos); - Sketcher::SketchObject* Obj = static_cast(sketchgui->getObject()); - - const std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues(); - - int CrvId = sketchgui->getPreselectCurve(); - if (CrvId != -1) { - const Part::Geometry *geo = Obj->getGeometry(CrvId); - if (geo->getTypeId() != Part::GeomLineSegment::getClassTypeId()) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), - QObject::tr("The selected edge is not a line segment")); - return false; - } - - // check if the edge has already a Horizontal or Vertical constraint - for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin(); - it != vals.end(); ++it) { - if ((*it)->Type == Sketcher::Horizontal && (*it)->First == CrvId){ - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Double constraint"), - QObject::tr("The selected edge has already a horizontal constraint!")); - return false; - } - if ((*it)->Type == Sketcher::Vertical && (*it)->First == CrvId) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), - QObject::tr("The selected edge has already a vertical constraint!")); - return false; - } - } - - // undo command open - Gui::Command::openCommand("add horizontal constraint"); - // issue the actual commands to create the constraint - Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Horizontal',%d)) ", - sketchgui->getObject()->getNameInDocument(),CrvId); - // finish the transaction and update - Gui::Command::commitCommand(); - - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); - bool autoRecompute = hGrp->GetBool("AutoRecompute",false); - - if(autoRecompute) - Gui::Command::updateActive(); - } - return false; - } - -protected: - const char** constraintCursor; -}; - - /** * @brief The CmdSketcherConstraint class * Superclass for all sketcher constraints to ease generation of constraint @@ -883,6 +802,7 @@ protected: */ class CmdSketcherConstraint : public Gui::Command { + friend class DrawSketchHandlerGenConstraint; public: CmdSketcherConstraint(const char* name) : Command(name) {} @@ -893,12 +813,124 @@ public: { return "CmdSketcherConstraint"; } protected: - virtual void applyConstraint() {} - virtual void activated(int /*iMsg*/) {} + /** + * @brief allowedSelSequences + * Each element is a vector representing sequence of selections allowable. + * TODO: Introduce structs to + */ + std::vector > allowedSelSequences; + + const char** constraintCursor = 0; + + virtual void applyConstraint(std::vector &, int) {} + virtual void activated(int /*iMsg*/); virtual bool isActive(void) { return isCreateGeoActive(getActiveGuiDocument()); } }; +class DrawSketchHandlerGenConstraint: public DrawSketchHandler +{ +public: + DrawSketchHandlerGenConstraint(const char* cursor[], CmdSketcherConstraint *_cmd) + : constraintCursor(cursor), cmd(_cmd) {} + virtual ~DrawSketchHandlerGenConstraint() + { + Gui::Selection().rmvSelectionGate(); + } + + virtual void activated(ViewProviderSketch *) + { + Gui::Selection().rmvSelectionGate(); + + // Estimate allowed selections from the first types in allowedSelTypes + int allowedSelTypes = 0; + for (std::vector< std::vector< SelType > >::const_iterator it = cmd->allowedSelSequences.begin(); + it != cmd->allowedSelSequences.end(); ++it) { + allowedSelTypes = allowedSelTypes | *((*it).begin()); + } + GenericConstraintSelection* selFilterGate = new GenericConstraintSelection(sketchgui->getObject()); + selFilterGate->setAllowedSelTypes(allowedSelTypes); + Gui::Selection().addSelectionGate(selFilterGate); + + setCursor(QPixmap(constraintCursor), 7, 7); + } + + virtual void mouseMove(Base::Vector2d /*onSketchPos*/) {} + + virtual bool pressButton(Base::Vector2d /*onSketchPos*/) + { + return false; + } + + virtual bool releaseButton(Base::Vector2d /*onSketchPos*/) + { +// Sketcher::SketchObject* Obj = static_cast(sketchgui->getObject()); + +// const std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues(); + + std::vector selSeq; + SelIdPair selIdPair; + selIdPair.GeoId = sketchgui->getPreselectCurve(); + selIdPair.PosId = Sketcher::none; + selSeq.push_back(selIdPair); + cmd->applyConstraint(selSeq, 0); // TODO: replace arg 2 by ongoingToken +// if (CrvId != -1) { +// const Part::Geometry *geo = Obj->getGeometry(CrvId); +// if (geo->getTypeId() != Part::GeomLineSegment::getClassTypeId()) { +// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), +// QObject::tr("The selected edge is not a line segment")); +// return false; +// } + +// // check if the edge has already a Horizontal or Vertical constraint +// for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin(); +// it != vals.end(); ++it) { +// if ((*it)->Type == Sketcher::Horizontal && (*it)->First == CrvId){ +// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Double constraint"), +// QObject::tr("The selected edge has already a horizontal constraint!")); +// return false; +// } +// if ((*it)->Type == Sketcher::Vertical && (*it)->First == CrvId) { +// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), +// QObject::tr("The selected edge has already a vertical constraint!")); +// return false; +// } +// } + +// // undo command open +// Gui::Command::openCommand("add horizontal constraint"); +// // issue the actual commands to create the constraint +// Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Horizontal',%d)) ", +// sketchgui->getObject()->getNameInDocument(),CrvId); +// // finish the transaction and update +// Gui::Command::commitCommand(); + +// ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); +// bool autoRecompute = hGrp->GetBool("AutoRecompute",false); + +// if(autoRecompute) +// Gui::Command::updateActive(); +// } + return false; + } + +protected: + const char** constraintCursor; + CmdSketcherConstraint* cmd; + + /// indices of currently ongoing sequences in cmd->allowedSequences + std::set ongoingTokens; + /// Index within the selection sequences active + int seqIndex; +}; + +void CmdSketcherConstraint::activated(int /*iMsg*/) +{ + ActivateHandler(getActiveGuiDocument(), + new DrawSketchHandlerGenConstraint(constraintCursor, this)); + getSelection().clearSelection(); +} + // ====================================================================================== /* XPM */ @@ -1023,9 +1055,11 @@ public: CmdSketcherConstrainHorizontal(); virtual ~CmdSketcherConstrainHorizontal(){} virtual const char* className() const - { return "CmdSketcherConstrainLock"; } + { return "CmdSketcherConstrainHorizontal"; } protected: - virtual void activated(int); +// virtual void activated(int); + virtual void applyConstraint(std::vector &selSeq, int seqIndex); + }; CmdSketcherConstrainHorizontal::CmdSketcherConstrainHorizontal() @@ -1040,35 +1074,101 @@ CmdSketcherConstrainHorizontal::CmdSketcherConstrainHorizontal() sPixmap = "Constraint_Horizontal"; sAccel = "H"; eType = ForEdit; + + allowedSelSequences = {{SelEdge}}; + constraintCursor = cursor_createhoriconstraint; } -void CmdSketcherConstrainHorizontal::activated(int /*iMsg*/) +//void CmdSketcherConstrainHorizontal::activated(int /*iMsg*/) +//{ +// ActivateHandler(getActiveGuiDocument(), new DrawSketchHandlerGenConstraint(cursor_createhoriconstraint, this)); + +// // get the selection +// std::vector selection = getSelection().getSelectionEx(); + +// // only one sketch with its subelements are allowed to be selected +// if (selection.size() != 1) { +//// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), +//// QObject::tr("Select an edge 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()); +// const std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues(); + +// std::vector ids; +// // go through the selected subelements +// for (std::vector::const_iterator it=SubNames.begin(); it != SubNames.end(); ++it) { +// // only handle edges +// if (it->size() > 4 && it->substr(0,4) == "Edge") { +// int GeoId = std::atoi(it->substr(4,4000).c_str()) - 1; + +// const Part::Geometry *geo = Obj->getGeometry(GeoId); +// if (geo->getTypeId() != Part::GeomLineSegment::getClassTypeId()) { +// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), +// QObject::tr("The selected edge is not a line segment")); +// return; +// } + +// // check if the edge has already a Horizontal or Vertical constraint +// for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin(); +// it != vals.end(); ++it) { +// if ((*it)->Type == Sketcher::Horizontal && (*it)->First == GeoId){ +// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Double constraint"), +// QObject::tr("The selected edge has already a horizontal constraint!")); +// return; +// } +// if ((*it)->Type == Sketcher::Vertical && (*it)->First == GeoId) { +// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), +// QObject::tr("The selected edge has already a vertical constraint!")); +// return; +// } +// } +// ids.push_back(GeoId); +// } +// } + +// if (ids.empty()) { +// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), +// QObject::tr("The selected item(s) can't accept a horizontal constraint!")); +// return; +// } + +// // undo command open +// openCommand("add horizontal constraint"); +// for (std::vector::iterator it=ids.begin(); it != ids.end(); it++) { +// // issue the actual commands to create the constraint +// doCommand(Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Horizontal',%d)) " +// ,selection[0].getFeatName(),*it); +// } +// // finish the transaction and update +// commitCommand(); + +// ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); +// bool autoRecompute = hGrp->GetBool("AutoRecompute",false); + +// if(autoRecompute) +// Gui::Command::updateActive(); + +// // clear the selection (convenience) +// getSelection().clearSelection(); +//} + +void CmdSketcherConstrainHorizontal::applyConstraint(std::vector &selSeq, int seqIndex) { - ActivateHandler(getActiveGuiDocument(), new DrawSketchHandlerGenConstraint(cursor_createhoriconstraint)); + switch (seqIndex) { + case 0: // {Edge} + // TODO: create the constraint + SketcherGui::ViewProviderSketch* sketchgui = static_cast(getActiveGuiDocument()->getInEdit()); + Sketcher::SketchObject* Obj = sketchgui->getSketchObject(); - // get the selection - std::vector selection = getSelection().getSelectionEx(); + const std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues(); - // only one sketch with its subelements are allowed to be selected - if (selection.size() != 1) { -// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), -// QObject::tr("Select an edge 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()); - const std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues(); - - std::vector ids; - // go through the selected subelements - for (std::vector::const_iterator it=SubNames.begin(); it != SubNames.end(); ++it) { - // only handle edges - if (it->size() > 4 && it->substr(0,4) == "Edge") { - int GeoId = std::atoi(it->substr(4,4000).c_str()) - 1; - - const Part::Geometry *geo = Obj->getGeometry(GeoId); + int CrvId = selSeq.front().GeoId; + if (CrvId != -1) { + const Part::Geometry *geo = Obj->getGeometry(CrvId); if (geo->getTypeId() != Part::GeomLineSegment::getClassTypeId()) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), QObject::tr("The selected edge is not a line segment")); @@ -1078,53 +1178,38 @@ void CmdSketcherConstrainHorizontal::activated(int /*iMsg*/) // check if the edge has already a Horizontal or Vertical constraint for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin(); it != vals.end(); ++it) { - if ((*it)->Type == Sketcher::Horizontal && (*it)->First == GeoId){ + if ((*it)->Type == Sketcher::Horizontal && (*it)->First == CrvId){ QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Double constraint"), QObject::tr("The selected edge has already a horizontal constraint!")); return; } - if ((*it)->Type == Sketcher::Vertical && (*it)->First == GeoId) { + if ((*it)->Type == Sketcher::Vertical && (*it)->First == CrvId) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), QObject::tr("The selected edge has already a vertical constraint!")); return; } } - ids.push_back(GeoId); + + // undo command open + Gui::Command::openCommand("add horizontal constraint"); + // issue the actual commands to create the constraint + Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Horizontal',%d)) ", + sketchgui->getObject()->getNameInDocument(),CrvId); + // finish the transaction and update + Gui::Command::commitCommand(); + + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); + bool autoRecompute = hGrp->GetBool("AutoRecompute",false); + + if(autoRecompute) + Gui::Command::updateActive(); } + + break; } - - if (ids.empty()) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"), - QObject::tr("The selected item(s) can't accept a horizontal constraint!")); - return; - } - - // undo command open - openCommand("add horizontal constraint"); - for (std::vector::iterator it=ids.begin(); it != ids.end(); it++) { - // issue the actual commands to create the constraint - doCommand(Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Horizontal',%d)) " - ,selection[0].getFeatName(),*it); - } - // finish the transaction and update - commitCommand(); - - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); - bool autoRecompute = hGrp->GetBool("AutoRecompute",false); - - if(autoRecompute) - Gui::Command::updateActive(); - - // clear the selection (convenience) - getSelection().clearSelection(); } -//bool CmdSketcherConstrainHorizontal::isActive(void) -//{ -// return isCreateGeoActive( getActiveGuiDocument() ); -//} - -// ============================================================================ +// ================================================================================ static const char *cursor_createvertconstraint[]={ "32 32 3 1",