+ Improve sketch validation tool

This commit is contained in:
wmayer 2013-10-16 17:28:04 +02:00
parent 957cedc1ff
commit b29fbc5f93
2 changed files with 121 additions and 18 deletions

View File

@ -24,6 +24,14 @@
#include "PreCompiled.h" #include "PreCompiled.h"
#ifndef _PreComp_ #ifndef _PreComp_
# include <Standard_math.hxx>
# include <QMessageBox>
# include <Inventor/nodes/SoBaseColor.h>
# include <Inventor/nodes/SoCoordinate3.h>
# include <Inventor/nodes/SoDrawStyle.h>
# include <Inventor/nodes/SoMarkerSet.h>
# include <Inventor/nodes/SoSeparator.h>
# include <Inventor/nodes/SoShapeHints.h>
#endif #endif
#include <Precision.hxx> #include <Precision.hxx>
@ -37,7 +45,8 @@
#include <Mod/Part/App/Geometry.h> #include <Mod/Part/App/Geometry.h>
#include <App/Document.h> #include <App/Document.h>
#include <Gui/TaskView/TaskView.h> #include <Gui/TaskView/TaskView.h>
#include <Gui/BitmapFactory.h> #include <Gui/Application.h>
#include <Gui/ViewProvider.h>
#include <Gui/WaitCursor.h> #include <Gui/WaitCursor.h>
using namespace SketcherGui; using namespace SketcherGui;
@ -46,13 +55,15 @@ using namespace Gui::TaskView;
/* TRANSLATOR SketcherGui::SketcherValidation */ /* TRANSLATOR SketcherGui::SketcherValidation */
SketcherValidation::SketcherValidation(Sketcher::SketchObject* Obj, QWidget* parent) SketcherValidation::SketcherValidation(Sketcher::SketchObject* Obj, QWidget* parent)
: QWidget(parent), ui(new Ui_TaskSketcherValidation()), sketch(Obj) : QWidget(parent), ui(new Ui_TaskSketcherValidation()), sketch(Obj), coincidenceRoot(0)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->fixButton->setEnabled(false);
} }
SketcherValidation::~SketcherValidation() SketcherValidation::~SketcherValidation()
{ {
hidePoints();
} }
void SketcherValidation::changeEvent(QEvent *e) void SketcherValidation::changeEvent(QEvent *e)
@ -63,13 +74,13 @@ void SketcherValidation::changeEvent(QEvent *e)
QWidget::changeEvent(e); QWidget::changeEvent(e);
} }
typedef struct { struct SketcherValidation::VertexIds {
Base::Vector3d v; Base::Vector3d v;
int GeoId; int GeoId;
Sketcher::PointPos PosId; Sketcher::PointPos PosId;
} VertexIds; };
struct Vertex_Less : public std::binary_function<const VertexIds&, struct SketcherValidation::Vertex_Less : public std::binary_function<const VertexIds&,
const VertexIds&, bool> const VertexIds&, bool>
{ {
Vertex_Less(double tolerance) : tolerance(tolerance){} Vertex_Less(double tolerance) : tolerance(tolerance){}
@ -88,7 +99,7 @@ private:
double tolerance; double tolerance;
}; };
struct Vertex_EqualTo : public std::binary_function<const VertexIds&, struct SketcherValidation::Vertex_EqualTo : public std::binary_function<const VertexIds&,
const VertexIds&, bool> const VertexIds&, bool>
{ {
Vertex_EqualTo(double tolerance) : tolerance(tolerance){} Vertex_EqualTo(double tolerance) : tolerance(tolerance){}
@ -108,14 +119,15 @@ private:
double tolerance; double tolerance;
}; };
typedef struct { struct SketcherValidation::ConstraintIds {
Base::Vector3d v;
int First; int First;
int Second; int Second;
Sketcher::PointPos FirstPos; Sketcher::PointPos FirstPos;
Sketcher::PointPos SecondPos; Sketcher::PointPos SecondPos;
} ConstraintIds; };
struct Constraint_Less : public std::binary_function<const ConstraintIds&, struct SketcherValidation::Constraint_Less : public std::binary_function<const ConstraintIds&,
const ConstraintIds&, bool> const ConstraintIds&, bool>
{ {
bool operator()(const ConstraintIds& x, bool operator()(const ConstraintIds& x,
@ -140,10 +152,6 @@ struct Constraint_Less : public std::binary_function<const ConstraintIds&,
}; };
void SketcherValidation::on_findButton_clicked() void SketcherValidation::on_findButton_clicked()
{
}
void SketcherValidation::on_fixButton_clicked()
{ {
std::vector<VertexIds> vertexIds; std::vector<VertexIds> vertexIds;
const std::vector<Part::Geometry *>& geom = sketch->getInternalGeometry(); const std::vector<Part::Geometry *>& geom = sketch->getInternalGeometry();
@ -176,7 +184,7 @@ void SketcherValidation::on_fixButton_clicked()
} }
std::set<ConstraintIds, Constraint_Less> coincidences; std::set<ConstraintIds, Constraint_Less> coincidences;
double prec = 0.1; // Precision::Confusion() double prec = 0.001/*Precision::Confusion()*/;
std::sort(vertexIds.begin(), vertexIds.end(), Vertex_Less(prec)); std::sort(vertexIds.begin(), vertexIds.end(), Vertex_Less(prec));
std::vector<VertexIds>::iterator vt = vertexIds.begin(); std::vector<VertexIds>::iterator vt = vertexIds.begin();
Vertex_EqualTo pred(prec); Vertex_EqualTo pred(prec);
@ -188,6 +196,7 @@ void SketcherValidation::on_fixButton_clicked()
for (vn = vt+1; vn != vertexIds.end(); ++vn) { for (vn = vt+1; vn != vertexIds.end(); ++vn) {
if (pred(*vt,*vn)) { if (pred(*vt,*vn)) {
ConstraintIds id; ConstraintIds id;
id.v = vt->v;
id.First = vt->GeoId; id.First = vt->GeoId;
id.FirstPos = vt->PosId; id.FirstPos = vt->PosId;
id.Second = vn->GeoId; id.Second = vn->GeoId;
@ -218,11 +227,36 @@ void SketcherValidation::on_fixButton_clicked()
} }
} }
this->vertexConstraints.clear();
this->vertexConstraints.reserve(coincidences.size());
std::vector<Base::Vector3d> points;
points.reserve(coincidences.size());
for (std::set<ConstraintIds, Constraint_Less>::iterator it = coincidences.begin(); it != coincidences.end(); ++it) {
this->vertexConstraints.push_back(*it);
points.push_back(it->v);
}
hidePoints();
if (this->vertexConstraints.empty()) {
QMessageBox::information(this, tr("No missing coincidences"),
tr("No missing coincidences found"));
ui->fixButton->setEnabled(false);
}
else {
showPoints(points);
QMessageBox::warning(this, tr("Missing coincidences"),
tr("%1 missing coincidences found").arg(this->vertexConstraints.size()));
ui->fixButton->setEnabled(true);
}
}
void SketcherValidation::on_fixButton_clicked()
{
// undo command open // undo command open
App::Document* doc = sketch->getDocument(); App::Document* doc = sketch->getDocument();
doc->openTransaction("add coincident constraint"); doc->openTransaction("add coincident constraint");
std::vector<Sketcher::Constraint*> constr; std::vector<Sketcher::Constraint*> constr;
for (std::set<ConstraintIds, Constraint_Less>::iterator it = coincidences.begin(); it != coincidences.end(); ++it) { for (std::vector<ConstraintIds>::iterator it = this->vertexConstraints.begin(); it != this->vertexConstraints.end(); ++it) {
Sketcher::Constraint* c = new Sketcher::Constraint(); Sketcher::Constraint* c = new Sketcher::Constraint();
c->Type = Sketcher::Coincident; c->Type = Sketcher::Coincident;
c->First = it->First; c->First = it->First;
@ -231,7 +265,11 @@ void SketcherValidation::on_fixButton_clicked()
c->SecondPos = it->SecondPos; c->SecondPos = it->SecondPos;
constr.push_back(c); constr.push_back(c);
} }
sketch->addConstraints(constr); sketch->addConstraints(constr);
this->vertexConstraints.clear();
ui->fixButton->setEnabled(false);
hidePoints();
for (std::vector<Sketcher::Constraint*>::iterator it = constr.begin(); it != constr.end(); ++it) { for (std::vector<Sketcher::Constraint*>::iterator it = constr.begin(); it != constr.end(); ++it) {
delete *it; delete *it;
} }
@ -242,6 +280,54 @@ void SketcherValidation::on_fixButton_clicked()
doc->recompute(); doc->recompute();
} }
void SketcherValidation::showPoints(const std::vector<Base::Vector3d>& pts)
{
SoCoordinate3 * coords = new SoCoordinate3();
SoDrawStyle * drawStyle = new SoDrawStyle();
drawStyle->pointSize = 6;
SoPointSet* pcPoints = new SoPointSet();
coincidenceRoot = new SoGroup();
coincidenceRoot->addChild(drawStyle);
SoSeparator* pointsep = new SoSeparator();
SoBaseColor * basecol = new SoBaseColor();
basecol->rgb.setValue(1.0f, 0.5f, 0.0f);
pointsep->addChild(basecol);
pointsep->addChild(coords);
pointsep->addChild(pcPoints);
coincidenceRoot->addChild(pointsep);
// Draw markers
SoBaseColor * markcol = new SoBaseColor();
markcol->rgb.setValue(1.0f, 1.0f, 0.0f);
SoMarkerSet* marker = new SoMarkerSet();
marker->markerIndex=SoMarkerSet::PLUS_9_9;
pointsep->addChild(markcol);
pointsep->addChild(marker);
int pts_size = (int)pts.size();
coords->point.setNum(pts_size);
SbVec3f* c = coords->point.startEditing();
for (int i = 0; i < pts_size; i++) {
const Base::Vector3d& v = pts[i];
c[i].setValue((float)v.x,(float)v.y,(float)v.z);
}
coords->point.finishEditing();
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(sketch);
vp->getRoot()->addChild(coincidenceRoot);
}
void SketcherValidation::hidePoints()
{
if (coincidenceRoot) {
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(sketch);
vp->getRoot()->removeChild(coincidenceRoot);
coincidenceRoot = 0;
}
}
// ----------------------------------------------- // -----------------------------------------------
TaskSketcherValidation::TaskSketcherValidation(Sketcher::SketchObject* Obj) TaskSketcherValidation::TaskSketcherValidation(Sketcher::SketchObject* Obj)

