From 31189b6c208c1b089af11ed9f054f8e212654cd3 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sun, 17 Dec 2017 05:57:35 +0100 Subject: [PATCH] Sketcher: Python Interface for Constraint Virtual Space ======================================================= set/get/toggle python interface to enable to set the virtual space status of a constraint via SketchObject. --- src/Mod/Sketcher/App/SketchObject.cpp | 56 +++++++++++++++++++++ src/Mod/Sketcher/App/SketchObject.h | 6 +++ src/Mod/Sketcher/App/SketchObjectPy.xml | 17 ++++++- src/Mod/Sketcher/App/SketchObjectPyImp.cpp | 50 ++++++++++++++++++ src/Mod/Sketcher/Gui/ViewProviderSketch.cpp | 4 +- 5 files changed, 130 insertions(+), 3 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 768fc1ef99..1772d93402 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -417,6 +417,62 @@ int SketchObject::toggleDriving(int ConstrId) return 0; } +int SketchObject::setVirtualSpace(int ConstrId, bool isinvirtualspace) +{ + const std::vector &vals = this->Constraints.getValues(); + + if (ConstrId < 0 || ConstrId >= int(vals.size())) + return -1; + + // copy the list + std::vector newVals(vals); + + // clone the changed Constraint + Constraint *constNew = vals[ConstrId]->clone(); + constNew->isInVirtualSpace = isinvirtualspace; + newVals[ConstrId] = constNew; + + this->Constraints.setValues(newVals); + + delete constNew; + + return 0; +} + +int SketchObject::getVirtualSpace(int ConstrId, bool &isinvirtualspace) +{ + const std::vector &vals = this->Constraints.getValues(); + + if (ConstrId < 0 || ConstrId >= int(vals.size())) + return -1; + + isinvirtualspace=vals[ConstrId]->isInVirtualSpace; + return 0; +} + +int SketchObject::toggleVirtualSpace(int ConstrId) +{ + const std::vector &vals = this->Constraints.getValues(); + + if (ConstrId < 0 || ConstrId >= int(vals.size())) + return -1; + + // copy the list + std::vector newVals(vals); + + // clone the changed Constraint + Constraint *constNew = vals[ConstrId]->clone(); + constNew->isInVirtualSpace = !constNew->isInVirtualSpace; + newVals[ConstrId] = constNew; + + this->Constraints.setValues(newVals); + + delete constNew; + + return 0; +} + + int SketchObject::setUpSketch() { return solvedSketch.setUpSketch(getCompleteGeometry(), Constraints.getValues(), diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index e1ccb241e6..f71b28ac6b 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -169,6 +169,12 @@ public: int getDriving(int ConstrId, bool &isdriving); /// toggle the driving status of this constraint int toggleDriving(int ConstrId); + /// set the driving status of this constraint and solve + int setVirtualSpace(int ConstrId, bool isinvirtualspace); + /// get the driving status of this constraint + int getVirtualSpace(int ConstrId, bool &isinvirtualspace); + /// toggle the driving status of this constraint + int toggleVirtualSpace(int ConstrId); /// move this point to a new location and solve int movePoint(int GeoId, PointPos PosId, const Base::Vector3d& toPoint, bool relative=false, bool updateGeoBeforeMoving=false); /// retrieves the coordinates of a point diff --git a/src/Mod/Sketcher/App/SketchObjectPy.xml b/src/Mod/Sketcher/App/SketchObjectPy.xml index 917f6496f6..f357f8431e 100644 --- a/src/Mod/Sketcher/App/SketchObjectPy.xml +++ b/src/Mod/Sketcher/App/SketchObjectPy.xml @@ -102,7 +102,22 @@ toggle the Driving status of a datum constraint - + + + + set the VirtualSpace status of a constraint + + + + + Get the VirtualSpace status of a constraint + + + + + toggle the VirtualSpace status of a constraint + + diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index f8504a5860..b0e6174b0f 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -743,6 +743,56 @@ PyObject* SketchObjectPy::toggleDriving(PyObject *args) Py_Return; } +PyObject* SketchObjectPy::setVirtualSpace(PyObject *args) +{ + PyObject* invirtualspace; + int constrid; + + if (!PyArg_ParseTuple(args, "iO!", &constrid, &PyBool_Type, &invirtualspace)) + return 0; + + if (this->getSketchObjectPtr()->setVirtualSpace(constrid, PyObject_IsTrue(invirtualspace) ? true : false)) { + std::stringstream str; + str << "Not able set virtual space for constraint with the given index: " << constrid; + PyErr_SetString(PyExc_ValueError, str.str().c_str()); + return 0; + } + + Py_Return; +} + +PyObject* SketchObjectPy::getVirtualSpace(PyObject *args) +{ + int constrid; + bool invirtualspace; + + if (!PyArg_ParseTuple(args, "i", &constrid)) + return 0; + + if (this->getSketchObjectPtr()->getVirtualSpace(constrid, invirtualspace)) { + PyErr_SetString(PyExc_ValueError, "Invalid constraint id"); + return 0; + } + + return Py::new_reference_to(Py::Boolean(invirtualspace)); +} + +PyObject* SketchObjectPy::toggleVirtualSpace(PyObject *args) +{ + int constrid; + + if (!PyArg_ParseTuple(args, "i", &constrid)) + return 0; + + if (this->getSketchObjectPtr()->toggleVirtualSpace(constrid)) { + std::stringstream str; + str << "Not able toggle virtual space for constraint with the given index: " << constrid; + PyErr_SetString(PyExc_ValueError, str.str().c_str()); + return 0; + } + + Py_Return; +} PyObject* SketchObjectPy::movePoint(PyObject *args) { diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index a53b20c453..86a903d384 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -4036,6 +4036,8 @@ Restart: rebuildConstraintsVisual(); assert(int(constrlist.size()) == edit->constrGroup->getNumChildren()); assert(int(edit->vConstrType.size()) == edit->constrGroup->getNumChildren()); + // update the virtual space + updateVirtualSpace(); // go through the constraints and update the position i = 0; for (std::vector::const_iterator it=constrlist.begin(); @@ -5132,8 +5134,6 @@ void ViewProviderSketch::rebuildConstraintsVisual(void) sep->unref(); mat->unref(); } - - updateVirtualSpace(); } void ViewProviderSketch::updateVirtualSpace(void)