From 2b34145e4c2b49e64939bde5f6f2aca5d6f65d30 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 13 Aug 2020 17:24:10 +0200 Subject: [PATCH] Sketcher: [skip ci] add function to sketch validation panel to remove degenerated geometry --- src/Mod/Sketcher/App/SketchAnalysis.cpp | 46 ++++++++ src/Mod/Sketcher/App/SketchAnalysis.h | 5 + .../Sketcher/Gui/TaskSketcherValidation.cpp | 37 +++++- src/Mod/Sketcher/Gui/TaskSketcherValidation.h | 2 + .../Sketcher/Gui/TaskSketcherValidation.ui | 107 +++++++++++------- 5 files changed, 154 insertions(+), 43 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchAnalysis.cpp b/src/Mod/Sketcher/App/SketchAnalysis.cpp index 70b10ac8d8..529926fd27 100644 --- a/src/Mod/Sketcher/App/SketchAnalysis.cpp +++ b/src/Mod/Sketcher/App/SketchAnalysis.cpp @@ -822,3 +822,49 @@ std::vector SketchAnalysis::getOpenVertices(void) const return points; } + +int SketchAnalysis::detectDegeneratedGeometries(double tolerance) +{ + int countDegenerated = 0; + const std::vector& geom = sketch->getInternalGeometry(); + for (std::size_t i=0; iConstruction) + continue; + + if (g->getTypeId().isDerivedFrom(Part::GeomCurve::getClassTypeId())) { + Part::GeomCurve* curve = static_cast(g); + double len = curve->length(curve->getFirstParameter(), curve->getLastParameter()); + if (len < tolerance) + countDegenerated++; + } + } + + return countDegenerated; +} + +int SketchAnalysis::removeDegeneratedGeometries(double tolerance) +{ + std::set delInternalGeometries; + const std::vector& geom = sketch->getInternalGeometry(); + for (std::size_t i=0; iConstruction) + continue; + + if (g->getTypeId().isDerivedFrom(Part::GeomCurve::getClassTypeId())) { + Part::GeomCurve* curve = static_cast(g); + double len = curve->length(curve->getFirstParameter(), curve->getLastParameter()); + if (len < tolerance) + delInternalGeometries.insert(static_cast(i)); + } + } + + for (auto it = delInternalGeometries.rbegin(); it != delInternalGeometries.rend(); ++it) { + sketch->delGeometry(*it); + } + + return static_cast(delInternalGeometries.size()); +} diff --git a/src/Mod/Sketcher/App/SketchAnalysis.h b/src/Mod/Sketcher/App/SketchAnalysis.h index 9f60802b34..08b3bd4884 100644 --- a/src/Mod/Sketcher/App/SketchAnalysis.h +++ b/src/Mod/Sketcher/App/SketchAnalysis.h @@ -104,6 +104,11 @@ public: /// Equality constraints simple routine Make step (see constructor) void makeMissingEquality(bool onebyone = true); + /// Detect degenerated geometries + int detectDegeneratedGeometries(double tolerance); + /// Remove degenerated geometries + int removeDegeneratedGeometries(double tolerance); + // Complex routines (see constructor) /// Fully automated multi-constraint autoconstraining diff --git a/src/Mod/Sketcher/Gui/TaskSketcherValidation.cpp b/src/Mod/Sketcher/Gui/TaskSketcherValidation.cpp index b2d4f853ee..afd57cf1ba 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherValidation.cpp +++ b/src/Mod/Sketcher/Gui/TaskSketcherValidation.cpp @@ -234,7 +234,7 @@ void SketcherValidation::on_findReversed_clicked() ui->swapReversed->setEnabled(false); } } else { - QMessageBox::warning(this, tr("Reversed external geometry"), + QMessageBox::information(this, tr("Reversed external geometry"), tr("No reversed external-geometry arcs were found.")); } } @@ -350,6 +350,41 @@ void SketcherValidation::hidePoints() } } +void SketcherValidation::on_findDegenerated_clicked() +{ + double prec = Precision::Confusion(); + int count = sketchAnalyser.detectDegeneratedGeometries(prec); + + if (count == 0) { + QMessageBox::information(this, tr("No degenerated geometry"), + tr("No degenerated geometry found")); + ui->fixDegenerated->setEnabled(false); + } + else { + QMessageBox::warning(this, tr("Degenerated geometry"), + tr("%1 degenerated geometry found").arg(count)); + ui->fixDegenerated->setEnabled(true); + } +} + +void SketcherValidation::on_fixDegenerated_clicked() +{ + // undo command open + App::Document* doc = sketch->getDocument(); + doc->openTransaction("Remove degenerated geometry"); + + double prec = Precision::Confusion(); + sketchAnalyser.removeDegeneratedGeometries(prec); + + ui->fixButton->setEnabled(false); + hidePoints(); + + // finish the transaction and update + Gui::WaitCursor wc; + doc->commitTransaction(); + doc->recompute(); +} + // ----------------------------------------------- TaskSketcherValidation::TaskSketcherValidation(Sketcher::SketchObject* Obj) diff --git a/src/Mod/Sketcher/Gui/TaskSketcherValidation.h b/src/Mod/Sketcher/Gui/TaskSketcherValidation.h index f83556a870..127a561e65 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherValidation.h +++ b/src/Mod/Sketcher/Gui/TaskSketcherValidation.h @@ -58,6 +58,8 @@ private Q_SLOTS: void on_orientLockEnable_clicked(); void on_orientLockDisable_clicked(); void on_delConstrExtr_clicked(); + void on_findDegenerated_clicked(); + void on_fixDegenerated_clicked(); private: void showPoints(const std::vector&); diff --git a/src/Mod/Sketcher/Gui/TaskSketcherValidation.ui b/src/Mod/Sketcher/Gui/TaskSketcherValidation.ui index 2e22ca0fef..4cf647b8cf 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherValidation.ui +++ b/src/Mod/Sketcher/Gui/TaskSketcherValidation.ui @@ -14,36 +14,6 @@ Sketcher validation - - - - Invalid constraints - - - - - - Fix - - - - - - - Find - - - - - - - Delete constraints to external geom. - - - - - - @@ -57,16 +27,19 @@ - - - - Fix - - - + + + + Ignore construction geometry + + + true + + + @@ -74,6 +47,13 @@ + + + + Fix + + + @@ -81,13 +61,56 @@ - - + + + + + + + Invalid constraints + + + + - Ignore construction geometry + Find - - true + + + + + + Fix + + + + + + + Delete constraints to external geom. + + + + + + + + + + Degenerated geometry + + + + + + Find + + + + + + + Fix