AppRaytracingGuiPy.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 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 <gp_Vec.hxx>
00027 # include <QFileInfo>
00028 # include <Inventor/SoInput.h>
00029 # include <Inventor/nodes/SoNode.h>
00030 # include <Inventor/nodes/SoOrthographicCamera.h>
00031 # include <vector>
00032 # include <Inventor/nodes/SoPerspectiveCamera.h>
00033 #endif
00034 
00035 #include <Base/PyObjectBase.h>
00036 #include <Base/Interpreter.h>
00037 #include <Gui/Application.h>
00038 #include <Gui/EditorView.h>
00039 #include <Gui/TextEdit.h>
00040 #include <Gui/MainWindow.h>
00041 #include <Gui/MainWindow.h>
00042 #include <Gui/View.h>
00043 
00044 #include <Mod/Raytracing/App/PovTools.h>
00045 
00046 #include "PovrayHighlighter.h"
00047 
00048 using namespace RaytracingGui;
00049 using namespace Raytracing;
00050 
00052 static PyObject *
00053 open(PyObject *self, PyObject *args)
00054 {
00055     // only used to open Povray files
00056     const char* Name;
00057     const char* DocName;
00058     if (!PyArg_ParseTuple(args, "s|s",&Name, &DocName))
00059         return NULL;
00060     PY_TRY {
00061         QString fileName = QString::fromUtf8(Name);
00062         QFileInfo fi;
00063         fi.setFile(fileName);
00064         QString ext = fi.completeSuffix().toLower();
00065         QList<Gui::EditorView*> views = Gui::getMainWindow()->findChildren<Gui::EditorView*>();
00066         for (QList<Gui::EditorView*>::Iterator it = views.begin(); it != views.end(); ++it) {
00067             if ((*it)->fileName() == fileName) {
00068                 (*it)->setFocus();
00069                 Py_Return;
00070             }
00071         }
00072 
00073         if (ext == QLatin1String("pov") || ext == QLatin1String("inc")) {
00074             Gui::TextEditor* editor = new Gui::TextEditor();
00075             editor->setSyntaxHighlighter(new PovrayHighlighter(editor));
00076             Gui::EditorView* edit = new Gui::EditorView(editor, Gui::getMainWindow());
00077             edit->open(fileName);
00078             edit->resize(400, 300);
00079             Gui::getMainWindow()->addWindow(edit);
00080         }
00081     } PY_CATCH;
00082 
00083     Py_Return;
00084 }
00085 
00087 static PyObject *
00088 povViewCamera(PyObject *self, PyObject *args)
00089 {
00090     // no arguments
00091     if (!PyArg_ParseTuple(args, ""))
00092         return NULL;
00093     PY_TRY {
00094                 std::string out;
00095                 const char* ppReturn=0;
00096 
00097                 Gui::Application::Instance->sendMsgToActiveView("GetCamera",&ppReturn);
00098 
00099                 SoNode* rootNode;
00100                 SoInput in;
00101                 in.setBuffer((void*)ppReturn,std::strlen(ppReturn));
00102             SoDB::read(&in,rootNode);
00103 
00104                 if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId()))
00105                         throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read "
00106                                                                   "camera information from ASCII stream....\n");
00107 
00108                 // root-node returned from SoDB::readAll() has initial zero
00109                 // ref-count, so reference it before we start using it to
00110                 // avoid premature destruction.
00111                 SoCamera * Cam = static_cast<SoCamera*>(rootNode);
00112                 Cam->ref();
00113 
00114                 SbRotation camrot = Cam->orientation.getValue();
00115 
00116                 SbVec3f upvec(0, 1, 0); // init to default up vector
00117                 camrot.multVec(upvec, upvec);
00118 
00119                 SbVec3f lookat(0, 0, -1); // init to default view direction vector
00120                 camrot.multVec(lookat, lookat);
00121 
00122                 SbVec3f pos = Cam->position.getValue();
00123                 float Dist = Cam->focalDistance.getValue();
00124 
00125                 // making gp out of the Coin stuff
00126                 gp_Vec gpPos(pos.getValue()[0],pos.getValue()[1],pos.getValue()[2]);
00127                 gp_Vec gpDir(lookat.getValue()[0],lookat.getValue()[1],lookat.getValue()[2]);
00128                 lookat *= Dist;
00129                 lookat += pos;
00130                 gp_Vec gpLookAt(lookat.getValue()[0],lookat.getValue()[1],lookat.getValue()[2]);
00131                 gp_Vec gpUp(upvec.getValue()[0],upvec.getValue()[1],upvec.getValue()[2]);
00132 
00133                 // call the write method of PovTools....
00134                 out = PovTools::getCamera(CamDef(gpPos,gpDir,gpLookAt,gpUp));
00135 
00136                 return Py::new_reference_to(Py::String(out));
00137     } PY_CATCH;
00138 }
00139 
00140 /* registration table  */
00141 struct PyMethodDef RaytracingGui_methods[] = {
00142     {"open"       ,open    ,METH_VARARGS,
00143      "open(string) -- Create a new text document and load the file into the document."},
00144     {"insert"     ,open    ,METH_VARARGS,
00145      "insert(string,string) -- Create a new text document and load the file into the document."},
00146     {"povViewCamera"     ,povViewCamera    ,METH_VARARGS,
00147      "string povViewCamera() -- returns the povray camera devinition of the active 3D view."},
00148     {NULL, NULL}
00149 };

Generated on Wed Nov 23 18:59:56 2011 for FreeCAD by  doxygen 1.6.1