Skip to content

Commit

Permalink
Horizontal and Vertical Constraint creation modes
Browse files Browse the repository at this point in the history
  • Loading branch information
AjinkyaDahale authored and wwmayer committed Jan 15, 2017
1 parent 6e7c491 commit a588349
Showing 1 changed file with 260 additions and 8 deletions.
268 changes: 260 additions & 8 deletions src/Mod/Sketcher/Gui/CommandConstraints.cpp
Expand Up @@ -738,6 +738,142 @@ int SketchSelection::setUp(void)


/* Constrain commands =======================================================*/

namespace SketcherGui {
class HoriVertConstraintSelection : public Gui::SelectionFilterGate
{
App::DocumentObject* object;
public:
HoriVertConstraintSelection(App::DocumentObject* obj)
: Gui::SelectionFilterGate((Gui::SelectionFilter*)0), object(obj)
{}

bool allow(App::Document *, App::DocumentObject *pObj, const char *sSubName)
{
if (pObj != this->object)
return false;
if (!sSubName || sSubName[0] == '\0')
return false;
std::string element(sSubName);
if (element.substr(0,4) == "Edge")
return true;
return false;
}
};
}

/* XPM */
static const char *cursor_createhoriconstraint[]={
"32 32 3 1",
"+ c white",
"# c red",
". c None",
"......+.........................",
"......+.........................",
"......+.........................",
"......+.........................",
"......+.........................",
"................................",
"+++++...+++++...................",
"................................",
"......+.........................",
"......+.........................",
"......+.........................",
"......+.........................",
"......+......#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............############.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................"};

class DrawSketchHandlerHoriConstraint: public DrawSketchHandler
{
public:
DrawSketchHandlerHoriConstraint() {}
virtual ~DrawSketchHandlerHoriConstraint()
{
Gui::Selection().rmvSelectionGate();
}

virtual void activated(ViewProviderSketch *)
{
Gui::Selection().rmvSelectionGate();
Gui::Selection().addSelectionGate(new HoriVertConstraintSelection(sketchgui->getObject()));
setCursor(QPixmap(cursor_createhoriconstraint), 7, 7);
}

virtual void mouseMove(Base::Vector2d onSketchPos) {Q_UNUSED(onSketchPos);}

virtual bool pressButton(Base::Vector2d onSketchPos)
{
Q_UNUSED(onSketchPos);
return false;
}

virtual bool releaseButton(Base::Vector2d onSketchPos)
{
Q_UNUSED(onSketchPos);
Sketcher::SketchObject* Obj = static_cast<Sketcher::SketchObject*>(sketchgui->getObject());

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

int CrvId = sketchgui->getPreselectCurve();
if (CrvId != -1) {
const Part::Geometry *geo = Obj->getGeometry(CrvId);
if (geo->getTypeId() != Part::GeomLineSegment::getClassTypeId()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("The selected edge is not a line segment"));
return false;
}

// check if the edge has already a Horizontal or Vertical constraint
for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin();
it != vals.end(); ++it) {
if ((*it)->Type == Sketcher::Horizontal && (*it)->First == CrvId){
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Double constraint"),
QObject::tr("The selected edge has already a horizontal constraint!"));
return false;
}
if ((*it)->Type == Sketcher::Vertical && (*it)->First == CrvId) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("The selected edge has already a vertical constraint!"));
return false;
}
}

// undo command open
Gui::Command::openCommand("add horizontal constraint");
// issue the actual commands to create the constraint
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Horizontal',%d)) ",
sketchgui->getObject()->getNameInDocument(),CrvId);
// finish the transaction and update
Gui::Command::commitCommand();

ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher");
bool autoRecompute = hGrp->GetBool("AutoRecompute",false);

if(autoRecompute)
Gui::Command::updateActive();
}
return false;
}
};

DEF_STD_CMD_A(CmdSketcherConstrainHorizontal);

CmdSketcherConstrainHorizontal::CmdSketcherConstrainHorizontal()
Expand All @@ -757,13 +893,16 @@ CmdSketcherConstrainHorizontal::CmdSketcherConstrainHorizontal()
void CmdSketcherConstrainHorizontal::activated(int iMsg)
{
Q_UNUSED(iMsg);

ActivateHandler(getActiveGuiDocument(), new DrawSketchHandlerHoriConstraint());

// get the selection
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();

// only one sketch with its subelements are allowed to be selected
if (selection.size() != 1) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select an edge from the sketch."));
// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
// QObject::tr("Select an edge from the sketch."));
return;
}

