Skip to content

Commit

Permalink
py3: ported Skechter to python3
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer authored and looooo committed Mar 1, 2017
1 parent 3a27378 commit bcde3cd
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 40 deletions.
6 changes: 4 additions & 2 deletions src/Mod/Sketcher/App/AppSketcher.cpp
Expand Up @@ -43,15 +43,15 @@ extern PyObject* initModule();
}

/* Python entry */
PyMODINIT_FUNC initSketcher()
PyMOD_INIT_FUNC(Sketcher)
{
// load dependent module
try {
Base::Interpreter().runString("import Part");
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
PyMOD_Return(0);
}

PyObject* sketcherModule = Sketcher::initModule();
Expand All @@ -73,4 +73,6 @@ PyMODINIT_FUNC initSketcher()
Sketcher::PropertyConstraintList::init();

Base::Console().Log("Loading Sketcher module... done\n");

PyMOD_Return(sketcherModule);
}
4 changes: 2 additions & 2 deletions src/Mod/Sketcher/App/ConstraintPy.xml
Expand Up @@ -20,13 +20,13 @@
<Documentation>
<UserDocu>First geometry index the Constraint refers to</UserDocu>
</Documentation>
<Parameter Name="First" Type="Int"/>
<Parameter Name="First" Type="Long"/>
</Attribute>
<Attribute Name="Second" ReadOnly="false">
<Documentation>
<UserDocu>Second geometry index the Constraint refers to</UserDocu>
</Documentation>
<Parameter Name="Second" Type="Int"/>
<Parameter Name="Second" Type="Long"/>
</Attribute>
<Attribute Name="Value" ReadOnly="true">
<Documentation>
Expand Down
38 changes: 32 additions & 6 deletions src/Mod/Sketcher/App/ConstraintPyImp.cpp
Expand Up @@ -77,8 +77,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/)

