SketchPyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2010     *
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 
00025 #include <Base/VectorPy.h>
00026 
00027 #include <Mod/Part/App/Geometry.h>
00028 #include <Mod/Part/App/GeometryCurvePy.h>
00029 #include <Mod/Part/App/LinePy.h>
00030 #include <Mod/Part/App/TopoShapePy.h>
00031 
00032 #include "Sketch.h"
00033 #include "Constraint.h"
00034 #include "ConstraintPy.h"
00035 
00036 // inclusion of the generated files (generated out of SketchPy.xml)
00037 #include "SketchPy.h"
00038 #include "SketchPy.cpp"
00039 
00040 using namespace Sketcher;
00041 using namespace Part;
00042 
00043 // returns a string which represents the object e.g. when printed in python
00044 std::string SketchPy::representation(void) const
00045 {
00046     return std::string("<Sketch object>");
00047 }
00048 
00049 PyObject *SketchPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00050 {
00051     // create a new instance of SketchPy and the Twin object 
00052     return new SketchPy(new Sketch());
00053 }
00054 
00055 // constructor method
00056 int SketchPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
00057 {
00058     return 0;
00059 }
00060 
00061 // +++ methodes implementer ++++++++++++++++++++++++++++++++++++++++++++++++
00062 
00063 PyObject* SketchPy::solve(PyObject *args)
00064 {
00065     return Py::new_reference_to(Py::Int(getSketchPtr()->solve()));
00066 }
00067 
00068 PyObject* SketchPy::addGeometry(PyObject *args)
00069 {
00070     PyObject *pcObj;
00071     if (!PyArg_ParseTuple(args, "O", &pcObj))
00072         return 0;
00073 
00074     if (PyObject_TypeCheck(pcObj, &(LinePy::Type))) {
00075         GeomLineSegment *line = static_cast<LinePy*>(pcObj)->getGeomLineSegmentPtr();
00076         return Py::new_reference_to(Py::Int(this->getSketchPtr()->addGeometry(line->clone())));
00077     }
00078     Py_Return; 
00079 }
00080 
00081 PyObject* SketchPy::addConstraint(PyObject *args)
00082 {
00083     int ret = -1;
00084     PyObject *pcObj;
00085     if (!PyArg_ParseTuple(args, "O", &pcObj))
00086         return 0;
00087 
00088     if (PyList_Check(pcObj)) {
00089         Py_ssize_t nSize = PyList_Size(pcObj);
00090         std::vector<Constraint*> values;
00091         values.resize(nSize);
00092 
00093         for (Py_ssize_t i=0; i<nSize;++i) {
00094             PyObject* item = PyList_GetItem(pcObj, i);
00095             if (!PyObject_TypeCheck(item, &(ConstraintPy::Type))) {
00096                 std::string error = std::string("types in list must be 'Constraint', not ");
00097                 error += item->ob_type->tp_name;
00098                 throw Py::TypeError(error);
00099             }
00100 
00101             values[i] = static_cast<ConstraintPy*>(item)->getConstraintPtr();
00102         }
00103 
00104         ret = getSketchPtr()->addConstraints(values);
00105     }
00106     else if(PyObject_TypeCheck(pcObj, &(ConstraintPy::Type))) {
00107         ConstraintPy  *pcObject = static_cast<ConstraintPy*>(pcObj);
00108         ret = getSketchPtr()->addConstraint(pcObject->getConstraintPtr());
00109     }
00110     else {
00111         std::string error = std::string("type must be 'Constraint' or list of 'Constraint', not ");
00112         error += pcObj->ob_type->tp_name;
00113         throw Py::TypeError(error);
00114     }
00115 
00116     return Py::new_reference_to(Py::Int(ret));
00117 
00118 }
00119 
00120 PyObject* SketchPy::clear(PyObject *args)
00121 {
00122     int index;
00123     if (!PyArg_ParseTuple(args, "i", &index))
00124         return 0;
00125 
00126     return Py::new_reference_to(Py::Int(getSketchPtr()->addVerticalConstraint(index)));
00127 }
00128 
00129 PyObject* SketchPy::movePoint(PyObject *args)
00130 {
00131     int index1,index2;
00132     PyObject *pcObj;
00133     int relative=0;
00134     if (!PyArg_ParseTuple(args, "iiO!|i", &index1,&index2,&(Base::VectorPy::Type),&pcObj,&relative))
00135         return 0;
00136     Base::Vector3d* toPoint = static_cast<Base::VectorPy*>(pcObj)->getVectorPtr();
00137 
00138     return Py::new_reference_to(Py::Int(getSketchPtr()->movePoint(index1,(Sketcher::PointPos)index2,*toPoint,(relative>0))));
00139 }
00140 
00141 // +++ attributes implementer ++++++++++++++++++++++++++++++++++++++++++++++++
00142 
00143 Py::Int SketchPy::getConstraint(void) const
00144 {
00145     //return Py::Int();
00146     throw Py::AttributeError("Not yet implemented");
00147 }
00148 
00149 Py::Tuple SketchPy::getConstraints(void) const
00150 {
00151     //return Py::Tuple();
00152     throw Py::AttributeError("Not yet implemented");
00153 }
00154 
00155 Py::Tuple SketchPy::getGeometries(void) const
00156 {
00157     return getSketchPtr()->getPyGeometry();
00158 }
00159 
00160 Py::Object SketchPy::getShape(void) const
00161 {
00162     return Py::Object(new TopoShapePy(new TopoShape(getSketchPtr()->toShape())));
00163 }
00164 
00165 
00166 // +++ custom attributes implementer ++++++++++++++++++++++++++++++++++++++++
00167 
00168 
00169 PyObject *SketchPy::getCustomAttributes(const char* /*attr*/) const
00170 {
00171     return 0;
00172 }
00173 
00174 int SketchPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00175 {
00176     return 0; 
00177 }
00178 
00179 

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