[Sketcher] Write SketchObject::replaceGeometries()
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -157,6 +157,8 @@ public:
|
||||
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
|
||||
@@ -171,6 +173,10 @@ public:
|
||||
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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user