if (PyArg_ParseTuple(args, "siO", &ConstraintType, &FirstIndex, &index_or_value)) {
// ConstraintType, GeoIndex1, GeoIndex2
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(index_or_value)) {
SecondIndex = PyLong_AsLong(index_or_value);
#else
if (PyInt_Check(index_or_value)) {
SecondIndex = PyInt_AsLong(index_or_value);
#endif
bool valid = false;
if (strcmp("Tangent",ConstraintType) == 0) {
this->getConstraintPtr()->Type = Tangent;
Expand Down Expand Up @@ -159,9 +164,15 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/)

if (PyArg_ParseTuple(args, "siiO", &ConstraintType, &FirstIndex, &any_index, &index_or_value)) {
// ConstraintType, GeoIndex1, PosIndex1, GeoIndex2
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(index_or_value)) {
FirstPos = any_index;
SecondIndex = PyLong_AsLong(index_or_value);
#else
if (PyInt_Check(index_or_value)) {
FirstPos = any_index;
SecondIndex = PyInt_AsLong(index_or_value);
#endif
bool valid = false;
if (strcmp("Perpendicular", ConstraintType) == 0) {
this->getConstraintPtr()->Type = Perpendicular;
Expand Down Expand Up @@ -245,8 +256,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/)

if (PyArg_ParseTuple(args, "siiiO", &ConstraintType, &intArg1, &intArg2, &intArg3, &oNumArg4)) {
// Value, ConstraintType, GeoIndex1, PosIndex1, GeoIndex2, PosIndex2
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(oNumArg4)) {
intArg4 = PyLong_AsLong(oNumArg4);
#else
if (PyInt_Check(oNumArg4)) {
intArg4 = PyInt_AsLong(oNumArg4);
#endif
bool valid = false;
if (strcmp("Coincident", ConstraintType) == 0) {
this->getConstraintPtr()->Type = Coincident;
Expand Down Expand Up @@ -337,8 +353,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/)

if (PyArg_ParseTuple(args, "siiiiO", &ConstraintType, &intArg1, &intArg2, &intArg3, &intArg4, &oNumArg5)) {
// ConstraintType, GeoIndex1, PosIndex1, GeoIndex2, PosIndex2, GeoIndex3
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(oNumArg5)) {
intArg5 = PyLong_AsLong(oNumArg5);
#else
if (PyInt_Check(oNumArg5)) {
intArg5 = PyInt_AsLong(oNumArg5);
#endif
if (strcmp("Symmetric",ConstraintType) == 0 ) {
this->getConstraintPtr()->Type = Symmetric;
this->getConstraintPtr()->First = intArg1;
Expand Down Expand Up @@ -404,8 +425,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/)
PyErr_Clear();

if (PyArg_ParseTuple(args, "siiiiiO", &ConstraintType, &FirstIndex, &FirstPos, &SecondIndex, &SecondPos, &ThirdIndex, &index_or_value)) {
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(index_or_value)) {
ThirdPos = PyLong_AsLong(index_or_value);
#else
if (PyInt_Check(index_or_value)) {
ThirdPos = PyInt_AsLong(index_or_value);
#endif
// ConstraintType, GeoIndex1, PosIndex1, GeoIndex2, PosIndex2, GeoIndex3, PosIndex3
if (strcmp("Symmetric",ConstraintType) == 0 ) {
this->getConstraintPtr()->Type = Symmetric;
Expand Down Expand Up @@ -495,22 +521,22 @@ std::string ConstraintPy::representation(void) const
return result.str();
}

Py::Int ConstraintPy::getFirst(void) const
Py::Long ConstraintPy::getFirst(void) const
{
return Py::Int(this->getConstraintPtr()->First);
return Py::Long(this->getConstraintPtr()->First);
}

void ConstraintPy::setFirst(Py::Int arg)
void ConstraintPy::setFirst(Py::Long arg)
{
this->getConstraintPtr()->First = arg;
}

Py::Int ConstraintPy::getSecond(void) const
Py::Long ConstraintPy::getSecond(void) const
{
return Py::Int(this->getConstraintPtr()->Second);
return Py::Long(this->getConstraintPtr()->Second);
}

void ConstraintPy::setSecond(Py::Int arg)
void ConstraintPy::setSecond(Py::Long arg)
{
this->getConstraintPtr()->Second = arg;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mod/Sketcher/App/SketchObjectPy.xml
Expand Up @@ -228,21 +228,21 @@
<Documentation>
<UserDocu>Number of Constraints in this sketch</UserDocu>
</Documentation>
<Parameter Name="ConstraintCount" Type="Int"/>
<Parameter Name="ConstraintCount" Type="Long"/>
</Attribute>
<Attribute Name="GeometryCount" ReadOnly="true">
<Documentation>
<UserDocu>Number of geometric objects in this sketch</UserDocu>
</Documentation>
<Parameter Name="GeometryCount" Type="Int"/>
<Parameter Name="GeometryCount" Type="Long"/>
</Attribute>
<Attribute Name="AxisCount" ReadOnly="true">
<Documentation>
<UserDocu>
return the the number of construction lines in the sketch which can be used as axes
</UserDocu>
</Documentation>
<Parameter Name="AxisCount" Type="Int"/>
<Parameter Name="AxisCount" Type="Long"/>
</Attribute>
</PythonExport>
</GenerateModel>
41 changes: 28 additions & 13 deletions src/Mod/Sketcher/App/SketchObjectPyImp.cpp
Expand Up @@ -124,7 +124,7 @@ PyObject* SketchObjectPy::addGeometry(PyObject *args)
PyErr_SetString(PyExc_TypeError, str.str().c_str());
return 0;
}
return Py::new_reference_to(Py::Int(ret));
return Py::new_reference_to(Py::Long(ret));
}
else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) ||
PyObject_TypeCheck(pcObj, &(PyTuple_Type))) {
Expand Down Expand Up @@ -186,7 +186,7 @@ PyObject* SketchObjectPy::addGeometry(PyObject *args)
Py::Tuple tuple(numGeo);
for (std::size_t i=0; i<numGeo; ++i) {
int geoId = ret - int(numGeo - i);
tuple.setItem(i, Py::Int(geoId));
tuple.setItem(i, Py::Long(geoId));
}

return Py::new_reference_to(tuple);
Expand Down Expand Up @@ -278,7 +278,7 @@ PyObject* SketchObjectPy::addConstraint(PyObject *args)
// this forces recalculation of the initial solution (not a full solve)
if(this->getSketchObjectPtr()->noRecomputes)
this->getSketchObjectPtr()->setUpSketch();
return Py::new_reference_to(Py::Int(ret));
return Py::new_reference_to(Py::Long(ret));
}
else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) ||
PyObject_TypeCheck(pcObj, &(PyTuple_Type))) {
Expand All @@ -302,7 +302,7 @@ PyObject* SketchObjectPy::addConstraint(PyObject *args)
Py::Tuple tuple(numCon);
for (std::size_t i=0; i<numCon; ++i) {
int conId = ret - int(numCon - i);
tuple.setItem(i, Py::Int(conId));
tuple.setItem(i, Py::Long(conId));
}
return Py::new_reference_to(tuple);
}
Expand Down Expand Up @@ -802,8 +802,13 @@ PyObject* SketchObjectPy::addSymmetric(PyObject *args)
std::vector<int> geoIdList;
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check((*it).ptr()))
geoIdList.push_back(PyLong_AsLong((*it).ptr()));
#else
if (PyInt_Check((*it).ptr()))
geoIdList.push_back(PyInt_AsLong((*it).ptr()));
#endif
}

int ret = this->getSketchObjectPtr()->addSymmetric(geoIdList,refGeoId,(Sketcher::PointPos) refPosId) + 1;
Expand All @@ -815,7 +820,7 @@ PyObject* SketchObjectPy::addSymmetric(PyObject *args)
Py::Tuple tuple(numGeo);
for (std::size_t i=0; i<numGeo; ++i) {
int geoId = ret - int(numGeo - i);
tuple.setItem(i, Py::Int(geoId));
tuple.setItem(i, Py::Long(geoId));
}

return Py::new_reference_to(tuple);
Expand All @@ -841,8 +846,13 @@ PyObject* SketchObjectPy::addCopy(PyObject *args)
std::vector<int> geoIdList;
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check((*it).ptr()))
geoIdList.push_back(PyLong_AsLong((*it).ptr()));
#else
if (PyInt_Check((*it).ptr()))
geoIdList.push_back(PyInt_AsLong((*it).ptr()));
#endif
}

int ret = this->getSketchObjectPtr()->addCopy(geoIdList, vect, PyObject_IsTrue(clone) ? true : false) + 1;
Expand All @@ -854,7 +864,7 @@ PyObject* SketchObjectPy::addCopy(PyObject *args)
Py::Tuple tuple(numGeo);
for (std::size_t i=0; i<numGeo; ++i) {
int geoId = ret - int(numGeo - i);
tuple.setItem(i, Py::Int(geoId));
tuple.setItem(i, Py::Long(geoId));
}

return Py::new_reference_to(tuple);
Expand Down Expand Up @@ -884,8 +894,13 @@ PyObject* SketchObjectPy::addRectangularArray(PyObject *args)
std::vector<int> geoIdList;
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check((*it).ptr()))
geoIdList.push_back(PyLong_AsLong((*it).ptr()));
#else
if (PyInt_Check((*it).ptr()))
geoIdList.push_back(PyInt_AsLong((*it).ptr()));
#endif
}

