From 645c7acf659b1531d9bb3c91c3354408ba3c7b26 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 28 May 2024 14:28:07 +0200 Subject: [PATCH] Sketch: Refactor SketchAnalysis Refactor autoconstraint() --- src/Mod/Sketcher/App/SketchAnalysis.cpp | 155 +++++++++++------------- src/Mod/Sketcher/App/SketchAnalysis.h | 4 + 2 files changed, 72 insertions(+), 87 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchAnalysis.cpp b/src/Mod/Sketcher/App/SketchAnalysis.cpp index 91619772ee..dedd328d64 100644 --- a/src/Mod/Sketcher/App/SketchAnalysis.cpp +++ b/src/Mod/Sketcher/App/SketchAnalysis.cpp @@ -783,7 +783,6 @@ void SketchAnalysis::makeMissingEqualityOneByOne() radiusequalityConstraints.clear(); } - void SketchAnalysis::solvesketch(int& status, int& dofs, bool updategeo) { status = sketch->solve(updategeo); @@ -807,9 +806,7 @@ void SketchAnalysis::solvesketch(int& status, int& dofs, bool updategeo) } } -int SketchAnalysis::autoconstraint(double precision, - double angleprecision, - bool includeconstruction) +void SketchAnalysis::autoDeleteAllConstraints() { App::Document* doc = sketch->getDocument(); doc->openTransaction("delete all constraints"); @@ -818,15 +815,67 @@ int SketchAnalysis::autoconstraint(double precision, doc->commitTransaction(); - int status, dofs; - - solvesketch(status, dofs, true); - - if (status) { // it should not be possible at this moment as we start from a clean situation - THROWMT(Base::RuntimeError, - QT_TRANSLATE_NOOP("Exceptions", + // a failure should not be possible at this moment as we start from a clean situation + solveSketch(QT_TRANSLATE_NOOP("Exceptions", "Autoconstrain error: Unsolvable sketch without constraints.")); +} + +void SketchAnalysis::autoHorizontalVerticalConstraints() +{ + App::Document* doc = sketch->getDocument(); + doc->openTransaction("add vertical/horizontal constraints"); + + makeMissingVerticalHorizontal(); + + // finish the transaction and update + doc->commitTransaction(); + + solveSketch(QT_TRANSLATE_NOOP("Exceptions", + "Autoconstrain error: Unsolvable sketch after applying " + "horizontal and vertical constraints.")); +} + +void SketchAnalysis::autoPointOnPointCoincident() +{ + App::Document* doc = sketch->getDocument(); + doc->openTransaction("add coincident constraint"); + + makeMissingPointOnPointCoincident(); + + // finish the transaction and update + doc->commitTransaction(); + + solveSketch(QT_TRANSLATE_NOOP("Exceptions", + "Autoconstrain error: Unsolvable sketch after applying " + "point-on-point constraints.")); +} + +void SketchAnalysis::autoMissingEquality() +{ + App::Document* doc = sketch->getDocument(); + doc->openTransaction("add equality constraints"); + + try { + makeMissingEquality(); } + catch (Base::RuntimeError&) { + doc->abortTransaction(); + throw; + } + + // finish the transaction and update + doc->commitTransaction(); + + solveSketch(QT_TRANSLATE_NOOP("Exceptions", + "Autoconstrain error: Unsolvable sketch after " + "applying equality constraints.")); +} + +int SketchAnalysis::autoconstraint(double precision, + double angleprecision, + bool includeconstruction) +{ + autoDeleteAllConstraints(); // STAGE 1: Vertical/Horizontal Line Segments int nhv = detectMissingVerticalHorizontalConstraints(angleprecision); @@ -845,95 +894,27 @@ int SketchAnalysis::autoconstraint(double precision, // STAGE 3: Equality constraint detection int ne = detectMissingEqualityConstraints(precision); - Base::Console().Log( - "Constraints: Vertical/Horizontal: %d found. Point-on-point: %d. Equality: %d\n", - nhv, - nc, - ne); + Base::Console().Log("Constraints: Vertical/Horizontal: %d found. " + "Point-on-point: %d. Equality: %d\n", + nhv, + nc, + ne); // Applying STAGE 1, if any if (nhv > 0) { - App::Document* doc = sketch->getDocument(); - doc->openTransaction("add vertical/horizontal constraints"); - - makeMissingVerticalHorizontal(); - - // finish the transaction and update - doc->commitTransaction(); - - solvesketch(status, dofs, true); - - if (status == -2) { // redundants - sketch->autoRemoveRedundants(false); - solvesketch(status, dofs, false); - } - - if (status) { - THROWMT(Base::RuntimeError, - QT_TRANSLATE_NOOP("Exceptions", - "Autoconstrain error: Unsolvable sketch after applying " - "horizontal and vertical constraints.")); - } + autoHorizontalVerticalConstraints(); } // Applying STAGE 2 if (nc > 0) { - App::Document* doc = sketch->getDocument(); - doc->openTransaction("add coincident constraint"); - - makeMissingPointOnPointCoincident(); - - // finish the transaction and update - doc->commitTransaction(); - - solvesketch(status, dofs, true); - - if (status == -2) { // redundants - sketch->autoRemoveRedundants(false); - solvesketch(status, dofs, false); - } - - if (status) { - THROWMT(Base::RuntimeError, - QT_TRANSLATE_NOOP("Exceptions", - "Autoconstrain error: Unsolvable sketch after applying " - "point-on-point constraints.")); - } + autoPointOnPointCoincident(); } // Applying STAGE 3 if (ne > 0) { - App::Document* doc = sketch->getDocument(); - doc->openTransaction("add equality constraints"); - - try { - makeMissingEquality(); - } - catch (Base::RuntimeError&) { - doc->abortTransaction(); - throw; - } - - // finish the transaction and update - doc->commitTransaction(); - - solvesketch(status, dofs, true); - - if (status == -2) { // redundants - sketch->autoRemoveRedundants(false); - solvesketch(status, dofs, false); - } - - if (status) { - THROWMT( - Base::RuntimeError, - QT_TRANSLATE_NOOP( - "Exceptions", - "Autoconstrain error: Unsolvable sketch after applying equality constraints.")); - } + autoMissingEquality(); } - return 0; } diff --git a/src/Mod/Sketcher/App/SketchAnalysis.h b/src/Mod/Sketcher/App/SketchAnalysis.h index 291c92aec4..e972fc411d 100644 --- a/src/Mod/Sketcher/App/SketchAnalysis.h +++ b/src/Mod/Sketcher/App/SketchAnalysis.h @@ -182,6 +182,10 @@ private: std::vector radiusequalityConstraints; private: + void autoDeleteAllConstraints(); + void autoHorizontalVerticalConstraints(); + void autoPointOnPointCoincident(); + void autoMissingEquality(); bool checkHorizontal(Base::Vector3d dir, double angleprecision); bool checkVertical(Base::Vector3d dir, double angleprecision); void makeConstraints(std::vector&);