Skip to content

Commit bb5afb9

Browse files
authored
Sketcher: Fix loss of expression on trim (FreeCAD#27505)
1 parent ec9ead0 commit bb5afb9

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

src/Mod/Sketcher/App/SketchObject.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3361,7 +3361,8 @@ void createNewConstraintsForTrim(
33613361
const std::vector<const Part::Geometry*> newGeos,
33623362
std::vector<int>& idsOfOldConstraints,
33633363
std::vector<Constraint*>& newConstraints,
3364-
std::set<int, std::greater<>>& geoIdsToBeDeleted
3364+
std::set<int, std::greater<>>& geoIdsToBeDeleted,
3365+
std::map<Constraint*, int>& newToOldConstraintMap
33653366
)
33663367
{
33673368
const auto& allConstraints = obj->Constraints.getValues();
@@ -3386,6 +3387,7 @@ void createNewConstraintsForTrim(
33863387
PointPos::end
33873388
)) {
33883389
newConstraints.push_back(newConstr.release());
3390+
newToOldConstraintMap[newConstraints.back()] = oldConstrId; // Map new to old
33893391
isPoint1ConstrainedOnGeoId1 = true;
33903392
continue;
33913393
}
@@ -3399,6 +3401,7 @@ void createNewConstraintsForTrim(
33993401
PointPos::start
34003402
)) {
34013403
newConstraints.push_back(newConstr.release());
3404+
newToOldConstraintMap[newConstraints.back()] = oldConstrId; // Map new to old
34023405
isPoint2ConstrainedOnGeoId2 = true;
34033406
continue;
34043407
}
@@ -3408,7 +3411,12 @@ void createNewConstraintsForTrim(
34083411
continue;
34093412
}
34103413
// constraint has not yet been changed
3414+
size_t sizeBefore = newConstraints.size();
34113415
obj->deriveConstraintsForPieces(GeoId, newIds, newGeos, con, newConstraints);
3416+
// Map all newly added derived constraints to the old ID
3417+
for (size_t i = sizeBefore; i < newConstraints.size(); ++i) {
3418+
newToOldConstraintMap[newConstraints[i]] = oldConstrId;
3419+
}
34123420
}
34133421

34143422
// Add point-on-object/coincidence constraints with the newly exposed points.
@@ -3518,6 +3526,7 @@ int SketchObject::trim(int GeoId, const Base::Vector3d& point)
35183526
std::vector<int> newIds;
35193527
std::vector<Part::Geometry*> newGeos;
35203528
std::vector<const Part::Geometry*> newGeosAsConsts;
3529+
std::map<Constraint*, int> newToOldConstraintMap;
35213530

35223531
switch (paramsOfNewGeos.size()) {
35233532
case 0: {
@@ -3580,7 +3589,8 @@ int SketchObject::trim(int GeoId, const Base::Vector3d& point)
35803589
newGeosAsConsts,
35813590
idsOfOldConstraints,
35823591
newConstraints,
3583-
geoIdsToBeDeleted
3592+
geoIdsToBeDeleted,
3593+
newToOldConstraintMap
35843594
);
35853595

35863596
//******************* Step D => Replacing geometries and constraints
@@ -3599,6 +3609,16 @@ int SketchObject::trim(int GeoId, const Base::Vector3d& point)
35993609
addConstraint(std::move(newConstr));
36003610
};
36013611

3612+
std::map<Constraint*, std::shared_ptr<App::Expression>> exprBackup;
3613+
for (auto const& [newConstr, oldId] : newToOldConstraintMap) {
3614+
if (oldId >= 0 && oldId < (int)allConstraints.size()) {
3615+
auto exprInfo = getExpression(Constraints.createPath(oldId));
3616+
if (exprInfo.expression) {
3617+
exprBackup[newConstr] = std::shared_ptr<App::Expression>(exprInfo.expression->copy());
3618+
}
3619+
}
3620+
}
3621+
36023622
delConstraints(std::move(idsOfOldConstraints), DeleteOption::NoFlag);
36033623

36043624
if (!isOriginalCurvePeriodic) {
@@ -3652,7 +3672,17 @@ int SketchObject::trim(int GeoId, const Base::Vector3d& point)
36523672
return constr->Type == ConstraintType::None;
36533673
});
36543674
delGeometries(geoIdsToBeDeleted.begin(), geoIdsToBeDeleted.end());
3655-
addConstraints(newConstraints);
3675+
3676+
int lastAddedIndex = addConstraints(newConstraints);
3677+
int firstAddedIndex = lastAddedIndex - (int)newConstraints.size() + 1;
3678+
3679+
// Restore expressions
3680+
for (int i = 0; i < (int)newConstraints.size(); ++firstAddedIndex, ++i) {
3681+
auto it = exprBackup.find(newConstraints[i]);
3682+
if (it != exprBackup.end()) {
3683+
setExpression(Constraints.createPath(firstAddedIndex), it->second);
3684+
}
3685+
}
36563686

36573687
if (noRecomputes) {
36583688
solve();

0 commit comments

Comments
 (0)