View File

@ -24,9 +24,12 @@
#ifndef SKETCHERGUI_TASKSKETCHERVALIDATION_H #ifndef SKETCHERGUI_TASKSKETCHERVALIDATION_H
#define SKETCHERGUI_TASKSKETCHERVALIDATION_H #define SKETCHERGUI_TASKSKETCHERVALIDATION_H
#include <vector>
#include <memory> #include <memory>
#include <Base/Vector3D.h>
#include <Gui/TaskView/TaskDialog.h> #include <Gui/TaskView/TaskDialog.h>
class SoGroup;
namespace Sketcher { class SketchObject; } namespace Sketcher { class SketchObject; }
namespace SketcherGui { namespace SketcherGui {
@ -47,9 +50,21 @@ private Q_SLOTS:
void on_findButton_clicked(); void on_findButton_clicked();
void on_fixButton_clicked(); void on_fixButton_clicked();
private:
void showPoints(const std::vector<Base::Vector3d>&);
void hidePoints();
private: private:
std::auto_ptr<Ui_TaskSketcherValidation> ui; std::auto_ptr<Ui_TaskSketcherValidation> ui;
Sketcher::SketchObject* sketch; Sketcher::SketchObject* sketch;
SoGroup* coincidenceRoot;
struct VertexIds;
struct Vertex_Less;
struct Vertex_EqualTo;
struct ConstraintIds;
struct Constraint_Less;
std::vector<ConstraintIds> vertexConstraints;
}; };
class TaskSketcherValidation : public Gui::TaskView::TaskDialog class TaskSketcherValidation : public Gui::TaskView::TaskDialog
@ -59,6 +74,8 @@ class TaskSketcherValidation : public Gui::TaskView::TaskDialog
public: public:
TaskSketcherValidation(Sketcher::SketchObject* Obj); TaskSketcherValidation(Sketcher::SketchObject* Obj);
~TaskSketcherValidation(); ~TaskSketcherValidation();
virtual QDialogButtonBox::StandardButtons getStandardButtons(void) const
{ return QDialogButtonBox::Close; }
}; };
} //namespace SketcherGui } //namespace SketcherGui