Skip to content

Commit

Permalink
+ fixes #1819: Getter Methods for Data inside a sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Dec 29, 2014
1 parent 84aad26 commit f51daa8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Mod/Sketcher/App/SketchObjectPy.xml
Expand Up @@ -73,6 +73,11 @@
<UserDocu>set the Datum of a Distance or Angle constraint</UserDocu>
</Documentation>
</Methode>
<Methode Name="getDatum">
<Documentation>
<UserDocu>Get the value of a datum constraint</UserDocu>
</Documentation>
</Methode>
<Methode Name="movePoint">
<Documentation>
<UserDocu>
Expand Down
74 changes: 74 additions & 0 deletions src/Mod/Sketcher/App/SketchObjectPyImp.cpp
Expand Up @@ -494,6 +494,80 @@ PyObject* SketchObjectPy::setDatum(PyObject *args)
Py_Return;
}

PyObject* SketchObjectPy::getDatum(PyObject *args)
{
const std::vector<Constraint*>& vals = this->getSketchObjectPtr()->Constraints.getValues();
Constraint* constr = 0;

do {
int index = 0;
if (PyArg_ParseTuple(args,"i", &index)) {
if (index < 0 || index >= static_cast<int>(vals.size())) {
PyErr_SetString(PyExc_IndexError, "index out of range");
return 0;
}

constr = vals[index];
break;
}

PyErr_Clear();
char* name;
if (PyArg_ParseTuple(args,"s", &name)) {
int id = 1;
for (std::vector<Constraint*>::const_iterator it = vals.begin(); it != vals.end(); ++it, ++id) {
std::string constrName = (*it)->Name;
if (constrName.empty()) {
std::stringstream str;
str << "Constraint" << id;
constrName = str.str();
}
if (constrName == name) {
constr = *it;
break;
}
}

if (!constr) {
std::stringstream str;
str << "Invalid constraint name: '" << name << "'";
PyErr_SetString(PyExc_NameError, str.str().c_str());
return 0;
}
else {
break;
}
}

// error handling
PyErr_SetString(PyExc_TypeError, "Wrong arguments");
return 0;
}
while (false);

ConstraintType type = constr->Type;
if (type != Distance &&
type != DistanceX &&
type != DistanceY &&
type != Radius &&
type != Angle) {
PyErr_SetString(PyExc_TypeError, "Constraint is not a datum");
return 0;
}

Base::Quantity datum;
datum.setValue(constr->Value);
if (type == Angle) {
datum.setValue(Base::toDegrees<double>(datum.getValue()));
datum.setUnit(Base::Unit::Angle);
}
else {
datum.setUnit(Base::Unit::Length);
}

return new Base::QuantityPy(new Base::Quantity(datum));
}

PyObject* SketchObjectPy::movePoint(PyObject *args)
{
PyObject *pcObj;
Expand Down

0 comments on commit f51daa8

Please sign in to comment.