Tessellation.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <TopExp_Explorer.hxx>
00027 #endif
00028
00029 #include "Tessellation.h"
00030 #include "ui_Tessellation.h"
00031 #include <Base/Console.h>
00032 #include <Base/Exception.h>
00033 #include <App/Application.h>
00034 #include <App/Document.h>
00035 #include <Gui/Application.h>
00036 #include <Gui/Command.h>
00037 #include <Gui/Document.h>
00038 #include <Gui/BitmapFactory.h>
00039 #include <Gui/ViewProvider.h>
00040 #include <Gui/WaitCursor.h>
00041 #include <Mod/Part/App/PartFeature.h>
00042
00043 using namespace MeshPartGui;
00044
00045
00046
00047 Tessellation::Tessellation(QWidget* parent)
00048 : QWidget(parent), ui(new Ui_Tessellation)
00049 {
00050 ui->setupUi(this);
00051 Gui::Command::doCommand(Gui::Command::Doc, "import MeshPart");
00052 findShapes();
00053 }
00054
00055 Tessellation::~Tessellation()
00056 {
00057 }
00058
00059 void Tessellation::changeEvent(QEvent *e)
00060 {
00061 if (e->type() == QEvent::LanguageChange) {
00062 ui->retranslateUi(this);
00063 }
00064 QWidget::changeEvent(e);
00065 }
00066
00067 void Tessellation::findShapes()
00068 {
00069 App::Document* activeDoc = App::GetApplication().getActiveDocument();
00070 if (!activeDoc) return;
00071 Gui::Document* activeGui = Gui::Application::Instance->getDocument(activeDoc);
00072 if (!activeGui) return;
00073
00074 this->document = QString::fromAscii(activeDoc->getName());
00075 std::vector<Part::Feature*> objs = activeDoc->getObjectsOfType<Part::Feature>();
00076
00077 for (std::vector<Part::Feature*>::iterator it = objs.begin(); it!=objs.end(); ++it) {
00078 const TopoDS_Shape& shape = (*it)->Shape.getValue();
00079 if (shape.IsNull()) continue;
00080 bool hasfaces = false;
00081 TopExp_Explorer xp(shape,TopAbs_FACE);
00082 while (xp.More()) {
00083 hasfaces = true;
00084 break;
00085 }
00086
00087 if (hasfaces) {
00088 QString label = QString::fromUtf8((*it)->Label.getValue());
00089 QString name = QString::fromAscii((*it)->getNameInDocument());
00090
00091 QTreeWidgetItem* child = new QTreeWidgetItem();
00092 child->setText(0, label);
00093 child->setToolTip(0, label);
00094 child->setData(0, Qt::UserRole, name);
00095 Gui::ViewProvider* vp = activeGui->getViewProvider(*it);
00096 if (vp) child->setIcon(0, vp->getIcon());
00097 ui->treeWidget->addTopLevelItem(child);
00098 }
00099 }
00100 }
00101
00102 bool Tessellation::accept()
00103 {
00104 if (ui->treeWidget->selectedItems().isEmpty()) {
00105 QMessageBox::critical(this, windowTitle(),
00106 tr("Select a shape for meshing, first."));
00107 return false;
00108 }
00109
00110 App::Document* activeDoc = App::GetApplication().getDocument((const char*)this->document.toAscii());
00111 if (!activeDoc) {
00112 QMessageBox::critical(this, windowTitle(),
00113 tr("No such document '%1'.").arg(this->document));
00114 return false;
00115 }
00116
00117 try {
00118 QString shape, label;
00119 Gui::WaitCursor wc;
00120
00121 activeDoc->openTransaction("Meshing");
00122 QList<QTreeWidgetItem *> items = ui->treeWidget->selectedItems();
00123 std::vector<Part::Feature*> shapes = Gui::Selection().getObjectsOfType<Part::Feature>();
00124 for (QList<QTreeWidgetItem *>::iterator it = items.begin(); it != items.end(); ++it) {
00125 shape = (*it)->data(0, Qt::UserRole).toString();
00126 label = (*it)->text(0);
00127
00128 QString cmd = QString::fromAscii(
00129 "__doc__=FreeCAD.getDocument(\"%1\")\n"
00130 "__mesh__=__doc__.addObject(\"Mesh::Feature\",\"Mesh\")\n"
00131 "__mesh__.Mesh=MeshPart.meshFromShape(__doc__.getObject(\"%2\").Shape,%3,0,0,%4)\n"
00132 "__mesh__.Label=\"%5 (Meshed)\"\n"
00133 "del __doc__, __mesh__\n")
00134 .arg(this->document)
00135 .arg(shape)
00136 .arg(ui->spinMaxEdgeLength->value())
00137 .arg(ui->spinDeviation->value())
00138 .arg(label);
00139 Gui::Command::doCommand(Gui::Command::Doc, (const char*)cmd.toAscii());
00140 }
00141 activeDoc->commitTransaction();
00142 }
00143 catch (const Base::Exception& e) {
00144 Base::Console().Error(e.what());
00145 }
00146
00147 return true;
00148 }
00149
00150
00151
00152 TaskTessellation::TaskTessellation()
00153 {
00154 widget = new Tessellation();
00155 Gui::TaskView::TaskBox* taskbox = new Gui::TaskView::TaskBox(
00156 QPixmap(),
00157 widget->windowTitle(), true, 0);
00158 taskbox->groupLayout()->addWidget(widget);
00159 Content.push_back(taskbox);
00160 }
00161
00162 TaskTessellation::~TaskTessellation()
00163 {
00164
00165 }
00166
00167 void TaskTessellation::open()
00168 {
00169 }
00170
00171 void TaskTessellation::clicked(int)
00172 {
00173 }
00174
00175 bool TaskTessellation::accept()
00176 {
00177 return widget->accept();
00178 }
00179
00180 bool TaskTessellation::reject()
00181 {
00182 return true;
00183 }
00184
00185 #include "moc_Tessellation.cpp"