Skip to content

Commit

Permalink
[Sketcher] Fix 'Reference' checkbox not working for radius/diameter ; f…
Browse files Browse the repository at this point in the history
…ixes #4627

As a consequence :
Remove message box when several arcs/circles are selected ...
... and enforce creation of equality constraints in this case
Former existing option of adding a dimensional constraint ...
... to each circle encouraged improper constraining
  • Loading branch information
0penBrain committed Apr 19, 2021
1 parent fc6d129 commit 7a7cfcd
Showing 1 changed file with 13 additions and 177 deletions.
190 changes: 13 additions & 177 deletions src/Mod/Sketcher/Gui/CommandConstraints.cpp
Expand Up @@ -4982,6 +4982,7 @@ void CmdSketcherConstrainRadius::activated(int iMsg)
if (geoIdRadiusMap.empty() && externalGeoIdRadiusMap.empty()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select one or more arcs or circles from the sketch."));
return;
}

if(poles && nonpoles) {
Expand Down Expand Up @@ -5040,42 +5041,27 @@ void CmdSketcherConstrainRadius::activated(int iMsg)

if(!geoIdRadiusMap.empty())
{
bool constrainEqual = false;
if (geoIdRadiusMap.size() > 1 && constraintCreationMode==Driving) {
int ret = QMessageBox::question(Gui::getMainWindow(), QObject::tr("Constrain equal"),
QObject::tr("Do you want to share the same radius for all selected elements?"),
QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel);
// use an equality constraint
if (ret == QMessageBox::Yes) {
constrainEqual = true;
}
else if (ret == QMessageBox::Cancel) {
// do nothing
return;
}
}

if (constrainEqual) {
// Create the one radius constraint now
int refGeoId = geoIdRadiusMap.front().first;
double radius = geoIdRadiusMap.front().second;

if(!commandopened)
openCommand(QT_TRANSLATE_NOOP("Command", "Add radius constraint"));

// Add the equality constraints
for (std::vector< std::pair<int, double> >::iterator it = geoIdRadiusMap.begin()+1; it != geoIdRadiusMap.end(); ++it) {
Gui::cmdAppObjectArgs(selection[0].getObject(),
"addConstraint(Sketcher.Constraint('Equal',%d,%d)) ",
refGeoId,it->first);
}

if(nonpoles)
Gui::cmdAppObjectArgs(selection[0].getObject(), "addConstraint(Sketcher.Constraint('Radius',%d,%f)) ",
refGeoId,radius);
else
Gui::cmdAppObjectArgs(selection[0].getObject(), "addConstraint(Sketcher.Constraint('Weight',%d,%f)) ",
refGeoId,radius);

// Add the equality constraints
for (std::vector< std::pair<int, double> >::iterator it = geoIdRadiusMap.begin()+1; it != geoIdRadiusMap.end(); ++it) {
Gui::cmdAppObjectArgs(selection[0].getObject(),
"addConstraint(Sketcher.Constraint('Equal',%d,%d)) ",
refGeoId,it->first);
}
}
else {
// Create the radius constraints now
Expand Down Expand Up @@ -5113,88 +5099,8 @@ void CmdSketcherConstrainRadius::activated(int iMsg)
vp->draw(false,false); // Redraw
}

ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher");
bool show = hGrp->GetBool("ShowDialogOnDistanceConstraint", true);
// Ask for the value of the radius immediately
if (show && constraintCreationMode==Driving) {
QDialog dlg(Gui::getMainWindow());
Ui::InsertDatum ui_Datum;
ui_Datum.setupUi(&dlg);
Base::Quantity init_val;
init_val.setValue(geoIdRadiusMap.front().second);

if(poles) {
dlg.setWindowTitle(EditDatumDialog::tr("Change weight"));
ui_Datum.label->setText(EditDatumDialog::tr("Weight:"));

}
else{
dlg.setWindowTitle(EditDatumDialog::tr("Change radius"));
ui_Datum.label->setText(EditDatumDialog::tr("Radius:"));
init_val.setUnit(Base::Unit::Length);
}

ui_Datum.labelEdit->setValue(init_val);
ui_Datum.labelEdit->selectNumber();
if (constrainEqual || geoIdRadiusMap.size() == 1)
ui_Datum.labelEdit->bind(Obj->Constraints.createPath(indexConstr));
else
ui_Datum.name->setDisabled(true);

if (dlg.exec() == QDialog::Accepted) {
Base::Quantity newQuant = ui_Datum.labelEdit->value();
double newRadius = newQuant.getValue();

try {
if (constrainEqual || geoIdRadiusMap.size() == 1) {
Gui::cmdAppObjectArgs(Obj, "setDatum(%i,App.Units.Quantity('%f %s'))",
indexConstr, newRadius, (const char*)newQuant.getUnit().getString().toUtf8());

QString constraintName = ui_Datum.name->text().trimmed();
if (Base::Tools::toStdString(constraintName) != Obj->Constraints[indexConstr]->Name) {
std::string escapedstr = Base::Tools::escapedUnicodeFromUtf8(constraintName.toUtf8().constData());
Gui::cmdAppObjectArgs(Obj, "renameConstraint(%d, u'%s')",
indexConstr, escapedstr.c_str());
}
}
else {
for (std::size_t i=0; i<geoIdRadiusMap.size();i++) {
Gui::cmdAppObjectArgs(Obj, "setDatum(%i,App.Units.Quantity('%f %s'))",
indexConstr+i, newRadius, (const char*)newQuant.getUnit().getString().toUtf8());
}
}

commitCommand();

if (Obj->noRecomputes && Obj->ExpressionEngine.depsAreTouched()) {
Obj->ExpressionEngine.execute();
Obj->solve();
}

tryAutoRecompute(Obj);

commitNeeded=false;
updateNeeded=false;
}
catch (const Base::Exception& e) {
QMessageBox::critical(qApp->activeWindow(), QObject::tr("Dimensional constraint"), QString::fromUtf8(e.what()));
abortCommand();

tryAutoRecomputeIfNotSolve(Obj); // we have to update the solver after this aborted addition.
}
}
else {
// command canceled
abortCommand();

updateNeeded=true;
}
}
else {
// now dialog was shown so commit the command
commitCommand();
commitNeeded=false;
}
finishDistanceConstraint(this, Obj, constraintCreationMode==Driving);

//updateActive();
getSelection().clearSelection();
}
Expand Down Expand Up @@ -5251,7 +5157,6 @@ void CmdSketcherConstrainRadius::applyConstraint(std::vector<SelIdPair> &selSeq,

const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

int indexConstr = ConStr.size() - 1;
bool fixed = isPointOrSegmentFixed(Obj,GeoId);
if(fixed || constraintCreationMode==Reference) {
Gui::cmdAppObjectArgs(Obj, "setDriving(%i,%s)",
Expand All @@ -5272,78 +5177,9 @@ void CmdSketcherConstrainRadius::applyConstraint(std::vector<SelIdPair> &selSeq,
vp->draw(); // Redraw
}

ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher");
bool show = hGrp->GetBool("ShowDialogOnDistanceConstraint", true);
// Ask for the value of the radius immediately
if (show && constraintCreationMode==Driving && !fixed) {
QDialog dlg(Gui::getMainWindow());
Ui::InsertDatum ui_Datum;
ui_Datum.setupUi(&dlg);
Base::Quantity init_val;

if(ispole) {
dlg.setWindowTitle(EditDatumDialog::tr("Change weight"));
ui_Datum.label->setText(EditDatumDialog::tr("Weight:"));

}
else{
dlg.setWindowTitle(EditDatumDialog::tr("Change radius"));
ui_Datum.label->setText(EditDatumDialog::tr("Radius:"));
init_val.setUnit(Base::Unit::Length);
}

init_val.setValue(radius);

ui_Datum.labelEdit->setValue(init_val);
ui_Datum.labelEdit->selectNumber();
ui_Datum.labelEdit->bind(Obj->Constraints.createPath(indexConstr));

if (dlg.exec() == QDialog::Accepted) {
Base::Quantity newQuant = ui_Datum.labelEdit->value();
double newRadius = newQuant.getValue();

try {
Gui::cmdAppObjectArgs(Obj, "setDatum(%i,App.Units.Quantity('%f %s'))",
indexConstr, newRadius, (const char*)newQuant.getUnit().getString().toUtf8());

QString constraintName = ui_Datum.name->text().trimmed();
if (Base::Tools::toStdString(constraintName) != Obj->Constraints[indexConstr]->Name) {
std::string escapedstr = Base::Tools::escapedUnicodeFromUtf8(constraintName.toUtf8().constData());
Gui::cmdAppObjectArgs(Obj, "renameConstraint(%d, u'%s')",
indexConstr, escapedstr.c_str());
}

commitCommand();

if (Obj->noRecomputes && Obj->ExpressionEngine.depsAreTouched()) {
Obj->ExpressionEngine.execute();
Obj->solve();
}

tryAutoRecompute(Obj);

commitNeeded=false;
updateNeeded=false;
}
catch (const Base::Exception& e) {
QMessageBox::critical(qApp->activeWindow(), QObject::tr("Dimensional constraint"), QString::fromUtf8(e.what()));
abortCommand();

tryAutoRecomputeIfNotSolve(Obj); // we have to update the solver after this aborted addition.
}
}
else {
// command canceled
abortCommand();

updateNeeded=true;
}
}
else {
// now dialog was shown so commit the command
commitCommand();
commitNeeded=false;
}
if(!fixed)
finishDistanceConstraint(this, Obj, constraintCreationMode==Driving);

//updateActive();
getSelection().clearSelection();

Expand Down

0 comments on commit 7a7cfcd

Please sign in to comment.