Skip to content

Commit

Permalink
[Sketcher] Write SketchObject::replaceGeometries()
Browse files Browse the repository at this point in the history
  • Loading branch information
AjinkyaDahale committed Jan 16, 2025
1 parent 5c52353 commit 2249c99
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/Mod/Sketcher/App/SketchObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1804,12 +1804,19 @@ int SketchObject::delGeometry(int GeoId, bool deleteinternalgeo)
}

int SketchObject::delGeometries(const std::vector<int>& GeoIds)
{
return delGeometries(GeoIds.begin(), GeoIds.end());
}

template <class InputIt>
int SketchObject::delGeometries(InputIt first, InputIt last)
{
std::vector<int> sGeoIds;
std::vector<int> 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);
}
Expand Down Expand Up @@ -1928,6 +1935,40 @@ int SketchObject::delGeometriesExclusiveList(const std::vector<int>& GeoIds)
return 0;
}

void SketchObject::replaceGeometries(std::vector<int> oldGeoIds,
std::vector<Part::Geometry*>& 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.
Expand Down
6 changes: 6 additions & 0 deletions src/Mod/Sketcher/App/SketchObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class SketcherExport SketchObject: public Part::Part2DObject
int delGeometriesExclusiveList(const std::vector<int>& GeoIds);
/// Does the same as \a delGeometry but allows one to delete several geometries in one step
int delGeometries(const std::vector<int>& GeoIds);
template<class InputIt>
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
Expand All @@ -171,6 +173,10 @@ class SketcherExport SketchObject: public Part::Part2DObject
int addConstraint(std::unique_ptr<Constraint> 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<int> oldGeoIds, std::vector<Part::Geometry*>& newGeos);
/** deletes a group of constraints at once, if norecomputes is active, the default behaviour is
* that it will solve the sketch.
*
Expand Down

0 comments on commit 2249c99

Please sign in to comment.