LinePyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 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 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <gp.hxx>
00027 # include <gp_Lin.hxx>
00028 # include <Geom_Line.hxx>
00029 # include <Geom_TrimmedCurve.hxx>
00030 # include <GC_MakeLine.hxx>
00031 # include <GC_MakeSegment.hxx>
00032 # include <Precision.hxx>
00033 #endif
00034 
00035 #include <Base/VectorPy.h>
00036 #include <Base/GeometryPyCXX.h>
00037 
00038 #include "Geometry.h"
00039 #include "LinePy.h"
00040 #include "LinePy.cpp"
00041 
00042 using namespace Part;
00043 
00044 extern const char* gce_ErrorStatusText(gce_ErrorType et);
00045 
00046 // returns a string which represents the object e.g. when printed in python
00047 std::string LinePy::representation(void) const
00048 {
00049     std::stringstream str;
00050     //if(Infinite)
00051     //   str << "<Line infinite>";
00052     //else {
00053         Base::Vector3d start = getGeomLineSegmentPtr()->getStartPoint();
00054         Base::Vector3d end   = getGeomLineSegmentPtr()->getEndPoint();
00055         str << "<Line (" 
00056             << start.x << ";" <<start.y << "," <<start.z << ") (" 
00057             << end.x   << ";" <<end.y   << "," <<end.z   << ") >"; 
00058     //}
00059 
00060     return str.str();
00061 }
00062 
00063 PyObject *LinePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00064 {
00065     // create a new instance of LinePy and the Twin object 
00066     return new LinePy(new GeomLineSegment);
00067 }
00068 
00069 // constructor method
00070 int LinePy::PyInit(PyObject* args, PyObject* /*kwd*/)
00071 {
00072     
00073     if (PyArg_ParseTuple(args, "")) {
00074         // default line
00075         Infinite=false;
00076         return 0;
00077     }
00078 
00079     PyErr_Clear();
00080     PyObject *pLine;
00081     if (PyArg_ParseTuple(args, "O!", &(LinePy::Type), &pLine)) {
00082         // Copy line
00083         LinePy* pcLine = static_cast<LinePy*>(pLine);
00084         // get Geom_Line of line segment
00085         Handle_Geom_TrimmedCurve that_curv = Handle_Geom_TrimmedCurve::DownCast
00086             (pcLine->getGeomLineSegmentPtr()->handle());
00087         Handle_Geom_Line that_line = Handle_Geom_Line::DownCast
00088             (that_curv->BasisCurve());
00089         // get Geom_Line of line segment
00090         Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
00091             (this->getGeomLineSegmentPtr()->handle());
00092         Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
00093             (this_curv->BasisCurve());
00094 
00095         Infinite = pcLine->Infinite;
00096 
00097         // Assign the lines
00098         this_line->SetLin(that_line->Lin());
00099         this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
00100         return 0;
00101     }
00102 
00103     PyErr_Clear();
00104     PyObject *pV1, *pV2;
00105     if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type), &pV1,
00106                                        &(Base::VectorPy::Type), &pV2)) {
00107         Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
00108         Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
00109         try {
00110             // Create line out of two points
00111             if (v1 == v2) Standard_Failure::Raise("Both points are equal");
00112             GC_MakeSegment ms(gp_Pnt(v1.x,v1.y,v1.z),
00113                               gp_Pnt(v2.x,v2.y,v2.z));
00114             if (!ms.IsDone()) {
00115                 PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(ms.Status()));
00116                 return -1;
00117             }
00118 
00119             // get Geom_Line of line segment
00120             Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
00121                 (this->getGeomLineSegmentPtr()->handle());
00122             Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
00123                 (this_curv->BasisCurve());
00124             Handle_Geom_TrimmedCurve that_curv = ms.Value();
00125             Handle_Geom_Line that_line = Handle_Geom_Line::DownCast(that_curv->BasisCurve());
00126             this_line->SetLin(that_line->Lin());
00127             this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
00128 
00129             Infinite = false;
00130             return 0;
00131         }
00132         catch (Standard_Failure) {
00133             Handle_Standard_Failure e = Standard_Failure::Caught();
00134             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00135             return -1;
00136         }
00137         catch (...) {
00138             PyErr_SetString(PyExc_Exception, "creation of line failed");
00139             return -1;
00140         }
00141     }
00142 
00143     PyErr_SetString(PyExc_TypeError, "Line constructor accepts:\n"
00144         "-- empty parameter list\n"
00145         "-- Line\n"
00146         "-- Point, Point");
00147     return -1;
00148 }
00149 
00150 PyObject* LinePy::setParameterRange(PyObject *args)
00151 {
00152     double first, last;
00153     if (!PyArg_ParseTuple(args, "dd", &first, &last))
00154         return NULL;
00155 
00156     try {
00157         Handle_Geom_TrimmedCurve this_curve = Handle_Geom_TrimmedCurve::DownCast
00158             (this->getGeomLineSegmentPtr()->handle());
00159         this_curve->SetTrim(first, last);
00160     }
00161     catch (Standard_Failure) {
00162         Handle_Standard_Failure e = Standard_Failure::Caught();
00163         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00164         return NULL;
00165     }
00166 
00167     Py_Return; 
00168 }
00169 
00170 Py::Object LinePy::getStartPoint(void) const
00171 {
00172     Handle_Geom_TrimmedCurve this_curve = Handle_Geom_TrimmedCurve::DownCast
00173         (this->getGeomLineSegmentPtr()->handle());
00174     gp_Pnt pnt = this_curve->StartPoint();
00175     return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
00176 }
00177 
00178 void LinePy::setStartPoint(Py::Object arg)
00179 {
00180     gp_Pnt p1, p2;
00181     Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
00182         (this->getGeomLineSegmentPtr()->handle());
00183     p2 = this_curv->EndPoint();
00184 
00185     PyObject *p = arg.ptr();
00186     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00187         Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
00188         p1.SetX(v.x);
00189         p1.SetY(v.y);
00190         p1.SetZ(v.z);
00191     }
00192     else if (PyTuple_Check(p)) {
00193         Py::Tuple tuple(arg);
00194         p1.SetX((double)Py::Float(tuple.getItem(0)));
00195         p1.SetY((double)Py::Float(tuple.getItem(1)));
00196         p1.SetZ((double)Py::Float(tuple.getItem(2)));
00197     }
00198     else {
00199         std::string error = std::string("type must be 'Vector' or tuple, not ");
00200         error += p->ob_type->tp_name;
00201         throw Py::TypeError(error);
00202     }
00203 
00204     try {
00205         // Create line out of two points
00206         if (p1.Distance(p2) < Precision::Confusion()) Standard_Failure::Raise("Both points are equal");
00207         GC_MakeSegment ms(p1, p2);
00208         if (!ms.IsDone()) {
00209             throw Py::Exception(gce_ErrorStatusText(ms.Status()));
00210         }
00211 
00212         // get Geom_Line of line segment
00213         Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
00214             (this_curv->BasisCurve());
00215         Handle_Geom_TrimmedCurve that_curv = ms.Value();
00216         Handle_Geom_Line that_line = Handle_Geom_Line::DownCast(that_curv->BasisCurve());
00217         this_line->SetLin(that_line->Lin());
00218         this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
00219     }
00220     catch (Standard_Failure) {
00221         Handle_Standard_Failure e = Standard_Failure::Caught();
00222         throw Py::Exception(e->GetMessageString());
00223     }
00224 }
00225 
00226 Py::Object LinePy::getEndPoint(void) const
00227 {
00228     Handle_Geom_TrimmedCurve this_curve = Handle_Geom_TrimmedCurve::DownCast
00229         (this->getGeomLineSegmentPtr()->handle());
00230     gp_Pnt pnt = this_curve->EndPoint();
00231     return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
00232 }
00233 
00234 void LinePy::setEndPoint(Py::Object arg)
00235 {
00236     gp_Pnt p1, p2;
00237     Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
00238         (this->getGeomLineSegmentPtr()->handle());
00239     p1 = this_curv->StartPoint();
00240 
00241     PyObject *p = arg.ptr();
00242     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00243         Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
00244         p2.SetX(v.x);
00245         p2.SetY(v.y);
00246         p2.SetZ(v.z);
00247     }
00248     else if (PyTuple_Check(p)) {
00249         Py::Tuple tuple(arg);
00250         p2.SetX((double)Py::Float(tuple.getItem(0)));
00251         p2.SetY((double)Py::Float(tuple.getItem(1)));
00252         p2.SetZ((double)Py::Float(tuple.getItem(2)));
00253     }
00254     else {
00255         std::string error = std::string("type must be 'Vector' or tuple, not ");
00256         error += p->ob_type->tp_name;
00257         throw Py::TypeError(error);
00258     }
00259 
00260     try {
00261         // Create line out of two points
00262         if (p1.Distance(p2) < Precision::Confusion()) Standard_Failure::Raise("Both points are equal");
00263         GC_MakeSegment ms(p1, p2);
00264         if (!ms.IsDone()) {
00265             throw Py::Exception(gce_ErrorStatusText(ms.Status()));
00266         }
00267 
00268         // get Geom_Line of line segment
00269         Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
00270             (this_curv->BasisCurve());
00271         Handle_Geom_TrimmedCurve that_curv = ms.Value();
00272         Handle_Geom_Line that_line = Handle_Geom_Line::DownCast(that_curv->BasisCurve());
00273         this_line->SetLin(that_line->Lin());
00274         this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
00275     }
00276     catch (Standard_Failure) {
00277         Handle_Standard_Failure e = Standard_Failure::Caught();
00278         throw Py::Exception(e->GetMessageString());
00279     }
00280 }
00281 
00282 Py::Boolean LinePy::getInfinite(void) const
00283 {
00284     return Py::Boolean(Infinite);
00285 }
00286 
00287 void  LinePy::setInfinite(Py::Boolean arg)
00288 {
00289     Infinite = arg;
00290 }
00291 
00292 
00293 PyObject *LinePy::getCustomAttributes(const char* /*attr*/) const
00294 {
00295     return 0;
00296 }
00297 
00298 int LinePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00299 {
00300     return 0; 
00301 }

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