diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 2b6921e46..76ec66d7f 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -1258,12 +1258,35 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg) PosId1 = Sketcher::start; } + if (isEdge(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // horizontal length of a line + if (GeoId1 < 0 && GeoId1 >= -2) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Cannot add a horizontal length constraint on an axis!")); + return; + } + + const Part::Geometry *geom = Obj->getGeometry(GeoId1); + if (geom->getTypeId() == Part::GeomLineSegment::getClassTypeId()) { + //convert to as if two endpoints of the line have been selected + PosId1 = Sketcher::start; + GeoId2 = GeoId1; + PosId2 = Sketcher::end; + } + } if (isVertex(GeoId1,PosId1) && isVertex(GeoId2,PosId2)) { // point to point horizontal distance Base::Vector3d pnt1 = Obj->getPoint(GeoId1,PosId1); Base::Vector3d pnt2 = Obj->getPoint(GeoId2,PosId2); double ActLength = pnt2.x-pnt1.x; + //negative sign avoidance: swap the points to make value positive + if (ActLength < -Precision::Confusion()) { + std::swap(GeoId1,GeoId2); + std::swap(PosId1,PosId2); + std::swap(pnt1, pnt2); + ActLength = -ActLength; + } + openCommand("add point to point horizontal distance constraint"); Gui::Command::doCommand( Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceX',%d,%d,%d,%d,%f)) ", @@ -1281,38 +1304,6 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg) return; } - else if (isEdge(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // horizontal length of a line - - if (GeoId1 < 0 && GeoId1 >= -2) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Cannot add a horizontal length constraint on an axis!")); - return; - } - - const Part::Geometry *geom = Obj->getGeometry(GeoId1); - if (geom->getTypeId() == Part::GeomLineSegment::getClassTypeId()) { - const Part::GeomLineSegment *lineSeg; - lineSeg = dynamic_cast(geom); - double ActLength = lineSeg->getEndPoint().x-lineSeg->getStartPoint().x; - - openCommand("add horizontal length constraint"); - Gui::Command::doCommand( - Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceX',%d,%f)) ", - selection[0].getFeatName(),GeoId1,ActLength); - - if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving - const std::vector &ConStr = Obj->Constraints.getValues(); - - Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)", - selection[0].getFeatName(),ConStr.size()-1,"False"); - finishDistanceConstraint(this, Obj,false); - } - else - finishDistanceConstraint(this, Obj,true); - - return; - } - } else if (isVertex(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // point on fixed x-coordinate if (GeoId1 < 0 && GeoId1 >= -2) { @@ -1421,12 +1412,36 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg) else if (GeoId1 == -1 && PosId1 == Sketcher::none) PosId1 = Sketcher::start; + if (isEdge(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // vertical length of a line + if (GeoId1 < 0 && GeoId1 >= -2) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Cannot add a vertical length constraint on an axis!")); + return; + } + + const Part::Geometry *geom = Obj->getGeometry(GeoId1); + if (geom->getTypeId() == Part::GeomLineSegment::getClassTypeId()) { + //convert to as if two endpoints of the line have been selected + PosId1 = Sketcher::start; + GeoId2 = GeoId1; + PosId2 = Sketcher::end; + } + } + if (isVertex(GeoId1,PosId1) && isVertex(GeoId2,PosId2)) { // point to point vertical distance Base::Vector3d pnt1 = Obj->getPoint(GeoId1,PosId1); Base::Vector3d pnt2 = Obj->getPoint(GeoId2,PosId2); double ActLength = pnt2.y-pnt1.y; + //negative sign avoidance: swap the points to make value positive + if (ActLength < -Precision::Confusion()) { + std::swap(GeoId1,GeoId2); + std::swap(PosId1,PosId2); + std::swap(pnt1, pnt2); + ActLength = -ActLength; + } + openCommand("add point to point vertical distance constraint"); Gui::Command::doCommand( Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceY',%d,%d,%d,%d,%f)) ", @@ -1444,38 +1459,6 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg) return; } - else if (isEdge(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // vertical length of a line - - if (GeoId1 < 0 && GeoId1 >= -2) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Cannot add a vertical length constraint on an axis!")); - return; - } - - const Part::Geometry *geom = Obj->getGeometry(GeoId1); - if (geom->getTypeId() == Part::GeomLineSegment::getClassTypeId()) { - const Part::GeomLineSegment *lineSeg; - lineSeg = dynamic_cast(geom); - double ActLength = lineSeg->getEndPoint().y-lineSeg->getStartPoint().y; - - openCommand("add vertical length constraint"); - Gui::Command::doCommand( - Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceY',%d,%f)) ", - selection[0].getFeatName(),GeoId1,ActLength); - - if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving - const std::vector &ConStr = Obj->Constraints.getValues(); - - Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)", - selection[0].getFeatName(),ConStr.size()-1,"False"); - finishDistanceConstraint(this, Obj,false); - } - else - finishDistanceConstraint(this, Obj,true); - - return; - } - } else if (isVertex(GeoId1,PosId1) && GeoId2 == Constraint::GeoUndef) { // point on fixed y-coordinate if (GeoId1 < 0 && GeoId1 >= -2) { @@ -2614,6 +2597,13 @@ void CmdSketcherConstrainAngle::activated(int iMsg) Base::Vector3d p = Obj->getPoint(GeoId3, PosId3 ); ActAngle = Obj->calculateAngleViaPoint(GeoId1,GeoId2,p.x,p.y); + //negative constraint value avoidance + if (ActAngle < -Precision::Angular()){ + std::swap(GeoId1, GeoId2); + std::swap(PosId1, PosId2); + ActAngle = -ActAngle; + } + Gui::Command::doCommand( Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('AngleViaPoint',%d,%d,%d,%d,%f)) ", selection[0].getFeatName(),GeoId1,GeoId2,GeoId3,PosId3,ActAngle);