Switching the UnitsApi to the new Quantity system
This commit is contained in:
parent
a7e221fdff
commit
3a3afa0783
|
@ -1133,9 +1133,6 @@ void Application::initConfig(int argc, char ** argv)
|
|||
|
||||
LoadParameters();
|
||||
|
||||
// set the default units
|
||||
UnitsApi::setDefaults();
|
||||
|
||||
// capture python variables
|
||||
SaveEnv("PYTHONPATH");
|
||||
SaveEnv("PYTHONHOME");
|
||||
|
|
|
@ -59,6 +59,13 @@ Quantity::Quantity(double Value, const Unit& unit)
|
|||
}
|
||||
|
||||
|
||||
double Quantity::getValueAs(const Quantity &q)const
|
||||
{
|
||||
return _Value/q.getValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Quantity::operator ==(const Quantity& that) const
|
||||
{
|
||||
return (this->_Value == that._Value) && (this->_Unit == that._Unit) ;
|
||||
|
|
|
@ -68,10 +68,19 @@ public:
|
|||
|
||||
static Quantity parse(const char* buffer);
|
||||
|
||||
/// returns the unit of the quantity
|
||||
const Unit & getUnit(void) const{return _Unit;}
|
||||
/// set the unit of the quantity
|
||||
void setUnit(const Unit &un){_Unit = un;}
|
||||
/// get the Value of the quantity
|
||||
double getValue(void) const{return _Value;}
|
||||
/// set the value of the quantity
|
||||
void setValue(double val){_Value = val;}
|
||||
/** get the Value in a special unit given as quantity.
|
||||
* One can use one of the predifeined quantity units in this class
|
||||
*/
|
||||
double getValueAs(const Quantity &)const;
|
||||
|
||||
|
||||
/// true if it has a number without a unit
|
||||
bool isDimensionless(void)const;
|
||||
|
|
|
@ -40,6 +40,20 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity
|
|||
</UserDocu>
|
||||
</Documentation>
|
||||
</Methode>
|
||||
<Methode Name="getValueAs">
|
||||
<Documentation>
|
||||
<UserDocu>
|
||||
returns a floating point value as the provided unit
|
||||
|
||||
Following parameters are allowed:
|
||||
getValueAs('m/s') # unit string to parse
|
||||
getValueAs(2.45,1) # translatrion value and unit signatur
|
||||
getValueAs(FreeCAD.Units.Pascal) # predefined standard units
|
||||
getValueAs(Qantity('N/m^2')) # a quantity
|
||||
getValueAs(Unit(0,1,0,0,0,0,0,0)) # a unit
|
||||
</UserDocu>
|
||||
</Documentation>
|
||||
</Methode>
|
||||
<Attribute Name="Value" ReadOnly="false">
|
||||
<Documentation>
|
||||
<UserDocu>Numeric Value of the Quantity (in internal system mm,kg,s)</UserDocu>
|
||||
|
@ -58,15 +72,5 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity
|
|||
</Documentation>
|
||||
<Parameter Name="UserString" Type="String" />
|
||||
</Attribute>
|
||||
|
||||
|
||||
<Attribute Name="NanoMeter" ReadOnly="true">
|
||||
<Documentation>
|
||||
<UserDocu>Definition of NanoMeter </UserDocu>
|
||||
</Documentation>
|
||||
<Parameter Name="NanoMeter" Type="Object" />
|
||||
</Attribute>
|
||||
|
||||
|
||||
</PythonExport>
|
||||
</GenerateModel>
|
||||
|
|
|
@ -1,190 +1,228 @@
|
|||
|
||||
#include "PreCompiled.h"
|
||||
|
||||
#include "Base/Quantity.h"
|
||||
#include "Base/Vector3D.h"
|
||||
|
||||
// inclusion of the generated files (generated out of QuantityPy.xml)
|
||||
#include "QuantityPy.h"
|
||||
#include "UnitPy.h"
|
||||
#include "QuantityPy.cpp"
|
||||
|
||||
using namespace Base;
|
||||
|
||||
// returns a string which represents the object e.g. when printed in python
|
||||
std::string QuantityPy::representation(void) const
|
||||
{
|
||||
std::stringstream ret;
|
||||
ret << getQuantityPtr()->getValue() << " ";
|
||||
ret << getQuantityPtr()->getUnit().getString();
|
||||
|
||||
return ret.str();
|
||||
}
|
||||
|
||||
PyObject *QuantityPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
|
||||
{
|
||||
// create a new instance of QuantityPy and the Twin object
|
||||
return new QuantityPy(new Quantity);
|
||||
}
|
||||
|
||||
// constructor method
|
||||
int QuantityPy::PyInit(PyObject* args, PyObject* kwd)
|
||||
{
|
||||
Quantity *self = getQuantityPtr();
|
||||
|
||||
double f = DOUBLE_MAX;
|
||||
int i1=0;
|
||||
int i2=0;
|
||||
int i3=0;
|
||||
int i4=0;
|
||||
int i5=0;
|
||||
int i6=0;
|
||||
int i7=0;
|
||||
int i8=0;
|
||||
if (PyArg_ParseTuple(args, "|diiiiiiii", &f,&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8)) {
|
||||
if(f!=DOUBLE_MAX)
|
||||
*self = Quantity(f,Unit(i1,i2,i3,i4,i5,i6,i7,i8));
|
||||
return 0;
|
||||
}
|
||||
PyErr_Clear(); // set by PyArg_ParseTuple()
|
||||
|
||||
PyObject *object;
|
||||
|
||||
if (PyArg_ParseTuple(args,"O!",&(Base::QuantityPy::Type), &object)) {
|
||||
// Note: must be static_cast, not reinterpret_cast
|
||||
*self = *(static_cast<Base::QuantityPy*>(object)->getQuantityPtr());
|
||||
return 0;
|
||||
}
|
||||
PyErr_Clear(); // set by PyArg_ParseTuple()
|
||||
const char* string;
|
||||
if (PyArg_ParseTuple(args,"s", &string)) {
|
||||
|
||||
*self = Quantity::parse(string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
PyObject* QuantityPy::pow(PyObject * args)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
|
||||
#include "Base/Quantity.h"
|
||||
#include "Base/Vector3D.h"
|
||||
|
||||
// inclusion of the generated files (generated out of QuantityPy.xml)
|
||||
#include "QuantityPy.h"
|
||||
#include "UnitPy.h"
|
||||
#include "QuantityPy.cpp"
|
||||
|
||||
using namespace Base;
|
||||
|
||||
// returns a string which represents the object e.g. when printed in python
|
||||
std::string QuantityPy::representation(void) const
|
||||
{
|
||||
std::stringstream ret;
|
||||
ret << getQuantityPtr()->getValue() << " ";
|
||||
ret << getQuantityPtr()->getUnit().getString();
|
||||
|
||||
return ret.str();
|
||||
}
|
||||
|
||||
PyObject *QuantityPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
|
||||
{
|
||||
// create a new instance of QuantityPy and the Twin object
|
||||
return new QuantityPy(new Quantity);
|
||||
}
|
||||
|
||||
// constructor method
|
||||
int QuantityPy::PyInit(PyObject* args, PyObject* kwd)
|
||||
{
|
||||
Quantity *self = getQuantityPtr();
|
||||
|
||||
double f = DOUBLE_MAX;
|
||||
int i1=0;
|
||||
int i2=0;
|
||||
int i3=0;
|
||||
int i4=0;
|
||||
int i5=0;
|
||||
int i6=0;
|
||||
int i7=0;
|
||||
int i8=0;
|
||||
if (PyArg_ParseTuple(args, "|diiiiiiii", &f,&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8)) {
|
||||
if(f!=DOUBLE_MAX)
|
||||
*self = Quantity(f,Unit(i1,i2,i3,i4,i5,i6,i7,i8));
|
||||
return 0;
|
||||
}
|
||||
PyErr_Clear(); // set by PyArg_ParseTuple()
|
||||
|
||||
PyObject *object;
|
||||
|
||||
if (PyArg_ParseTuple(args,"O!",&(Base::QuantityPy::Type), &object)) {
|
||||
// Note: must be static_cast, not reinterpret_cast
|
||||
*self = *(static_cast<Base::QuantityPy*>(object)->getQuantityPtr());
|
||||
return 0;
|
||||
}
|
||||
PyErr_Clear(); // set by PyArg_ParseTuple()
|
||||
const char* string;
|
||||
if (PyArg_ParseTuple(args,"s", &string)) {
|
||||
|
||||
*self = Quantity::parse(string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
PyObject* QuantityPy::pow(PyObject * args)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::getUserPrefered(PyObject *args)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr();
|
||||
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
|
||||
return new QuantityPy(new Quantity(*a+*b) );
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::number_subtract_handler(PyObject *self, PyObject *other)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr();
|
||||
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
|
||||
return new QuantityPy(new Quantity(*a-*b) );
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
|
||||
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
|
||||
|
||||
return new QuantityPy(new Quantity(*a * *b) );
|
||||
}
|
||||
else if (PyFloat_Check(other)) {
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
|
||||
double b = PyFloat_AsDouble(other);
|
||||
return new QuantityPy(new Quantity(*a*b) );
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_TypeError, "A Quantity can only be multiplied by Quantity or number");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Py::Float QuantityPy::getValue(void) const
|
||||
{
|
||||
return Py::Float(getQuantityPtr()->getValue());
|
||||
}
|
||||
|
||||
void QuantityPy::setValue(Py::Float arg)
|
||||
{
|
||||
getQuantityPtr()->setValue(arg);
|
||||
}
|
||||
|
||||
Py::Object QuantityPy::getUnit(void) const
|
||||
{
|
||||
return Py::Object(new UnitPy(new Unit(getQuantityPtr()->getUnit())));
|
||||
}
|
||||
|
||||
void QuantityPy::setUnit(Py::Object arg)
|
||||
{
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::getValueAs(PyObject *args)
|
||||
{
|
||||
Quantity quant;
|
||||
|
||||
double f = DOUBLE_MAX;
|
||||
int i1=0;
|
||||
int i2=0;
|
||||
int i3=0;
|
||||
int i4=0;
|
||||
int i5=0;
|
||||
int i6=0;
|
||||
int i7=0;
|
||||
int i8=0;
|
||||
if (PyArg_ParseTuple(args, "d|iiiiiiii", &f,&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8)) {
|
||||
if(f!=DOUBLE_MAX)
|
||||
quant = Quantity(f,Unit(i1,i2,i3,i4,i5,i6,i7,i8));
|
||||
|
||||
}else{
|
||||
PyErr_Clear(); // set by PyArg_ParseTuple()
|
||||
|
||||
PyObject *object;
|
||||
|
||||
if (PyArg_ParseTuple(args,"O!",&(Base::QuantityPy::Type), &object)) {
|
||||
// Note: must be static_cast, not reinterpret_cast
|
||||
quant = * static_cast<Base::QuantityPy*>(object)->getQuantityPtr();
|
||||
|
||||
}else{
|
||||
PyErr_Clear(); // set by PyArg_ParseTuple()
|
||||
const char* string;
|
||||
if (PyArg_ParseTuple(args,"s", &string)) {
|
||||
|
||||
quant = Quantity::parse(string);
|
||||
|
||||
}else{
|
||||
PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
quant = getQuantityPtr()->getValueAs(quant);
|
||||
|
||||
return new QuantityPy(new Quantity(quant) );
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr();
|
||||
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
|
||||
return new QuantityPy(new Quantity(*a+*b) );
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::number_subtract_handler(PyObject *self, PyObject *other)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr();
|
||||
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
|
||||
return new QuantityPy(new Quantity(*a-*b) );
|
||||
}
|
||||
|
||||
PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
|
||||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
|
||||
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
|
||||
|
||||
return new QuantityPy(new Quantity(*a * *b) );
|
||||
}
|
||||
else if (PyFloat_Check(other)) {
|
||||
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
|
||||
double b = PyFloat_AsDouble(other);
|
||||
return new QuantityPy(new Quantity(*a*b) );
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_TypeError, "A Quantity can only be multiplied by Quantity or number");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Py::Float QuantityPy::getValue(void) const
|
||||
{
|
||||
return Py::Float(getQuantityPtr()->getValue());
|
||||
}
|
||||
|
||||
void QuantityPy::setValue(Py::Float arg)
|
||||
{
|
||||
getQuantityPtr()->setValue(arg);
|
||||
}
|
||||
|
||||
Py::Object QuantityPy::getUnit(void) const
|
||||
{
|
||||
return Py::Object(new UnitPy(new Unit(getQuantityPtr()->getUnit())));
|
||||
}
|
||||
|
||||
void QuantityPy::setUnit(Py::Object arg)
|
||||
{
|
||||
union PyType_Object pyType = {&(Base::UnitPy::Type)};
|
||||
Py::Type UnitType(pyType.o);
|
||||
if(!arg.isType(UnitType))
|
||||
throw Py::AttributeError("Not yet implemented");
|
||||
|
||||
getQuantityPtr()->setUnit(*static_cast<Base::UnitPy*>((*arg))->getUnitPtr());
|
||||
}
|
||||
|
||||
if(!arg.isType(UnitType))
|
||||
throw Py::AttributeError("Not yet implemented");
|
||||
|
||||
getQuantityPtr()->setUnit(*static_cast<Base::UnitPy*>((*arg))->getUnitPtr());
|
||||
}
|
||||
|
||||
|
||||
Py::String QuantityPy::getUserString(void) const
|
||||
{
|
||||
//return Py::String();
|
||||
throw Py::AttributeError("Not yet implemented");
|
||||
}
|
||||
|
||||
PyObject *QuantityPy::getCustomAttributes(const char* /*attr*/) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int QuantityPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// All the standard unit definitions ===============================
|
||||
|
||||
Py::Object QuantityPy::getNanoMeter(void) const
|
||||
}
|
||||
|
||||
PyObject *QuantityPy::getCustomAttributes(const char* /*attr*/) const
|
||||
{
|
||||
return Py::Object(new QuantityPy(new Quantity(Quantity::NanoMetre)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int QuantityPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -31,8 +31,7 @@
|
|||
#include "UnitsApi.h"
|
||||
#include "UnitsSchemaInternal.h"
|
||||
#include "UnitsSchemaImperial1.h"
|
||||
#include "UnitsSchemaMKS.h"
|
||||
//#include "UnitsApiPy.h"
|
||||
#include "UnitsSchemaMKS.h"
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
@ -49,54 +48,28 @@
|
|||
|
||||
using namespace Base;
|
||||
|
||||
// suppress annoying warnings from generated source files
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(disable : 4003)
|
||||
# pragma warning(disable : 4018)
|
||||
# pragma warning(disable : 4065)
|
||||
# pragma warning( disable : 4273 )
|
||||
# pragma warning(disable : 4335) // disable MAC file format warning on VC
|
||||
#endif
|
||||
|
||||
// === names =============================================================
|
||||
|
||||
char *QuantityNames[] = {
|
||||
"length" ,
|
||||
"area" ,
|
||||
"volume" ,
|
||||
"angle" ,
|
||||
"time span" ,
|
||||
"velocity" ,
|
||||
"acceleration",
|
||||
"mass" ,
|
||||
"temperature"
|
||||
};
|
||||
|
||||
const QString UnitsApi::getQuantityName(QuantityType t)
|
||||
{
|
||||
// check limits
|
||||
assert(t<9);
|
||||
// returns
|
||||
return QString::fromLatin1(QuantityNames[t]);
|
||||
}
|
||||
//const QString UnitsApi::getQuantityName(QuantityType t)
|
||||
//{
|
||||
// // check limits
|
||||
// assert(t<9);
|
||||
// // returns
|
||||
// return QString::fromLatin1(QuantityNames[t]);
|
||||
//}
|
||||
// === static attributes ================================================
|
||||
|
||||
UnitsSchema *UnitsApi::UserPrefSystem = new UnitsSchemaInternal();
|
||||
|
||||
double UnitsApi::UserPrefFactor [50];
|
||||
QString UnitsApi::UserPrefUnit [50];
|
||||
//double UnitsApi::UserPrefFactor [50];
|
||||
//QString UnitsApi::UserPrefUnit [50];
|
||||
int UnitsApi::UserPrefDecimals = 2;
|
||||
|
||||
UnitsApi::UnitsApi(const char* filter)
|
||||
{
|
||||
bool temp;
|
||||
Result = parse(filter,temp);
|
||||
}
|
||||
|
||||
UnitsApi::UnitsApi(const std::string& filter)
|
||||
{
|
||||
bool temp;
|
||||
Result = parse(filter.c_str(),temp);
|
||||
}
|
||||
|
||||
UnitsApi::~UnitsApi()
|
||||
|
@ -107,26 +80,26 @@ void UnitsApi::setSchema(UnitSystem s)
|
|||
{
|
||||
delete UserPrefSystem;
|
||||
switch (s) {
|
||||
case SI1 : UserPrefSystem = new UnitsSchemaInternal(); break;
|
||||
case SI2 : UserPrefSystem = new UnitsSchemaMKS(); break;
|
||||
case SI1 : UserPrefSystem = new UnitsSchemaInternal(); break;
|
||||
case SI2 : UserPrefSystem = new UnitsSchemaMKS(); break;
|
||||
case Imperial1: UserPrefSystem = new UnitsSchemaImperial1(); break;
|
||||
}
|
||||
UserPrefSystem->setSchemaUnits();
|
||||
//UserPrefSystem->setSchemaUnits();
|
||||
}
|
||||
|
||||
|
||||
double UnitsApi::translateUnit(const char* str)
|
||||
{
|
||||
bool temp;
|
||||
return parse(str,temp );
|
||||
}
|
||||
|
||||
double UnitsApi::translateUnit(const QString & str)
|
||||
{
|
||||
bool temp;
|
||||
return parse(str.toUtf8() ,temp);
|
||||
}
|
||||
|
||||
//double UnitsApi::translateUnit(const char* str)
|
||||
//{
|
||||
// bool temp;
|
||||
// return parse(str,temp );
|
||||
//}
|
||||
//
|
||||
//double UnitsApi::translateUnit(const QString & str)
|
||||
//{
|
||||
// bool temp;
|
||||
// return parse(str.toUtf8() ,temp);
|
||||
//}
|
||||
//
|
||||
|
||||
// === static translation methodes ==========================================
|
||||
|
||||
|
@ -140,71 +113,43 @@ Base::Quantity UnitsApi::schemaPrefUnit(const Base::Unit &unit,QString &outUnitS
|
|||
return UserPrefSystem->schemaPrefUnit(unit,outUnitString);
|
||||
}
|
||||
|
||||
QString UnitsApi::toStrWithUserPrefs(QuantityType t,double Value)
|
||||
{
|
||||
return UserPrefSystem->toStrWithUserPrefs(t,Value);
|
||||
//double UnitValue = Value/UserPrefFactor[t];
|
||||
//return QString::fromAscii("%1 %2").arg(UnitValue).arg(UserPrefUnit[t]);
|
||||
}
|
||||
|
||||
void UnitsApi::toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit)
|
||||
{
|
||||
UserPrefSystem->toStrWithUserPrefs(t,Value,outValue,outUnit);
|
||||
}
|
||||
|
||||
PyObject *UnitsApi::toPyWithUserPrefs(QuantityType t,double Value)
|
||||
{
|
||||
return PyFloat_FromDouble(Value * UserPrefFactor[t]);
|
||||
}
|
||||
|
||||
double UnitsApi::toDblWithUserPrefs(QuantityType t,const QString & Str)
|
||||
{
|
||||
return toDblWithUserPrefs(t,(const char*) Str.toUtf8());
|
||||
}
|
||||
|
||||
double UnitsApi::toDblWithUserPrefs(QuantityType t,const char* Str)
|
||||
{
|
||||
bool UsedUnit;
|
||||
double Value = parse( Str,UsedUnit );
|
||||
|
||||
if (UsedUnit)
|
||||
return Value;
|
||||
else
|
||||
return toDblWithUserPrefs(t,Value);
|
||||
}
|
||||
|
||||
double UnitsApi::toDblWithUserPrefs(QuantityType t,double UserVal)
|
||||
{
|
||||
return UserVal*UserPrefFactor[t];
|
||||
}
|
||||
|
||||
double UnitsApi::toDblWithUserPrefs(QuantityType t,PyObject *ArgObj)
|
||||
//QString UnitsApi::toStrWithUserPrefs(QuantityType t,double Value)
|
||||
//{
|
||||
// return UserPrefSystem->toStrWithUserPrefs(t,Value);
|
||||
// //double UnitValue = Value/UserPrefFactor[t];
|
||||
// //return QString::fromAscii("%1 %2").arg(UnitValue).arg(UserPrefUnit[t]);
|
||||
//}
|
||||
//
|
||||
//void UnitsApi::toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit)
|
||||
//{
|
||||
// UserPrefSystem->toStrWithUserPrefs(t,Value,outValue,outUnit);
|
||||
//}
|
||||
//
|
||||
//PyObject *UnitsApi::toPyWithUserPrefs(QuantityType t,double Value)
|
||||
//{
|
||||
// return PyFloat_FromDouble(Value * UserPrefFactor[t]);
|
||||
//}
|
||||
//
|
||||
double UnitsApi::toDbl(PyObject *ArgObj,const Base::Unit &u)
|
||||
{
|
||||
if (PyString_Check(ArgObj))
|
||||
return toDblWithUserPrefs(t,PyString_AsString(ArgObj));
|
||||
QString str = QString::fromAscii(PyString_AsString(ArgObj));
|
||||
else if (PyFloat_Check(ArgObj))
|
||||
return toDblWithUserPrefs(t,PyFloat_AsDouble(ArgObj));
|
||||
double d = PyFloat_AsDouble(ArgObj);
|
||||
else if (PyInt_Check(ArgObj))
|
||||
return toDblWithUserPrefs(t,(double)PyInt_AsLong(ArgObj));
|
||||
double d = (double)PyInt_AsLong(ArgObj);
|
||||
else
|
||||
throw Base::Exception("Wrong parameter type!");
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
void UnitsApi::setPrefOf(QuantityType t,const char* Str)
|
||||
Quantity UnitsApi::toQuantity(PyObject *ArgObj,const Base::Unit &u)
|
||||
{
|
||||
double Factor = translateUnit(Str);
|
||||
UserPrefUnit[t] = QString::fromLatin1(Str);
|
||||
UserPrefFactor[t] = Factor;
|
||||
}
|
||||
|
||||
const QString & UnitsApi::getPrefUnitOf(QuantityType t)
|
||||
{
|
||||
return UserPrefUnit[t];
|
||||
}
|
||||
|
||||
const double UnitsApi::getPrefFactorOf(QuantityType t)
|
||||
{
|
||||
return UserPrefFactor[t];
|
||||
|
||||
return Quantity();
|
||||
}
|
||||
|
||||
void UnitsApi::setDecimals(int prec)
|
||||
|
@ -217,71 +162,57 @@ int UnitsApi::getDecimals()
|
|||
return UserPrefDecimals;
|
||||
}
|
||||
|
||||
void UnitsApi::setDefaults(void)
|
||||
{
|
||||
setPrefOf( Length ,"mm" );
|
||||
setPrefOf( Area ,"mm^2" );
|
||||
setPrefOf( Volume ,"mm^3" );
|
||||
setPrefOf( Angle ,"deg" );
|
||||
setPrefOf( TimeSpan ,"s" );
|
||||
setPrefOf( Velocity ,"mm/s" );
|
||||
setPrefOf( Acceleration ,"mm/s^2" );
|
||||
setPrefOf( Mass ,"kg" );
|
||||
setPrefOf( Temperature ,"K" );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// === Parser & Scanner stuff ===============================================
|
||||
|
||||
// include the Scanner and the Parser for the filter language
|
||||
|
||||
double ScanResult=0;
|
||||
bool UU = false;
|
||||
|
||||
// error func
|
||||
void Unit_yyerror(char *errorinfo)
|
||||
{ throw Base::Exception(errorinfo); }
|
||||
|
||||
|
||||
// for VC9 (isatty and fileno not supported anymore)
|
||||
#ifdef _MSC_VER
|
||||
int isatty (int i) {return _isatty(i);}
|
||||
int fileno(FILE *stream) {return _fileno(stream);}
|
||||
#endif
|
||||
|
||||
namespace UnitParser {
|
||||
|
||||
// show the parser the lexer method
|
||||
#define yylex UnitsApilex
|
||||
int UnitsApilex(void);
|
||||
|
||||
// Parser, defined in UnitsApi.y
|
||||
#include "UnitsApi.tab.c"
|
||||
|
||||
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
||||
// Scanner, defined in UnitsApi.l
|
||||
#include "lex.UnitsApi.c"
|
||||
#endif // DOXYGEN_SHOULD_SKIP_THIS
|
||||
}
|
||||
|
||||
double UnitsApi::parse(const char* buffer,bool &UsedUnit)
|
||||
{
|
||||
// parse from buffer
|
||||
UnitParser::YY_BUFFER_STATE my_string_buffer = UnitParser::UnitsApi_scan_string (buffer);
|
||||
// set the global return variables
|
||||
ScanResult = DOUBLE_MIN;
|
||||
UU = false;
|
||||
// run the parser
|
||||
UnitParser::Unit_yyparse ();
|
||||
UsedUnit = UU;
|
||||
UU=false;
|
||||
// free the scan buffer
|
||||
UnitParser::UnitsApi_delete_buffer (my_string_buffer);
|
||||
|
||||
if (ScanResult == DOUBLE_MIN)
|
||||
throw Base::Exception("Unknown error in Unit expression");
|
||||
return ScanResult;
|
||||
}
|
||||
//// === Parser & Scanner stuff ===============================================
|
||||
//
|
||||
//// include the Scanner and the Parser for the filter language
|
||||
//
|
||||
//double ScanResult=0;
|
||||
//bool UU = false;
|
||||
//
|
||||
//// error func
|
||||
//void Unit_yyerror(char *errorinfo)
|
||||
//{ throw Base::Exception(errorinfo); }
|
||||
//
|
||||
//
|
||||
//// for VC9 (isatty and fileno not supported anymore)
|
||||
//#ifdef _MSC_VER
|
||||
//int isatty (int i) {return _isatty(i);}
|
||||
//int fileno(FILE *stream) {return _fileno(stream);}
|
||||
//#endif
|
||||
//
|
||||
//namespace UnitParser {
|
||||
//
|
||||
//// show the parser the lexer method
|
||||
//#define yylex UnitsApilex
|
||||
//int UnitsApilex(void);
|
||||
//
|
||||
//// Parser, defined in UnitsApi.y
|
||||
//#include "UnitsApi.tab.c"
|
||||
//
|
||||
//#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
||||
//// Scanner, defined in UnitsApi.l
|
||||
//#include "lex.UnitsApi.c"
|
||||
//#endif // DOXYGEN_SHOULD_SKIP_THIS
|
||||
//}
|
||||
//
|
||||
//double UnitsApi::parse(const char* buffer,bool &UsedUnit)
|
||||
//{
|
||||
// // parse from buffer
|
||||
// UnitParser::YY_BUFFER_STATE my_string_buffer = UnitParser::UnitsApi_scan_string (buffer);
|
||||
// // set the global return variables
|
||||
// ScanResult = DOUBLE_MIN;
|
||||
// UU = false;
|
||||
// // run the parser
|
||||
// UnitParser::Unit_yyparse ();
|
||||
// UsedUnit = UU;
|
||||
// UU=false;
|
||||
// // free the scan buffer
|
||||
// UnitParser::UnitsApi_delete_buffer (my_string_buffer);
|
||||
//
|
||||
// if (ScanResult == DOUBLE_MIN)
|
||||
// throw Base::Exception("Unknown error in Unit expression");
|
||||
// return ScanResult;
|
||||
//}
|
||||
|
|
|
@ -36,6 +36,8 @@
|
|||
#include <Python.h>
|
||||
#include <QString>
|
||||
#include "UnitsSchema.h"
|
||||
#include "Quantity.h"
|
||||
|
||||
|
||||
namespace Base {
|
||||
|
||||
|
@ -62,56 +64,25 @@ public:
|
|||
|
||||
|
||||
/// raw parser interface to calculat units (only from and to internal)
|
||||
static double translateUnit(const char*);
|
||||
static double translateUnit(const QString &);
|
||||
//tatic Quantity translateUnit(const char*);
|
||||
//static Quantity translateUnit(const QString &);
|
||||
|
||||
static QString schemaTranslate(Base::Quantity quant);
|
||||
static Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
|
||||
/// generate a value for a quantity with default user prefered system
|
||||
static double toDbl(PyObject *ArgObj,const Base::Unit &u=Base::Unit());
|
||||
/// generate a value for a quantity with default user prefered system
|
||||
static Quantity toQuantity(PyObject *ArgObj,const Base::Unit &u=Base::Unit());
|
||||
|
||||
/** @name Translation from internal to user prefs */
|
||||
//@{
|
||||
/// generate a string (UTF-8) for a quantity in user prefered system
|
||||
static QString toStrWithUserPrefs(QuantityType t,double Value);
|
||||
/// generate a string for the value and the unit seperately for a quantity in user prefered system
|
||||
static void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit);
|
||||
/// generate a python for a quantity in user prefered system
|
||||
static PyObject *toPyWithUserPrefs(QuantityType t,double Value);
|
||||
//@}
|
||||
|
||||
/** @name Translation to internal regarding user prefs
|
||||
* That means if no unit is issued the user prefs are in
|
||||
* charge. If one unit is used the user prefs get ignored
|
||||
*/
|
||||
//@{
|
||||
/// generate a value for a quantity with default user prefered system
|
||||
static double toDblWithUserPrefs(QuantityType t,const QString & Str);
|
||||
/// generate a value for a quantity with default user prefered system
|
||||
static double toDblWithUserPrefs(QuantityType t,const char* Str);
|
||||
/// generate a value for a quantity with default user prefered system
|
||||
static double toDblWithUserPrefs(QuantityType t,double UserVal);
|
||||
/// generate a value for a quantity with default user prefered system
|
||||
static double toDblWithUserPrefs(QuantityType t,PyObject *ArgObj);
|
||||
//@}
|
||||
|
||||
/** @name query and set the user preferences */
|
||||
//@{
|
||||
/// set the default unit of a quantity type (e.g. m/s)
|
||||
static void setPrefOf(QuantityType t,const char* Str);
|
||||
/// get the default unit of a quantity (e.g. m/s)
|
||||
static const QString & getPrefUnitOf(QuantityType t);
|
||||
/// get the name of a quantity (e.g. lenth)
|
||||
static const QString getQuantityName(QuantityType t);
|
||||
/// get the translation factor for the default unit of a quantity
|
||||
static const double getPrefFactorOf(QuantityType t);
|
||||
// set the number of decimals
|
||||
static void setDecimals(int);
|
||||
// fet the number of decimals
|
||||
static int getDecimals();
|
||||
/// set the application defaults
|
||||
static void setDefaults(void);
|
||||
//static void setDefaults(void);
|
||||
//@}
|
||||
|
||||
double Result;
|
||||
//double Result;
|
||||
|
||||
// Python interface
|
||||
static PyMethodDef Methods[];
|
||||
|
@ -121,19 +92,15 @@ protected:
|
|||
// not used at the moment
|
||||
static UnitsSchema * UserPrefSystem;
|
||||
|
||||
/// cached factor to translate
|
||||
static double UserPrefFactor [50] ;
|
||||
/// name of the unit the user wants to use as quantities
|
||||
static QString UserPrefUnit [50] ;
|
||||
/// number of decimals for floats
|
||||
static int UserPrefDecimals;
|
||||
|
||||
// do the real work
|
||||
static double parse(const char*,bool &UsedUnit);
|
||||
//static double parse(const char*,bool &UsedUnit);
|
||||
|
||||
protected: // the python API wrapper methodes
|
||||
static PyObject *sTranslateUnit (PyObject *self,PyObject *args,PyObject *kwd);
|
||||
static PyObject *sGetWithPrefs (PyObject *self,PyObject *args,PyObject *kwd);
|
||||
//static PyObject *sTranslateUnit (PyObject *self,PyObject *args,PyObject *kwd);
|
||||
//static PyObject *sGetWithPrefs (PyObject *self,PyObject *args,PyObject *kwd);
|
||||
static PyObject *sParseQuantity (PyObject *self,PyObject *args,PyObject *kwd);
|
||||
};
|
||||
|
||||
|
|
|
@ -43,31 +43,31 @@ using namespace Base;
|
|||
|
||||
// UnitsApi Methods // Methods structure
|
||||
PyMethodDef UnitsApi::Methods[] = {
|
||||
{"translateUnit", (PyCFunction) UnitsApi::sTranslateUnit ,1,
|
||||
"translateUnit(string) -> double\n\n"
|
||||
"calculate a mathematical expression with units to a number. \n"
|
||||
"can be used for simple unit translation like: \n"
|
||||
" translateUnit('10m')\n"
|
||||
" or for more complex espressions:\n"
|
||||
" translateUnit('sin(pi)/50.0 m/s^2')\n"
|
||||
},
|
||||
{"getWithPrefs", (PyCFunction) UnitsApi::sGetWithPrefs ,1,
|
||||
"getWithPrefs(type,[string|float|int]) -> double\n\n"
|
||||
"Translation to internal regarding user prefs \n"
|
||||
" That means if no unit is issued the user prefs are in \n"
|
||||
" charge. If one unit is used the user prefs get ignored\n"
|
||||
" type can be: \n"
|
||||
" Length \n"
|
||||
" Area \n"
|
||||
" Volume \n"
|
||||
" Angle \n"
|
||||
" TimeSpan \n"
|
||||
" Velocity \n"
|
||||
" Acceleration \n"
|
||||
" Mass \n"
|
||||
" Temperature \n"
|
||||
//{"translateUnit", (PyCFunction) UnitsApi::sTranslateUnit ,1,
|
||||
// "translateUnit(string) -> double\n\n"
|
||||
// "calculate a mathematical expression with units to a number. \n"
|
||||
// "can be used for simple unit translation like: \n"
|
||||
// " translateUnit('10m')\n"
|
||||
// " or for more complex espressions:\n"
|
||||
// " translateUnit('sin(pi)/50.0 m/s^2')\n"
|
||||
//},
|
||||
//{"getWithPrefs", (PyCFunction) UnitsApi::sGetWithPrefs ,1,
|
||||
// "getWithPrefs(type,[string|float|int]) -> double\n\n"
|
||||
// "Translation to internal regarding user prefs \n"
|
||||
// " That means if no unit is issued the user prefs are in \n"
|
||||
// " charge. If one unit is used the user prefs get ignored\n"
|
||||
// " type can be: \n"
|
||||
// " Length \n"
|
||||
// " Area \n"
|
||||
// " Volume \n"
|
||||
// " Angle \n"
|
||||
// " TimeSpan \n"
|
||||
// " Velocity \n"
|
||||
// " Acceleration \n"
|
||||
// " Mass \n"
|
||||
// " Temperature \n"
|
||||
|
||||
},
|
||||
//},
|
||||
{"parseQuantity", (PyCFunction) UnitsApi::sParseQuantity ,1,
|
||||
"parseQuantity(string) -> Base.Quantity()\n\n"
|
||||
},
|
||||
|
@ -75,51 +75,51 @@ PyMethodDef UnitsApi::Methods[] = {
|
|||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
PyObject* UnitsApi::sTranslateUnit(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
|
||||
{
|
||||
char *pstr;
|
||||
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
try {
|
||||
return Py::new_reference_to(Py::Object(Py::Float(UnitsApi::translateUnit(pstr))));
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
|
||||
return 0L;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
PyObject* UnitsApi::sGetWithPrefs(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
|
||||
{
|
||||
char *type;
|
||||
PyObject *obj;
|
||||
if (!PyArg_ParseTuple(args, "sO", &type,&obj)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
try {
|
||||
QuantityType t;
|
||||
if(strcmp("Length",type)==0)
|
||||
t = Length;
|
||||
else{
|
||||
PyErr_Format(PyExc_IOError, "invalid quantity type: %s!", type);
|
||||
return 0L;
|
||||
}
|
||||
|
||||
double result = toDblWithUserPrefs(t,obj);
|
||||
return Py::new_reference_to(Py::Object(Py::Float(result)));
|
||||
}
|
||||
catch (const Base::Exception&) {
|
||||
PyErr_Format(PyExc_IOError, "invalid unit expression \n");
|
||||
return 0L;
|
||||
}
|
||||
catch (const std::exception&) {
|
||||
PyErr_Format(PyExc_IOError, "invalid unit expression \n");
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
//PyObject* UnitsApi::sTranslateUnit(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
|
||||
//{
|
||||
// char *pstr;
|
||||
// if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
|
||||
// return NULL; // NULL triggers exception
|
||||
// try {
|
||||
// return Py::new_reference_to(Py::Object(Py::Float(UnitsApi::translateUnit(pstr))));
|
||||
// }
|
||||
// catch (const Base::Exception& e) {
|
||||
// PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
|
||||
// return 0L;
|
||||
// }
|
||||
// catch (const std::exception& e) {
|
||||
// PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
|
||||
// return 0L;
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//PyObject* UnitsApi::sGetWithPrefs(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
|
||||
//{
|
||||
// char *type;
|
||||
// PyObject *obj;
|
||||
// if (!PyArg_ParseTuple(args, "sO", &type,&obj)) // convert args: Python->C
|
||||
// return NULL; // NULL triggers exception
|
||||
// try {
|
||||
// QuantityType t;
|
||||
// if(strcmp("Length",type)==0)
|
||||
// t = Length;
|
||||
// else{
|
||||
// PyErr_Format(PyExc_IOError, "invalid quantity type: %s!", type);
|
||||
// return 0L;
|
||||
// }
|
||||
//
|
||||
// double result = toDblWithUserPrefs(t,obj);
|
||||
// return Py::new_reference_to(Py::Object(Py::Float(result)));
|
||||
// }
|
||||
// catch (const Base::Exception&) {
|
||||
// PyErr_Format(PyExc_IOError, "invalid unit expression \n");
|
||||
// return 0L;
|
||||
// }
|
||||
// catch (const std::exception&) {
|
||||
// PyErr_Format(PyExc_IOError, "invalid unit expression \n");
|
||||
// return 0L;
|
||||
// }
|
||||
//}
|
||||
|
||||
PyObject* UnitsApi::sParseQuantity(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
|
||||
{
|
||||
|
|
|
@ -33,26 +33,14 @@
|
|||
|
||||
|
||||
namespace Base {
|
||||
|
||||
/** Units systems*/
|
||||
enum UnitSystem {
|
||||
SI1 = 0 , /** internal (mm,kg,s) SI system (http://en.wikipedia.org/wiki/International_System_of_Units) */
|
||||
SI2 = 1 , /** MKS (m,kg,s) SI system */
|
||||
Imperial1 = 2 /** the Imperial system (http://en.wikipedia.org/wiki/Imperial_units) */
|
||||
} ;
|
||||
|
||||
/** quantity types*/
|
||||
enum QuantityType{
|
||||
Length ,
|
||||
Area ,
|
||||
Volume ,
|
||||
Angle ,
|
||||
TimeSpan ,
|
||||
Velocity ,
|
||||
Acceleration,
|
||||
Mass ,
|
||||
Temperature
|
||||
} ;
|
||||
/** Units systems*/
|
||||
enum UnitSystem {
|
||||
SI1 = 0 , /** internal (mm,kg,s) SI system (http://en.wikipedia.org/wiki/International_System_of_Units) */
|
||||
SI2 = 1 , /** MKS (m,kg,s) SI system */
|
||||
Imperial1 = 2 /** the Imperial system (http://en.wikipedia.org/wiki/Imperial_units) */
|
||||
} ;
|
||||
|
||||
|
||||
/** The UnitSchema class
|
||||
* The subclasses of this class define the stuff for a
|
||||
|
@ -61,17 +49,7 @@ namespace Base {
|
|||
class UnitsSchema
|
||||
{
|
||||
public:
|
||||
/// if called set all the units of the units schema
|
||||
virtual void setSchemaUnits(void)=0;
|
||||
/// return the value and the unit as string
|
||||
virtual void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit)=0;
|
||||
/** return one string with the formated value/unit pair.
|
||||
* The designer of the unit schema can decide how he wants the
|
||||
* value presented. Only rule is its still parseble by the
|
||||
* units parser.
|
||||
*/
|
||||
virtual QString toStrWithUserPrefs(QuantityType t,double Value)=0;
|
||||
|
||||
/// this methode translate the quantity in a string as the user may expect it
|
||||
virtual QString schemaTranslate(Base::Quantity quant)=0;
|
||||
// returns the prefered unit as string and the quantity to translate
|
||||
virtual Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString)=0;
|
||||
|
|
|
@ -34,35 +34,6 @@
|
|||
using namespace Base;
|
||||
|
||||
|
||||
void UnitsSchemaImperial1::setSchemaUnits(void)
|
||||
{
|
||||
UnitsApi::setPrefOf( Length ,"in" );
|
||||
UnitsApi::setPrefOf( Area ,"in^2" );
|
||||
UnitsApi::setPrefOf( Volume ,"in^3" );
|
||||
UnitsApi::setPrefOf( Angle ,"deg" );
|
||||
UnitsApi::setPrefOf( TimeSpan ,"s" );
|
||||
UnitsApi::setPrefOf( Velocity ,"in/s" );
|
||||
UnitsApi::setPrefOf( Acceleration ,"in/s^2" );
|
||||
UnitsApi::setPrefOf( Mass ,"lb" );
|
||||
UnitsApi::setPrefOf( Temperature ,"K" );
|
||||
|
||||
}
|
||||
|
||||
void UnitsSchemaImperial1::toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit)
|
||||
{
|
||||
double UnitValue = Value/UnitsApi::getPrefFactorOf(t);
|
||||
outUnit = UnitsApi::getPrefUnitOf(t);
|
||||
outValue = QString::fromAscii("%1").arg(UnitValue);
|
||||
|
||||
}
|
||||
|
||||
|
||||
QString UnitsSchemaImperial1::toStrWithUserPrefs(QuantityType t,double Value)
|
||||
{
|
||||
double UnitValue = Value/UnitsApi::getPrefFactorOf(t);
|
||||
return QString::fromAscii("%1 %2").arg(UnitValue).arg(UnitsApi::getPrefUnitOf(t));
|
||||
}
|
||||
|
||||
QString UnitsSchemaImperial1::schemaTranslate(Base::Quantity quant)
|
||||
{
|
||||
double UnitValue = quant.getValue();
|
||||
|
|
|
@ -39,10 +39,6 @@ namespace Base {
|
|||
class UnitsSchemaImperial1: public UnitsSchema
|
||||
{
|
||||
public:
|
||||
void setSchemaUnits(void);
|
||||
void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit);
|
||||
QString toStrWithUserPrefs(QuantityType t,double Value);
|
||||
|
||||
virtual QString schemaTranslate(Base::Quantity quant);
|
||||
Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
|
||||
};
|
||||
|
|
|
@ -34,34 +34,6 @@
|
|||
using namespace Base;
|
||||
|
||||
|
||||
void UnitsSchemaInternal::setSchemaUnits(void)
|
||||
{
|
||||
UnitsApi::setPrefOf( Length ,"mm" );
|
||||
UnitsApi::setPrefOf( Area ,"mm^2" );
|
||||
UnitsApi::setPrefOf( Volume ,"mm^3" );
|
||||
UnitsApi::setPrefOf( Angle ,"deg" );
|
||||
UnitsApi::setPrefOf( TimeSpan ,"s" );
|
||||
UnitsApi::setPrefOf( Velocity ,"mm/s" );
|
||||
UnitsApi::setPrefOf( Acceleration ,"mm/s^2" );
|
||||
UnitsApi::setPrefOf( Mass ,"kg" );
|
||||
UnitsApi::setPrefOf( Temperature ,"K" );
|
||||
|
||||
}
|
||||
|
||||
void UnitsSchemaInternal::toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit)
|
||||
{
|
||||
double UnitValue = Value/UnitsApi::getPrefFactorOf(t);
|
||||
outUnit = UnitsApi::getPrefUnitOf(t);
|
||||
outValue = QString::fromAscii("%1").arg(UnitValue);
|
||||
|
||||
}
|
||||
|
||||
QString UnitsSchemaInternal::toStrWithUserPrefs(QuantityType t,double Value)
|
||||
{
|
||||
double UnitValue = Value/UnitsApi::getPrefFactorOf(t);
|
||||
return QString::fromAscii("%1 %2").arg(UnitValue).arg(UnitsApi::getPrefUnitOf(t));
|
||||
}
|
||||
|
||||
QString UnitsSchemaInternal::schemaTranslate(Base::Quantity quant)
|
||||
{
|
||||
double UnitValue = quant.getValue();
|
||||
|
|
|
@ -40,10 +40,6 @@ namespace Base {
|
|||
class UnitsSchemaInternal: public UnitsSchema
|
||||
{
|
||||
public:
|
||||
void setSchemaUnits(void);
|
||||
void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit);
|
||||
QString toStrWithUserPrefs(QuantityType t,double Value);
|
||||
|
||||
virtual QString schemaTranslate(Base::Quantity quant);
|
||||
Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
|
||||
};
|
||||
|
|
|
@ -34,34 +34,6 @@
|
|||
using namespace Base;
|
||||
|
||||
|
||||
void UnitsSchemaMKS::setSchemaUnits(void)
|
||||
{
|
||||
UnitsApi::setPrefOf( Length ,"m" );
|
||||
UnitsApi::setPrefOf( Area ,"m^2" );
|
||||
UnitsApi::setPrefOf( Volume ,"m^3" );
|
||||
UnitsApi::setPrefOf( Angle ,"deg" );
|
||||
UnitsApi::setPrefOf( TimeSpan ,"s" );
|
||||
UnitsApi::setPrefOf( Velocity ,"m/s" );
|
||||
UnitsApi::setPrefOf( Acceleration ,"m/s^2" );
|
||||
UnitsApi::setPrefOf( Mass ,"kg" );
|
||||
UnitsApi::setPrefOf( Temperature ,"K" );
|
||||
|
||||
}
|
||||
|
||||
void UnitsSchemaMKS::toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit)
|
||||
{
|
||||
double UnitValue = Value/UnitsApi::getPrefFactorOf(t);
|
||||
outUnit = UnitsApi::getPrefUnitOf(t);
|
||||
outValue = QString::fromAscii("%1").arg(UnitValue);
|
||||
|
||||
}
|
||||
|
||||
QString UnitsSchemaMKS::toStrWithUserPrefs(QuantityType t,double Value)
|
||||
{
|
||||
double UnitValue = Value/UnitsApi::getPrefFactorOf(t);
|
||||
return QString::fromAscii("%1 %2").arg(UnitValue).arg(UnitsApi::getPrefUnitOf(t));
|
||||
}
|
||||
|
||||
QString UnitsSchemaMKS::schemaTranslate(Base::Quantity quant)
|
||||
{
|
||||
double UnitValue = quant.getValue();
|
||||
|
|
|
@ -38,10 +38,6 @@ namespace Base {
|
|||
class UnitsSchemaMKS: public UnitsSchema
|
||||
{
|
||||
public:
|
||||
void setSchemaUnits(void);
|
||||
void toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit);
|
||||
QString toStrWithUserPrefs(QuantityType t,double Value);
|
||||
|
||||
virtual QString schemaTranslate(Base::Quantity quant);
|
||||
Base::Quantity schemaPrefUnit(const Base::Unit &unit,QString &outUnitString);
|
||||
};
|
||||
|
|
|
@ -69,14 +69,14 @@ DlgSettingsUnitsImp::~DlgSettingsUnitsImp()
|
|||
|
||||
void DlgSettingsUnitsImp::fillUpListBox()
|
||||
{
|
||||
tableWidget->setRowCount(10);
|
||||
for (int i = 0 ; i<9;i++) {
|
||||
QTableWidgetItem *newItem = new QTableWidgetItem(UnitsApi::getQuantityName((Base::QuantityType)i));
|
||||
tableWidget->setItem(i, 0, newItem);
|
||||
|
||||
newItem = new QTableWidgetItem(UnitsApi::getPrefUnitOf((Base::QuantityType)i));
|
||||
tableWidget->setItem(i, 1, newItem);
|
||||
}
|
||||
//tableWidget->setRowCount(10);
|
||||
//for (int i = 0 ; i<9;i++) {
|
||||
// QTableWidgetItem *newItem = new QTableWidgetItem(UnitsApi::getQuantityName((Base::QuantityType)i));
|
||||
// tableWidget->setItem(i, 0, newItem);
|
||||
//
|
||||
// newItem = new QTableWidgetItem(UnitsApi::getPrefUnitOf((Base::QuantityType)i));
|
||||
// tableWidget->setItem(i, 1, newItem);
|
||||
//}
|
||||
}
|
||||
|
||||
void DlgSettingsUnitsImp::currentIndexChanged(int index)
|
||||
|
|
|
@ -549,12 +549,12 @@ QVariant PropertyFloatItem::toString(const QVariant& prop) const
|
|||
const std::vector<App::Property*>& props = getPropertyData();
|
||||
if (!props.empty()) {
|
||||
if (props.front()->getTypeId().isDerivedFrom(App::PropertyDistance::getClassTypeId())) {
|
||||
QString unit = Base::UnitsApi::getPrefUnitOf(Base::Length);
|
||||
QString unit = QString::fromAscii("mm");
|
||||
unit.prepend(QLatin1String(" "));
|
||||
data += unit;
|
||||
}
|
||||
else if (props.front()->getTypeId().isDerivedFrom(App::PropertyLength::getClassTypeId())) {
|
||||
QString unit = Base::UnitsApi::getPrefUnitOf(Base::Length);
|
||||
QString unit = QString::fromAscii("mm");
|
||||
unit.prepend(QLatin1String(" "));
|
||||
data += unit;
|
||||
}
|
||||
|
@ -564,7 +564,7 @@ QVariant PropertyFloatItem::toString(const QVariant& prop) const
|
|||
//data += unit;
|
||||
}
|
||||
else if (props.front()->getTypeId().isDerivedFrom(App::PropertyAcceleration::getClassTypeId())) {
|
||||
QString unit = Base::UnitsApi::getPrefUnitOf(Base::Acceleration);
|
||||
QString unit = QString::fromAscii("mm/s^2");
|
||||
unit.prepend(QLatin1String(" "));
|
||||
data += unit;
|
||||
}
|
||||
|
@ -608,13 +608,13 @@ void PropertyFloatItem::setEditorData(QWidget *editor, const QVariant& data) con
|
|||
if (prop.empty())
|
||||
return;
|
||||
else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyDistance::getClassTypeId())) {
|
||||
QString unit = Base::UnitsApi::getPrefUnitOf(Base::Length);
|
||||
QString unit = QString::fromAscii("mm");
|
||||
unit.prepend(QLatin1String(" "));
|
||||
sb->setSuffix(unit);
|
||||
}
|
||||
else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyLength::getClassTypeId())) {
|
||||
sb->setMinimum(0.0);
|
||||
QString unit = Base::UnitsApi::getPrefUnitOf(Base::Length);
|
||||
QString unit = QString::fromAscii("mm");
|
||||
unit.prepend(QLatin1String(" "));
|
||||
sb->setSuffix(unit);
|
||||
}
|
||||
|
@ -626,7 +626,7 @@ void PropertyFloatItem::setEditorData(QWidget *editor, const QVariant& data) con
|
|||
}
|
||||
else if (prop.front()->getTypeId().isDerivedFrom(App::PropertyAcceleration::getClassTypeId())) {
|
||||
sb->setMinimum(0.0);
|
||||
QString unit = Base::UnitsApi::getPrefUnitOf(Base::Acceleration);
|
||||
QString unit = QString::fromAscii("mm/s^2");
|
||||
unit.prepend(QLatin1String(" "));
|
||||
sb->setSuffix(unit);
|
||||
}
|
||||
|
|
|
@ -129,14 +129,14 @@ int WaypointPy::PyInit(PyObject* args, PyObject* kwd)
|
|||
getWaypointPtr()->Velocity = 0;
|
||||
}
|
||||
else
|
||||
getWaypointPtr()->Velocity = Base::UnitsApi::toDblWithUserPrefs(Base::Velocity,vel);
|
||||
getWaypointPtr()->Velocity = Base::UnitsApi::toDbl(vel,Base::Unit::Velocity);
|
||||
getWaypointPtr()->Cont = cont?true:false;
|
||||
getWaypointPtr()->Tool = tool;
|
||||
getWaypointPtr()->Base = base;
|
||||
if(acc == 0)
|
||||
getWaypointPtr()->Accelaration = 100;
|
||||
else
|
||||
getWaypointPtr()->Accelaration = Base::UnitsApi::toDblWithUserPrefs(Base::Acceleration,acc);;
|
||||
getWaypointPtr()->Accelaration = Base::UnitsApi::toDbl(acc,Base::Unit::Acceleration);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -131,12 +131,12 @@ void TaskRobot6Axis::viewTcp(const Base::Placement pos)
|
|||
pos.getRotation().getYawPitchRoll(A,B,C);
|
||||
|
||||
QString result = QString::fromAscii("TCP:( %1, %2, %3, %4, %5, %6 )")
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().x),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().y),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().z),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,A),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,B),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,C),0,'f',1);
|
||||
.arg(pos.getPosition().x,0,'f',1)
|
||||
.arg(pos.getPosition().y,0,'f',1)
|
||||
.arg(pos.getPosition().z,0,'f',1)
|
||||
.arg(A,0,'f',1)
|
||||
.arg(B,0,'f',1)
|
||||
.arg(C,0,'f',1);
|
||||
|
||||
ui->label_TCP->setText(result);
|
||||
}
|
||||
|
@ -147,12 +147,12 @@ void TaskRobot6Axis::viewTool(const Base::Placement pos)
|
|||
pos.getRotation().getYawPitchRoll(A,B,C);
|
||||
|
||||
QString result = QString::fromAscii("Tool:( %1, %2, %3, %4, %5, %6 )")
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().x),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().y),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().z),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,A),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,B),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,C),0,'f',1);
|
||||
.arg(pos.getPosition().x,0,'f',1)
|
||||
.arg(pos.getPosition().y,0,'f',1)
|
||||
.arg(pos.getPosition().z,0,'f',1)
|
||||
.arg(A,0,'f',1)
|
||||
.arg(B,0,'f',1)
|
||||
.arg(C,0,'f',1);
|
||||
|
||||
ui->label_Tool->setText(result);
|
||||
}
|
||||
|
|
|
@ -119,12 +119,12 @@ void TaskTrajectory::viewTool(const Base::Placement pos)
|
|||
pos.getRotation().getYawPitchRoll(A,B,C);
|
||||
|
||||
QString result = QString::fromAscii("Pos:(%1, %2, %3, %4, %5, %6)")
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().x),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().y),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Length,pos.getPosition().z),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,A),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,B),0,'f',1)
|
||||
.arg(Base::UnitsApi::toDblWithUserPrefs(Base::Angle,C),0,'f',1);
|
||||
.arg(pos.getPosition().x,0,'f',1)
|
||||
.arg(pos.getPosition().y,0,'f',1)
|
||||
.arg(pos.getPosition().z,0,'f',1)
|
||||
.arg(A,0,'f',1)
|
||||
.arg(B,0,'f',1)
|
||||
.arg(C,0,'f',1);
|
||||
|
||||
ui->label_Pos->setText(result);
|
||||
}
|
||||
|
|
|
@ -112,7 +112,7 @@ void TaskSketcherGeneral::toggleGridView(bool on)
|
|||
|
||||
void TaskSketcherGeneral::setGridSize(const QString& val)
|
||||
{
|
||||
float gridSize = (float) Base::UnitsApi::translateUnit(val);
|
||||
float gridSize = (float) Base::Quantity::parse(val.toAscii()).getValue();
|
||||
if (gridSize > 0)
|
||||
sketchView->GridSize.setValue(gridSize);
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ class TestCmd:
|
|||
QtUnitGui.addTest("TestApp.All")
|
||||
QtUnitGui.setTest("TestApp.All")
|
||||
QtUnitGui.addTest("BaseTests")
|
||||
QtUnitGui.addTest("UnitTests")
|
||||
QtUnitGui.addTest("Document")
|
||||
QtUnitGui.addTest("UnicodeTests")
|
||||
QtUnitGui.addTest("MeshTestsApp")
|
||||
|
|
|
@ -2,7 +2,10 @@
|
|||
|
||||
import FreeCAD, os, unittest, math
|
||||
|
||||
|
||||
def tu(str):
|
||||
return FreeCAD.Units.Quantity(str).Value
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# define the functions to test the FreeCAD UnitApi code
|
||||
#---------------------------------------------------------------------------
|
||||
|
@ -12,9 +15,9 @@ def compare(x,y): return math.fabs(x-y)<0.00001
|
|||
class UnitBasicCases(unittest.TestCase):
|
||||
|
||||
def testConversions(self):
|
||||
tu = FreeCAD.Units.translateUnit
|
||||
#tu = FreeCAD.Units.translateUnit
|
||||
self.failUnless(compare( tu('10 m') , 10000.0 ) )
|
||||
self.failUnless(compare( tu('3/8 in') , 0.014763779527 ) )
|
||||
self.failUnless(compare( tu('3/8 in') , 9.525 ) )
|
||||
self.failUnless(compare( tu('100 km/h') , 27777.77777777 ) )
|
||||
self.failUnless(compare( tu('m^2*kg*s^-3*A^-2') , 1000000.0 ) )
|
||||
self.failUnless(compare( tu('(m^2*kg)/(A^2*s^3)') , 1000000.0 ) )
|
||||
|
@ -22,13 +25,13 @@ class UnitBasicCases(unittest.TestCase):
|
|||
self.failUnless(compare( tu('2*pi rad') / tu('gon') , 400.0 ) )
|
||||
|
||||
def testImperial(self):
|
||||
tu = FreeCAD.Units.translateUnit
|
||||
self.failUnless(compare( tu('3/8 in') , 0.014763779527 ) )
|
||||
self.failUnless(compare( tu('1fo+(3+7/16)in') , 392.112500 ) )
|
||||
self.failUnless(compare( tu('1\'+(3+7/16)"') , 392.112500 ) )
|
||||
#tu = FreeCAD.Units.translateUnit
|
||||
self.failUnless(compare( tu('3/8 in') , 9.525 ) )
|
||||
self.failUnless(compare( tu('1fo (3+7/16)in') , 392.112500 ) )
|
||||
self.failUnless(compare( tu('1\' (3+7/16)"') , 392.112500 ) )
|
||||
|
||||
def testTrigonometric(self):
|
||||
tu = FreeCAD.Units.translateUnit
|
||||
#tu = FreeCAD.Units.translateUnit
|
||||
self.failUnless(compare( tu('sin(pi)') , math.sin(math.pi) ) )
|
||||
self.failUnless(compare( tu('cos(pi)') , math.cos(math.pi) ) )
|
||||
self.failUnless(compare( tu('tan(pi)') , math.tan(math.pi) ) )
|
||||
|
|
Loading…
Reference in New Issue
Block a user