Skip to content

Commit

Permalink
Sketcher New Feature: Toggle Mode for Reference/Driving constraints
Browse files Browse the repository at this point in the history
===================================================================

It allows to create constraints directly into Reference or Driving mode.

It does not include icons
  • Loading branch information
abdullahtahiriyo authored and wwmayer committed May 30, 2015
1 parent d5ec887 commit 765b720
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 20 deletions.
140 changes: 122 additions & 18 deletions src/Mod/Sketcher/Gui/CommandConstraints.cpp
Expand Up @@ -36,6 +36,8 @@
#include <Gui/Command.h>
#include <Gui/MainWindow.h>
#include <Gui/DlgEditFileIncludeProptertyExternal.h>
#include <Gui/Action.h>
#include <Gui/BitmapFactory.h>

#include <Mod/Part/App/Geometry.h>
#include <Mod/Sketcher/App/SketchObject.h>
Expand All @@ -50,6 +52,17 @@ using namespace std;
using namespace SketcherGui;
using namespace Sketcher;

/***** Creation Mode ************/
namespace SketcherGui
{
enum ConstraintCreationMode {
Driving,
Reference
};
}

ConstraintCreationMode constraintCreationMode=Driving;

bool isCreateConstraintActive(Gui::Document *doc)
{
if (doc)
Expand Down Expand Up @@ -729,7 +742,7 @@ void CmdSketcherConstrainLock::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceY',%d,%d,%f)) ",
selection[0].getFeatName(),GeoId,PosId,pnt.y);

if (GeoId < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -932,7 +945,7 @@ void CmdSketcherConstrainDistance::activated(int iMsg)
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,(pnt2-pnt1).Length());
}

if (bothexternal) { // it is a constraint on a external line, make it non-driving
if (bothexternal || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -964,7 +977,7 @@ void CmdSketcherConstrainDistance::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Distance',%d,%d,%d,%f)) ",
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,ActDist);

if (bothexternal) { // it is a constraint on a external line, make it non-driving
if (bothexternal || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -995,7 +1008,7 @@ void CmdSketcherConstrainDistance::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Distance',%d,%f)) ",
selection[0].getFeatName(),GeoId1,ActLength);

if (GeoId1 < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -1175,7 +1188,7 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceX',%d,%d,%d,%d,%f)) ",
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,ActLength);

if (bothexternal) { // it is a constraint on a external line, make it non-driving
if (bothexternal || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -1206,7 +1219,7 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceX',%d,%f)) ",
selection[0].getFeatName(),GeoId1,ActLength);

if (GeoId1 < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -1235,7 +1248,7 @@ void CmdSketcherConstrainDistanceX::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceX',%d,%d,%f)) ",
selection[0].getFeatName(),GeoId1,PosId1,ActX);

if (GeoId1 < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -1326,7 +1339,7 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceY',%d,%d,%d,%d,%f)) ",
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,ActLength);

if (bothexternal) { // it is a constraint on a external line, make it non-driving
if (bothexternal || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -1357,7 +1370,7 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceY',%d,%f)) ",
selection[0].getFeatName(),GeoId1,ActLength);

if (GeoId1 < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -1386,7 +1399,7 @@ void CmdSketcherConstrainDistanceY::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('DistanceY',%d,%d,%f)) ",
selection[0].getFeatName(),GeoId1,PosId1,ActY);

if (GeoId1 < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -2135,7 +2148,7 @@ void CmdSketcherConstrainRadius::activated(int iMsg)
if(!geoIdRadiusMap.empty())
{
bool constrainEqual = false;
if (geoIdRadiusMap.size() > 1) {
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);
Expand All @@ -2160,7 +2173,7 @@ void CmdSketcherConstrainRadius::activated(int iMsg)
Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Radius',%d,%f)) ",
selection[0].getFeatName(),refGeoId,radius);

// Add the equality constraints
for (std::vector< std::pair<int, double> >::iterator it = geoIdRadiusMap.begin()+1; it != geoIdRadiusMap.end(); ++it) {
Gui::Command::doCommand(
Expand All @@ -2176,6 +2189,16 @@ void CmdSketcherConstrainRadius::activated(int iMsg)
Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Radius',%d,%f)) ",
selection[0].getFeatName(),it->first,it->second);

if(constraintCreationMode==Reference) {

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

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
selection[0].getFeatName(),ConStr.size()-1,"False");

}

}
}

Expand All @@ -2199,7 +2222,7 @@ void CmdSketcherConstrainRadius::activated(int iMsg)
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) {
if (show && constraintCreationMode==Driving) {
QDialog dlg(Gui::getMainWindow());
Ui::InsertDatum ui_Datum;
ui_Datum.setupUi(&dlg);
Expand Down Expand Up @@ -2360,7 +2383,7 @@ void CmdSketcherConstrainAngle::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('AngleViaPoint',%d,%d,%d,%d,%f)) ",
selection[0].getFeatName(),GeoId1,GeoId2,GeoId3,PosId3,ActAngle);

if (bothexternal) { // it is a constraint on a external line, make it non-driving
if (bothexternal || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -2438,7 +2461,7 @@ void CmdSketcherConstrainAngle::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Angle',%d,%d,%d,%d,%f)) ",
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,ActAngle);

if (bothexternal) { // it is a constraint on a external line, make it non-driving
if (bothexternal || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -2469,7 +2492,7 @@ void CmdSketcherConstrainAngle::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Angle',%d,%f)) ",
selection[0].getFeatName(),GeoId1,ActAngle);

if (GeoId1 < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand All @@ -2493,7 +2516,7 @@ void CmdSketcherConstrainAngle::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Angle',%d,%f)) ",
selection[0].getFeatName(),GeoId1,angle);

if (GeoId1 < -2) { // it is a constraint on a external line, make it non-driving
if (GeoId1 < -2 || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -2902,7 +2925,7 @@ void CmdSketcherConstrainSnellsLaw::activated(int iMsg)
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('SnellsLaw',%d,%d,%d,%d,%d,%.12f)) ",
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2,GeoId3,n2divn1);

if (allexternal) { // it is a constraint on a external line, make it non-driving
if (allexternal || constraintCreationMode==Reference) { // it is a constraint on a external line, make it non-driving
const std::vector<Sketcher::Constraint *> &ConStr = Obj->Constraints.getValues();

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.setDriving(%i,%s)",
Expand Down Expand Up @@ -3334,6 +3357,86 @@ bool CmdSketcherConstrainInternalAlignment::isActive(void)
return isCreateConstraintActive( getActiveGuiDocument() );
}

/*** Creation Mode ***/
DEF_STD_CMD_A(CmdSketcherConstraintCreationMode);

CmdSketcherConstraintCreationMode::CmdSketcherConstraintCreationMode()
: Command("Sketcher_ConstraintCreationMode")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Toggle driving/reference constraint mode");
sToolTipText = QT_TR_NOOP("Toggle between inserting driving or reference constraints");
sWhatsThis = "Sketcher_ConstraintCreationMode";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_Toggle_Constraint_Driving";
sAccel = "";
eType = ForEdit;
}

void CmdSketcherConstraintCreationMode::activated(int iMsg)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();

if(constraintCreationMode==Driving) {
constraintCreationMode=Reference;

rcCmdMgr.getCommandByName("Sketcher_ConstraintCreationMode")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Sketcher_Toggle_Constraint_Driven"));

rcCmdMgr.getCommandByName("Sketcher_ConstrainLock")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Sketcher_ConstrainLock_Driven"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainDistance")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_Length_Driven"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainDistanceX")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_HorizontalDistance_Driven"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainDistanceY")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_VerticalDistance_Driven"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainRadius")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_Radius_Driven"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainAngle")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_InternalAngle_Driven"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainSnellsLaw")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_SnellsLaw_Driven"));
}
else {
constraintCreationMode=Driving;

rcCmdMgr.getCommandByName("Sketcher_ConstraintCreationMode")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Sketcher_Toggle_Constraint_Driving"));

rcCmdMgr.getCommandByName("Sketcher_ConstrainLock")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Sketcher_ConstrainLock"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainDistance")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_Length"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainDistanceX")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_HorizontalDistance"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainDistanceY")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_VerticalDistance"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainRadius")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_Radius"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainAngle")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_InternalAngle"));
rcCmdMgr.getCommandByName("Sketcher_ConstrainSnellsLaw")->getAction()->setIcon(
Gui::BitmapFactory().pixmap("Constraint_SnellsLaw"));
}
}

