DlgPrimitives.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 #include <Python.h>
00027 #include <Inventor/SoPickedPoint.h>
00028 #include <Inventor/events/SoMouseButtonEvent.h>
00029 #endif
00030 
00031 #include <Base/Interpreter.h>
00032 #include <Base/Rotation.h>
00033 #include <App/Application.h>
00034 #include <App/Document.h>
00035 #include <Gui/Application.h>
00036 #include <Gui/Document.h>
00037 #include <Gui/Command.h>
00038 #include <Gui/View3DInventor.h>
00039 #include <Gui/View3DInventorViewer.h>
00040 
00041 #include "DlgPrimitives.h"
00042 
00043 using namespace PartGui;
00044 
00045 /* TRANSLATOR PartGui::DlgPrimitives */
00046 
00047 DlgPrimitives::DlgPrimitives(QWidget* parent, Qt::WFlags fl)
00048   : Gui::LocationDialogComp<Ui_DlgPrimitives>(parent, fl)
00049 {
00050     Gui::Command::doCommand(Gui::Command::Doc, "from FreeCAD import Base");
00051     Gui::Command::doCommand(Gui::Command::Doc, "import Part,PartGui");
00052 
00053     connect(ui.viewPositionButton, SIGNAL(clicked()),
00054             this, SLOT(on_viewPositionButton_clicked()));
00055 
00056     // set limits
00057     //
00058     // plane
00059     ui.planeLength->setMaximum(INT_MAX);
00060     ui.planeWidth->setMaximum(INT_MAX);
00061     // box
00062     ui.boxLength->setMaximum(INT_MAX);
00063     ui.boxWidth->setMaximum(INT_MAX);
00064     ui.boxHeight->setMaximum(INT_MAX);
00065     // cylinder
00066     ui.cylinderRadius->setMaximum(INT_MAX);
00067     ui.cylinderHeight->setMaximum(INT_MAX);
00068     // cone
00069     ui.coneRadius1->setMaximum(INT_MAX);
00070     ui.coneRadius2->setMaximum(INT_MAX);
00071     ui.coneHeight->setMaximum(INT_MAX);
00072     // sphere
00073     ui.sphereRadius->setMaximum(INT_MAX);
00074     // ellipsoid
00075     ui.ellipsoidRadius1->setMaximum(INT_MAX);
00076     ui.ellipsoidRadius2->setMaximum(INT_MAX);
00077     // torus
00078     ui.torusRadius1->setMaximum(INT_MAX);
00079     ui.torusRadius2->setMaximum(INT_MAX);
00080     // wedge
00081     ui.wedgeXmin->setMinimum(INT_MIN);
00082     ui.wedgeXmin->setMaximum(INT_MAX);
00083     ui.wedgeYmin->setMinimum(INT_MIN);
00084     ui.wedgeYmin->setMaximum(INT_MAX);
00085     ui.wedgeZmin->setMinimum(INT_MIN);
00086     ui.wedgeZmin->setMaximum(INT_MAX);
00087     ui.wedgeX2min->setMinimum(INT_MIN);
00088     ui.wedgeX2min->setMaximum(INT_MAX);
00089     ui.wedgeZ2min->setMinimum(INT_MIN);
00090     ui.wedgeZ2min->setMaximum(INT_MAX);
00091     ui.wedgeXmax->setMinimum(INT_MIN);
00092     ui.wedgeXmax->setMaximum(INT_MAX);
00093     ui.wedgeYmax->setMinimum(INT_MIN);
00094     ui.wedgeYmax->setMaximum(INT_MAX);
00095     ui.wedgeZmax->setMinimum(INT_MIN);
00096     ui.wedgeZmax->setMaximum(INT_MAX);
00097     ui.wedgeX2max->setMinimum(INT_MIN);
00098     ui.wedgeX2max->setMaximum(INT_MAX);
00099     ui.wedgeZ2max->setMinimum(INT_MIN);
00100     ui.wedgeZ2max->setMaximum(INT_MAX);
00101     // helix
00102     ui.helixPitch->setMaximum(INT_MAX);
00103     ui.helixHeight->setMaximum(INT_MAX);
00104     ui.helixRadius->setMaximum(INT_MAX);
00105     // circle
00106     ui.circleRadius->setMaximum(INT_MAX);
00107     // vertex
00108     ui.VertexXAxisValue->setMaximum(INT_MAX);
00109     ui.VertexYAxisValue->setMaximum(INT_MAX);
00110     ui.VertexZAxisValue->setMaximum(INT_MAX);
00111     ui.VertexXAxisValue->setMinimum(-INT_MAX);
00112     ui.VertexYAxisValue->setMinimum(-INT_MAX);
00113     ui.VertexZAxisValue->setMinimum(-INT_MAX);
00114 }
00115 
00116 /*  
00117  *  Destroys the object and frees any allocated resources
00118  */
00119 DlgPrimitives::~DlgPrimitives()
00120 {
00121     // no need to delete child widgets, Qt does it all for us
00122     if (!this->activeView.isNull()) {
00123         Gui::View3DInventorViewer* viewer = static_cast<Gui::View3DInventor*>
00124             (this->activeView.data())->getViewer();
00125         viewer->setEditing(false);
00126         viewer->setRedirectToSceneGraph(false);
00127         viewer->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), pickCallback,this);
00128     }
00129 }
00130 
00131 QString DlgPrimitives::toPlacement() const
00132 {
00133     Base::Vector3f d = ui.getDirection();
00134     Base::Rotation rot(Base::Vector3d(0.0,0.0,1.0),
00135                        Base::Vector3d(d.x,d.y,d.z));
00136     return QString::fromAscii("Base.Placement(Base.Vector(%1,%2,%3),Base.Rotation(%4,%5,%6,%7))")
00137         .arg(ui.xPos->value(),0,'f',2)
00138         .arg(ui.yPos->value(),0,'f',2)
00139         .arg(ui.zPos->value(),0,'f',2)
00140         .arg(rot[0],0,'f',2)
00141         .arg(rot[1],0,'f',2)
00142         .arg(rot[2],0,'f',2)
00143         .arg(rot[3],0,'f',2);
00144 }
00145 
00146 void DlgPrimitives::pickCallback(void * ud, SoEventCallback * n)
00147 {
00148     const SoMouseButtonEvent * mbe = static_cast<const SoMouseButtonEvent*>(n->getEvent());
00149     Gui::View3DInventorViewer* view  = reinterpret_cast<Gui::View3DInventorViewer*>(n->getUserData());
00150 
00151     // Mark all incoming mouse button events as handled, especially, to deactivate the selection node
00152     n->getAction()->setHandled();
00153     if (mbe->getButton() == SoMouseButtonEvent::BUTTON1) {
00154         if (mbe->getState() == SoButtonEvent::UP) {
00155             n->setHandled();
00156             view->setEditing(false);
00157             view->setRedirectToSceneGraph(false);
00158             DlgPrimitives* dlg = reinterpret_cast<DlgPrimitives*>(ud);
00159             dlg->activeView = 0;
00160             view->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), pickCallback,ud);
00161         }
00162         else if (mbe->getState() == SoButtonEvent::DOWN) {
00163             const SoPickedPoint * point = n->getPickedPoint();
00164             if (point) {
00165                 SbVec3f pnt = point->getPoint();
00166                 SbVec3f nor = point->getNormal();
00167                 DlgPrimitives* dlg = reinterpret_cast<DlgPrimitives*>(ud);
00168                 dlg->ui.xPos->setValue(pnt[0]);
00169                 dlg->ui.yPos->setValue(pnt[1]);
00170                 dlg->ui.zPos->setValue(pnt[2]);
00171                 dlg->ui.setDirection(Base::Vector3f(nor[0],nor[1],nor[2]));
00172                 n->setHandled();
00173             }
00174         }
00175     }
00176 }
00177 
00178 void DlgPrimitives::on_viewPositionButton_clicked()
00179 {
00180     Gui::Document* doc = Gui::Application::Instance->activeDocument();
00181     if (!doc) {
00182         QMessageBox::warning(this, tr("Create %1")
00183             .arg(ui.comboBox1->currentText()), tr("No active document"));
00184         return;
00185     }
00186 
00187     Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView());
00188     if (view && !this->activeView) {
00189         Gui::View3DInventorViewer* viewer = view->getViewer();
00190         if (!viewer->isEditing()) {
00191             this->activeView = view;
00192             viewer->setEditing(true);
00193             viewer->setRedirectToSceneGraph(true);
00194             viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(), pickCallback, this);
00195         }
00196      }
00197 }
00198 
00199 void DlgPrimitives::accept()
00200 {
00201     try {
00202         QString cmd; QString name;
00203         App::Document* doc = App::GetApplication().getActiveDocument();
00204         if (!doc) {
00205             QMessageBox::warning(this, tr("Create %1")
00206                 .arg(ui.comboBox1->currentText()), tr("No active document"));
00207             return;
00208         }
00209         if (ui.comboBox1->currentIndex() == 0) {         // plane
00210             name = QString::fromAscii(doc->getUniqueObjectName("Plane").c_str());
00211             cmd = QString::fromAscii(
00212                 "App.ActiveDocument.addObject(\"Part::Plane\",\"%1\")\n"
00213                 "App.ActiveDocument.%1.Length=%2\n"
00214                 "App.ActiveDocument.%1.Width=%3\n"
00215                 "App.ActiveDocument.%1.Placement=%4\n")
00216                 .arg(name)
00217                 .arg(ui.planeLength->value(),0,'f',2)
00218                 .arg(ui.planeWidth->value(),0,'f',2)
00219                 .arg(this->toPlacement());
00220         }
00221         else if (ui.comboBox1->currentIndex() == 1) {         // box
00222             name = QString::fromAscii(doc->getUniqueObjectName("Box").c_str());
00223             cmd = QString::fromAscii(
00224                 "App.ActiveDocument.addObject(\"Part::Box\",\"%1\")\n"
00225                 "App.ActiveDocument.%1.Length=%2\n"
00226                 "App.ActiveDocument.%1.Width=%3\n"
00227                 "App.ActiveDocument.%1.Height=%4\n"
00228                 "App.ActiveDocument.%1.Placement=%5\n")
00229                 .arg(name)
00230                 .arg(ui.boxLength->value(),0,'f',2)
00231                 .arg(ui.boxWidth->value(),0,'f',2)
00232                 .arg(ui.boxHeight->value(),0,'f',2)
00233                 .arg(this->toPlacement());
00234         }
00235         else if (ui.comboBox1->currentIndex() == 2) {  // cylinder
00236             name = QString::fromAscii(doc->getUniqueObjectName("Cylinder").c_str());
00237             cmd = QString::fromAscii(
00238                 "App.ActiveDocument.addObject(\"Part::Cylinder\",\"%1\")\n"
00239                 "App.ActiveDocument.%1.Radius=%2\n"
00240                 "App.ActiveDocument.%1.Height=%3\n"
00241                 "App.ActiveDocument.%1.Angle=%4\n"
00242                 "App.ActiveDocument.%1.Placement=%5\n")
00243                 .arg(name)
00244                 .arg(ui.cylinderRadius->value(),0,'f',2)
00245                 .arg(ui.cylinderHeight->value(),0,'f',2)
00246                 .arg(ui.cylinderAngle->value(),0,'f',2)
00247                 .arg(this->toPlacement());
00248         }
00249         else if (ui.comboBox1->currentIndex() == 3) {  // cone
00250             name = QString::fromAscii(doc->getUniqueObjectName("Cone").c_str());
00251             cmd = QString::fromAscii(
00252                 "App.ActiveDocument.addObject(\"Part::Cone\",\"%1\")\n"
00253                 "App.ActiveDocument.%1.Radius1=%2\n"
00254                 "App.ActiveDocument.%1.Radius2=%3\n"
00255                 "App.ActiveDocument.%1.Height=%4\n"
00256                 "App.ActiveDocument.%1.Angle=%5\n"
00257                 "App.ActiveDocument.%1.Placement=%6\n")
00258                 .arg(name)
00259                 .arg(ui.coneRadius1->value(),0,'f',2)
00260                 .arg(ui.coneRadius2->value(),0,'f',2)
00261                 .arg(ui.coneHeight->value(),0,'f',2)
00262                 .arg(ui.coneAngle->value(),0,'f',2)
00263                 .arg(this->toPlacement());
00264         }
00265         else if (ui.comboBox1->currentIndex() == 4) {  // sphere
00266             name = QString::fromAscii(doc->getUniqueObjectName("Sphere").c_str());
00267             cmd = QString::fromAscii(
00268                 "App.ActiveDocument.addObject(\"Part::Sphere\",\"%1\")\n"
00269                 "App.ActiveDocument.%1.Radius=%2\n"
00270                 "App.ActiveDocument.%1.Angle1=%3\n"
00271                 "App.ActiveDocument.%1.Angle2=%4\n"
00272                 "App.ActiveDocument.%1.Angle3=%5\n"
00273                 "App.ActiveDocument.%1.Placement=%6\n")
00274                 .arg(name)
00275                 .arg(ui.sphereRadius->value(),0,'f',2)
00276                 .arg(ui.sphereAngle1->value(),0,'f',2)
00277                 .arg(ui.sphereAngle2->value(),0,'f',2)
00278                 .arg(ui.sphereAngle3->value(),0,'f',2)
00279                 .arg(this->toPlacement());
00280         }
00281         else if (ui.comboBox1->currentIndex() == 5) {  // ellipsoid
00282             name = QString::fromAscii(doc->getUniqueObjectName("Ellipsoid").c_str());
00283             cmd = QString::fromAscii(
00284                 "App.ActiveDocument.addObject(\"Part::Ellipsoid\",\"%1\")\n"
00285                 "App.ActiveDocument.%1.Radius1=%2\n"
00286                 "App.ActiveDocument.%1.Radius2=%3\n"
00287                 "App.ActiveDocument.%1.Angle1=%4\n"
00288                 "App.ActiveDocument.%1.Angle2=%5\n"
00289                 "App.ActiveDocument.%1.Angle3=%6\n"
00290                 "App.ActiveDocument.%1.Placement=%7\n")
00291                 .arg(name)
00292                 .arg(ui.ellipsoidRadius1->value(),0,'f',2)
00293                 .arg(ui.ellipsoidRadius2->value(),0,'f',2)
00294                 .arg(ui.ellipsoidAngle1->value(),0,'f',2)
00295                 .arg(ui.ellipsoidAngle2->value(),0,'f',2)
00296                 .arg(ui.ellipsoidAngle3->value(),0,'f',2)
00297                 .arg(this->toPlacement());
00298         }
00299         else if (ui.comboBox1->currentIndex() == 6) {  // torus
00300             name = QString::fromAscii(doc->getUniqueObjectName("Torus").c_str());
00301             cmd = QString::fromAscii(
00302                 "App.ActiveDocument.addObject(\"Part::Torus\",\"%1\")\n"
00303                 "App.ActiveDocument.%1.Radius1=%2\n"
00304                 "App.ActiveDocument.%1.Radius2=%3\n"
00305                 "App.ActiveDocument.%1.Angle1=%4\n"
00306                 "App.ActiveDocument.%1.Angle2=%5\n"
00307                 "App.ActiveDocument.%1.Angle3=%6\n"
00308                 "App.ActiveDocument.%1.Placement=%7\n")
00309                 .arg(name)
00310                 .arg(ui.torusRadius1->value(),0,'f',2)
00311                 .arg(ui.torusRadius2->value(),0,'f',2)
00312                 .arg(ui.torusAngle1->value(),0,'f',2)
00313                 .arg(ui.torusAngle2->value(),0,'f',2)
00314                 .arg(ui.torusAngle3->value(),0,'f',2)
00315                 .arg(this->toPlacement());
00316         }
00317         else if (ui.comboBox1->currentIndex() == 7) {  // wedge
00318             name = QString::fromAscii(doc->getUniqueObjectName("Wedge").c_str());
00319             cmd = QString::fromAscii(
00320                 "App.ActiveDocument.addObject(\"Part::Wedge\",\"%1\")\n"
00321                 "App.ActiveDocument.%1.Xmin=%2\n"
00322                 "App.ActiveDocument.%1.Ymin=%3\n"
00323                 "App.ActiveDocument.%1.Zmin=%4\n"
00324                 "App.ActiveDocument.%1.X2min=%5\n"
00325                 "App.ActiveDocument.%1.Z2min=%6\n"
00326                 "App.ActiveDocument.%1.Xmax=%7\n"
00327                 "App.ActiveDocument.%1.Ymax=%8\n"
00328                 "App.ActiveDocument.%1.Zmax=%9\n"
00329                 "App.ActiveDocument.%1.X2max=%10\n"
00330                 "App.ActiveDocument.%1.Z2max=%11\n"
00331                 "App.ActiveDocument.%1.Placement=%12\n")
00332                 .arg(name)
00333                 .arg(ui.wedgeXmin->value(),0,'f',2)
00334                 .arg(ui.wedgeYmin->value(),0,'f',2)
00335                 .arg(ui.wedgeZmin->value(),0,'f',2)
00336                 .arg(ui.wedgeX2min->value(),0,'f',2)
00337                 .arg(ui.wedgeZ2min->value(),0,'f',2)
00338                 .arg(ui.wedgeXmax->value(),0,'f',2)
00339                 .arg(ui.wedgeYmax->value(),0,'f',2)
00340                 .arg(ui.wedgeZmax->value(),0,'f',2)
00341                 .arg(ui.wedgeX2max->value(),0,'f',2)
00342                 .arg(ui.wedgeZ2max->value(),0,'f',2)
00343                 .arg(this->toPlacement());
00344         }
00345         else if (ui.comboBox1->currentIndex() == 8) {  // helix
00346             name = QString::fromAscii(doc->getUniqueObjectName("Helix").c_str());
00347             cmd = QString::fromAscii(
00348                 "App.ActiveDocument.addObject(\"Part::Helix\",\"%1\")\n"
00349                 "App.ActiveDocument.%1.Pitch=%2\n"
00350                 "App.ActiveDocument.%1.Height=%3\n"
00351                 "App.ActiveDocument.%1.Radius=%4\n"
00352                 "App.ActiveDocument.%1.Angle=%5\n"
00353                 "App.ActiveDocument.%1.Placement=%6\n")
00354                 .arg(name)
00355                 .arg(ui.helixPitch->value(),0,'f',2)
00356                 .arg(ui.helixHeight->value(),0,'f',2)
00357                 .arg(ui.helixRadius->value(),0,'f',2)
00358                 .arg(ui.helixAngle->value(),0,'f',2)
00359                 .arg(this->toPlacement());
00360         }
00361         else if (ui.comboBox1->currentIndex() == 9) {  // circle
00362             name = QString::fromAscii(doc->getUniqueObjectName("Circle").c_str());
00363             cmd = QString::fromAscii(
00364                 "App.ActiveDocument.addObject(\"Part::Circle\",\"%1\")\n"
00365                 "App.ActiveDocument.%1.Radius=%2\n"
00366                 "App.ActiveDocument.%1.Angle0=%3\n"
00367                 "App.ActiveDocument.%1.Angle1=%4\n"
00368                 "App.ActiveDocument.%1.Placement=%5\n")
00369                 .arg(name)
00370                 .arg(ui.circleRadius->value(),0,'f',2)
00371                 .arg(ui.circleAngle0->value(),0,'f',2)
00372                 .arg(ui.circleAngle1->value(),0,'f',2)
00373                 .arg(this->toPlacement());
00374         }
00375         else if (ui.comboBox1->currentIndex() == 10) {  // vertex
00376             name = QString::fromAscii(doc->getUniqueObjectName("Vertex").c_str());
00377             cmd = QString::fromAscii(
00378                 "App.ActiveDocument.addObject(\"Part::Vertex\",\"%1\")\n"
00379                 "App.ActiveDocument.%1.X=%2\n"
00380                 "App.ActiveDocument.%1.Y=%3\n"
00381                 "App.ActiveDocument.%1.Z=%4\n"
00382                 "App.ActiveDocument.%1.Placement=%5\n")
00383                 .arg(name)
00384                 .arg(ui.VertexXAxisValue->value(),0,'f',2)
00385                 .arg(ui.VertexYAxisValue->value(),0,'f',2)
00386                 .arg(ui.VertexZAxisValue->value(),0,'f',2)
00387                 .arg(this->toPlacement());
00388         }
00389 
00390         // Execute the Python block
00391         QString prim = tr("Create %1").arg(ui.comboBox1->currentText());
00392         Gui::Application::Instance->activeDocument()->openCommand(prim.toUtf8());
00393         Gui::Command::doCommand(Gui::Command::Doc, (const char*)cmd.toAscii());
00394         Gui::Application::Instance->activeDocument()->commitCommand();
00395         Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.recompute()");
00396         Gui::Command::doCommand(Gui::Command::Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");
00397     }
00398     catch (const Base::PyException& e) {
00399         QMessageBox::warning(this, tr("Create %1")
00400             .arg(ui.comboBox1->currentText()), QString::fromLatin1(e.what()));
00401     }
00402 }
00403 
00404 #include "moc_DlgPrimitives.cpp"

Generated on Wed Nov 23 19:00:06 2011 for FreeCAD by  doxygen 1.6.1