diff --git a/CHANGELOG.md b/CHANGELOG.md index b079743ec..db43f2a1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ New constraint features: in the text window. * Distance constraint labels can now be formatted to use SI prefixes. Values are edited in the configured unit regardless of label format. + * When creating a constraint, if an exactly identical constraint already + exists, it is now selected instead of adding a redundant constraint. * It is now possible to turn off automatic creation of horizontal/vertical constraints on line segments. * Automatic creation of constraints no longer happens if the constraint diff --git a/src/constraint.cpp b/src/constraint.cpp index 5580f3578..083a48f72 100644 --- a/src/constraint.cpp +++ b/src/constraint.cpp @@ -745,6 +745,17 @@ void Constraint::MenuConstrain(Command id) { default: ssassert(false, "Unexpected menu ID"); } + for(const Constraint &cc : SK.constraint) { + if(c.h.v != cc.h.v && c.Equals(cc)) { + // Oops, we already have this exact constraint. Remove the one we just added. + SK.constraint.RemoveById(c.h); + SS.GW.ClearSelection(); + // And now select the old one, to give feedback. + SS.GW.MakeSelected(cc.h); + return; + } + } + if(SK.constraint.FindByIdNoOops(c.h)) { Constraint *constraint = SK.GetConstraint(c.h); if(SS.TestRankForGroup(c.group) == SolveResult::REDUNDANT_OKAY && @@ -755,7 +766,6 @@ void Constraint::MenuConstrain(Command id) { } SS.GW.ClearSelection(); - SS.GW.Invalidate(); } #endif /* ! LIBRARY */ diff --git a/src/sketch.h b/src/sketch.h index 49db229e5..cee3cd0c7 100644 --- a/src/sketch.h +++ b/src/sketch.h @@ -655,6 +655,15 @@ class ConstraintBase { bool reference; // a ref dimension, that generates no eqs std::string comment; // since comments are represented as constraints + bool Equals(const ConstraintBase &c) const { + return type == c.type && group.v == c.group.v && workplane.v == c.workplane.v && + valA == c.valA && valP.v == c.valP.v && ptA.v == c.ptA.v && ptB.v == c.ptB.v && + entityA.v == c.entityA.v && entityB.v == c.entityB.v && + entityC.v == c.entityC.v && entityD.v == c.entityD.v && + other == c.other && other2 == c.other2 && reference == c.reference && + comment == c.comment; + } + bool HasLabel() const; void Generate(IdList *param);