Skip to content

Commit

Permalink
allow to set constraints via Python
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jul 16, 2017
1 parent b8a1e33 commit c4ca27e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 32 deletions.
72 changes: 40 additions & 32 deletions src/App/PropertyStandard.cpp
Expand Up @@ -580,7 +580,7 @@ void PropertyEnumeration::setPathValue(const ObjectIdentifier &path, const boost
// PropertyIntegerConstraint
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

TYPESYSTEM_SOURCE(App::PropertyIntegerConstraint, App::PropertyInteger);
TYPESYSTEM_SOURCE(App::PropertyIntegerConstraint, App::PropertyInteger)

//**************************************************************************
// Construction/Destruction
Expand All @@ -595,11 +595,17 @@ PropertyIntegerConstraint::PropertyIntegerConstraint()

PropertyIntegerConstraint::~PropertyIntegerConstraint()
{

if (_ConstStruct && _ConstStruct->isDeletable())
delete _ConstStruct;
}

void PropertyIntegerConstraint::setConstraints(const Constraints* sConstrain)
{
if (_ConstStruct != sConstrain) {
if (_ConstStruct && _ConstStruct->isDeletable())
delete _ConstStruct;
}

_ConstStruct = sConstrain;
}

Expand Down Expand Up @@ -644,20 +650,16 @@ void PropertyIntegerConstraint::setPyObject(PyObject *value)
throw Base::TypeError("Type in tuple must be int");
}

if (!_ConstStruct) {
Constraints* c = new Constraints();
c->LowerBound = values[1];
c->UpperBound = values[2];
c->StepSize = std::max<long>(1, values[3]);
if (values[0] > c->UpperBound)
values[0] = c->UpperBound;
else if (values[0] < c->LowerBound)
values[0] = c->LowerBound;
setConstraints(c);
}
else {
throw Base::RuntimeError("Cannot override limits of constraint");
}
Constraints* c = new Constraints();
c->setDeletable(true);
c->LowerBound = values[1];
c->UpperBound = values[2];
c->StepSize = std::max<long>(1, values[3]);
if (values[0] > c->UpperBound)
values[0] = c->UpperBound;
else if (values[0] < c->LowerBound)
values[0] = c->LowerBound;
setConstraints(c);

aboutToSetValue();
_lValue = values[0];
Expand Down Expand Up @@ -1108,7 +1110,7 @@ const boost::any PropertyFloat::getPathValue(const ObjectIdentifier &path) const
// PropertyFloatConstraint
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

TYPESYSTEM_SOURCE(App::PropertyFloatConstraint, App::PropertyFloat);
TYPESYSTEM_SOURCE(App::PropertyFloatConstraint, App::PropertyFloat)

//**************************************************************************
// Construction/Destruction
Expand All @@ -1122,11 +1124,16 @@ PropertyFloatConstraint::PropertyFloatConstraint()

PropertyFloatConstraint::~PropertyFloatConstraint()
{

if (_ConstStruct && _ConstStruct->isDeletable())
delete _ConstStruct;
}

void PropertyFloatConstraint::setConstraints(const Constraints* sConstrain)
{
if (_ConstStruct != sConstrain) {
if (_ConstStruct && _ConstStruct->isDeletable())
delete _ConstStruct;
}
_ConstStruct = sConstrain;
}

Expand Down Expand Up @@ -1186,20 +1193,21 @@ void PropertyFloatConstraint::setPyObject(PyObject *value)
throw Base::TypeError("Type in tuple must be float or int");
}

if (!_ConstStruct) {
Constraints* c = new Constraints();
c->LowerBound = values[1];
c->UpperBound = values[2];
c->StepSize = std::max<double>(0.1, values[3]);
if (values[0] > c->UpperBound)
values[0] = c->UpperBound;
else if (values[0] < c->LowerBound)
values[0] = c->LowerBound;
setConstraints(c);
}
else {
throw Base::RuntimeError("Cannot override limits of constraint");
}
double stepSize = values[3];
// need a value > 0
if (stepSize < DBL_EPSILON)
throw Base::ValueError("Step size must be greater than zero");

Constraints* c = new Constraints();
c->setDeletable(true);
c->LowerBound = values[1];
c->UpperBound = values[2];
c->StepSize = stepSize;
if (values[0] > c->UpperBound)
values[0] = c->UpperBound;
else if (values[0] < c->LowerBound)
values[0] = c->LowerBound;
setConstraints(c);

aboutToSetValue();
_dValue = values[0];
Expand Down
54 changes: 54 additions & 0 deletions src/App/PropertyStandard.h
Expand Up @@ -227,6 +227,33 @@ class AppExport PropertyIntegerConstraint: public PropertyInteger
/// the boundary struct
struct Constraints {
long LowerBound, UpperBound, StepSize;
Constraints()
: LowerBound(0)
, UpperBound(0)
, StepSize(0)
, candelete(false)
{
}
Constraints(long l, long u, long s)
: LowerBound(l)
, UpperBound(u)
, StepSize(s)
, candelete(false)
{
}
~Constraints()
{
}
void setDeletable(bool on)
{
candelete = on;
}
bool isDeletable() const
{
return candelete;
}
private:
bool candelete;
};
/** setting the boundaries
* This sets the constraint struct. It can be dynamically
Expand Down Expand Up @@ -495,6 +522,33 @@ class AppExport PropertyFloatConstraint: public PropertyFloat
/// the boundary struct
struct Constraints {
double LowerBound, UpperBound, StepSize;
Constraints()
: LowerBound(0)
, UpperBound(0)
, StepSize(0)
, candelete(false)
{
}
Constraints(double l, double u, double s)
: LowerBound(l)
, UpperBound(u)
, StepSize(s)
, candelete(false)
{
}
~Constraints()
{
}
void setDeletable(bool on)
{
candelete = on;
}
bool isDeletable() const
{
return candelete;
}
private:
bool candelete;
};
/** setting the boundaries
* This sets the constraint struct. It can be dynamcly
Expand Down

0 comments on commit c4ca27e

Please sign in to comment.