bool CmdSketcherConstraintCreationMode::isActive(void)
{
Gui::Document * doc=getActiveGuiDocument();
if (doc) {
// checks if a Sketch Viewprovider is in Edit and is in no special mode
if (doc->getInEdit() && doc->getInEdit()->isDerivedFrom
(SketcherGui::ViewProviderSketch::getClassTypeId())) {
if (dynamic_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit())->
getSketchMode() == ViewProviderSketch::STATUS_NONE)
return true;
}
}
return false;
}

void CreateSketcherCommandsConstraints(void)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
Expand All @@ -3355,5 +3458,6 @@ void CreateSketcherCommandsConstraints(void)
rcCmdMgr.addCommand(new CmdSketcherConstrainSymmetric());
rcCmdMgr.addCommand(new CmdSketcherConstrainSnellsLaw());
rcCmdMgr.addCommand(new CmdSketcherConstrainInternalAlignment());
rcCmdMgr.addCommand(new CmdSketcherConstraintCreationMode());

}
8 changes: 6 additions & 2 deletions src/Mod/Sketcher/Gui/Workbench.cpp
Expand Up @@ -204,7 +204,9 @@ inline void SketcherAddWorkbenchConstraints<Gui::MenuItem>(Gui::MenuItem& cons){
<< "Sketcher_ConstrainRadius"
<< "Sketcher_ConstrainAngle"
<< "Sketcher_ConstrainSnellsLaw"
<< "Sketcher_ConstrainInternalAlignment";
<< "Sketcher_ConstrainInternalAlignment"
<< "Separator"
<< "Sketcher_ConstraintCreationMode";
}

template <>
Expand All @@ -225,7 +227,9 @@ inline void SketcherAddWorkbenchConstraints<Gui::ToolBarItem>(Gui::ToolBarItem&
<< "Sketcher_ConstrainDistance"
<< "Sketcher_ConstrainRadius"
<< "Sketcher_ConstrainAngle"
<< "Sketcher_ConstrainSnellsLaw";
<< "Sketcher_ConstrainSnellsLaw"
<< "Separator"
<< "Sketcher_ConstraintCreationMode";
}

template <typename T>
Expand Down

0 comments on commit 765b720

Please sign in to comment.