AppPartDesignPy.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/GeometryPyCXX.h>
00030 #include <Base/VectorPy.h>
00031 #include <Base/Tools.h>
00032 
00033 static PyObject * makeFilletArc(PyObject *self, PyObject *args)
00034 {
00035     PyObject *pM1;
00036     PyObject *pP;
00037     PyObject *pQ;
00038     PyObject *pN;
00039     double r2;
00040     int ccw;
00041     if (!PyArg_ParseTuple(args, "O!O!O!O!di",
00042             &(Base::VectorPy::Type), &pM1,
00043             &(Base::VectorPy::Type), &pP,
00044             &(Base::VectorPy::Type), &pQ,
00045             &(Base::VectorPy::Type), &pN,
00046             &r2, &ccw))
00047         return NULL;
00048 
00049     Base::Vector3d M1 = Py::Vector(pM1, false).toVector();
00050     Base::Vector3d P  = Py::Vector(pP,  false).toVector();
00051     Base::Vector3d Q  = Py::Vector(pQ,  false).toVector();
00052     Base::Vector3d N  = Py::Vector(pN,  false).toVector();
00053 
00054     Base::Vector3d u = Q - P;
00055     Base::Vector3d v = P - M1;
00056     Base::Vector3d b;
00057     if (ccw)
00058         b = u % N;
00059     else
00060         b = N % u;
00061     b.Normalize();
00062 
00063     double uu = u * u;
00064     double uv = u * v;
00065     double r1 = v.Length();
00066 
00067     // distinguish between internal and external fillets
00068     r2 *= Base::sgn(uv);
00069 
00070     double cc = 2.0 * r2 * (b * v - r1);
00071     double d = uv * uv - uu * cc;
00072     if (d < 0) {
00073         PyErr_SetString(PyExc_Exception, "Unable to caluclate intersection points");
00074         return NULL;
00075     }
00076 
00077     double t;
00078     double t1 = (-uv + sqrt(d)) / uu;
00079     double t2 = (-uv - sqrt(d)) / uu;
00080 
00081     if (fabs(t1) < fabs(t2))
00082         t = t1;
00083     else
00084         t = t2;
00085 
00086     Base::Vector3d M2 = P + (u*t) + (b*r2);
00087     Base::Vector3d S1 = (r2 * M1 + r1 * M2)/(r1+r2);
00088     Base::Vector3d S2 = M2 - (b*r2);
00089 
00090     Py::Tuple tuple(3);
00091     tuple.setItem(0, Py::Vector(S1));
00092     tuple.setItem(1, Py::Vector(S2));
00093     tuple.setItem(2, Py::Vector(M2));
00094 
00095     return Py::new_reference_to(tuple);
00096 }
00097 
00098 /* registration table  */
00099 struct PyMethodDef PartDesign_methods[] = {
00100     {"makeFilletArc" ,makeFilletArc,METH_VARARGS,
00101      "makeFilletArc(...) -- Fillet arc."},
00102     {NULL, NULL}        /* end of table marker */
00103 };

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