From a4f74f6d97231e671a357562e3060f64bc928f22 Mon Sep 17 00:00:00 2001 From: Ajinkya Dahale Date: Thu, 2 Jan 2025 10:27:41 +0530 Subject: [PATCH] [Sketcher] Write `SketchObject::replaceGeometries()` --- src/Mod/Sketcher/App/SketchObject.cpp | 43 ++++++++++++++++++++++++++- src/Mod/Sketcher/App/SketchObject.h | 6 ++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 754889d05e..f9056c56ba 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -1804,12 +1804,19 @@ int SketchObject::delGeometry(int GeoId, bool deleteinternalgeo) } int SketchObject::delGeometries(const std::vector& GeoIds) +{ + return delGeometries(GeoIds.begin(), GeoIds.end()); +} + +template +int SketchObject::delGeometries(InputIt first, InputIt last) { std::vector sGeoIds; std::vector negativeGeoIds; // Separate GeoIds into negative (external) and non-negative GeoIds - for (int geoId : GeoIds) { + for (auto it = first; it != last; ++it) { + int geoId = *it; if (geoId < 0 && geoId <= GeoEnum::RefExt) { negativeGeoIds.push_back(geoId); } @@ -1928,6 +1935,40 @@ int SketchObject::delGeometriesExclusiveList(const std::vector& GeoIds) return 0; } +void SketchObject::replaceGeometries(std::vector oldGeoIds, + std::vector& newGeos) +{ + auto vals = getInternalGeometry(); + auto newVals(vals); + + if (std::any_of(oldGeoIds.begin(), oldGeoIds.end(), [](auto geoId) { + return geoId < 0; + })) { + THROWM(ValueError, "Cannot replace external geometries and axes."); + } + + auto oldGeoIdIter = oldGeoIds.begin(); + auto newGeoIter = newGeos.begin(); + + for (; oldGeoIdIter != oldGeoIds.end() && newGeoIter != newGeos.end(); + ++oldGeoIdIter, ++newGeoIter) { + GeometryFacade::copyId(getGeometry(*oldGeoIdIter), *newGeoIter); + newVals[*oldGeoIdIter] = *newGeoIter; + } + + if (newGeoIter != newGeos.end()) { + for (; newGeoIter != newGeos.end(); ++newGeoIter) { + generateId(*newGeoIter); + newVals.push_back(*newGeoIter); + } + } + else { + delGeometries(oldGeoIdIter, oldGeoIds.end()); + } + + Geometry.setValues(std::move(newVals)); +} + int SketchObject::deleteAllGeometry() { // no need to check input data validity as this is an sketchobject managed operation. diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index b132017bff..62a13821dd 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -157,6 +157,8 @@ public: int delGeometriesExclusiveList(const std::vector& GeoIds); /// Does the same as \a delGeometry but allows one to delete several geometries in one step int delGeometries(const std::vector& GeoIds); + template + int delGeometries(InputIt first, InputIt last); /// deletes all the elements/constraints of the sketch except for external geometry int deleteAllGeometry(); /// deletes all the constraints of the sketch @@ -171,6 +173,10 @@ public: int addConstraint(std::unique_ptr constraint); /// delete constraint int delConstraint(int ConstrId); + /// Replaces geometries at `oldGeoIds` with `newGeos`, lower Ids first. + /// If `oldGeoIds` is bigger, deletes the remaining. + /// If `newGeos` is bigger, adds the remaining geometries at the end. + void replaceGeometries(std::vector oldGeoIds, std::vector& newGeos); /** deletes a group of constraints at once, if norecomputes is active, the default behaviour is * that it will solve the sketch. *