Sync multiconstraint with current state

This commit is contained in:
77maxikov 2022-12-30 17:49:33 +03:00 committed by Paul Kahler
parent 50cbecbe72
commit 3d3d5c789d

View File

@ -195,6 +195,8 @@ bool Constraint::ConstrainCurveCurveTangent(Constraint *c, Entity *eA, Entity *e
} }
void Constraint::MenuConstrain(Command id) { void Constraint::MenuConstrain(Command id) {
std::vector<Constraint> newcons;
Constraint c = {}; Constraint c = {};
c.group = SS.GW.activeGroup; c.group = SS.GW.activeGroup;
c.workplane = SS.GW.ActiveWorkplane(); c.workplane = SS.GW.ActiveWorkplane();
@ -262,7 +264,9 @@ void Constraint::MenuConstrain(Command id) {
c.valA = 0; c.valA = 0;
c.ModifyToSatisfy(); c.ModifyToSatisfy();
AddConstraint(&c); newcons.push_back(c);
for (auto&& nc:newcons)
AddConstraint(&nc);
break; break;
} }
@ -297,20 +301,25 @@ void Constraint::MenuConstrain(Command id) {
" * a point and a plane face (point on face)\n")); " * a point and a plane face (point on face)\n"));
return; return;
} }
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
break; break;
case Command::EQUAL: case Command::EQUAL:
if(gs.lineSegments == 2 && gs.n == 2) { if(gs.lineSegments >= 2 && gs.lineSegments == gs.n) {
c.type = Type::EQUAL_LENGTH_LINES; c.type = Type::EQUAL_LENGTH_LINES;
c.entityA = gs.entity[0]; c.entityA = gs.entity[0];
c.entityB = gs.entity[1]; for (std::vector<hEntity>::size_type k = 1;k < gs.entity.size(); ++k){
c.entityB = gs.entity[k];
newcons.push_back(c);
}
} else if(gs.lineSegments == 2 && gs.points == 2 && gs.n == 4) { } else if(gs.lineSegments == 2 && gs.points == 2 && gs.n == 4) {
c.type = Type::EQ_PT_LN_DISTANCES; c.type = Type::EQ_PT_LN_DISTANCES;
c.entityA = gs.entity[0]; c.entityA = gs.entity[0];
c.ptA = gs.point[0]; c.ptA = gs.point[0];
c.entityB = gs.entity[1]; c.entityB = gs.entity[1];
c.ptB = gs.point[1]; c.ptB = gs.point[1];
newcons.push_back(c);
} else if(gs.lineSegments == 1 && gs.points == 2 && gs.n == 3) { } else if(gs.lineSegments == 1 && gs.points == 2 && gs.n == 3) {
// The same line segment for the distances, but different // The same line segment for the distances, but different
// points. // points.
@ -319,27 +328,34 @@ void Constraint::MenuConstrain(Command id) {
c.ptA = gs.point[0]; c.ptA = gs.point[0];
c.entityB = gs.entity[0]; c.entityB = gs.entity[0];
c.ptB = gs.point[1]; c.ptB = gs.point[1];
newcons.push_back(c);
} else if(gs.lineSegments == 2 && gs.points == 1 && gs.n == 3) { } else if(gs.lineSegments == 2 && gs.points == 1 && gs.n == 3) {
c.type = Type::EQ_LEN_PT_LINE_D; c.type = Type::EQ_LEN_PT_LINE_D;
c.entityA = gs.entity[0]; c.entityA = gs.entity[0];
c.entityB = gs.entity[1]; c.entityB = gs.entity[1];
c.ptA = gs.point[0]; c.ptA = gs.point[0];
newcons.push_back(c);
} else if(gs.vectors == 4 && gs.n == 4) { } else if(gs.vectors == 4 && gs.n == 4) {
c.type = Type::EQUAL_ANGLE; c.type = Type::EQUAL_ANGLE;
c.entityA = gs.vector[0]; c.entityA = gs.vector[0];
c.entityB = gs.vector[1]; c.entityB = gs.vector[1];
c.entityC = gs.vector[2]; c.entityC = gs.vector[2];
c.entityD = gs.vector[3]; c.entityD = gs.vector[3];
newcons.push_back(c);
} else if(gs.vectors == 3 && gs.n == 3) { } else if(gs.vectors == 3 && gs.n == 3) {
c.type = Type::EQUAL_ANGLE; c.type = Type::EQUAL_ANGLE;
c.entityA = gs.vector[0]; c.entityA = gs.vector[0];
c.entityB = gs.vector[1]; c.entityB = gs.vector[1];
c.entityC = gs.vector[1]; c.entityC = gs.vector[1];
c.entityD = gs.vector[2]; c.entityD = gs.vector[2];
} else if(gs.circlesOrArcs == 2 && gs.n == 2) { newcons.push_back(c);
} else if(gs.circlesOrArcs >= 2 && gs.circlesOrArcs == gs.n) {
c.type = Type::EQUAL_RADIUS; c.type = Type::EQUAL_RADIUS;
c.entityA = gs.entity[0]; c.entityA = gs.entity[0];
c.entityB = gs.entity[1]; for (std::vector<hEntity>::size_type k = 1;k < gs.entity.size(); ++k){
c.entityB = gs.entity[k];
newcons.push_back(c);
}
} else if(gs.arcs == 1 && gs.lineSegments == 1 && gs.n == 2) { } else if(gs.arcs == 1 && gs.lineSegments == 1 && gs.n == 2) {
c.type = Type::EQUAL_LINE_ARC_LEN; c.type = Type::EQUAL_LINE_ARC_LEN;
if(SK.GetEntity(gs.entity[0])->type == Entity::Type::ARC_OF_CIRCLE) { if(SK.GetEntity(gs.entity[0])->type == Entity::Type::ARC_OF_CIRCLE) {
@ -349,6 +365,7 @@ void Constraint::MenuConstrain(Command id) {
c.entityA = gs.entity[0]; c.entityA = gs.entity[0];
c.entityB = gs.entity[1]; c.entityB = gs.entity[1];
} }
newcons.push_back(c);
} else { } else {
Error(_("Bad selection for equal length / radius constraint. " Error(_("Bad selection for equal length / radius constraint. "
"This constraint can apply to:\n\n" "This constraint can apply to:\n\n"
@ -368,19 +385,21 @@ void Constraint::MenuConstrain(Command id) {
"(line segment length equals arc length)\n")); "(line segment length equals arc length)\n"));
return; return;
} }
if(c.type == Type::EQUAL_ANGLE) { for (auto&& nc : newcons){
// Infer the nearest supplementary angle from the sketch. if(nc.type == Type::EQUAL_ANGLE) {
Vector a1 = SK.GetEntity(c.entityA)->VectorGetNum(), // Infer the nearest supplementary angle from the sketch.
b1 = SK.GetEntity(c.entityB)->VectorGetNum(), Vector a1 = SK.GetEntity(c.entityA)->VectorGetNum(),
a2 = SK.GetEntity(c.entityC)->VectorGetNum(), b1 = SK.GetEntity(c.entityB)->VectorGetNum(),
b2 = SK.GetEntity(c.entityD)->VectorGetNum(); a2 = SK.GetEntity(c.entityC)->VectorGetNum(),
double d1 = a1.Dot(b1), d2 = a2.Dot(b2); b2 = SK.GetEntity(c.entityD)->VectorGetNum();
double d1 = a1.Dot(b1), d2 = a2.Dot(b2);
if(d1*d2 < 0) { if(d1*d2 < 0) {
c.other = true; nc.other = true;
}
} }
AddConstraint(&nc);
} }
AddConstraint(&c);
break; break;
case Command::RATIO: case Command::RATIO:
@ -414,6 +433,7 @@ void Constraint::MenuConstrain(Command id) {
c.valA = 0; c.valA = 0;
c.ModifyToSatisfy(); c.ModifyToSatisfy();
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
break; break;
@ -448,6 +468,7 @@ void Constraint::MenuConstrain(Command id) {
c.valA = 0; c.valA = 0;
c.ModifyToSatisfy(); c.ModifyToSatisfy();
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
break; break;
@ -461,6 +482,7 @@ void Constraint::MenuConstrain(Command id) {
// it on-line; so auto-remove that. Handle as one undo group. // it on-line; so auto-remove that. Handle as one undo group.
SS.UndoRemember(); SS.UndoRemember();
DeleteAllConstraintsFor(Type::PT_ON_LINE, c.entityA, c.ptA); DeleteAllConstraintsFor(Type::PT_ON_LINE, c.entityA, c.ptA);
newcons.push_back(c);
AddConstraint(&c, /*rememberForUndo=*/false); AddConstraint(&c, /*rememberForUndo=*/false);
break; break;
} else if(gs.lineSegments == 1 && gs.workplanes == 1 && gs.n == 2) { } else if(gs.lineSegments == 1 && gs.workplanes == 1 && gs.n == 2) {
@ -468,6 +490,8 @@ void Constraint::MenuConstrain(Command id) {
int i = SK.GetEntity(gs.entity[0])->IsWorkplane() ? 1 : 0; int i = SK.GetEntity(gs.entity[0])->IsWorkplane() ? 1 : 0;
c.entityA = gs.entity[i]; c.entityA = gs.entity[i];
c.entityB = gs.entity[1-i]; c.entityB = gs.entity[1-i];
newcons.push_back(c);
AddConstraint(&c);
} else { } else {
Error(_("Bad selection for at midpoint constraint. This " Error(_("Bad selection for at midpoint constraint. This "
"constraint can apply to:\n\n" "constraint can apply to:\n\n"
@ -477,7 +501,7 @@ void Constraint::MenuConstrain(Command id) {
"(line's midpoint on plane)\n")); "(line's midpoint on plane)\n"));
return; return;
} }
AddConstraint(&c);
break; break;
case Command::SYMMETRIC: case Command::SYMMETRIC:
@ -565,10 +589,12 @@ void Constraint::MenuConstrain(Command id) {
Entity::NO_ENTITY); Entity::NO_ENTITY);
DeleteAllConstraintsFor(Type::VERTICAL, (gs.entity[0]), DeleteAllConstraintsFor(Type::VERTICAL, (gs.entity[0]),
Entity::NO_ENTITY); Entity::NO_ENTITY);
newcons.push_back(c);
AddConstraint(&c, /*rememberForUndo=*/false); AddConstraint(&c, /*rememberForUndo=*/false);
break; break;
} }
} }
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
break; break;
@ -580,8 +606,16 @@ void Constraint::MenuConstrain(Command id) {
"applying a horizontal or vertical constraint.")); "applying a horizontal or vertical constraint."));
return; return;
} }
if(gs.lineSegments == 1 && gs.n == 1) { if(gs.lineSegments > 0 && gs.lineSegments == gs.n) {
c.entityA = gs.entity[0]; for (auto enti : gs.entity){
c.entityA = enti;
if(id == Command::HORIZONTAL) {
c.type = Type::HORIZONTAL;
} else {
c.type = Type::VERTICAL;
}
newcons.push_back(c);
}
Entity *e = SK.GetEntity(c.entityA); Entity *e = SK.GetEntity(c.entityA);
ha = e->point[0]; ha = e->point[0];
hb = e->point[1]; hb = e->point[1];
@ -595,12 +629,8 @@ void Constraint::MenuConstrain(Command id) {
" * a line segment\n")); " * a line segment\n"));
return; return;
} }
if(id == Command::HORIZONTAL) { for (auto && nc: newcons)
c.type = Type::HORIZONTAL; AddConstraint(&nc);
} else {
c.type = Type::VERTICAL;
}
AddConstraint(&c);
break; break;
} }
@ -640,6 +670,7 @@ void Constraint::MenuConstrain(Command id) {
nfree->NormalForceTo(Quaternion::From(fu, fv)); nfree->NormalForceTo(Quaternion::From(fu, fv));
} }
newcons.push_back(c);
AddConstraint(&c, /*rememberForUndo=*/false); AddConstraint(&c, /*rememberForUndo=*/false);
break; break;
} }
@ -716,6 +747,7 @@ void Constraint::MenuConstrain(Command id) {
} }
c.ModifyToSatisfy(); c.ModifyToSatisfy();
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
break; break;
} }
@ -725,10 +757,13 @@ void Constraint::MenuConstrain(Command id) {
c.type = Type::PARALLEL; c.type = Type::PARALLEL;
c.entityA = gs.face[0]; c.entityA = gs.face[0];
c.entityB = gs.face[1]; c.entityB = gs.face[1];
} else if(gs.vectors == 2 && gs.n == 2) { } else if(gs.vectors > 1 && gs.vectors == gs.n) {
c.type = Type::PARALLEL; c.type = Type::PARALLEL;
c.entityA = gs.vector[0]; c.entityA = gs.vector[0];
c.entityB = gs.vector[1]; for (std::vector<hEntity>::size_type k = 1; k < gs.vector.size();++k ){
c.entityB = gs.vector[k];
newcons.push_back(c);
}
} else if(gs.lineSegments == 1 && gs.arcs == 1 && gs.n == 2) { } else if(gs.lineSegments == 1 && gs.arcs == 1 && gs.n == 2) {
Entity *line = SK.GetEntity(gs.entity[0]), Entity *line = SK.GetEntity(gs.entity[0]),
*arc = SK.GetEntity(gs.entity[1]); *arc = SK.GetEntity(gs.entity[1]);
@ -741,6 +776,7 @@ void Constraint::MenuConstrain(Command id) {
c.type = Type::ARC_LINE_TANGENT; c.type = Type::ARC_LINE_TANGENT;
c.entityA = arc->h; c.entityA = arc->h;
c.entityB = line->h; c.entityB = line->h;
newcons.push_back(c);
} else if(gs.lineSegments == 1 && gs.cubics == 1 && gs.n == 2) { } else if(gs.lineSegments == 1 && gs.cubics == 1 && gs.n == 2) {
Entity *line = SK.GetEntity(gs.entity[0]), Entity *line = SK.GetEntity(gs.entity[0]),
*cubic = SK.GetEntity(gs.entity[1]); *cubic = SK.GetEntity(gs.entity[1]);
@ -753,6 +789,7 @@ void Constraint::MenuConstrain(Command id) {
c.type = Type::CUBIC_LINE_TANGENT; c.type = Type::CUBIC_LINE_TANGENT;
c.entityA = cubic->h; c.entityA = cubic->h;
c.entityB = line->h; c.entityB = line->h;
newcons.push_back(c);
} else if(gs.cubics + gs.arcs == 2 && gs.n == 2) { } else if(gs.cubics + gs.arcs == 2 && gs.n == 2) {
if(!SS.GW.LockedInWorkplane()) { if(!SS.GW.LockedInWorkplane()) {
Error(_("Curve-curve tangency must apply in workplane.")); Error(_("Curve-curve tangency must apply in workplane."));
@ -766,6 +803,7 @@ void Constraint::MenuConstrain(Command id) {
c.type = Type::CURVE_CURVE_TANGENT; c.type = Type::CURVE_CURVE_TANGENT;
c.entityA = eA->h; c.entityA = eA->h;
c.entityB = eB->h; c.entityB = eB->h;
newcons.push_back(c);
} else { } else {
Error(_("Bad selection for parallel / tangent constraint. This " Error(_("Bad selection for parallel / tangent constraint. This "
"constraint can apply to:\n\n" "constraint can apply to:\n\n"
@ -777,7 +815,8 @@ void Constraint::MenuConstrain(Command id) {
"an endpoint (tangent)\n")); "an endpoint (tangent)\n"));
return; return;
} }
AddConstraint(&c); for (auto&& nc:newcons)
AddConstraint(&nc);
break; break;
case Command::PERPENDICULAR: case Command::PERPENDICULAR:
@ -798,6 +837,7 @@ void Constraint::MenuConstrain(Command id) {
" * two normals\n")); " * two normals\n"));
return; return;
} }
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
break; break;
@ -811,6 +851,7 @@ void Constraint::MenuConstrain(Command id) {
" * a point\n")); " * a point\n"));
return; return;
} }
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
break; break;
@ -821,6 +862,7 @@ void Constraint::MenuConstrain(Command id) {
c.group = SS.GW.activeGroup; c.group = SS.GW.activeGroup;
c.workplane = SS.GW.ActiveWorkplane(); c.workplane = SS.GW.ActiveWorkplane();
c.comment = _("NEW COMMENT -- DOUBLE-CLICK TO EDIT"); c.comment = _("NEW COMMENT -- DOUBLE-CLICK TO EDIT");
newcons.push_back(c);
AddConstraint(&c); AddConstraint(&c);
} else { } else {
SS.GW.pending.operation = GraphicsWindow::Pending::COMMAND; SS.GW.pending.operation = GraphicsWindow::Pending::COMMAND;
@ -832,31 +874,32 @@ void Constraint::MenuConstrain(Command id) {
default: ssassert(false, "Unexpected menu ID"); default: ssassert(false, "Unexpected menu ID");
} }
for (auto nc:newcons){
for(const Constraint &cc : SK.constraint) { for(const Constraint &cc : SK.constraint) {
if(c.h != cc.h && c.Equals(cc)) { if(nc.h != cc.h && nc.Equals(cc)) {
// Oops, we already have this exact constraint. Remove the one we just added. // Oops, we already have this exact constraint. Remove the one we just added.
SK.constraint.RemoveById(c.h); SK.constraint.RemoveById(nc.h);
SS.GW.ClearSelection(); SS.GW.ClearSelection();
// And now select the old one, to give feedback. // And now select the old one, to give feedback.
SS.GW.MakeSelected(cc.h); SS.GW.MakeSelected(cc.h);
return; return;
}
} }
}
if(SK.constraint.FindByIdNoOops(c.h)) { if(SK.constraint.FindByIdNoOops(nc.h)) {
Constraint *constraint = SK.GetConstraint(c.h); Constraint *constraint = SK.GetConstraint(nc.h);
if(SS.TestRankForGroup(c.group) == SolveResult::REDUNDANT_OKAY && if(SS.TestRankForGroup(nc.group) == SolveResult::REDUNDANT_OKAY &&
!SK.GetGroup(SS.GW.activeGroup)->allowRedundant && !SK.GetGroup(SS.GW.activeGroup)->allowRedundant &&
constraint->HasLabel()) { constraint->HasLabel()) {
constraint->reference = true; constraint->reference = true;
}
} }
}
if((id == Command::DISTANCE_DIA || id == Command::ANGLE || if((id == Command::DISTANCE_DIA || id == Command::ANGLE ||
id == Command::RATIO || id == Command::DIFFERENCE) && id == Command::RATIO || id == Command::DIFFERENCE) &&
SS.immediatelyEditDimension) { SS.immediatelyEditDimension) {
SS.GW.EditConstraint(c.h); SS.GW.EditConstraint(nc.h);
}
} }
SS.GW.ClearSelection(); SS.GW.ClearSelection();