From e7c7c07cdac6594c8519aca5775b21495ef8cdcb Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 22 Jun 2017 21:52:53 +0200 Subject: [PATCH] implement object serialization for Vector class --- src/Base/VectorPy.xml | 11 +++++++++-- src/Base/VectorPyImp.cpp | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Base/VectorPy.xml b/src/Base/VectorPy.xml index c4954a85f2..b26041a136 100644 --- a/src/Base/VectorPy.xml +++ b/src/Base/VectorPy.xml @@ -18,7 +18,14 @@ This is the Vector export class This class represents a 3D float vector - + + + __reduce__() + Serialization of Vector objects + + + + add(Vector) returns the sum of this and another vector @@ -32,7 +39,7 @@ - + negative() returns the negative (opposite) of this vector diff --git a/src/Base/VectorPyImp.cpp b/src/Base/VectorPyImp.cpp index b1b3bf4f2b..633f652415 100644 --- a/src/Base/VectorPyImp.cpp +++ b/src/Base/VectorPyImp.cpp @@ -88,6 +88,26 @@ int VectorPy::PyInit(PyObject* args, PyObject* /*kwd*/) return -1; } +PyObject* VectorPy::__reduce__(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return 0; + + Py::Tuple tuple(2); + + union PyType_Object pyType = {&VectorPy::Type}; + Py::Object type(pyType.o); + tuple.setItem(0, type); + + Base::Vector3d v = this->value(); + Py::Tuple xyz(3); + xyz.setItem(0, Py::Float(v.x)); + xyz.setItem(1, Py::Float(v.y)); + xyz.setItem(2, Py::Float(v.z)); + tuple.setItem(1, xyz); + return Py::new_reference_to(tuple); +} + PyObject* VectorPy::number_add_handler(PyObject *self, PyObject *other) { if (!PyObject_TypeCheck(self, &(VectorPy::Type))) {