SketchObjectPyImp.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 #ifndef _PreComp_
00025 # include <sstream>
00026 #endif
00027 
00028 #include "Mod/Sketcher/App/SketchObject.h"
00029 #include <Mod/Part/App/LinePy.h>
00030 #include <Mod/Part/App/Geometry.h>
00031 #include <Base/VectorPy.h>
00032 #include <App/Document.h>
00033 
00034 // inclusion of the generated files (generated out of SketchObjectSFPy.xml)
00035 #include "SketchObjectPy.h"
00036 #include "SketchObjectPy.cpp"
00037 // other python types
00038 #include "ConstraintPy.h"
00039 
00040 using namespace Sketcher;
00041 
00042 // returns a string which represents the object e.g. when printed in python
00043 std::string SketchObjectPy::representation(void) const
00044 {
00045     return "<Sketcher::SketchObject>";
00046 }
00047 
00048 
00049 PyObject* SketchObjectPy::solve(PyObject *args)
00050 {
00051     PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00052     return 0;
00053 }
00054 
00055 PyObject* SketchObjectPy::addGeometry(PyObject *args)
00056 {
00057     PyObject *pcObj;
00058     if (!PyArg_ParseTuple(args, "O", &pcObj))
00059         return 0;
00060 
00061     if (PyObject_TypeCheck(pcObj, &(Part::GeometryPy::Type))) {
00062         Part::Geometry *geo = static_cast<Part::GeometryPy*>(pcObj)->getGeometryPtr();
00063         return Py::new_reference_to(Py::Int(this->getSketchObjectPtr()->addGeometry(geo)));
00064     }
00065     Py_Return;
00066 }
00067 
00068 PyObject* SketchObjectPy::delGeometry(PyObject *args)
00069 {
00070     int Index;
00071     if (!PyArg_ParseTuple(args, "i", &Index))
00072         return 0;
00073 
00074     if (this->getSketchObjectPtr()->delGeometry(Index)) {
00075         std::stringstream str;
00076         str << "Not able to delete a geometry with the given index: " << Index;
00077         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00078         return 0;
00079     }
00080 
00081     Py_Return;
00082 }
00083 
00084 PyObject* SketchObjectPy::toggleConstruction(PyObject *args)
00085 {
00086     int Index;
00087     if (!PyArg_ParseTuple(args, "i", &Index))
00088         return 0;
00089 
00090     if (this->getSketchObjectPtr()->toggleConstruction(Index)) {
00091         std::stringstream str;
00092         str << "Not able to toggle a geometry with the given index: " << Index;
00093         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00094         return 0;
00095     }
00096 
00097     Py_Return;
00098 }
00099 
00100 PyObject* SketchObjectPy::addConstraint(PyObject *args)
00101 {
00102     PyObject *pcObj;
00103     if (!PyArg_ParseTuple(args, "O", &pcObj))
00104         return 0;
00105 
00106     if (PyObject_TypeCheck(pcObj, &(Sketcher::ConstraintPy::Type))) {
00107         Sketcher::Constraint *constr = static_cast<Sketcher::ConstraintPy*>(pcObj)->getConstraintPtr();
00108         return Py::new_reference_to(Py::Int(this->getSketchObjectPtr()->addConstraint(constr)));
00109     }
00110     Py_Return;
00111 }
00112 
00113 PyObject* SketchObjectPy::delConstraint(PyObject *args)
00114 {
00115     int Index;
00116     if (!PyArg_ParseTuple(args, "i", &Index))
00117         return 0;
00118 
00119     if (this->getSketchObjectPtr()->delConstraint(Index)) {
00120         std::stringstream str;
00121         str << "Not able to delete a constraint with the given index: " << Index;
00122         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00123         return 0;
00124     }
00125 
00126     Py_Return;
00127 }
00128 
00129 PyObject* SketchObjectPy::addExternal(PyObject *args)
00130 {
00131     char *ObjectName;
00132     char *SubName;
00133     if (!PyArg_ParseTuple(args, "ss:Give an object and subelement name", &ObjectName,&SubName))
00134         return 0;
00135 
00136     // get the target object for the external link
00137     App::DocumentObject * Obj = this->getSketchObjectPtr()->getDocument()->getObject(ObjectName);
00138     if (!Obj) {
00139         std::stringstream str;
00140         str << ObjectName << "does not exist in the document";
00141         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00142         return 0;
00143     }
00144     // check if its belong to the sketch support
00145     if (this->getSketchObjectPtr()->Support.getValue() != Obj) {
00146         std::stringstream str;
00147         str << ObjectName << "is not supported by this sketch";
00148         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00149         return 0;
00150     }
00151 
00152     // add the external
00153     if (this->getSketchObjectPtr()->addExternal(Obj,SubName)) {
00154         std::stringstream str;
00155         str << "Not able to add external shape element";
00156         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00157         return 0;
00158     }
00159 
00160     Py_Return;
00161 }
00162 
00163 PyObject* SketchObjectPy::delExternal(PyObject *args)
00164 {
00165     PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00166     return 0;
00167 }
00168 
00169 PyObject* SketchObjectPy::delConstraintOnPoint(PyObject *args)
00170 {
00171     int Index;
00172     if (!PyArg_ParseTuple(args, "i", &Index))
00173         return 0;
00174 
00175     if (this->getSketchObjectPtr()->delConstraintOnPoint(Index)) {
00176         std::stringstream str;
00177         str << "Not able to delete a constraint on point with the given index: " << Index;
00178         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00179         return 0;
00180     }
00181 
00182     Py_Return;
00183 }
00184 
00185 PyObject* SketchObjectPy::setDatum(PyObject *args)
00186 {
00187     double Datum;
00188     int    Index;
00189     if (!PyArg_ParseTuple(args, "id", &Index, &Datum))
00190         return 0;
00191 
00192     int err=this->getSketchObjectPtr()->setDatum(Index, Datum);
00193     if (err) {
00194         std::stringstream str;
00195         if (err == -1)
00196             str << "Invalid constraint index: " << Index;
00197         else if (err == -3)
00198             str << "Cannot set the datum because the sketch contains conflicting constraints";
00199         else if (err == -2)
00200             str << "Datum " << Datum << " for the constraint with index " << Index << " is invalid";
00201         else if (err == -4)
00202             str << "Negative datum values are not valid for the constraint with index " << Index;
00203         else if (err == -5)
00204             str << "Zero is not a valid datum for the constraint with index " << Index;
00205         else
00206             str << "Unexpected problem at setting datum " << Datum << " for the constraint with index " << Index;
00207         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00208         return 0;
00209     }
00210 
00211     Py_Return;
00212 }
00213 
00214 PyObject* SketchObjectPy::movePoint(PyObject *args)
00215 {
00216     PyObject *pcObj;
00217     int GeoId, PointType;
00218     int relative=0;
00219 
00220     if (!PyArg_ParseTuple(args, "iiO!|i", &GeoId, &PointType, &(Base::VectorPy::Type), &pcObj, &relative))
00221         return 0;
00222 
00223     Base::Vector3d v1 = static_cast<Base::VectorPy*>(pcObj)->value();
00224 
00225     if (this->getSketchObjectPtr()->movePoint(GeoId,(Sketcher::PointPos)PointType,v1,(relative>0))) {
00226         std::stringstream str;
00227         str << "Not able to move point with the id and type: (" << GeoId << ", " << PointType << ")";
00228         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00229         return 0;
00230     }
00231 
00232     Py_Return;
00233 
00234 }
00235 
00236 PyObject* SketchObjectPy::fillet(PyObject *args)
00237 {
00238     PyObject *pcObj1, *pcObj2;
00239     int geoId1, geoId2, posId1, trim=1;
00240     double radius;
00241 
00242     // Two Lines, radius
00243     if (PyArg_ParseTuple(args, "iiO!O!d|i", &geoId1, &geoId2, &(Base::VectorPy::Type), &pcObj1, &(Base::VectorPy::Type), &pcObj2, &radius, &trim)) {
00244 
00245         Base::Vector3d v1 = static_cast<Base::VectorPy*>(pcObj1)->value();
00246         Base::Vector3d v2 = static_cast<Base::VectorPy*>(pcObj2)->value();
00247 
00248         if (this->getSketchObjectPtr()->fillet(geoId1, geoId2, v1, v2, radius, trim?true:false)) {
00249             std::stringstream str;
00250             str << "Not able to fillet lineSegments with ids : (" << geoId1 << ", " << geoId2 << ") and points (" << v1.x << ", " << v1.y << ", " << v1.z << ") & "
00251             << "(" << v2.x << ", " << v2.y << ", " << v2.z << ")";
00252             PyErr_SetString(PyExc_ValueError, str.str().c_str());
00253             return 0;
00254         }
00255     // Point, radius
00256     }
00257     PyErr_Clear();
00258 
00259     if (PyArg_ParseTuple(args, "iid|i", &geoId1, &posId1, &radius, &trim)) {
00260         if (this->getSketchObjectPtr()->fillet(geoId1, (Sketcher::PointPos) posId1, radius, trim?true:false)) {
00261             std::stringstream str;
00262             str << "Not able to fillet point with ( geoId: " << geoId1 << ", PointPos: " << posId1 << " )";
00263             PyErr_SetString(PyExc_ValueError, str.str().c_str());
00264             return 0;
00265         }
00266     }
00267     Py_Return;
00268 }
00269 
00270 PyObject* SketchObjectPy::trim(PyObject *args)
00271 {
00272     PyObject *pcObj;
00273     int GeoId;
00274 
00275     if (!PyArg_ParseTuple(args, "iO!", &GeoId, &(Base::VectorPy::Type), &pcObj))
00276         return 0;
00277 
00278     Base::Vector3d v1 = static_cast<Base::VectorPy*>(pcObj)->value();
00279 
00280     if (this->getSketchObjectPtr()->trim(GeoId,v1)) {
00281         std::stringstream str;
00282         str << "Not able to trim curve with the given index: " << GeoId;
00283         PyErr_SetString(PyExc_ValueError, str.str().c_str());
00284         return 0;
00285     }
00286 
00287     Py_Return; 
00288 
00289 }
00290 
00291 Py::Int SketchObjectPy::getConstraintCount(void) const
00292 {
00293     return Py::Int(this->getSketchObjectPtr()->Constraints.getSize());
00294 }
00295 
00296 Py::Int SketchObjectPy::getGeometryCount(void) const
00297 {
00298     return Py::Int(this->getSketchObjectPtr()->Geometry.getSize());
00299 }
00300 
00301 PyObject *SketchObjectPy::getCustomAttributes(const char* /*attr*/) const
00302 {
00303     return 0;
00304 }
00305 
00306 int SketchObjectPy::setCustomAttributes(const char* attr, PyObject* obj)
00307 {
00308     // search in PropertyList
00309     App::Property *prop = getSketchObjectPtr()->getPropertyByName(attr);
00310     if (prop) {
00311         // Read-only attributes must not be set over its Python interface
00312         short Type =  getSketchObjectPtr()->getPropertyType(prop);
00313         if (Type & App::Prop_ReadOnly) {
00314             std::stringstream s;
00315             s << "Object attribute '" << attr << "' is read-only";
00316             throw Py::AttributeError(s.str());
00317         }
00318 
00319         prop->setPyObject(obj);
00320 
00321         if (strcmp(attr,"Geometry") == 0)
00322             getSketchObjectPtr()->rebuildVertexIndex();
00323 
00324         return 1;
00325     }
00326 
00327     return 0;
00328 }

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