diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 1bde4c70f..330ff5ca6 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -141,7 +141,7 @@ int SketchObject::setDatum(int ConstrId, double Datum) { // set the changed value for the constraint const std::vector &vals = this->Constraints.getValues(); - if (ConstrId < 0 || ConstrId >= (int)vals.size()) + if (ConstrId < 0 || ConstrId >= int(vals.size())) return -1; ConstraintType type = vals[ConstrId]->Type; if (type != Distance && @@ -303,7 +303,7 @@ int SketchObject::addGeometry(const Part::Geometry *geo) int SketchObject::delGeometry(int GeoId) { const std::vector< Part::Geometry * > &vals = getInternalGeometry(); - if (GeoId < 0 || GeoId >= (int)vals.size()) + if (GeoId < 0 || GeoId >= int(vals.size())) return -1; std::vector< Part::Geometry * > newVals(vals); @@ -333,7 +333,7 @@ int SketchObject::delGeometry(int GeoId) int SketchObject::toggleConstruction(int GeoId) { const std::vector< Part::Geometry * > &vals = getInternalGeometry(); - if (GeoId < 0 || GeoId >= (int)vals.size()) + if (GeoId < 0 || GeoId >= int(vals.size())) return -1; std::vector< Part::Geometry * > newVals(vals); @@ -367,7 +367,7 @@ int SketchObject::addConstraint(const Constraint *constraint) int SketchObject::delConstraint(int ConstrId) { const std::vector< Constraint * > &vals = this->Constraints.getValues(); - if (ConstrId < 0 || ConstrId >= (int)vals.size()) + if (ConstrId < 0 || ConstrId >= int(vals.size())) return -1; std::vector< Constraint * > newVals(vals); @@ -1039,14 +1039,14 @@ int SketchObject::addExternal(App::DocumentObject *Obj, const char* SubName) std::vector Objects = ExternalGeometry.getValues(); std::vector SubElements = ExternalGeometry.getSubValues(); - std::vector originalObjects = Objects; - std::vector originalSubElements = SubElements; + const std::vector originalObjects = Objects; + const std::vector originalSubElements = SubElements; - std::vector ::iterator it; - it = std::find(originalSubElements.begin(), originalSubElements.end(), SubName); + std::vector::iterator it; + it = std::find(SubElements.begin(), SubElements.end(), SubName); // avoid duplicates - if (it != originalSubElements.end()) + if (it != SubElements.end()) return -1; // add the new ones @@ -1055,7 +1055,6 @@ int SketchObject::addExternal(App::DocumentObject *Obj, const char* SubName) // set the Link list. ExternalGeometry.setValues(Objects,SubElements); - try { rebuildExternalGeometry(); } @@ -1065,6 +1064,7 @@ int SketchObject::addExternal(App::DocumentObject *Obj, const char* SubName) ExternalGeometry.setValues(originalObjects,originalSubElements); return -1; } + Constraints.acceptGeometry(getCompleteGeometry()); rebuildVertexIndex(); return ExternalGeometry.getValues().size()-1; @@ -1072,9 +1072,51 @@ int SketchObject::addExternal(App::DocumentObject *Obj, const char* SubName) int SketchObject::delExternal(int ExtGeoId) { - // FIXME: still to implement - return 0; + // get the actual lists of the externals + std::vector Objects = ExternalGeometry.getValues(); + std::vector SubElements = ExternalGeometry.getSubValues(); + if (ExtGeoId < 0 || ExtGeoId >= int(SubElements.size())) + return -1; + + const std::vector originalObjects = Objects; + const std::vector originalSubElements = SubElements; + + Objects.erase(Objects.begin()+ExtGeoId); + SubElements.erase(SubElements.begin()+ExtGeoId); + + const std::vector< Constraint * > &constraints = Constraints.getValues(); + std::vector< Constraint * > newConstraints(0); + int GeoId = -3 - ExtGeoId; + for (std::vector::const_iterator it = constraints.begin(); + it != constraints.end(); ++it) { + if ((*it)->First != GeoId && (*it)->Second != GeoId) { + Constraint *copiedConstr = (*it)->clone(); + if (copiedConstr->First < GeoId && + copiedConstr->First != Constraint::GeoUndef) + copiedConstr->First += 1; + if (copiedConstr->Second < GeoId && + copiedConstr->Second != Constraint::GeoUndef) + copiedConstr->Second += 1; + newConstraints.push_back(copiedConstr); + } + } + + ExternalGeometry.setValues(Objects,SubElements); + try { + rebuildExternalGeometry(); + } + catch (const Base::Exception& e) { + Base::Console().Error("%s\n", e.what()); + // revert to original values + ExternalGeometry.setValues(originalObjects,originalSubElements); + return -1; + } + + Constraints.setValues(newConstraints); + Constraints.acceptGeometry(getCompleteGeometry()); + rebuildVertexIndex(); + return 0; } const Part::Geometry* SketchObject::getGeometry(int GeoId) const @@ -1095,8 +1137,6 @@ void SketchObject::rebuildExternalGeometry(void) // get the actual lists of the externals std::vector Objects = ExternalGeometry.getValues(); std::vector SubElements = ExternalGeometry.getSubValues(); - if (Objects.size() == 0) - return; Base::Placement Plm = Placement.getValue(); Base::Vector3d Pos = Plm.getPosition(); @@ -1403,7 +1443,7 @@ void SketchObject::onDocumentRestored() void SketchObject::getGeoVertexIndex(int VertexId, int &GeoId, PointPos &PosId) { - if (VertexId < 0 || VertexId >= (int)VertexId2GeoId.size()) { + if (VertexId < 0 || VertexId >= int(VertexId2GeoId.size())) { GeoId = Constraint::GeoUndef; PosId = none; return; diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index 84f2dd0c7..eef799bf7 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -76,14 +76,17 @@ public: int transferConstraints(int fromGeoId, PointPos fromPosId, int toGeoId, PointPos toPosId); /// add an external geometry reference int addExternal(App::DocumentObject *Obj, const char* SubName); - /// delete external + /** delete external + * ExtGeoId >= 0 with 0 corresponding to the first user defined + * external geometry + */ int delExternal(int ExtGeoId); /** returns a pointer to a given Geometry index, possible indexes are: * id>=0 for user defined geometries, * id==-1 for the horizontal sketch axis, * id==-2 for the vertical sketch axis - * id<=-3 for projected external geometries, + * id<=-3 for user defined projected external geometries, */ const Part::Geometry* getGeometry(int GeoId) const; /// returns a list of all internal geometries diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index d7ecedd90..5f093872f 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -164,8 +164,18 @@ PyObject* SketchObjectPy::addExternal(PyObject *args) PyObject* SketchObjectPy::delExternal(PyObject *args) { - PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented"); - return 0; + int Index; + if (!PyArg_ParseTuple(args, "i", &Index)) + return 0; + + if (this->getSketchObjectPtr()->delExternal(Index)) { + std::stringstream str; + str << "Not able to delete an external geometry with the given index: " << Index; + PyErr_SetString(PyExc_ValueError, str.str().c_str()); + return 0; + } + + Py_Return; } PyObject* SketchObjectPy::delConstraintOnPoint(PyObject *args)