Expand Down Expand Up @@ -832,9 +971,121 @@ void CmdSketcherConstrainHorizontal::activated(int iMsg)

bool CmdSketcherConstrainHorizontal::isActive(void)
{
return isCreateConstraintActive( getActiveGuiDocument() );
return isCreateGeoActive( getActiveGuiDocument() );
}

// ======================================================================================

static const char *cursor_createvertconstraint[]={
"32 32 3 1",
"+ c white",
"# c red",
". c None",
"......+.........................",
"......+.........................",
"......+.........................",
"......+.........................",
"......+.........................",
"................................",
"+++++...+++++...................",
"................................",
"......+.........................",
"......+.........................",
"......+.........................",
"......+.........................",
"......+......#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
".............#..........#.......",
"..............#........#........",
"...............#......#.........",
"................#....#..........",
".................#..#...........",
"..................##............",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................"};

class DrawSketchHandlerVertConstraint: public DrawSketchHandler
{
public:
DrawSketchHandlerVertConstraint() {}
virtual ~DrawSketchHandlerVertConstraint()
{
Gui::Selection().rmvSelectionGate();
}

virtual void activated(ViewProviderSketch *)
{
Gui::Selection().rmvSelectionGate();
Gui::Selection().addSelectionGate(new HoriVertConstraintSelection(sketchgui->getObject()));
setCursor(QPixmap(cursor_createvertconstraint), 7, 7);
}

virtual void mouseMove(Base::Vector2d onSketchPos) {Q_UNUSED(onSketchPos);}

virtual bool pressButton(Base::Vector2d onSketchPos)
{
Q_UNUSED(onSketchPos);
return false;
}

virtual bool releaseButton(Base::Vector2d onSketchPos)
{
Q_UNUSED(onSketchPos);
Sketcher::SketchObject* Obj = static_cast<Sketcher::SketchObject*>(sketchgui->getObject());

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

int CrvId = sketchgui->getPreselectCurve();
if (CrvId != -1) {
const Part::Geometry *geo = Obj->getGeometry(CrvId);
if (geo->getTypeId() != Part::GeomLineSegment::getClassTypeId()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("The selected edge is not a line segment"));
return false;
}

// check if the edge has already a Horizontal or Vertical constraint
for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin();
it != vals.end(); ++it) {
if ((*it)->Type == Sketcher::Horizontal && (*it)->First == CrvId){
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Double constraint"),
QObject::tr("The selected edge has already a horizontal constraint!"));
return false;
}
if ((*it)->Type == Sketcher::Vertical && (*it)->First == CrvId) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("The selected edge has already a vertical constraint!"));
return false;
}
}

// undo command open
Gui::Command::openCommand("add horizontal constraint");
// issue the actual commands to create the constraint
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Vertical',%d)) ",
sketchgui->getObject()->getNameInDocument(),CrvId);
// finish the transaction and update
Gui::Command::commitCommand();

ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher");
bool autoRecompute = hGrp->GetBool("AutoRecompute",false);

if(autoRecompute)
Gui::Command::updateActive();
}
return false;
}
};

DEF_STD_CMD_A(CmdSketcherConstrainVertical);

Expand All @@ -855,13 +1106,16 @@ CmdSketcherConstrainVertical::CmdSketcherConstrainVertical()
void CmdSketcherConstrainVertical::activated(int iMsg)
{
Q_UNUSED(iMsg);

ActivateHandler(getActiveGuiDocument(), new DrawSketchHandlerVertConstraint());

// get the selection
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();

// only one sketch with its subelements are allowed to be selected
if (selection.size() != 1) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select an edge from the sketch."));
// QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
// QObject::tr("Select an edge from the sketch."));
return;
}

Expand Down Expand Up @@ -930,10 +1184,9 @@ void CmdSketcherConstrainVertical::activated(int iMsg)

bool CmdSketcherConstrainVertical::isActive(void)
{
return isCreateConstraintActive( getActiveGuiDocument() );
return isCreateGeoActive( getActiveGuiDocument() );
}


// ======================================================================================

namespace SketcherGui {
Expand Down Expand Up @@ -1205,7 +1458,6 @@ void CmdSketcherConstrainLock::updateAction(int mode)

bool CmdSketcherConstrainLock::isActive(void)
{
// return isCreateConstraintActive( getActiveGuiDocument() );
return isCreateGeoActive( getActiveGuiDocument() );
}

Expand Down

0 comments on commit a588349

Please sign in to comment.