py3: ported Skechter to python3

This commit is contained in:
wmayer 2016-01-23 19:04:03 +01:00 committed by looooo
parent 3a27378844
commit bcde3cd5a3
9 changed files with 91 additions and 40 deletions

View File

@ -43,7 +43,7 @@ extern PyObject* initModule();
}
/* Python entry */
PyMODINIT_FUNC initSketcher()
PyMOD_INIT_FUNC(Sketcher)
{
// load dependent module
try {
@ -51,7 +51,7 @@ PyMODINIT_FUNC initSketcher()
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
PyMOD_Return(0);
}
PyObject* sketcherModule = Sketcher::initModule();
@ -73,4 +73,6 @@ PyMODINIT_FUNC initSketcher()
Sketcher::PropertyConstraintList::init();
Base::Console().Log("Loading Sketcher module... done\n");
PyMOD_Return(sketcherModule);
}

View File

@ -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>

View File

@ -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;
@ -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;
@ -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;
@ -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;
@ -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;
@ -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;
}

View File

@ -228,13 +228,13 @@
<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>
@ -242,7 +242,7 @@
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>

View File

@ -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))) {
@ -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);
@ -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))) {
@ -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);
}
@ -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;
@ -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);
@ -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;
@ -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);
@ -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,
@ -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
@ -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

View File

@ -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>

View File

@ -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)
@ -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))) {
@ -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);
}
@ -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 ");
@ -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");

View File

@ -72,14 +72,20 @@ public:
private:
};
PyObject* initModule()
{
return (new Module)->module().ptr();
}
} // namespace SketcherGui
/* Python entry */
PyMODINIT_FUNC initSketcherGui()
PyMOD_INIT_FUNC(SketcherGui)
{
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
return;
PyMOD_Return(0);
}
try {
Base::Interpreter().runString("import PartGui");
@ -87,10 +93,10 @@ PyMODINIT_FUNC initSketcherGui()
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
PyMOD_Return(0);
}
(void)new SketcherGui::Module();
PyObject* mod = SketcherGui::initModule();
Base::Console().Log("Loading GUI of Sketcher module... done\n");
// instantiating the commands
@ -116,4 +122,6 @@ PyMODINIT_FUNC initSketcherGui()
// add resources and reloads the translators
loadSketcherResource();
PyMOD_Return(mod);
}

View File

@ -32,11 +32,11 @@
class SketcherWorkbench ( Workbench ):
"Sketcher workbench object"
"Sketcher workbench object"
def __init__(self):
self.__class__.Icon = FreeCAD.getResourceDir() + "Mod/Sketcher/Resources/icons/SketcherWorkbench.svg"
self.__class__.MenuText = "Sketcher"
self.__class__.ToolTip = "Sketcher workbench"
self.__class__.ToolTip = "Sketcher workbench"
def Initialize(self):
# load the module