UnitsApiPy.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Juergen 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 
00026 #ifndef _PreComp_
00027 # include <Python.h>
00028 #endif
00029 
00030 #include <CXX/Objects.hxx>
00031 #include "Exception.h"
00033 #include "UnitsApi.h"
00034 
00035 
00036 
00037 using namespace Base;
00038 
00039 //**************************************************************************
00040 // Python stuff of UnitsApi
00041 
00042 // UnitsApi Methods                                             // Methods structure
00043 PyMethodDef UnitsApi::Methods[] = {
00044     {"translateUnit",  (PyCFunction) UnitsApi::sTranslateUnit  ,1,
00045      "translateUnit(string) -> double\n\n"
00046      "calculate a mathematical expression with units to a number. \n"
00047      "can be used for simple unit translation like: \n"
00048      " translateUnit('10m')\n"
00049      " or for more complex espressions:\n"
00050      " translateUnit('sin(pi)/50.0 m/s^2')\n"
00051     },
00052     {"getWithPrefs",  (PyCFunction) UnitsApi::sGetWithPrefs  ,1,
00053      "getWithPrefs(type,[string|float|int]) -> double\n\n"
00054      "Translation to internal regarding user prefs \n"
00055      " That means if no unit is issued the user prefs are in \n"
00056      " charge. If one unit is used the user prefs get ignored\n"
00057      " type can be: \n"
00058      " Length  \n"  
00059      " Area  \n"  
00060      " Volume  \n"  
00061      " Angle  \n" 
00062      " TimeSpan  \n"
00063      " Velocity  \n"
00064      " Acceleration \n" 
00065      " Mass   \n"
00066      " Temperature \n"
00067 
00068     },
00069 
00070     {NULL, NULL, 0, NULL}               /* Sentinel */
00071 };
00072 
00073 PyObject* UnitsApi::sTranslateUnit(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
00074 {
00075     char *pstr;
00076     if (!PyArg_ParseTuple(args, "s", &pstr))     // convert args: Python->C
00077         return NULL;                             // NULL triggers exception
00078     try {
00079         return Py::new_reference_to(Py::Object(Py::Float(UnitsApi::translateUnit(pstr))));
00080     }
00081     catch (const Base::Exception& e) {
00082         PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
00083         return 0L;
00084     }
00085     catch (const std::exception& e) {
00086         PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
00087         return 0L;
00088     }
00089 }
00090 
00091 PyObject* UnitsApi::sGetWithPrefs(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
00092 {
00093     char     *type;
00094     PyObject *obj;
00095     if (!PyArg_ParseTuple(args, "sO", &type,&obj))     // convert args: Python->C
00096         return NULL;                                   // NULL triggers exception
00097     try {
00098         QuantityType t;
00099         if(strcmp("Length",type)==0)
00100             t = Length;
00101         else{
00102             PyErr_Format(PyExc_IOError, "invalid quantity type: %s!", type);
00103             return 0L;
00104         }
00105 
00106         double result = toDblWithUserPrefs(t,obj);
00107         return Py::new_reference_to(Py::Object(Py::Float(result)));
00108     }
00109     catch (const Base::Exception&) {
00110         PyErr_Format(PyExc_IOError, "invalid unit expression \n");
00111         return 0L;
00112     }
00113     catch (const std::exception&) {
00114         PyErr_Format(PyExc_IOError, "invalid unit expression \n");
00115         return 0L;
00116     }
00117 }
00118 

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