0000548: 3D Circle from three points in Part wb

git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5387 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
wmayer 2012-01-04 16:17:57 +00:00
parent 03e162b18e
commit f01807be7e
5 changed files with 1807 additions and 1614 deletions

View File

@ -341,7 +341,7 @@ PyObject* Workbench::getPyObject()
return new WorkbenchPy(this);
}
void Workbench::addTaskWatcher(std::vector<Gui::TaskView::TaskWatcher*> &Watcher)
void Workbench::addTaskWatcher(const std::vector<Gui::TaskView::TaskWatcher*> &Watcher)
{
Gui::TaskView::TaskView* taskView = Control().taskPanel();
if (taskView)

View File

@ -90,7 +90,7 @@ public:
virtual void deactivated();
/// helper to add TaskWatcher to the TaskView
void addTaskWatcher(std::vector<Gui::TaskView::TaskWatcher*> &Watcher);
void addTaskWatcher(const std::vector<Gui::TaskView::TaskWatcher*> &Watcher);
/// remove the added TaskWatcher
void removeTaskWatcher(void);

View File

@ -24,15 +24,22 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <Python.h>
#include <gp_Ax1.hxx>
#include <gp_Ax3.hxx>
#include <gp_Dir.hxx>
#include <gp_Pnt.hxx>
#include <GC_MakeArcOfCircle.hxx>
#include <Geom_Circle.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Handle_Geom_Circle.hxx>
#include <Handle_Geom_TrimmedCurve.hxx>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/events/SoMouseButtonEvent.h>
#endif
#include <Base/Interpreter.h>
#include <Base/Rotation.h>
#include <Base/Tools.h>
#include <App/Application.h>
#include <App/Document.h>
#include <Gui/Application.h>
@ -47,6 +54,91 @@
using namespace PartGui;
namespace PartGui {
void Picker::createPrimitive(QWidget* widget, const QString& descr, Gui::Document* doc)
{
try {
App::Document* app = doc->getDocument();
QString cmd = this->command(app);
// Execute the Python block
doc->openCommand(descr.toUtf8());
Gui::Command::doCommand(Gui::Command::Doc, (const char*)cmd.toAscii());
doc->commitCommand();
Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.recompute()");
Gui::Command::doCommand(Gui::Command::Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");
}
catch (const Base::PyException& e) {
QMessageBox::warning(widget, descr, QString::fromLatin1(e.what()));
}
}
QString Picker::toPlacement(const gp_Ax2& axis) const
{
gp_Dir dir = axis.Direction();
gp_Pnt pnt = gp_Pnt(0.0,0.0,0.0);
gp_Ax3 ax3(pnt, dir, axis.XDirection());
gp_Trsf Trf;
Trf.SetTransformation(ax3);
Trf.Invert();
gp_XYZ theAxis(0,0,1);
Standard_Real theAngle = 0.0;
Trf.GetRotation(theAxis,theAngle);
Base::Rotation rot(Base::convertTo<Base::Vector3d>(theAxis), theAngle);
gp_Pnt loc = axis.Location();
return QString::fromAscii("Base.Placement(Base.Vector(%1,%2,%3),Base.Rotation(%4,%5,%6,%7))")
.arg(loc.X(),0,'f',2)
.arg(loc.Y(),0,'f',2)
.arg(loc.Z(),0,'f',2)
.arg(rot[0],0,'f',2)
.arg(rot[1],0,'f',2)
.arg(rot[2],0,'f',2)
.arg(rot[3],0,'f',2);
}
class CircleFromThreePoints : public Picker
{
public:
CircleFromThreePoints() : Picker()
{
}
bool pickedPoint(const SoPickedPoint * point)
{
SbVec3f pnt = point->getPoint();
points.push_back(gp_Pnt(pnt[0],pnt[1],pnt[2]));
return points.size() == 3;
}
QString command(App::Document* doc) const
{
GC_MakeArcOfCircle arc(points[0], points[1], points[2]);
Handle_Geom_TrimmedCurve trim = arc.Value();
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(trim->BasisCurve());
QString name = QString::fromAscii(doc->getUniqueObjectName("Circle").c_str());
return QString::fromAscii(
"App.ActiveDocument.addObject(\"Part::Circle\",\"%1\")\n"
"App.ActiveDocument.%1.Radius=%2\n"
"App.ActiveDocument.%1.Angle0=%3\n"
"App.ActiveDocument.%1.Angle1=%4\n"
"App.ActiveDocument.%1.Placement=%5\n")
.arg(name)
.arg(circle->Radius(),0,'f',2)
.arg(Base::toDegrees(trim->FirstParameter()),0,'f',2)
.arg(Base::toDegrees(trim->LastParameter ()),0,'f',2)
.arg(toPlacement(circle->Position()));
}
private:
std::vector<gp_Pnt> points;
};
}
/* TRANSLATOR PartGui::DlgPrimitives */
DlgPrimitives::DlgPrimitives(QWidget* parent)
@ -136,6 +228,66 @@ DlgPrimitives::~DlgPrimitives()
{
}
void DlgPrimitives::pickCallback(void * ud, SoEventCallback * n)
{
const SoMouseButtonEvent * mbe = static_cast<const SoMouseButtonEvent*>(n->getEvent());
Gui::View3DInventorViewer* view = reinterpret_cast<Gui::View3DInventorViewer*>(n->getUserData());
// Mark all incoming mouse button events as handled, especially, to deactivate the selection node
n->setHandled();
if (mbe->getButton() == SoMouseButtonEvent::BUTTON1) {
if (mbe->getState() == SoButtonEvent::DOWN) {
const SoPickedPoint * point = n->getPickedPoint();
if (point) {
Picker* pick = reinterpret_cast<Picker*>(ud);
if (pick->pickedPoint(point)) {
pick->loop.exit(0);
}
}
}
}
else if (mbe->getButton() == SoMouseButtonEvent::BUTTON2) {
if (mbe->getState() == SoButtonEvent::UP) {
Picker* pick = reinterpret_cast<Picker*>(ud);
pick->loop.exit(1);
}
}
}
void DlgPrimitives::executeCallback(Picker* p)
{
Gui::Document* doc = Gui::Application::Instance->activeDocument();
if (!doc) {
return;
}
Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView());
if (view) {
Gui::View3DInventorViewer* viewer = view->getViewer();
if (!viewer->isEditing()) {
viewer->setEditing(true);
viewer->setRedirectToSceneGraph(true);
viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(), pickCallback, p);
this->setDisabled(true);
int ret = p->loop.exec();
this->setEnabled(true);
viewer->setEditing(false);
viewer->setRedirectToSceneGraph(false);
viewer->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), pickCallback, p);
if (ret == 0) {
p->createPrimitive(this, ui.comboBox1->currentText(), doc);
}
}
}
}
void DlgPrimitives::on_buttonCircleFromThreePoints_clicked()
{
CircleFromThreePoints pp;
executeCallback(&pp);
}
void DlgPrimitives::createPrimitive(const QString& placement)
{
try {
@ -384,9 +536,6 @@ void DlgPrimitives::createPrimitive(const QString& placement)
Location::Location(QWidget* parent)
{
ui.setupUi(this);
connect(ui.viewPositionButton, SIGNAL(clicked()),
this, SLOT(on_viewPositionButton_clicked()));
}
Location::~Location()

View File

@ -23,14 +23,36 @@
#ifndef PARTGUI_DLGPRIMITIVES_H
#define PARTGUI_DLGPRIMITIVES_H
#include <QEventLoop>
#include <QPointer>
#include <Gui/TaskView/TaskDialog.h>
#include "ui_DlgPrimitives.h"
#include "ui_Location.h"
class gp_Ax2;
class SoEventCallback;
namespace App { class Document; }
namespace Gui { class Document; }
namespace PartGui {
class Picker
{
public:
Picker()
{
}
virtual ~Picker()
{
}
virtual bool pickedPoint(const SoPickedPoint * point) = 0;
virtual QString command(App::Document*) const = 0;
void createPrimitive(QWidget* widget, const QString&, Gui::Document*);
QString toPlacement(const gp_Ax2&) const;
QEventLoop loop;
};
class DlgPrimitives : public QWidget
{
@ -41,6 +63,13 @@ public:
~DlgPrimitives();
void createPrimitive(const QString&);
private Q_SLOTS:
void on_buttonCircleFromThreePoints_clicked();
private:
static void pickCallback(void * ud, SoEventCallback * n);
void executeCallback(Picker*);
private:
Ui_DlgPrimitives ui;
};

File diff suppressed because it is too large Load Diff