DlgRevolution.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 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 <QMessageBox>
00027 # include <TopExp_Explorer.hxx>
00028 #endif
00029 
00030 #include "ui_DlgRevolution.h"
00031 #include "DlgRevolution.h"
00032 #include "../App/PartFeature.h"
00033 #include <App/Application.h>
00034 #include <App/Document.h>
00035 #include <App/DocumentObject.h>
00036 #include <Gui/Application.h>
00037 #include <Gui/BitmapFactory.h>
00038 #include <Gui/Command.h>
00039 #include <Gui/Document.h>
00040 #include <Gui/Utilities.h>
00041 #include <Gui/ViewProvider.h>
00042 #include <Gui/WaitCursor.h>
00043 
00044 using namespace PartGui;
00045 
00046 DlgRevolution::DlgRevolution(QWidget* parent, Qt::WFlags fl)
00047   : Gui::LocationDialog(parent, fl)
00048 {
00049     ui = new Ui_RevolutionComp(this);
00050     ui->baseX->setRange(-DBL_MAX,DBL_MAX);
00051     ui->baseY->setRange(-DBL_MAX,DBL_MAX);
00052     ui->baseZ->setRange(-DBL_MAX,DBL_MAX);
00053     findShapes();
00054 
00055     Gui::ItemViewSelection sel(ui->treeWidget);
00056     sel.applyFrom(Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId()));
00057 }
00058 
00059 /*  
00060  *  Destroys the object and frees any allocated resources
00061  */
00062 DlgRevolution::~DlgRevolution()
00063 {
00064     // no need to delete child widgets, Qt does it all for us
00065     delete ui;
00066 }
00067 
00068 void DlgRevolution::directionActivated(int index)
00069 {
00070     ui->directionActivated(this, index);
00071 }
00072 
00073 Base::Vector3f DlgRevolution::getDirection() const
00074 {
00075     return ui->getDirection();
00076 }
00077 
00078 void DlgRevolution::changeEvent(QEvent *e)
00079 {
00080     if (e->type() == QEvent::LanguageChange) {
00081         ui->retranslate(this);
00082     }
00083     else {
00084         QDialog::changeEvent(e);
00085     }
00086 }
00087 
00088 void DlgRevolution::findShapes()
00089 {
00090     App::Document* activeDoc = App::GetApplication().getActiveDocument();
00091     if (!activeDoc) return;
00092     Gui::Document* activeGui = Gui::Application::Instance->getDocument(activeDoc);
00093 
00094     std::vector<App::DocumentObject*> objs = activeDoc->getObjectsOfType
00095         (Part::Feature::getClassTypeId());
00096     for (std::vector<App::DocumentObject*>::iterator it = objs.begin(); it!=objs.end(); ++it) {
00097         const TopoDS_Shape& shape = static_cast<Part::Feature*>(*it)->Shape.getValue();
00098         if (shape.IsNull()) continue;
00099 
00100         TopExp_Explorer xp;
00101         xp.Init(shape,TopAbs_SOLID);
00102         if (xp.More()) continue; // solids not allowed
00103         xp.Init(shape,TopAbs_COMPSOLID);
00104         if (xp.More()) continue; // compound solids not allowed
00105         // So allowed are: vertex, edge, wire, face, shell and compound
00106         QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeWidget);
00107         item->setText(0, QString::fromUtf8((*it)->Label.getValue()));
00108         item->setData(0, Qt::UserRole, QString::fromAscii((*it)->getNameInDocument()));
00109         Gui::ViewProvider* vp = activeGui->getViewProvider(*it);
00110         if (vp) item->setIcon(0, vp->getIcon());
00111     }
00112 }
00113 
00114 void DlgRevolution::accept()
00115 {
00116     if (ui->treeWidget->selectedItems().isEmpty()) {
00117         QMessageBox::critical(this, windowTitle(), 
00118             tr("Select a shape for revolution, first."));
00119         return;
00120     }
00121 
00122     Gui::WaitCursor wc;
00123     App::Document* activeDoc = App::GetApplication().getActiveDocument();
00124     activeDoc->openTransaction("Revolve");
00125 
00126     QString shape, type, name;
00127     QList<QTreeWidgetItem *> items = ui->treeWidget->selectedItems();
00128     for (QList<QTreeWidgetItem *>::iterator it = items.begin(); it != items.end(); ++it) {
00129         shape = (*it)->data(0, Qt::UserRole).toString();
00130         type = QString::fromAscii("Part::Revolution");
00131         name = QString::fromAscii(activeDoc->getUniqueObjectName("Revolve").c_str());
00132         Base::Vector3f axis = this->getDirection();
00133 
00134         QString code = QString::fromAscii(
00135             "FreeCAD.ActiveDocument.addObject(\"%1\",\"%2\")\n"
00136             "FreeCAD.ActiveDocument.%2.Source = FreeCAD.ActiveDocument.%3\n"
00137             "FreeCAD.ActiveDocument.%2.Axis = (%4,%5,%6)\n"
00138             "FreeCAD.ActiveDocument.%2.Base = (%7,%8,%9)\n"
00139             "FreeCAD.ActiveDocument.%2.Angle = %10\n"
00140             "FreeCADGui.ActiveDocument.%3.Visibility = False\n")
00141             .arg(type).arg(name).arg(shape)
00142             .arg(axis.x,0,'f',2)
00143             .arg(axis.y,0,'f',2)
00144             .arg(axis.z,0,'f',2)
00145             .arg(ui->baseX->value(),0,'f',2)
00146             .arg(ui->baseY->value(),0,'f',2)
00147             .arg(ui->baseZ->value(),0,'f',2)
00148             .arg(ui->angle->value(),0,'f',2);
00149         Gui::Application::Instance->runPythonCode((const char*)code.toAscii());
00150         QByteArray to = name.toAscii();
00151         QByteArray from = shape.toAscii();
00152         Gui::Command::copyVisual(to, "ShapeColor", from);
00153         Gui::Command::copyVisual(to, "LineColor", from);
00154         Gui::Command::copyVisual(to, "PointColor", from);
00155     }
00156 
00157     activeDoc->commitTransaction();
00158     activeDoc->recompute();
00159     QDialog::accept();
00160 }
00161 
00162 // ---------------------------------------
00163 
00164 TaskRevolution::TaskRevolution()
00165 {
00166     widget = new DlgRevolution();
00167     taskbox = new Gui::TaskView::TaskBox(
00168         Gui::BitmapFactory().pixmap("Part_Revolve"),
00169         widget->windowTitle(), true, 0);
00170     taskbox->groupLayout()->addWidget(widget);
00171     Content.push_back(taskbox);
00172 }
00173 
00174 TaskRevolution::~TaskRevolution()
00175 {
00176     // automatically deleted in the sub-class
00177 }
00178 
00179 bool TaskRevolution::accept()
00180 {
00181     widget->accept();
00182     return (widget->result() == QDialog::Accepted);
00183 }
00184 
00185 #include "moc_DlgRevolution.cpp"

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