int ret = this->getSketchObjectPtr()->addCopy(geoIdList,vect, PyObject_IsTrue(clone) ? true : false,
Expand Down Expand Up @@ -961,7 +976,7 @@ PyObject* SketchObjectPy::changeConstraintsLocking(PyObject *args)

int naff = obj->changeConstraintsLocking((bool)bLock);

return Py::new_reference_to(Py::Int(naff));
return Py::new_reference_to(Py::Long(naff));
}

//Deprecated
Expand Down Expand Up @@ -1069,19 +1084,19 @@ PyObject* SketchObjectPy::increaseBSplineDegree(PyObject *args)
Py_Return;
}

Py::Int SketchObjectPy::getConstraintCount(void) const
Py::Long SketchObjectPy::getConstraintCount(void) const
{
return Py::Int(this->getSketchObjectPtr()->Constraints.getSize());
return Py::Long(this->getSketchObjectPtr()->Constraints.getSize());
}

Py::Int SketchObjectPy::getGeometryCount(void) const
Py::Long SketchObjectPy::getGeometryCount(void) const
{
return Py::Int(this->getSketchObjectPtr()->Geometry.getSize());
return Py::Long(this->getSketchObjectPtr()->Geometry.getSize());
}

Py::Int SketchObjectPy::getAxisCount(void) const
Py::Long SketchObjectPy::getAxisCount(void) const
{
return Py::Int(this->getSketchObjectPtr()->getAxisCount());
return Py::Long(this->getSketchObjectPtr()->getAxisCount());
}

