DocumentThread.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Werner Mayer <wmayer@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 <QMutexLocker>
00027 #endif
00028 
00029 #include "DocumentThread.h"
00030 #include "DocumentProtector.h"
00031 
00032 #include <Base/Console.h>
00033 #include <Base/Sequencer.h>
00034 #include <Base/Interpreter.h>
00035 #include <App/Application.h>
00036 #include <App/Document.h>
00037 #include <Mod/Mesh/App/Mesh.h>
00038 
00039 using namespace Sandbox;
00040 
00041 
00042 DocumentThread::DocumentThread(QObject* parent)
00043   : QThread(parent)
00044 {
00045 }
00046 
00047 DocumentThread::~DocumentThread()
00048 {
00049 }
00050 
00051 void DocumentThread::run()
00052 {
00053     App::Document* doc = App::GetApplication().getActiveDocument();
00054     DocumentProtector dp(doc);
00055     App::DocumentObject* obj = dp.addObject("Mesh::Ellipsoid", (const char*)objectName().toAscii());
00056     dp.recompute();
00057 }
00058 
00059 // --------------------------------------
00060 
00061 WorkerThread::WorkerThread(QObject* parent)
00062   : QThread(parent)
00063 {
00064 }
00065 
00066 WorkerThread::~WorkerThread()
00067 {
00068 }
00069 
00070 void WorkerThread::run()
00071 {
00072 #ifdef FC_DEBUG
00073     int max = 10000;
00074 #else
00075     int max = 100000000;
00076 #endif
00077     Base::SequencerLauncher seq("Do something meaningful...", max);
00078     double val=0;
00079     for (int i=0; i<max; i++) {
00080         for (int j=0; j<max; j++) {
00081             val = sin(0.12345);
00082         }
00083         seq.next(true);
00084     }
00085 }
00086 
00087 // --------------------------------------
00088 
00089 QMutex PythonThread::mutex(QMutex::Recursive);
00090 
00091 PythonThread::PythonThread(QObject* parent)
00092   : QThread(parent)
00093 {
00094 }
00095 
00096 PythonThread::~PythonThread()
00097 {
00098 }
00099 
00100 void PythonThread::run()
00101 {
00102     QMutexLocker mutex_lock(&mutex);
00103     Base::PyGILStateLocker locker;
00104     try {
00105 #if 0
00106         PyObject *module, *dict;
00107         module = PyImport_AddModule("__main__");
00108         dict = PyModule_GetDict(module);
00109         PyObject* dict_copy = PyDict_Copy(dict);
00110 
00111         std::string buf;
00112         buf = std::string(
00113             "import Sandbox\n"
00114             "doc=App.ActiveDocument\n"
00115             "dp=Sandbox.DocumentProtector(doc)\n"
00116             "dp.addObject(\"Mesh::Ellipsoid\",\"Mesh\")\n"
00117             "dp.recompute()\n");
00118         PyObject *presult = PyRun_String(buf.c_str(), Py_file_input, dict_copy, dict_copy);
00119         Py_DECREF(dict_copy);
00120         msleep(10);
00121 #else
00122 
00123         Base::Interpreter().runString(
00124             "import Sandbox, Mesh, MeshGui\n"
00125             "doc=App.ActiveDocument\n"
00126             "dp=Sandbox.DocumentProtector(doc)\n"
00127             "dp.addObject(\"Mesh::Ellipsoid\",\"Mesh\")\n"
00128             "dp.recompute()\n");
00129         msleep(10);
00130 #endif
00131     }
00132     catch (const Base::PyException& e) {
00133         Base::Console().Error(e.what());
00134     }
00135 }
00136 
00137 // --------------------------------------
00138 
00139 MeshLoaderThread::MeshLoaderThread(const QString& fn, QObject* parent)
00140   : QThread(parent), filename(fn)
00141 {
00142 }
00143 
00144 MeshLoaderThread::~MeshLoaderThread()
00145 {
00146 }
00147 
00148 Base::Reference<Mesh::MeshObject> MeshLoaderThread::getMesh() const
00149 {
00150     return this->mesh;
00151 }
00152 
00153 void MeshLoaderThread::run()
00154 {
00155     this->mesh = new Mesh::MeshObject();
00156     this->mesh->load((const char*)filename.toUtf8());
00157 }
00158 
00159 // --------------------------------------
00160 
00161 PROPERTY_SOURCE(Sandbox::SandboxObject, App::DocumentObject)
00162 
00163 SandboxObject::SandboxObject()
00164 {
00165     ADD_PROPERTY(Integer,(4711)  );
00166 }
00167 
00168 SandboxObject::~SandboxObject()
00169 {
00170 }
00171 
00172 short SandboxObject::mustExecute(void) const
00173 {
00174     if (Integer.isTouched())
00175         return 1;
00176     return 0;
00177 }
00178 
00179 App::DocumentObjectExecReturn *SandboxObject::execute(void)
00180 {
00181     Base::Console().Message("SandboxObject::execute()\n");
00182     return 0;
00183 }
00184 
00185 void SandboxObject::onChanged(const App::Property* prop)
00186 {
00187     if (prop == &Integer)
00188         Base::Console().Message("SandboxObject::onChanged(%d)\n", Integer.getValue());
00189     App::DocumentObject::onChanged(prop);
00190 }
00191 
00192 void SandboxObject::setIntValue(int v)
00193 {
00194     Base::Console().Message("SandboxObject::setIntValue(%d)\n", v);
00195     Integer.setValue(v);
00196 }
00197 
00198 void SandboxObject::resetValue()
00199 {
00200     Base::Console().Message("SandboxObject::resetValue()\n");
00201     Integer.setValue(4711);
00202 }
00203 
00204 DocumentTestThread::DocumentTestThread(QObject* parent)
00205   : QThread(parent)
00206 {
00207 }
00208 
00209 DocumentTestThread::~DocumentTestThread()
00210 {
00211 }
00212 
00213 void DocumentTestThread::run()
00214 {
00215     Base::Console().Message("DocumentTestThread::run()\n");
00216     App::Document* doc = App::GetApplication().getActiveDocument();
00217     DocumentProtector dp(doc);
00218     SandboxObject* obj = static_cast<SandboxObject*>(dp.addObject("Sandbox::SandboxObject"));
00219 
00220     DocumentObjectProtector op(obj);
00221     App::PropertyString Name;Name.setValue("MyLabel");
00222     op.setProperty("Label", Name);
00223 
00224     App::PropertyInteger Int;Int.setValue(2);
00225     op.setProperty("Integer", Int);
00226     op.execute(CallableWithArgs<SandboxObject,int,&SandboxObject::setIntValue>(obj,4));
00227 
00228     dp.recompute();
00229     op.execute(Callable<SandboxObject,&SandboxObject::resetValue>(obj));
00230 }

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