Sketcher: Extend SketchObject Python Interface to get/set SketchGeometryExtension GeometryId (convenience interface)

This commit is contained in:
Abdullah Tahiri
2020-10-28 11:48:07 +01:00
committed by abdullahtahiriyo
parent d994e2fe3b
commit 063e0a6767
4 changed files with 107 additions and 0 deletions

View File

@@ -78,6 +78,8 @@
#include <Mod/Part/App/DatumFeature.h>
#include <Mod/Part/App/BodyBase.h>
#include "GeometryFacade.h"
#include "SketchObject.h"
#include "Sketch.h"
#include <Mod/Sketcher/App/SketchObjectPy.h>
@@ -7657,6 +7659,59 @@ std::vector<Base::Vector3d> SketchObject::getOpenVertices(void) const
return points;
}
// SketchGeometryExtension interface
int SketchObject::setGeometryId(int GeoId, long id)
{
Base::StateLocker lock(managedoperation, true); // no need to check input data validity as this is an sketchobject managed operation.
if (GeoId < 0 || GeoId >= int(Geometry.getValues().size()))
return -1;
const std::vector< Part::Geometry * > &vals = getInternalGeometry();
std::vector< Part::Geometry * > newVals(vals);
// deep copy
for(size_t i=0; i<newVals.size(); i++) {
newVals[i] = newVals[i]->clone();
if((int)i == GeoId) {
auto gf = GeometryFacade::getFacade(newVals[i]);
gf->setId(id);
}
}
// There is not actual internal transaction going on here, however neither the geometry indices nor the vertices need to be updated
// so this is a convenient way of preventing it.
{
Base::StateLocker lock(internaltransaction, true);
this->Geometry.setValues(std::move(newVals));
}
return 0;
}
int SketchObject::getGeometryId(int GeoId, long &id) const
{
if (GeoId < 0 || GeoId >= int(Geometry.getValues().size()))
return -1;
const std::vector< Part::Geometry * > &vals = getInternalGeometry();
auto gf = GeometryFacade::getFacade(vals[GeoId]);
id = gf->getId();
return 0;
}
// Python Sketcher feature ---------------------------------------------------------
namespace App {

View File

@@ -418,6 +418,10 @@ public:
// Validation routines
std::vector<Base::Vector3d> getOpenVertices(void) const;
public: // geometry extension functionalities for single element sketch object user convenience
int setGeometryId(int GeoId, long id);
int getGeometryId(int GeoId, long &id) const;
protected:
/// get called by the container when a property has changed
virtual void onChanged(const App::Property* /*prop*/) override;

View File

@@ -458,5 +458,15 @@ If there is no such constraint an exception is raised.
</Documentation>
<Parameter Name="GeometryFacadeList" Type="List"/>
</Attribute>
<Methode Name="setGeometryId">
<Documentation>
<UserDocu>switch a geometry to a construction line</UserDocu>
</Documentation>
</Methode>
<Methode Name="getGeometryId">
<Documentation>
<UserDocu>switch a geometry to a construction line</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@@ -1792,6 +1792,44 @@ void SketchObjectPy::setGeometryFacadeList(Py::List value)
getSketchObjectPtr()->Geometry.setValues(std::move(list));
}
PyObject* SketchObjectPy::getGeometryId(PyObject *args)
{
int Index;
if (!PyArg_ParseTuple(args, "i", &Index))
return 0;
long Id;
if (this->getSketchObjectPtr()->getGeometryId(Index, Id)) {
std::stringstream str;
str << "Not able to set geometry Id of a geometry with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
Py_Return;
}
return Py::new_reference_to(Py::Long(Id));
}
PyObject* SketchObjectPy::setGeometryId(PyObject *args)
{
int Index;
long Id;
if (!PyArg_ParseTuple(args, "il", &Index, &Id))
return 0;
if (this->getSketchObjectPtr()->setGeometryId(Index, Id)) {
std::stringstream str;
str << "Not able to set construction mode of a geometry with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject *SketchObjectPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;