AppCompleteGui.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Jürgen Riegel (juergen.riegel@web.de)              *
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 <Python.h>
00027 #endif
00028 
00029 #include <Base/Console.h>
00030 #include <Base/Interpreter.h>
00031 #include <Gui/Application.h>
00032 #include <Gui/WorkbenchManager.h>
00033 #include <Gui/Language/Translator.h>
00034 #include "Workbench.h"
00035 
00036 #include <Mod/Complete/App/CompleteConfiguration.h>
00037 
00038 
00039 // use a different name to CreateCommand()
00040 void CreateCompleteCommands(void);
00041 
00042 void loadCompleteResource()
00043 {
00044     // add resources and reloads the translators
00045     Q_INIT_RESOURCE(Complete);
00046     Gui::Translator::instance()->refresh();
00047 }
00048 
00049 /* registration table  */
00050 extern struct PyMethodDef CompleteGui_Import_methods[];
00051 
00052 
00053 /* Python entry */
00054 extern "C" {
00055 void CompleteGuiExport initCompleteGui()
00056 {
00057     if (!Gui::Application::Instance) {
00058         PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
00059         return;
00060     }
00061 
00062     // load dependent module
00063     try {
00064         Base::Interpreter().loadModule("PartGui");
00065         Base::Interpreter().loadModule("MeshGui");
00066         try {
00067             Base::Interpreter().loadModule("MeshPartGui");
00068         }
00069         catch (const Base::Exception& e) {
00070             Base::Console().Error("Failed to load MeshPartGui: %s\n", e.what());
00071             PyErr_Clear();
00072         }
00073         Base::Interpreter().loadModule("PointsGui");
00074         //Base::Interpreter().loadModule("MeshPartGui");
00075         //Base::Interpreter().loadModule("AssemblyGui");
00076         Base::Interpreter().loadModule("DrawingGui");
00077         Base::Interpreter().loadModule("RaytracingGui");
00078 #       ifdef COMPLETE_SHOW_SKETCHER
00079         Base::Interpreter().loadModule("SketcherGui");
00080 #       endif
00081         Base::Interpreter().loadModule("PartDesignGui");
00082         Base::Interpreter().loadModule("ImageGui");
00083         //Base::Interpreter().loadModule("CamGui");
00084         Base::Interpreter().loadModule("TestGui");
00085 #       ifdef COMPLETE_USE_DRAFTING
00086         Py::Module module(PyImport_ImportModule("FreeCADGui"),true);
00087         Py::Callable method(module.getAttr(std::string("getWorkbench")));
00088 
00089         // Get the CompleteWorkbench handler
00090         Py::Tuple args(1);
00091         args.setItem(0,Py::String("DraftWorkbench"));
00092         Py::Object handler(method.apply(args));
00093 
00094         std::string type;
00095         if (!handler.hasAttr(std::string("__Workbench__"))) {
00096             // call its GetClassName method if possible
00097             Py::Callable method(handler.getAttr(std::string("GetClassName")));
00098             Py::Tuple args;
00099             Py::String result(method.apply(args));
00100             type = result.as_std_string();
00101             if (type == "Gui::PythonWorkbench") {
00102                 Gui::Workbench* wb = Gui::WorkbenchManager::instance()->createWorkbench("DraftWorkbench", type);
00103                 handler.setAttr(std::string("__Workbench__"), Py::Object(wb->getPyObject(), true));
00104             }
00105 
00106             // import the matching module first
00107             Py::Callable activate(handler.getAttr(std::string("Initialize")));
00108             activate.apply(args);
00109         }
00110 
00111         // Get the CompleteWorkbench handler
00112         args.setItem(0,Py::String("CompleteWorkbench"));
00113 #       endif
00114     }
00115     catch(const Base::Exception& e) {
00116         PyErr_SetString(PyExc_ImportError, e.what());
00117         return;
00118     }
00119     catch (Py::Exception& e) {
00120         Py::Object o = Py::type(e);
00121         if (o.isString()) {
00122             Py::String s(o);
00123             Base::Console().Error("%s\n", s.as_std_string().c_str());
00124         }
00125         else {
00126             Py::String s(o.repr());
00127             Base::Console().Error("%s\n", s.as_std_string().c_str());
00128         }
00129         // Prints message to console window if we are in interactive mode
00130         PyErr_Print();
00131     }
00132 
00133     (void) Py_InitModule("CompleteGui", CompleteGui_Import_methods);   /* mod name, table ptr */
00134     Base::Console().Log("Loading GUI of Complete module... done\n");
00135 
00136     // instantiating the commands
00137     CreateCompleteCommands();
00138     CompleteGui::Workbench::init();
00139 
00140      // add resources and reloads the translators
00141     loadCompleteResource();
00142 }
00143 
00144 } // extern "C" {

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