Navigation Menu

Skip to content

Commit

Permalink
code review: move transaction to outside from loop, fix several bugs,…
Browse files Browse the repository at this point in the history
… fix whitespaces
  • Loading branch information
wwmayer committed Aug 4, 2014
1 parent 1011dd4 commit be3299a
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 216 deletions.
10 changes: 5 additions & 5 deletions src/Mod/Sketcher/App/SketchObject.cpp
Expand Up @@ -1633,11 +1633,11 @@ void SketchObject::getGeoVertexIndex(int VertexId, int &GeoId, PointPos &PosId)

int SketchObject::getVertexIndexGeoPos(int GeoId, PointPos PosId) const
{

for(int i=0;i<VertexId2GeoId.size();i++)
if(VertexId2GeoId[i]==GeoId && VertexId2PosId[i]==PosId)
return i;
for(int i=0;i<VertexId2GeoId.size();i++) {
if(VertexId2GeoId[i]==GeoId && VertexId2PosId[i]==PosId)
return i;
}

return -1;
}

Expand Down
52 changes: 29 additions & 23 deletions src/Mod/Sketcher/Gui/CommandConstraints.cpp
Expand Up @@ -529,7 +529,7 @@ void CmdSketcherConstrainCoincident::activated(int iMsg)
const std::vector<std::string> &SubNames = selection[0].getSubNames();
Sketcher::SketchObject* Obj = dynamic_cast<Sketcher::SketchObject*>(selection[0].getObject());
const std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues();

if (SubNames.size() < 2) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select two or more vertexes from the sketch."));
Expand All @@ -552,33 +552,39 @@ void CmdSketcherConstrainCoincident::activated(int iMsg)
getIdsFromName(SubNames[0], Obj, GeoId1, PosId1);

// undo command open
bool constraintsAdded = false;
openCommand("add coincident constraint");
for (std::size_t i=1; i<SubNames.size(); i++) {
getIdsFromName(SubNames[i], Obj, GeoId2, PosId2);

// check if any of the coincident constraints exist
bool constraintExists=false;

for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin();
it != vals.end(); ++it) {
if ( (*it)->Type == Sketcher::Coincident &&
( (*it)->First == GeoId1 && (*it)->FirstPos == PosId1 &&
(*it)->Second == GeoId2 && (*it)->SecondPos == PosId2 ) ||
( (*it)->First == GeoId2 && (*it)->FirstPos == PosId2 &&
(*it)->Second == GeoId1 && (*it)->SecondPos == PosId1 ) ) {
constraintExists=true;
break;
}
}

if (!constraintExists)
Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2);

// check if any of the coincident constraints exist
bool constraintExists=false;

for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin(); it != vals.end(); ++it) {
if ((*it)->Type == Sketcher::Coincident &&
( (*it)->First == GeoId1 && (*it)->FirstPos == PosId1 &&
(*it)->Second == GeoId2 && (*it)->SecondPos == PosId2 ) ||
( (*it)->First == GeoId2 && (*it)->FirstPos == PosId2 &&
(*it)->Second == GeoId1 && (*it)->SecondPos == PosId1 ) ) {
constraintExists=true;
break;
}
}

if (!constraintExists) {
constraintsAdded = true;
Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoId1,PosId1,GeoId2,PosId2);
}
}

// finish the transaction and update
commitCommand();
// finish or abort the transaction and update
if (constraintsAdded)
commitCommand();
else
abortCommand();

updateActive();

// clear the selection (convenience)
Expand Down
161 changes: 82 additions & 79 deletions src/Mod/Sketcher/Gui/CommandSketcherAccel.cpp
Expand Up @@ -47,18 +47,19 @@ using namespace Sketcher;

bool isSketcherAcceleratorActive(Gui::Document *doc, bool actsOnSelection )
{
if (doc)
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 (doc->getInEdit() && doc->getInEdit()->isDerivedFrom(SketcherGui::ViewProviderSketch::getClassTypeId())) {
if (dynamic_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit())
->getSketchMode() == ViewProviderSketch::STATUS_NONE)
->getSketchMode() == ViewProviderSketch::STATUS_NONE) {
if (!actsOnSelection)
return true;
else{
if(Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) > 0)
return true;
}

return true;
else if (Gui::Selection().countObjectsOfType(Sketcher::SketchObject::getClassTypeId()) > 0)
return true;
}
}
}

return false;
}

Expand Down Expand Up @@ -87,62 +88,61 @@ void CmdSketcherCloseShape::activated(int iMsg)
// 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."));
QObject::tr("Select at least two edges from the sketch."));
return;
}

// get the needed lists and objects
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if (SubNames.size() < 2) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select at least two edges from the sketch."));
return;
}
Sketcher::SketchObject* Obj = dynamic_cast<Sketcher::SketchObject*>(selection[0].getObject());

int GeoIdFirst=-1;
int GeoIdLast=-1;

// undo command open
openCommand("add coincident constraint");
// go through the selected subelements
for (unsigned int i=0; i<(SubNames.size()-1); i++ ) {
// only handle edges
if (SubNames[i].size() > 4 && SubNames[i].substr(0,4) == "Edge" &&
SubNames[i+1].size() > 4 && SubNames[i+1].substr(0,4) == "Edge" ) {

int GeoId1 = std::atoi(SubNames[i].substr(4,4000).c_str()) - 1;
int GeoId2 = std::atoi(SubNames[i+1].substr(4,4000).c_str()) - 1;

if(GeoIdFirst==-1)
GeoIdFirst=GeoId1;

GeoIdLast=GeoId2;

const Part::Geometry *geo1 = Obj->getGeometry(GeoId1);
const Part::Geometry *geo2 = Obj->getGeometry(GeoId2);
if ( (geo1->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo1->getTypeId() != Part::GeomArcOfCircle::getClassTypeId() ) ||
(geo2->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo2->getTypeId() != Part::GeomArcOfCircle::getClassTypeId()) ) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("One selected edge is not connectable"));
return;
}

