From 180772b7eed3760efdd5e5d7bfbfcd9fea7bc4da Mon Sep 17 00:00:00 2001 From: AjinkyaDahale Date: Mon, 23 Jan 2017 05:54:23 +0530 Subject: [PATCH] Moved coincident and vertical to general constraint mode framework --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 300 ++++++-------------- 1 file changed, 93 insertions(+), 207 deletions(-) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index ab2611918747..109692fd23e0 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -788,7 +788,7 @@ namespace SketcherGui { } void setAllowedSelTypes(int types) { - if (0 < types && types < 64) this->allowedSelTypes = types; + if (0 <= types && types < 64) allowedSelTypes = types; } protected: @@ -862,11 +862,12 @@ class DrawSketchHandlerGenConstraint: public DrawSketchHandler return false; } - virtual bool releaseButton(Base::Vector2d /*onSketchPos*/) + virtual bool releaseButton(Base::Vector2d onSketchPos) { SelIdPair selIdPair; selIdPair.GeoId = Constraint::GeoUndef; selIdPair.PosId = Sketcher::none; + std::stringstream ss; SelType newSelType; //For each SelType allowed, check if button is released there and assign it to selIdPair @@ -877,24 +878,29 @@ class DrawSketchHandlerGenConstraint: public DrawSketchHandler selIdPair.GeoId = Sketcher::GeoEnum::RtPnt; selIdPair.PosId = Sketcher::start; newSelType = SelRoot; + ss << "RootPoint"; } else if (allowedSelTypes & SketcherGui::SelVertex && VtId != -1) { sketchgui->getSketchObject()->getGeoVertexIndex(VtId, selIdPair.GeoId, selIdPair.PosId); newSelType = SelVertex; + ss << "Vertex" << VtId + 1; } else if (allowedSelTypes & SketcherGui::SelEdge && CrvId != -1) { selIdPair.GeoId = CrvId; newSelType = SelEdge; + ss << "Edge" << CrvId + 1; } else if (allowedSelTypes & SketcherGui::SelHAxis && CrsId == 1) { selIdPair.GeoId = Sketcher::GeoEnum::HAxis; newSelType = SelHAxis; + ss << "H_Axis"; } else if (allowedSelTypes & SketcherGui::SelVAxis && CrsId == 2) { selIdPair.GeoId = Sketcher::GeoEnum::VAxis; newSelType = SelVAxis; + ss << "V_Axis"; } else if (allowedSelTypes & SketcherGui::SelExternalEdge) { //TODO: Figure out how this works @@ -910,7 +916,12 @@ class DrawSketchHandlerGenConstraint: public DrawSketchHandler else { // TODO: If mouse is released on something allowed, select it and move forward selSeq.push_back(selIdPair); - + Gui::Selection().addSelection(sketchgui->getSketchObject()->getDocument()->getName(), + sketchgui->getSketchObject()->getNameInDocument(), + ss.str().c_str(), + onSketchPos.x, + onSketchPos.y, + 0.f); _tempOnSequences->clear(); allowedSelTypes = 0; for (std::set::iterator token = ongoingSequences->begin(); @@ -933,6 +944,7 @@ class DrawSketchHandlerGenConstraint: public DrawSketchHandler // TODO: Progress to next seqIndex std::swap(_tempOnSequences, ongoingSequences); seqIndex++; + selFilterGate->setAllowedSelTypes(allowedSelTypes); } return false; @@ -966,6 +978,8 @@ class DrawSketchHandlerGenConstraint: public DrawSketchHandler allowedSelTypes = allowedSelTypes | (*it).at(seqIndex); } selFilterGate->setAllowedSelTypes(allowedSelTypes); + + Gui::Selection().clearSelection(); } }; @@ -1139,85 +1153,20 @@ static const char *cursor_createvertconstraint[]={ "................................", "................................"}; -class DrawSketchHandlerVertConstraint: public DrawSketchHandler +class CmdSketcherConstrainVertical : public CmdSketcherConstraint { public: - DrawSketchHandlerVertConstraint() {} - virtual ~DrawSketchHandlerVertConstraint() - { - 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(cursor_createvertconstraint), 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('Vertical',%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); + CmdSketcherConstrainVertical(); + virtual ~CmdSketcherConstrainVertical(){} + virtual const char* className() const + { return "CmdSketcherConstrainVertical"; } +protected: + virtual void applyConstraint(std::vector &selSeq, int seqIndex); - if(autoRecompute) - Gui::Command::updateActive(); - } - return false; - } }; -DEF_STD_CMD_A(CmdSketcherConstrainVertical); - CmdSketcherConstrainVertical::CmdSketcherConstrainVertical() - :Command("Sketcher_ConstrainVertical") + :CmdSketcherConstraint("Sketcher_ConstrainVertical") { sAppModule = "Sketcher"; sGroup = QT_TR_NOOP("Sketcher"); @@ -1228,37 +1177,24 @@ CmdSketcherConstrainVertical::CmdSketcherConstrainVertical() sPixmap = "Constraint_Vertical"; sAccel = "V"; eType = ForEdit; + + allowedSelSequences = {{SelEdge}}; + constraintCursor = cursor_createvertconstraint; } -void CmdSketcherConstrainVertical::activated(int iMsg) +void CmdSketcherConstrainVertical::applyConstraint(std::vector &selSeq, int seqIndex) { - Q_UNUSED(iMsg); - - ActivateHandler(getActiveGuiDocument(), new DrawSketchHandlerVertConstraint()); - - // 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(); + switch (seqIndex) { + case 0: // {Edge} + // TODO: create the constraint + SketcherGui::ViewProviderSketch* sketchgui = static_cast(getActiveGuiDocument()->getInEdit()); + Sketcher::SketchObject* Obj = sketchgui->getSketchObject(); - 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 std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues(); - 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")); @@ -1268,50 +1204,35 @@ void CmdSketcherConstrainVertical::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("Impossible constraint"), - QObject::tr("The selected edge has already a horizontal 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("Double constraint"), - QObject::tr("The selected edge has already a vertical 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 vertical constraint!")); - return; - } - - // undo command open - openCommand("add vertical constraint"); - for (std::vector::iterator it=ids.begin(); it != ids.end(); it++) { - // issue the actual command to create the constraint - doCommand(Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Vertical',%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); + // undo command open + Gui::Command::openCommand("add vertical constraint"); + // issue the actual commands to create the constraint + Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Vertical',%d)) ", + sketchgui->getObject()->getNameInDocument(),CrvId); + // finish the transaction and update + Gui::Command::commitCommand(); - if(autoRecompute) - Gui::Command::updateActive(); + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); + bool autoRecompute = hGrp->GetBool("AutoRecompute",false); - // clear the selection (convenience) - getSelection().clearSelection(); -} + if(autoRecompute) + Gui::Command::updateActive(); + } -bool CmdSketcherConstrainVertical::isActive(void) -{ - return isCreateGeoActive( getActiveGuiDocument() ); + break; + } } // ====================================================================================== @@ -1512,7 +1433,6 @@ class DrawSketchHandlerCoincident: public DrawSketchHandler virtual bool releaseButton(Base::Vector2d onSketchPos) { - Q_UNUSED(onSketchPos); int VtId = sketchgui->getPreselectPoint(); int CrsId = sketchgui->getPreselectCross(); std::stringstream ss; @@ -1577,10 +1497,19 @@ class DrawSketchHandlerCoincident: public DrawSketchHandler Sketcher::PointPos PosId1, PosId2; }; -DEF_STD_CMD_A(CmdSketcherConstrainCoincident); +class CmdSketcherConstrainCoincident : public CmdSketcherConstraint +{ +public: + CmdSketcherConstrainCoincident(); + virtual ~CmdSketcherConstrainCoincident(){} + virtual const char* className() const + { return "CmdSketcherConstrainCoincident"; } +protected: + virtual void applyConstraint(std::vector &selSeq, int seqIndex); +}; CmdSketcherConstrainCoincident::CmdSketcherConstrainCoincident() - :Command("Sketcher_ConstrainCoincident") + :CmdSketcherConstraint("Sketcher_ConstrainCoincident") { sAppModule = "Sketcher"; sGroup = QT_TR_NOOP("Sketcher"); @@ -1591,85 +1520,42 @@ CmdSketcherConstrainCoincident::CmdSketcherConstrainCoincident() sPixmap = "Constraint_PointOnPoint"; sAccel = "C"; eType = ForEdit; + + allowedSelSequences = {{SelVertex, SelVertex}, {SelVertex, SelRoot}, + {SelRoot, SelVertex}}; + constraintCursor = cursor_createcoincident; } -void CmdSketcherConstrainCoincident::activated(int iMsg) +void CmdSketcherConstrainCoincident::applyConstraint(std::vector &selSeq, int seqIndex) { - Q_UNUSED(iMsg); - ActivateHandler(getActiveGuiDocument(), new DrawSketchHandlerCoincident()); - - // 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 vertexes 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()); - - if (SubNames.size() < 2) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Select two or more vertexes from the sketch.")); - return; - } - - for (std::vector::const_iterator it = SubNames.begin(); it != SubNames.end(); ++it) { - int GeoId; - Sketcher::PointPos PosId; - getIdsFromName(*it, Obj, GeoId, PosId); - if (isEdge(GeoId,PosId)) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Select two or more vertexes from the sketch.")); - return; - } - } + switch (seqIndex) { + case 0: // {SelVertex, SelVertex} + case 1: // {SelVertex, SelRoot} + case 2: // {SelRoot, SelVertex} + // TODO: create the constraint + SketcherGui::ViewProviderSketch* sketchgui = static_cast(getActiveGuiDocument()->getInEdit()); + Sketcher::SketchObject* Obj = sketchgui->getSketchObject(); - int GeoId1, GeoId2; - Sketcher::PointPos PosId1, PosId2; - getIdsFromName(SubNames[0], Obj, GeoId1, PosId1); + int GeoId1 = selSeq.at(0).GeoId, GeoId2 = selSeq.at(1).GeoId; + Sketcher::PointPos PosId1 = selSeq.at(0).PosId, PosId2 = selSeq.at(1).PosId; - // undo command open - bool constraintsAdded = false; - openCommand("add coincident constraint"); - for (std::size_t i=1; iarePointsCoincident(GeoId1,PosId1,GeoId2,PosId2); - - if (!constraintExists) { - constraintsAdded = true; + bool constraintExists = Obj->arePointsCoincident(GeoId1, PosId1, GeoId2, PosId2); + if (!constraintExists && (GeoId1 != GeoId2)) { Gui::Command::doCommand( - Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ", - selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2); + Gui::Command::Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident', %d, %d, %d, %d)) ", + sketchgui->getObject()->getNameInDocument(), GeoId1, PosId1, GeoId2, PosId2); + Gui::Command::commitCommand(); + } + else { + Gui::Command::abortCommand(); } - } - - // finish or abort the transaction and update - if (constraintsAdded) - commitCommand(); - else - abortCommand(); - - 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 CmdSketcherConstrainCoincident::isActive(void) -{ - // return isCreateConstraintActive( getActiveGuiDocument() ); - return isCreateGeoActive( getActiveGuiDocument() ); + break; + } } // ======================================================================================