AppSandboxGui.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 <Standard_math.hxx>
00027 # include <Python.h>
00028 # include <Inventor/nodes/SoLineSet.h>
00029 # include <Inventor/nodes/SoBaseColor.h>
00030 # include <Inventor/nodes/SoSeparator.h>
00031 # include <Inventor/nodes/SoCoordinate3.h>
00032 #endif
00033 
00034 #include <Base/Console.h>
00035 #include <Base/Interpreter.h>
00036 #include <Base/GeometryPyCXX.h>
00037 #include <Base/VectorPy.h>
00038 #include <CXX/Extensions.hxx>
00039 #include <CXX/Objects.hxx>
00040 #include <App/Application.h>
00041 #include <App/Document.h>
00042 #include <App/DocumentObserver.h>
00043 #include <Gui/Document.h>
00044 #include <Gui/View3DInventor.h>
00045 #include <Gui/View3DInventorViewer.h>
00046 #include <Gui/Application.h>
00047 #include <Mod/Part/App/PropertyGeometryList.h>
00048 
00049 #include "Workbench.h"
00050 
00051 // use a different name to CreateCommand()
00052 void CreateSandboxCommands(void);
00053 
00054 
00055 /* module functions */
00056 
00057 class ObjectObserver : public App::DocumentObserver
00058 {
00059 public:
00060     ObjectObserver(App::DocumentObject* o, SoCoordinate3* c) : object(o), coords(c), radius(25.0)
00061     {
00062     }
00063 private:
00064     void slotCreatedDocument(const App::Document& Doc)
00065     {
00066     }
00067     void slotDeletedDocument(const App::Document& Doc)
00068     {
00069     }
00070     void slotCreatedObject(const App::DocumentObject& Obj)
00071     {
00072     }
00073     void slotDeletedObject(const App::DocumentObject& Obj)
00074     {
00075     }
00076     void slotChangedObject(const App::DocumentObject& Obj, const App::Property& Prop)
00077     {
00078         if (object == &Obj && Prop.getTypeId() == Part::PropertyGeometryList::getClassTypeId()) {
00079             const Part::PropertyGeometryList& geom = static_cast<const Part::PropertyGeometryList&>(Prop);
00080             const std::vector<Part::Geometry*>& items = geom.getValues();
00081             if (items.size() != 2)
00082                 return;
00083             Part::Geometry* g1 = items[0];
00084             Part::Geometry* g2 = items[1];
00085             if (!g1 || g1->getTypeId() != Part::GeomArcOfCircle::getClassTypeId())
00086                 return;
00087             if (!g2 || g2->getTypeId() != Part::GeomLineSegment::getClassTypeId())
00088                 return;
00089             Part::GeomArcOfCircle* arc = static_cast<Part::GeomArcOfCircle*>(g1);
00090             Part::GeomLineSegment* seg = static_cast<Part::GeomLineSegment*>(g2);
00091 
00092             try {
00093                 Base::Vector3d m1 = arc->getCenter();
00094               //Base::Vector3d a3 = arc->getStartPoint();
00095                 Base::Vector3d a3 = arc->getEndPoint();
00096                 Base::Vector3d l1 = seg->getStartPoint();
00097                 Base::Vector3d l2 = seg->getEndPoint();
00098 #if 0
00099                 Py::Module pd("FilletArc");
00100                 Py::Callable method(pd.getAttr(std::string("makeFilletArc")));
00101                 Py::Callable vector(pd.getAttr(std::string("Vector")));
00102 
00103                 Py::Tuple xyz(3);
00104                 Py::Tuple args(6);
00105                 xyz.setItem(0, Py::Float(m1.x));
00106                 xyz.setItem(1, Py::Float(m1.y));
00107                 xyz.setItem(2, Py::Float(m1.z));
00108                 args.setItem(0,vector.apply(xyz));
00109 
00110                 xyz.setItem(0, Py::Float(a3.x));
00111                 xyz.setItem(1, Py::Float(a3.y));
00112                 xyz.setItem(2, Py::Float(a3.z));
00113                 args.setItem(1,vector.apply(xyz));
00114 
00115                 xyz.setItem(0, Py::Float(l2.x));
00116                 xyz.setItem(1, Py::Float(l2.y));
00117                 xyz.setItem(2, Py::Float(l2.z));
00118                 args.setItem(2,vector.apply(xyz));
00119 
00120                 xyz.setItem(0, Py::Float((float)0));
00121                 xyz.setItem(1, Py::Float((float)0));
00122                 xyz.setItem(2, Py::Float((float)1));
00123                 args.setItem(3,vector.apply(xyz));
00124 
00125                 args.setItem(4,Py::Float(radius));
00126                 args.setItem(5,Py::Int((int)0));
00127                 Py::Tuple ret(method.apply(args));
00128                 Py::Object S1(ret.getItem(0));
00129                 Py::Object S2(ret.getItem(1));
00130                 Py::Object M2(ret.getItem(2));
00131 
00132                 Base::Vector3d s1, s2, m2;
00133                 s1.x = (double)Py::Float(S1.getAttr("x"));
00134                 s1.y = (double)Py::Float(S1.getAttr("y"));
00135                 s1.z = (double)Py::Float(S1.getAttr("z"));
00136 
00137                 s2.x = (double)Py::Float(S2.getAttr("x"));
00138                 s2.y = (double)Py::Float(S2.getAttr("y"));
00139                 s2.z = (double)Py::Float(S2.getAttr("z"));
00140 
00141                 m2.x = (double)Py::Float(M2.getAttr("x"));
00142                 m2.y = (double)Py::Float(M2.getAttr("y"));
00143                 m2.z = (double)Py::Float(M2.getAttr("z"));
00144 
00145                 coords->point.set1Value(0, (float)s1.x,(float)s1.y,(float)s1.z);
00146                 coords->point.set1Value(1, (float)m2.x,(float)m2.y,(float)m2.z);
00147                 coords->point.set1Value(2, (float)s2.x,(float)s2.y,(float)s2.z);
00148 
00149                 Base::Console().Message("M1=<%.4f,%.4f>\n", m1.x,m1.y);
00150                 Base::Console().Message("M2=<%.4f,%.4f>\n", m2.x,m2.y);
00151                 Base::Console().Message("S1=<%.4f,%.4f>\n", s1.x,s1.y);
00152                 Base::Console().Message("S2=<%.4f,%.4f>\n", s2.x,s2.y);
00153                 Base::Console().Message("P=<%.4f,%.4f>\n", a3.x,a3.y);
00154                 Base::Console().Message("Q=<%.4f,%.4f>\n", l2.x,l2.y);
00155                 Base::Console().Message("\n");
00156 #else
00157                 Py::Module pd("PartDesign");
00158                 Py::Callable method(pd.getAttr(std::string("makeFilletArc")));
00159 
00160                 Py::Tuple args(6);
00161                 args.setItem(0,Py::Vector(m1));
00162                 args.setItem(1,Py::Vector(a3));
00163                 args.setItem(2,Py::Vector(l2));
00164                 args.setItem(3,Py::Vector(Base::Vector3d(0,0,1)));
00165                 args.setItem(4,Py::Float(radius));
00166               //args.setItem(5,Py::Int((int)0));
00167                 args.setItem(5,Py::Int((int)1));
00168                 Py::Tuple ret(method.apply(args));
00169                 Py::Vector S1(ret.getItem(0));
00170                 Py::Vector S2(ret.getItem(1));
00171                 Py::Vector M2(ret.getItem(2));
00172 
00173                 Base::Vector3d s1 = S1.toVector();
00174                 Base::Vector3d s2 = S2.toVector();
00175                 Base::Vector3d m2 = M2.toVector();
00176                 coords->point.set1Value(0, (float)s1.x,(float)s1.y,(float)s1.z);
00177                 coords->point.set1Value(1, (float)m2.x,(float)m2.y,(float)m2.z);
00178                 coords->point.set1Value(2, (float)s2.x,(float)s2.y,(float)s2.z);
00179 
00180                 Base::Console().Message("M1=<%.4f,%.4f>\n", m1.x,m1.y);
00181                 Base::Console().Message("M2=<%.4f,%.4f>\n", m2.x,m2.y);
00182                 Base::Console().Message("S1=<%.4f,%.4f>\n", s1.x,s1.y);
00183                 Base::Console().Message("S2=<%.4f,%.4f>\n", s2.x,s2.y);
00184                 Base::Console().Message("P=<%.4f,%.4f>\n", a3.x,a3.y);
00185                 Base::Console().Message("Q=<%.4f,%.4f>\n", l2.x,l2.y);
00186                 Base::Console().Message("\n");
00187 #endif
00188             }
00189             catch (Py::Exception&) {
00190                 Base::PyException e; // extract the Python error text
00191                 Base::Console().Error("%s\n", e.what());
00192             }
00193         }
00194     }
00195 
00196     App::DocumentObject* object;
00197     SoCoordinate3* coords;
00198     double radius;
00199 };
00200 
00201 class SandboxModuleGui : public Py::ExtensionModule<SandboxModuleGui>
00202 {
00203 
00204 public:
00205     SandboxModuleGui() : Py::ExtensionModule<SandboxModuleGui>("SandboxGui")
00206     {
00207         add_varargs_method("interactiveFilletArc",&SandboxModuleGui::interactiveFilletArc,
00208             "Interactive fillet arc");
00209         initialize("This module is the SandboxGui module"); // register with Python
00210     }
00211     
00212     virtual ~SandboxModuleGui() {}
00213 
00214 private:
00215     Py::Object interactiveFilletArc(const Py::Tuple& args)
00216     {
00217         Gui::Document* doc = Gui::Application::Instance->activeDocument();
00218         if (doc) {
00219             Gui::View3DInventor* view = qobject_cast<Gui::View3DInventor*>(doc->getActiveView());
00220             if (view) {
00221                 Gui::View3DInventorViewer* viewer = view->getViewer();
00222                 SoSeparator* scene = static_cast<SoSeparator*>(viewer->getSceneGraph());
00223                 SoSeparator* node = new SoSeparator();
00224                 SoBaseColor* rgb = new SoBaseColor();
00225                 rgb->rgb.setValue(1,1,0);
00226                 node->addChild(rgb);
00227                 SoCoordinate3* coords = new SoCoordinate3();
00228                 node->addChild(coords);
00229                 node->addChild(new SoLineSet());
00230                 scene->addChild(node);
00231 
00232                 ObjectObserver* obs = new ObjectObserver(doc->getDocument()->getActiveObject(), coords);
00233                 obs->attachDocument(doc->getDocument());
00234             }
00235         }
00236         return Py::None();
00237     }
00238 };
00239 
00240 
00241 /* Python entry */
00242 extern "C" {
00243 void SandboxGuiExport initSandboxGui()
00244 {
00245     if (!Gui::Application::Instance) {
00246         PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
00247         return;
00248     }
00249 
00250     // Load Python modules this module depends on
00251     try {
00252         Base::Interpreter().loadModule("Sandbox");
00253     }
00254     catch(const Base::Exception& e) {
00255         PyErr_SetString(PyExc_ImportError, e.what());
00256         return;
00257     }
00258 
00259     // instanciating the commands
00260     CreateSandboxCommands();
00261     SandboxGui::Workbench::init();
00262     SandboxGui::SoWidgetShape::initClass();
00263 
00264     // the following constructor call registers our extension module
00265     // with the Python runtime system
00266     (void)new SandboxModuleGui;
00267     Base::Console().Log("Loading GUI of Sandbox module... done\n");
00268 }
00269 
00270 } // extern "C"

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