From 4ad41e1ad162fdecbb65d35fa3389b105887fc8d Mon Sep 17 00:00:00 2001 From: AjinkyaDahale Date: Mon, 23 Jan 2017 03:37:35 +0530 Subject: [PATCH] Generalize DrawSketchHandlerGenConstraint::releaseButton The select-then-constrain method has been temporarily sacrificed --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 246 +++++++++----------- 1 file changed, 104 insertions(+), 142 deletions(-) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 03f8480ad4b2..a705520f59cb 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -330,7 +330,7 @@ void SketcherGui::makeTangentToEllipseviaNewPoint(const Sketcher::SketchObject* const Part::Geometry *geom2, int geoId1, int geoId2 - ) +) { const Part::GeomEllipse *ellipse = static_cast(geom1); @@ -394,6 +394,7 @@ void SketcherGui::makeTangentToEllipseviaNewPoint(const Sketcher::SketchObject* if(autoRecompute) Gui::Command::updateActive(); } + /// Makes a simple tangency constraint using extra point + tangent via point /// geom1 => an arc of ellipse /// geom2 => any of an arc of ellipse, a circle, or an arc (of circle) @@ -405,7 +406,7 @@ void SketcherGui::makeTangentToArcOfEllipseviaNewPoint(const Sketcher::SketchObj const Part::Geometry *geom2, int geoId1, int geoId2 - ) +) { const Part::GeomArcOfEllipse *aoe = static_cast(geom1); @@ -840,16 +841,15 @@ class DrawSketchHandlerGenConstraint: public DrawSketchHandler virtual void activated(ViewProviderSketch *) { - Gui::Selection().rmvSelectionGate(); + ongoingSequences = new std::set(); //TODO: make it contain more values + _tempOnSequences = new std::set(); + selFilterGate = new GenericConstraintSelection(sketchgui->getObject()); - // 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); + resetOngoingSequences(); + + selSeq = *(new std::vector()); + + Gui::Selection().rmvSelectionGate(); Gui::Selection().addSelectionGate(selFilterGate); setCursor(QPixmap(constraintCursor), 7, 7); @@ -864,64 +864,109 @@ class DrawSketchHandlerGenConstraint: public DrawSketchHandler 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.GeoId = Constraint::GeoUndef; 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(); -// } + SelType newSelType; + + //For each SelType allowed, check if button is released there and assign it to selIdPair + int VtId = sketchgui->getPreselectPoint(); + int CrvId = sketchgui->getPreselectCurve(); + int CrsId = sketchgui->getPreselectCross(); + if (allowedSelTypes & SketcherGui::SelRoot && CrsId == 0) { + selIdPair.GeoId = Sketcher::GeoEnum::RtPnt; + selIdPair.PosId = Sketcher::start; + newSelType = SelRoot; + } + else if (allowedSelTypes & SketcherGui::SelVertex && VtId != -1) { + sketchgui->getSketchObject()->getGeoVertexIndex(VtId, + selIdPair.GeoId, + selIdPair.PosId); + newSelType = SelVertex; + } + else if (allowedSelTypes & SketcherGui::SelEdge && CrvId != -1) { + selIdPair.GeoId = CrvId; + newSelType = SelEdge; + } + else if (allowedSelTypes & SketcherGui::SelHAxis && CrsId == 1) { + selIdPair.GeoId = Sketcher::GeoEnum::HAxis; + newSelType = SelHAxis; + } + else if (allowedSelTypes & SketcherGui::SelVAxis && CrsId == 2) { + selIdPair.GeoId = Sketcher::GeoEnum::VAxis; + newSelType = SelVAxis; + } + else if (allowedSelTypes & SketcherGui::SelExternalEdge) { + //TODO: Figure out how this works + newSelType = SelExternalEdge; + } + + if (selIdPair.GeoId == Constraint::GeoUndef) { + // If mouse is released on "blank" space, start over + selSeq.clear(); + resetOngoingSequences(); + Gui::Selection().clearSelection(); + } + else { + // TODO: If mouse is released on something allowed, select it and move forward + selSeq.push_back(selIdPair); + + _tempOnSequences->clear(); + allowedSelTypes = 0; + for (std::set::iterator token = ongoingSequences->begin(); + token != ongoingSequences->end(); ++token) { + if ((cmd->allowedSelSequences).at(*token).at(seqIndex) == newSelType) { + if (seqIndex == (cmd->allowedSelSequences).at(*token).size()-1) { + // TODO: One of the sequences is completed. Pass to cmd->applyConstraint + cmd->applyConstraint(selSeq, *token); // TODO: replace arg 2 by ongoingToken + + selSeq.clear(); + resetOngoingSequences(); + + return false; + } + _tempOnSequences->insert(*token); + allowedSelTypes = allowedSelTypes | (cmd->allowedSelSequences).at(*token).at(seqIndex+1); + } + } + + // TODO: Progress to next seqIndex + std::swap(_tempOnSequences, ongoingSequences); + seqIndex++; + } + return false; } protected: + GenericConstraintSelection* selFilterGate; + const char** constraintCursor; CmdSketcherConstraint* cmd; + std::vector selSeq; + int allowedSelTypes = 0; + /// indices of currently ongoing sequences in cmd->allowedSequences - std::set ongoingTokens; + std::set *ongoingSequences, *_tempOnSequences; /// Index within the selection sequences active - int seqIndex; + unsigned int seqIndex; + + void resetOngoingSequences() { + ongoingSequences->clear(); + for (unsigned int i = 0; i < cmd->allowedSelSequences.size(); i++) { + ongoingSequences->insert(i); + } + seqIndex = 0; + + // Estimate allowed selections from the first types in allowedSelTypes + allowedSelTypes = 0; + for (std::vector< std::vector< SelType > >::const_iterator it = cmd->allowedSelSequences.begin(); + it != cmd->allowedSelSequences.end(); ++it) { + allowedSelTypes = allowedSelTypes | (*it).at(seqIndex); + } + selFilterGate->setAllowedSelTypes(allowedSelTypes); + } }; void CmdSketcherConstraint::activated(int /*iMsg*/) @@ -931,7 +976,7 @@ void CmdSketcherConstraint::activated(int /*iMsg*/) getSelection().clearSelection(); } -// ====================================================================================== +// ============================================================================ /* XPM */ static const char *cursor_createhoriconstraint[]={ @@ -1079,83 +1124,6 @@ CmdSketcherConstrainHorizontal::CmdSketcherConstrainHorizontal() constraintCursor = cursor_createhoriconstraint; } -//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) { switch (seqIndex) { @@ -1675,11 +1643,6 @@ void CmdSketcherConstrainLock::updateAction(int mode) } } -//bool CmdSketcherConstrainLock::isActive(void) -//{ -// return isCreateGeoActive( getActiveGuiDocument() ); -//} - // ====================================================================================== /* XPM */ @@ -1760,7 +1723,6 @@ class DrawSketchHandlerCoincident: public DrawSketchHandler int GeoId_temp; Sketcher::PointPos PosId_temp; - if (VtId != -1) { sketchgui->getSketchObject()->getGeoVertexIndex(VtId,GeoId_temp,PosId_temp); ss << "Vertex" << VtId + 1;