PyObject *SketchObjectPy::getCustomAttributes(const char* /*attr*/) const
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Sketcher/App/SketchPy.xml
Expand Up @@ -54,7 +54,7 @@
<Documentation>
<UserDocu>0: exactly constraint, -1 under-constraint, 1 over-constraint</UserDocu>
</Documentation>
<Parameter Name="Constraint" Type="Int"/>
<Parameter Name="Constraint" Type="Long"/>
</Attribute>
<Attribute Name="Conflicts" ReadOnly="true">
<Documentation>
Expand Down
14 changes: 7 additions & 7 deletions src/Mod/Sketcher/App/SketchPyImp.cpp
Expand Up @@ -65,7 +65,7 @@ PyObject* SketchPy::solve(PyObject *args)
if (!PyArg_ParseTuple(args, ""))
return 0;
getSketchPtr()->resetSolver();
return Py::new_reference_to(Py::Int(getSketchPtr()->solve()));
return Py::new_reference_to(Py::Long(getSketchPtr()->solve()));
}

PyObject* SketchPy::addGeometry(PyObject *args)
Expand All @@ -76,7 +76,7 @@ PyObject* SketchPy::addGeometry(PyObject *args)

if (PyObject_TypeCheck(pcObj, &(Part::GeometryPy::Type))) {
Part::Geometry *geo = static_cast<Part::GeometryPy*>(pcObj)->getGeometryPtr();
return Py::new_reference_to(Py::Int(this->getSketchPtr()->addGeometry(geo)));
return Py::new_reference_to(Py::Long(this->getSketchPtr()->addGeometry(geo)));
}
else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) ||
PyObject_TypeCheck(pcObj, &(PyTuple_Type))) {
Expand All @@ -94,7 +94,7 @@ PyObject* SketchPy::addGeometry(PyObject *args)
Py::Tuple tuple(numGeo);
for (std::size_t i=0; i<numGeo; ++i) {
int geoId = ret - int(numGeo - i);
tuple.setItem(i, Py::Int(geoId));
tuple.setItem(i, Py::Long(geoId));
}
return Py::new_reference_to(tuple);
}
Expand Down Expand Up @@ -125,14 +125,14 @@ PyObject* SketchPy::addConstraint(PyObject *args)
Py::Tuple tuple(numCon);
for (std::size_t i=0; i<numCon; ++i) {
int conId = ret - int(numCon - i);
tuple.setItem(i, Py::Int(conId));
tuple.setItem(i, Py::Long(conId));
}
return Py::new_reference_to(tuple);
}
else if(PyObject_TypeCheck(pcObj, &(ConstraintPy::Type))) {
ConstraintPy *pcObject = static_cast<ConstraintPy*>(pcObj);
int ret = getSketchPtr()->addConstraint(pcObject->getConstraintPtr());
return Py::new_reference_to(Py::Int(ret));
return Py::new_reference_to(Py::Long(ret));
}
else {
std::string error = std::string("type must be 'Constraint' or list of 'Constraint', not ");
Expand Down Expand Up @@ -160,12 +160,12 @@ PyObject* SketchPy::movePoint(PyObject *args)
return 0;
Base::Vector3d* toPoint = static_cast<Base::VectorPy*>(pcObj)->getVectorPtr();

return Py::new_reference_to(Py::Int(getSketchPtr()->movePoint(index1,(Sketcher::PointPos)index2,*toPoint,(relative>0))));
return Py::new_reference_to(Py::Long(getSketchPtr()->movePoint(index1,(Sketcher::PointPos)index2,*toPoint,(relative>0))));
}

// +++ attributes implementer ++++++++++++++++++++++++++++++++++++++++++++++++

Py::Int SketchPy::getConstraint(void) const
Py::Long SketchPy::getConstraint(void) const
{
//return Py::Int();
throw Py::AttributeError("Not yet implemented");
Expand Down

0 comments on commit bcde3cd

Please sign in to comment.