// undo command open
openCommand("add coincident constraint");
Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoId1,Sketcher::end,GeoId2,Sketcher::start);

// finish the transaction and update
commitCommand();

SubNames[i+1].size() > 4 && SubNames[i+1].substr(0,4) == "Edge" ) {

int GeoId1 = std::atoi(SubNames[i].substr(4,4000).c_str()) - 1;
int GeoId2 = std::atoi(SubNames[i+1].substr(4,4000).c_str()) - 1;

if(GeoIdFirst==-1)
GeoIdFirst=GeoId1;

GeoIdLast=GeoId2;

const Part::Geometry *geo1 = Obj->getGeometry(GeoId1);
const Part::Geometry *geo2 = Obj->getGeometry(GeoId2);
if ((geo1->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo1->getTypeId() != Part::GeomArcOfCircle::getClassTypeId() ) ||
(geo2->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo2->getTypeId() != Part::GeomArcOfCircle::getClassTypeId()) ) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("One selected edge is not connectable"));
abortCommand();
return;
}

Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoId1,Sketcher::end,GeoId2,Sketcher::start);
}
}

// Close Last Edge with First Edge
// undo command open
openCommand("add coincident constraint");
Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoIdLast,Sketcher::end,GeoIdFirst,Sketcher::start);
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoIdLast,Sketcher::end,GeoIdFirst,Sketcher::start);

// finish the transaction and update
commitCommand();

Expand Down Expand Up @@ -183,47 +183,51 @@ void CmdSketcherConnect::activated(int iMsg)
// 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."));
QObject::tr("Select at least two edges from the sketch."));
return;
}

// get the needed lists and objects
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if (SubNames.size() < 2) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
QObject::tr("Select at least two edges from the sketch."));
return;
}
Sketcher::SketchObject* Obj = dynamic_cast<Sketcher::SketchObject*>(selection[0].getObject());


// undo command open
openCommand("add coincident constraint");

// go through the selected subelements
for (unsigned int i=0; i<(SubNames.size()-1); i++ ) {
// only handle edges
if (SubNames[i].size() > 4 && SubNames[i].substr(0,4) == "Edge" &&
SubNames[i+1].size() > 4 && SubNames[i+1].substr(0,4) == "Edge" ) {

int GeoId1 = std::atoi(SubNames[i].substr(4,4000).c_str()) - 1;
int GeoId2 = std::atoi(SubNames[i+1].substr(4,4000).c_str()) - 1;

const Part::Geometry *geo1 = Obj->getGeometry(GeoId1);
const Part::Geometry *geo2 = Obj->getGeometry(GeoId2);
if ( (geo1->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo1->getTypeId() != Part::GeomArcOfCircle::getClassTypeId() ) ||
(geo2->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo2->getTypeId() != Part::GeomArcOfCircle::getClassTypeId()) ) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("One selected edge is not connectable"));
return;
}

// undo command open
openCommand("add coincident constraint");
Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoId1,Sketcher::end,GeoId2,Sketcher::start);

// finish the transaction and update
commitCommand();

SubNames[i+1].size() > 4 && SubNames[i+1].substr(0,4) == "Edge" ) {

int GeoId1 = std::atoi(SubNames[i].substr(4,4000).c_str()) - 1;
int GeoId2 = std::atoi(SubNames[i+1].substr(4,4000).c_str()) - 1;

const Part::Geometry *geo1 = Obj->getGeometry(GeoId1);
const Part::Geometry *geo2 = Obj->getGeometry(GeoId2);
if ((geo1->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo1->getTypeId() != Part::GeomArcOfCircle::getClassTypeId()) ||
(geo2->getTypeId() != Part::GeomLineSegment::getClassTypeId() &&
geo2->getTypeId() != Part::GeomArcOfCircle::getClassTypeId())) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Impossible constraint"),
QObject::tr("One selected edge is not connectable"));
abortCommand();
return;
}

Gui::Command::doCommand(
Doc,"App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%d,%d,%d,%d)) ",
selection[0].getFeatName(),GeoId1,Sketcher::end,GeoId2,Sketcher::start);
}
}

// finish the transaction and update
commitCommand();
updateActive();

// clear the selection (convenience)
Expand Down Expand Up @@ -282,18 +286,17 @@ void CmdSketcherSelectConstraints::activated(int iMsg)
int GeoId = std::atoi(it->substr(4,4000).c_str()) - 1;

// push all the constraints
int i=1;
int i=1;
for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin();
it != vals.end(); ++it,++i) {
if ( (*it)->First == GeoId || (*it)->Second == GeoId || (*it)->Third == GeoId){
ss.str(std::string());
ss << "Constraint" << i;
Gui::Selection().addSelection(doc_name.c_str(), obj_name.c_str(), ss.str().c_str());
}
ss.str(std::string());
ss << "Constraint" << i;
Gui::Selection().addSelection(doc_name.c_str(), obj_name.c_str(), ss.str().c_str());
}
}
}
}

}

bool CmdSketcherSelectConstraints::isActive(void)
Expand Down

0 comments on commit be3299a

Please sign in to comment.