AppDrawingGuiPy.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2006 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 #include "PreCompiled.h"
00024 #ifndef _PreComp_
00025 # include <QIcon>
00026 # include <QImage>
00027 # include <sstream>
00028 #endif
00029 
00030 #include "DrawingView.h"
00031 #include <Mod/Drawing/App/FeaturePage.h>
00032 
00033 #include <Base/Console.h>
00034 #include <Base/Exception.h>
00035 #include <Base/FileInfo.h>
00036 #include <Base/Stream.h>
00037 #include <App/Application.h>
00038 #include <App/DocumentObjectPy.h>
00039 #include <Gui/MainWindow.h>
00040 #include <Gui/BitmapFactory.h>
00041 
00042 using namespace DrawingGui;
00043 
00044 
00045 /* module functions */
00046 static PyObject * 
00047 open(PyObject *self, PyObject *args) 
00048 {
00049     const char* Name;
00050     if (!PyArg_ParseTuple(args, "s",&Name))
00051         return NULL; 
00052     
00053     PY_TRY {
00054         Base::FileInfo file(Name);
00055         if (file.hasExtension("svg") || file.hasExtension("svgz")) {
00056             QString fileName = QString::fromUtf8(Name);
00057             // Displaying the image in a view
00058             DrawingView* view = new DrawingView(Gui::getMainWindow());
00059             view->load(fileName);
00060             view->setWindowIcon(Gui::BitmapFactory().pixmap("actions/drawing-landscape"));
00061             view->setWindowTitle(QObject::tr("Drawing viewer"));
00062             view->resize( 400, 300 );
00063             Gui::getMainWindow()->addWindow(view);
00064         }
00065         else {
00066             PyErr_SetString(PyExc_Exception, "unknown filetype");
00067             return NULL;
00068         }
00069     } PY_CATCH;
00070 
00071     Py_Return; 
00072 }
00073 
00074 /* module functions */
00075 static PyObject *
00076 importer(PyObject *self, PyObject *args)
00077 {
00078     const char* Name;
00079     const char* dummy;
00080     if (!PyArg_ParseTuple(args, "s|s",&Name,&dummy))
00081         return NULL; 
00082     
00083     PY_TRY {
00084         Base::FileInfo file(Name);
00085         if (file.hasExtension("svg") || file.hasExtension("svgz")) {
00086             QString fileName = QString::fromUtf8(Name);
00087             // Displaying the image in a view
00088             DrawingView* view = new DrawingView(Gui::getMainWindow());
00089             view->load(fileName);
00090             view->setWindowIcon(Gui::BitmapFactory().pixmap("actions/drawing-landscape"));
00091             view->setWindowTitle(QObject::tr("Drawing viewer"));
00092             view->resize( 400, 300 );
00093             Gui::getMainWindow()->addWindow(view);
00094         } else {
00095             PyErr_SetString(PyExc_Exception, "unknown filetype");
00096             return NULL;
00097         }
00098     } PY_CATCH;
00099 
00100     Py_Return; 
00101 }
00102 
00103 static PyObject * 
00104 exporter(PyObject *self, PyObject *args)
00105 {
00106     PyObject* object;
00107     const char* filename;
00108     if (!PyArg_ParseTuple(args, "Os",&object,&filename))
00109         return NULL;
00110 
00111     PY_TRY {
00112         Py::List list(object);
00113         for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
00114             PyObject* item = (*it).ptr();
00115             if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) {
00116                 App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(item)->getDocumentObjectPtr();
00117                 if (obj->getTypeId().isDerivedFrom(Drawing::FeaturePage::getClassTypeId())) {
00118                     std::string fn = static_cast<Drawing::FeaturePage*>(obj)->PageResult.getValue();
00119                     Base::FileInfo fi_in(fn);
00120                     Base::ifstream str_in(fi_in, std::ios::in | std::ios::binary);
00121                     if (!str_in) {
00122                         std::stringstream str;
00123                         str << "Cannot open file '" << fn << "' for reading";
00124                         PyErr_SetString(PyExc_IOError, str.str().c_str());
00125                         return NULL;
00126                     }
00127 
00128                     Base::FileInfo fi_out(filename);
00129                     Base::ofstream str_out(fi_out, std::ios::out | std::ios::binary);
00130                     if (!str_out) {
00131                         std::stringstream str;
00132                         str << "Cannot open file '" << filename << "' for writing";
00133                         PyErr_SetString(PyExc_IOError, str.str().c_str());
00134                         return NULL;
00135                     }
00136 
00137                     str_in >> str_out.rdbuf();
00138                     str_in.close();
00139                     str_out.close();
00140                     break;
00141                 }
00142             }
00143         }
00144     } PY_CATCH;
00145 
00146     Py_Return;
00147 }
00148 
00149 /* registration table  */
00150 struct PyMethodDef DrawingGui_Import_methods[] = {
00151     {"open"     ,open ,     METH_VARARGS}, /* method name, C func ptr, always-tuple */
00152     {"insert"   ,importer,  METH_VARARGS},
00153     {"export"   ,exporter,  METH_VARARGS},
00154     {NULL, NULL}                    /* end of table marker */
00155 };

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