Navigation Menu

Skip to content

Commit

Permalink
+ fixes #1410: Base::Quantity::getValueAs does not properly check the…
Browse files Browse the repository at this point in the history
… dimension
  • Loading branch information
wwmayer committed Feb 14, 2014
1 parent e08635b commit 3ce05cd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Base/QuantityPy.xml
Expand Up @@ -41,7 +41,7 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity

Following parameters are allowed:
getValueAs('m/s') # unit string to parse
getValueAs(2.45,1) # translatrion value and unit signatur
getValueAs(2.45,1) # translatrion value and unit signature
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
Expand Down
91 changes: 61 additions & 30 deletions src/Base/QuantityPyImp.cpp
Expand Up @@ -117,45 +117,76 @@ PyObject* QuantityPy::getUserPreferred(PyObject *args)
PyObject* QuantityPy::getValueAs(PyObject *args)
{
Quantity quant;
quant.setInvalid();

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()

// first try Quantity
if (!quant.isValid()) {
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(QString::fromLatin1(string));

}else{
PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected");
return 0;
}
}

if (!quant.isValid()) {
PyObject *object;
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!",&(Base::UnitPy::Type), &object)) {
// Note: must be static_cast, not reinterpret_cast
quant.setUnit(*static_cast<Base::UnitPy*>(object)->getUnitPtr());
quant.setValue(1.0);
}
}

if (!quant.isValid()) {
PyObject *object;
double value;
PyErr_Clear();
if (PyArg_ParseTuple(args,"dO!",&value, &(Base::UnitPy::Type), &object)) {
// Note: must be static_cast, not reinterpret_cast
quant.setUnit(*static_cast<Base::UnitPy*>(object)->getUnitPtr());
quant.setValue(value);
}
}

if (!quant.isValid()) {
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;
PyErr_Clear();
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));
}
}
}
quant = getQuantityPtr()->getValueAs(quant);

return new QuantityPy(new Quantity(quant) );
if (!quant.isValid()) {
PyErr_Clear();
const char* string;
if (PyArg_ParseTuple(args,"s", &string)) {
quant = Quantity::parse(QString::fromLatin1(string));
}
}

if (!quant.isValid()) {
PyErr_SetString(PyExc_TypeError, "Either quantity, string, float or unit expected");
return 0;
}

if (getQuantityPtr()->getUnit() != quant.getUnit() && quant.isQuantity()) {
PyErr_SetString(PyExc_ValueError, "Unit mismatch");
return 0;
}

quant = getQuantityPtr()->getValueAs(quant);
return new QuantityPy(new Quantity(quant));
}

PyObject * QuantityPy::number_float_handler (PyObject *self)
Expand Down

0 comments on commit 3ce05cd

Please sign in to comment.