diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index 71607a2fe424..ecf5969b71eb 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -292,20 +292,91 @@ PyObject* SketchObjectPy::setDatum(PyObject *args) int Index; PyObject* object; Base::Quantity Quantity; - if (PyArg_ParseTuple(args,"iO!", &Index, &(Base::QuantityPy::Type), &object)) { - Quantity = *(static_cast(object)->getQuantityPtr()); - if (Quantity.getUnit() == Base::Unit::Angle) - //Datum = Quantity.getValueAs(Base::Quantity::Radian); - Datum = Base::toRadians(Quantity.getValue()); - else - Datum = Quantity.getValue(); - } - else { + + do { + // handle (int,Quantity) + if (PyArg_ParseTuple(args,"iO!", &Index, &(Base::QuantityPy::Type), &object)) { + Quantity = *(static_cast(object)->getQuantityPtr()); + if (Quantity.getUnit() == Base::Unit::Angle) { + Datum = Base::toRadians(Quantity.getValue()); + break; + } + else { + Datum = Quantity.getValue(); + break; + } + } + + // handle (int,double) PyErr_Clear(); - if (!PyArg_ParseTuple(args, "id", &Index, &Datum)) - return 0; - Quantity.setValue(Datum); + if (PyArg_ParseTuple(args, "id", &Index, &Datum)) { + Quantity.setValue(Datum); + break; + } + + // handle (string,Quantity) + char* constrName; + PyErr_Clear(); + if (PyArg_ParseTuple(args,"sO!", &constrName, &(Base::QuantityPy::Type), &object)) { + Quantity = *(static_cast(object)->getQuantityPtr()); + if (Quantity.getUnit() == Base::Unit::Angle) { + Datum = Base::toRadians(Quantity.getValue()); + } + else { + Datum = Quantity.getValue(); + } + + int i = 0; + Index = -1; + const std::vector& vals = this->getSketchObjectPtr()->Constraints.getValues(); + for (std::vector::const_iterator it = vals.begin(); it != vals.end(); ++it, ++i) { + if ((*it)->Name == constrName) { + Index = i; + break; + } + } + + if (Index >= 0) { + break; + } + else { + std::stringstream str; + str << "Invalid constraint name: '" << constrName << "'"; + PyErr_SetString(PyExc_ValueError, str.str().c_str()); + return 0; + } + } + + // handle (string,double) + PyErr_Clear(); + if (PyArg_ParseTuple(args, "sd", &constrName, &Datum)) { + Quantity.setValue(Datum); + int i = 0; + Index = -1; + const std::vector& vals = this->getSketchObjectPtr()->Constraints.getValues(); + for (std::vector::const_iterator it = vals.begin(); it != vals.end(); ++it, ++i) { + if ((*it)->Name == constrName) { + Index = i; + break; + } + } + + if (Index >= 0) { + break; + } + else { + std::stringstream str; + str << "Invalid constraint name: '" << constrName << "'"; + PyErr_SetString(PyExc_ValueError, str.str().c_str()); + return 0; + } + } + + // error handling + PyErr_SetString(PyExc_TypeError, "Wrong arguments"); + return 0; } + while (false); int err=this->getSketchObjectPtr()->setDatum(Index, Datum); if (err) { diff --git a/src/Mod/Spreadsheet/Spreadsheet.py b/src/Mod/Spreadsheet/Spreadsheet.py index 87894786189a..917b5b34161c 100644 --- a/src/Mod/Spreadsheet/Spreadsheet.py +++ b/src/Mod/Spreadsheet/Spreadsheet.py @@ -685,6 +685,7 @@ def compute(self,obj): else: if Draft.getType(obj.TargetObject) == "Sketch": if obj.TargetProperty.isdigit(): + # try setting by constraint id try: c = int(obj.TargetProperty) obj.TargetObject.setDatum(c,float(value)) @@ -692,6 +693,14 @@ def compute(self,obj): if DEBUG: print "setting constraint ",obj.TargetProperty, " of object ",obj.TargetObject.Name, " to ",value except: if DEBUG: print "unable to set constraint ",obj.TargetProperty, " of object ",obj.TargetObject.Name, " to ",value + else: + # try setting by constraint name + try: + obj.TargetObject.setDatum(obj.TargetProperty,float(value)) + FreeCAD.ActiveDocument.recompute() + if DEBUG: print "setting constraint ",obj.TargetProperty, " of object ",obj.TargetObject.Name, " to ",value + except: + if DEBUG: print "unable to set constraint ",obj.TargetProperty, " of object ",obj.TargetObject.Name, " to ",value def __getstate__(self):