From 85b2fec8f657bc4659b0f891e36db02408ce675f Mon Sep 17 00:00:00 2001 From: logari81 Date: Mon, 14 Nov 2011 18:45:59 +0000 Subject: [PATCH] + revert to previous values when setDatum fails + detect well known invalid values for setDatum + hide some negative distance datums in the gui git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5133 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d --- src/Mod/Sketcher/App/SketchObject.cpp | 31 ++++++++++++------- src/Mod/Sketcher/App/SketchObjectPyImp.cpp | 4 +++ src/Mod/Sketcher/Gui/EditDatumDialog.cpp | 26 +++++++++++++--- .../Sketcher/Gui/TaskSketcherConstrains.cpp | 7 +++-- src/Mod/Sketcher/Gui/ViewProviderSketch.cpp | 5 +-- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 47bd48a18..ddcf3f6b3 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -136,6 +136,9 @@ int SketchObject::setDatum(int ConstrId, double Datum) type != Angle) return -1; + if ((type == Distance || type == Radius) && Datum <= 0) + return (Datum == 0) ? -5 : -4; + // copy the list std::vector newVals(vals); // clone the changed Constraint @@ -148,21 +151,25 @@ int SketchObject::setDatum(int ConstrId, double Datum) // set up a sketch (including dofs counting and diagnosing of conflicts) Sketch sketch; int dofs = sketch.setUpSketch(Geometry.getValues(), Constraints.getValues()); + int err=0; if (dofs < 0) // over-constrained sketch - return -3; - if (sketch.hasConflicts()) // conflicting constraints - return -3; - // solving - if (sketch.solve() != 0) - return -2; + err = -3; + else if (sketch.hasConflicts()) // conflicting constraints + err = -3; + else if (sketch.solve() != 0) // solving + err = -2; - // set the newly solved geometry - std::vector geomlist = sketch.getGeometry(); - Geometry.setValues(geomlist); - for (std::vector::iterator it = geomlist.begin(); it != geomlist.end(); ++it) - if (*it) delete *it; + if (err == 0) { + // set the newly solved geometry + std::vector geomlist = sketch.getGeometry(); + Geometry.setValues(geomlist); + for (std::vector::iterator it = geomlist.begin(); it != geomlist.end(); ++it) + if (*it) delete *it; + } + else + this->Constraints.setValues(vals); - return 0; + return err; } int SketchObject::movePoint(int geoIndex, PointPos PosId, const Base::Vector3d& toPoint, bool relative) diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index a250cd47e..eab18a2f1 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -198,6 +198,10 @@ PyObject* SketchObjectPy::setDatum(PyObject *args) str << "Cannot set the datum because the sketch contains conflicting constraints"; else if (err == -2) str << "Datum " << Datum << " for the constraint with index " << Index << " is invalid"; + else if (err == -4) + str << "Negative datum values are not valid for the constraint with index " << Index; + else if (err == -5) + str << "Zero is not a valid datum for the constraint with index " << Index; else str << "Unexpected problem at setting datum " << Datum << " for the constraint with index " << Index; PyErr_SetString(PyExc_ValueError, str.str().c_str()); diff --git a/src/Mod/Sketcher/Gui/EditDatumDialog.cpp b/src/Mod/Sketcher/Gui/EditDatumDialog.cpp index cb28222c9..ed1be8a0a 100644 --- a/src/Mod/Sketcher/Gui/EditDatumDialog.cpp +++ b/src/Mod/Sketcher/Gui/EditDatumDialog.cpp @@ -31,6 +31,7 @@ # include +#include #include #include #include @@ -75,7 +76,7 @@ void EditDatumDialog::exec(bool atCursor) double datum = Constr->Value; if (Constr->Type == Sketcher::Angle) - datum = datum * 180./M_PI; + datum = Base::toDegrees(datum); Gui::MDIView *mdi = Gui::Application::Instance->activeDocument()->getActiveView(); Gui::View3DInventorViewer *viewer = static_cast(mdi)->getViewer(); @@ -85,7 +86,14 @@ void EditDatumDialog::exec(bool atCursor) Ui::InsertDatum ui_ins_datum; ui_ins_datum.setupUi(&dlg); - ui_ins_datum.lineEdit->setText(QLocale::system().toString(datum,'g',6)); + double init_val; + if (Constr->Type == Sketcher::Angle || + Constr->Type == Sketcher::DistanceX || Constr->Type == Sketcher::DistanceY) + init_val = std::abs(datum); + else + init_val = datum; + + ui_ins_datum.lineEdit->setText(QLocale::system().toString(init_val,'g',6)); ui_ins_datum.lineEdit->selectAll(); if (atCursor) @@ -96,15 +104,25 @@ void EditDatumDialog::exec(bool atCursor) double newDatum = ui_ins_datum.lineEdit->text().toDouble(&ok); if (ok) { if (Constr->Type == Sketcher::Angle) - newDatum = newDatum * M_PI/180.; + newDatum = Base::toRadians(newDatum); + + if (newDatum < 0 && + (Constr->Type == Sketcher::Angle || + Constr->Type == Sketcher::DistanceX || Constr->Type == Sketcher::DistanceY)) { + // Permit negative values to flip the sign of the constraint + newDatum = ((datum >= 0) ? -1 : 1) * std::abs(newDatum); + } + try { Gui::Command::openCommand("Modify sketch constraints"); Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.setDatum(%i,%f)", vp->getObject()->getNameInDocument(), ConstrNbr, newDatum); + Gui::Command::commitCommand(); } catch (const Base::Exception& e) { - QMessageBox::critical(qApp->activeWindow(), QObject::tr("Distance constraint"), QString::fromUtf8(e.what())); + QMessageBox::critical(qApp->activeWindow(), QObject::tr("Dimensional constraint"), QString::fromUtf8(e.what())); + Gui::Command::abortCommand(); } } } diff --git a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp index 46cdf50d4..0b72fbdab 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp +++ b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp @@ -35,6 +35,7 @@ #include +#include #include #include #include @@ -261,13 +262,13 @@ void TaskSketcherConstrains::slotConstraintsChanged(void) break; case Sketcher::DistanceX: if(Filter<3 || (*it)->Name != ""){ - name = QString::fromLatin1("%1 (%2)").arg(name).arg((*it)->Value); + name = QString::fromLatin1("%1 (%2)").arg(name).arg(std::abs((*it)->Value)); ui->listWidgetConstraints->addItem(new ConstraintItem(hdist,name,i-1,(*it)->Type)); } break; case Sketcher::DistanceY: if(Filter<3 || (*it)->Name != ""){ - name = QString::fromLatin1("%1 (%2)").arg(name).arg((*it)->Value); + name = QString::fromLatin1("%1 (%2)").arg(name).arg(std::abs((*it)->Value)); ui->listWidgetConstraints->addItem(new ConstraintItem(vdist,name,i-1,(*it)->Type)); } break; @@ -279,7 +280,7 @@ void TaskSketcherConstrains::slotConstraintsChanged(void) break; case Sketcher::Angle: if(Filter<3 || (*it)->Name != ""){ - name = QString::fromLatin1("%1 (%2)").arg(name).arg((*it)->Value * 180./M_PI); + name = QString::fromLatin1("%1 (%2)").arg(name).arg(Base::toDegrees(std::abs((*it)->Value))); ui->listWidgetConstraints->addItem(new ConstraintItem(angl,name,i-1,(*it)->Type)); } break; diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index 92e065263..788169aa4 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -68,6 +68,7 @@ #include /// Here the FreeCAD includes sorted by Base,App,Gui...... +#include #include #include #include @@ -1917,7 +1918,7 @@ Restart: SbVec3f midpos = (p1_ + p2)/2; SoDatumLabel *asciiText = dynamic_cast(sep->getChild(4)); - asciiText->string = SbString().sprintf("%.2f",Constr->Value); + asciiText->string = SbString().sprintf("%.2f",std::abs(Constr->Value)); // Get Bounding box dimensions for Datum text Gui::View3DInventorViewer *viewer = static_cast(mdi)->getViewer(); @@ -2197,7 +2198,7 @@ Restart: break; SoDatumLabel *asciiText = dynamic_cast(sep->getChild(4)); - asciiText->string = SbString().sprintf("%.2f",Constr->Value * 180./M_PI); + asciiText->string = SbString().sprintf("%.2f",Base::toDegrees(std::abs(Constr->Value))); // Get Bounding box dimensions for Datum text Gui::View3DInventorViewer *viewer = static_cast(mdi)->getViewer();