Mirroring.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 <QMessageBox>
00027 # include <QRegExp>
00028 # include <QTreeWidget>
00029 # include <TopoDS_Shape.hxx>
00030 # include <TopExp_Explorer.hxx>
00031 # include <boost/bind.hpp>
00032 # include <cfloat>
00033 #endif
00034 
00035 #include "Mirroring.h"
00036 #include "ui_Mirroring.h"
00037 #include "../App/PartFeature.h"
00038 #include <Base/Exception.h>
00039 #include <App/Application.h>
00040 #include <App/Document.h>
00041 #include <App/DocumentObject.h>
00042 #include <Gui/Application.h>
00043 #include <Gui/BitmapFactory.h>
00044 #include <Gui/Command.h>
00045 #include <Gui/Document.h>
00046 #include <Gui/Selection.h>
00047 #include <Gui/Utilities.h>
00048 #include <Gui/ViewProvider.h>
00049 #include <Gui/WaitCursor.h>
00050 
00051 using namespace PartGui;
00052 
00053 /* TRANSLATOR PartGui::Mirroring */
00054 
00055 Mirroring::Mirroring(QWidget* parent)
00056   : QWidget(parent), ui(new Ui_Mirroring)
00057 {
00058     ui->setupUi(this);
00059     ui->baseX->setRange(-DBL_MAX, DBL_MAX);
00060     ui->baseY->setRange(-DBL_MAX, DBL_MAX);
00061     ui->baseZ->setRange(-DBL_MAX, DBL_MAX);
00062     findShapes();
00063 
00064     Gui::ItemViewSelection sel(ui->shapes);
00065     sel.applyFrom(Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId()));
00066 }
00067 
00068 /*  
00069  *  Destroys the object and frees any allocated resources
00070  */
00071 Mirroring::~Mirroring()
00072 {
00073     // no need to delete child widgets, Qt does it all for us
00074     delete ui;
00075 }
00076 
00077 void Mirroring::changeEvent(QEvent *e)
00078 {
00079     if (e->type() == QEvent::LanguageChange) {
00080         ui->retranslateUi(this);
00081     }
00082     QWidget::changeEvent(e);
00083 }
00084 
00085 void Mirroring::findShapes()
00086 {
00087     App::Document* activeDoc = App::GetApplication().getActiveDocument();
00088     if (!activeDoc) return;
00089     Gui::Document* activeGui = Gui::Application::Instance->getDocument(activeDoc);
00090     if (!activeGui) return;
00091 
00092     this->document = QString::fromAscii(activeDoc->getName());
00093     std::vector<App::DocumentObject*> objs = activeDoc->getObjectsOfType
00094         (Part::Feature::getClassTypeId());
00095 
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()) {
00099             QString label = QString::fromUtf8((*it)->Label.getValue());
00100             QString name = QString::fromAscii((*it)->getNameInDocument());
00101             
00102             QTreeWidgetItem* child = new QTreeWidgetItem();
00103             child->setText(0, label);
00104             child->setToolTip(0, label);
00105             child->setData(0, Qt::UserRole, name);
00106             Gui::ViewProvider* vp = activeGui->getViewProvider(*it);
00107             if (vp) child->setIcon(0, vp->getIcon());
00108             ui->shapes->addTopLevelItem(child);
00109         }
00110     }
00111 }
00112 
00113 bool Mirroring::accept()
00114 {
00115     if (ui->shapes->selectedItems().isEmpty()) {
00116         QMessageBox::critical(this, windowTitle(),
00117             tr("Select a shape for mirroring, first."));
00118         return false;
00119     }
00120 
00121     App::Document* activeDoc = App::GetApplication().getDocument((const char*)this->document.toAscii());
00122     if (!activeDoc) {
00123         QMessageBox::critical(this, windowTitle(),
00124             tr("No such document '%1'.").arg(this->document));
00125         return false;
00126     }
00127 
00128     Gui::WaitCursor wc;
00129     unsigned int count = activeDoc->countObjectsOfType(Base::Type::fromName("Part::Mirroring"));
00130     activeDoc->openTransaction("Mirroring");
00131 
00132     QString shape, label;
00133     QRegExp rx(QString::fromAscii(" \\(Mirror #\\d+\\)$"));
00134     QList<QTreeWidgetItem *> items = ui->shapes->selectedItems();
00135     float normx=0, normy=0, normz=0;
00136     int index = ui->comboBox->currentIndex();
00137     if (index == 0)
00138         normz = 1.0f;
00139     else if (index == 1)
00140         normy = 1.0f;
00141     else
00142         normx = 1.0f;
00143     double basex = ui->baseX->value();
00144     double basey = ui->baseY->value();
00145     double basez = ui->baseZ->value();
00146     for (QList<QTreeWidgetItem *>::iterator it = items.begin(); it != items.end(); ++it) {
00147         shape = (*it)->data(0, Qt::UserRole).toString();
00148         label = (*it)->text(0);
00149 
00150         // if we already have the suffix " (Mirror #<number>)" remove it
00151         int pos = label.indexOf(rx);
00152         if (pos > -1)
00153             label = label.left(pos);
00154         label.append(QString::fromAscii(" (Mirror #%1)").arg(++count));
00155 
00156         QString code = QString::fromAscii(
00157             "__doc__=FreeCAD.getDocument(\"%1\")\n"
00158             "__doc__.addObject(\"Part::Mirroring\")\n"
00159             "__doc__.ActiveObject.Source=__doc__.getObject(\"%2\")\n"
00160             "__doc__.ActiveObject.Label=\"%3\"\n"
00161             "__doc__.ActiveObject.Normal=(%4,%5,%6)\n"
00162             "__doc__.ActiveObject.Base=(%7,%8,%9)\n"
00163             "del __doc__")
00164             .arg(this->document).arg(shape).arg(label)
00165             .arg(normx).arg(normy).arg(normz)
00166             .arg(basex).arg(basey).arg(basez);
00167         Gui::Application::Instance->runPythonCode((const char*)code.toAscii());
00168         QByteArray from = shape.toAscii();
00169         Gui::Command::copyVisual("ActiveObject", "ShapeColor", from);
00170         Gui::Command::copyVisual("ActiveObject", "LineColor", from);
00171         Gui::Command::copyVisual("ActiveObject", "PointColor", from);
00172     }
00173 
00174     activeDoc->commitTransaction();
00175     activeDoc->recompute();
00176     return true;
00177 }
00178 
00179 // ---------------------------------------
00180 
00181 TaskMirroring::TaskMirroring()
00182 {
00183     widget = new Mirroring();
00184     taskbox = new Gui::TaskView::TaskBox(
00185         Gui::BitmapFactory().pixmap("Part_MirrorPNG"),
00186         widget->windowTitle(), false, 0);
00187     taskbox->groupLayout()->addWidget(widget);
00188     Content.push_back(taskbox);
00189 }
00190 
00191 TaskMirroring::~TaskMirroring()
00192 {
00193     // automatically deleted in the sub-class
00194 }
00195 
00196 bool TaskMirroring::accept()
00197 {
00198     return widget->accept();
00199 }
00200 
00201 #include "moc_Mirroring.cpp"

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