From b328943698950f10e23d5a35942f66dfb6dff42a Mon Sep 17 00:00:00 2001 From: jrheinlaender Date: Fri, 18 Jan 2013 19:06:37 +0430 Subject: [PATCH] 0000437 Sketcher: ask for value immediately after creating a length constraint --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 117 ++++++-------------- src/Mod/Sketcher/Gui/EditDatumDialog.cpp | 15 ++- src/Mod/Sketcher/Gui/EditDatumDialog.h | 4 +- 3 files changed, 46 insertions(+), 90 deletions(-) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 3cce6cf0a..086ae112e 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -37,6 +37,7 @@ #include #include "ViewProviderSketch.h" +#include "EditDatumDialog.h" using namespace std; using namespace SketcherGui; @@ -54,8 +55,15 @@ bool isCreateConstraintActive(Gui::Document *doc) return false; } -void updateDatumDistance(Gui::Document *doc, Constraint *constr) +// Utility method to avoid repeating the same code over and over again +void finishDistanceConstraint(Gui::Command* cmd, Sketcher::SketchObject* sketch) { + // Get the latest constraint + const std::vector &ConStr = sketch->Constraints.getValues(); + Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + + // Guess some reasonable distance for placing the datum text + Gui::Document *doc = cmd->getActiveGuiDocument(); float sf = 1.f; if (doc && doc->getInEdit() && doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) { SketcherGui::ViewProviderSketch *vp = dynamic_cast(doc->getInEdit()); @@ -64,6 +72,14 @@ void updateDatumDistance(Gui::Document *doc, Constraint *constr) constr->LabelDistance = 2. * sf; vp->draw(); // Redraw } + + // Ask for the value of the distance immediately + EditDatumDialog *editDatumDialog = new EditDatumDialog(sketch, ConStr.size() - 1); + editDatumDialog->exec(false); + delete editDatumDialog; + + //updateActive(); + cmd->getSelection().clearSelection(); } bool checkBothExternal(int GeoId1, int GeoId2) @@ -621,14 +637,8 @@ void CmdSketcherConstrainDistance::activated(int iMsg) } commitCommand(); - // Get the latest constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } else if ((isVertex(GeoId1,PosId1) && isEdge(GeoId2,PosId2)) || @@ -653,14 +663,7 @@ void CmdSketcherConstrainDistance::activated(int iMsg) selection[0].getFeatName(),GeoId1,PosId1,GeoId2,ActDist); commitCommand(); - // Get the latest constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; - - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); + finishDistanceConstraint(this, Obj); return; } } @@ -684,14 +687,8 @@ void CmdSketcherConstrainDistance::activated(int iMsg) selection[0].getFeatName(),GeoId1,ActLength); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } } @@ -859,14 +856,8 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg) selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,ActLength); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } else if (isEdge(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // horizontal length of a line @@ -890,14 +881,8 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg) selection[0].getFeatName(),GeoId1,ActLength); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } } @@ -919,14 +904,8 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg) selection[0].getFeatName(),GeoId1,PosId1,ActX); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } @@ -1009,14 +988,8 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg) selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,ActLength); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } else if (isEdge(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // vertical length of a line @@ -1040,14 +1013,8 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg) selection[0].getFeatName(),GeoId1,ActLength); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } } @@ -1069,14 +1036,8 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg) selection[0].getFeatName(),GeoId1,PosId1,ActY); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; + finishDistanceConstraint(this, Obj); - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); return; } @@ -1491,8 +1452,8 @@ void CmdSketcherConstrainRadius::activated(int iMsg) Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Radius',%d,%f)) ", selection[0].getFeatName(),GeoId,ActRadius); commitCommand(); - //updateActive(); - getSelection().clearSelection(); + + finishDistanceConstraint(this, Obj); return; } else if (geom->getTypeId() == Part::GeomCircle::getClassTypeId()) { @@ -1504,8 +1465,8 @@ void CmdSketcherConstrainRadius::activated(int iMsg) Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Radius',%d,%f)) ", selection[0].getFeatName(),GeoId,ActRadius); commitCommand(); - //updateActive(); - getSelection().clearSelection(); + + finishDistanceConstraint(this, Obj); return; } } @@ -1618,14 +1579,7 @@ void CmdSketcherConstrainAngle::activated(int iMsg) selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,ActAngle); commitCommand(); - // Get the latest created constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; - - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); + finishDistanceConstraint(this, Obj); return; } } else if (isEdge(GeoId1,PosId1)) { // line angle @@ -1649,14 +1603,7 @@ void CmdSketcherConstrainAngle::activated(int iMsg) selection[0].getFeatName(),GeoId1,ActAngle); commitCommand(); - // Get the latest constraint - const std::vector &ConStr = dynamic_cast(selection[0].getObject())->Constraints.getValues(); - Sketcher::Constraint *constr = ConStr[ConStr.size() -1]; - - updateDatumDistance(getActiveGuiDocument(), constr); - - //updateActive(); - getSelection().clearSelection(); + finishDistanceConstraint(this, Obj); return; } } diff --git a/src/Mod/Sketcher/Gui/EditDatumDialog.cpp b/src/Mod/Sketcher/Gui/EditDatumDialog.cpp index 58f1f027a..80de72074 100644 --- a/src/Mod/Sketcher/Gui/EditDatumDialog.cpp +++ b/src/Mod/Sketcher/Gui/EditDatumDialog.cpp @@ -45,9 +45,16 @@ using namespace SketcherGui; -EditDatumDialog::EditDatumDialog(ViewProviderSketch* vp, int ConstrNbr) : vp(vp), ConstrNbr(ConstrNbr) +EditDatumDialog::EditDatumDialog(ViewProviderSketch* vp, int ConstrNbr) : ConstrNbr(ConstrNbr) { - const std::vector &Constraints = vp->getSketchObject()->Constraints.getValues(); + sketch = vp->getSketchObject(); + const std::vector &Constraints = sketch->Constraints.getValues(); + Constr = Constraints[ConstrNbr]; +} + +EditDatumDialog::EditDatumDialog(Sketcher::SketchObject* pcSketch, int ConstrNbr) : sketch(pcSketch), ConstrNbr(ConstrNbr) +{ + const std::vector &Constraints = sketch->Constraints.getValues(); Constr = Constraints[ConstrNbr]; } @@ -68,7 +75,7 @@ void EditDatumDialog::exec(bool atCursor) Constr->Type == Sketcher::DistanceX || Constr->Type == Sketcher::DistanceY || Constr->Type == Sketcher::Radius || Constr->Type == Sketcher::Angle) { - if (vp->getSketchObject()->hasConflicts()) { + if (sketch->hasConflicts()) { QMessageBox::critical(qApp->activeWindow(), QObject::tr("Distance constraint"), QObject::tr("Not allowed to edit the datum because the sketch contains conflicting constraints")); return; @@ -121,7 +128,7 @@ void EditDatumDialog::exec(bool atCursor) try { Gui::Command::openCommand("Modify sketch constraints"); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.setDatum(%i,%f)", - vp->getObject()->getNameInDocument(), + sketch->getNameInDocument(), ConstrNbr, newDatum); Gui::Command::commitCommand(); } diff --git a/src/Mod/Sketcher/Gui/EditDatumDialog.h b/src/Mod/Sketcher/Gui/EditDatumDialog.h index b466c0822..9fcb1e046 100644 --- a/src/Mod/Sketcher/Gui/EditDatumDialog.h +++ b/src/Mod/Sketcher/Gui/EditDatumDialog.h @@ -26,6 +26,7 @@ class SoSensor; namespace Sketcher { class Constraint; +class SketchObject; } namespace SketcherGui { @@ -34,13 +35,14 @@ class ViewProviderSketch; class EditDatumDialog { public: EditDatumDialog(ViewProviderSketch* vp, int ConstrNbr); + EditDatumDialog(Sketcher::SketchObject* pcSketch, int ConstrNbr); ~EditDatumDialog(); static void run(void * data, SoSensor * sensor); void exec(bool atCursor=true); private: - ViewProviderSketch* vp; + Sketcher::SketchObject* sketch; Sketcher::Constraint* Constr; int ConstrNbr; };