From a5537a992f6ef28d80d1914e25ac670a8b6c33d7 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 21 Nov 2016 22:01:34 +0100 Subject: [PATCH] add simple Python wrapper for Vector2d class --- src/App/Application.cpp | 5 +++ src/Base/GeometryPyCXX.cpp | 61 +++++++++++++++++++++++++++++++++++++ src/Base/GeometryPyCXX.h | 27 ++++++++++++++++ src/CXX/Python2/Objects.hxx | 5 +++ 4 files changed, 98 insertions(+) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index fa2354c4a5..7754554d9e 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -107,6 +107,7 @@ #include "Expression.h" #include "Transactions.h" #include +#include // If you stumble here, run the target "BuildExtractRevision" on Windows systems // or the Python script "SubWCRev.py" on Linux based systems which builds @@ -245,6 +246,10 @@ Application::Application(std::map &mConfig) Base::ProgressIndicatorPy::init_type(); Base::Interpreter().addType(Base::ProgressIndicatorPy::type_object(), pBaseModule,"ProgressIndicator"); + + Base::Vector2dPy::init_type(); + Base::Interpreter().addType(Base::Vector2dPy::type_object(), + pBaseModule,"Vector2d"); } Application::~Application() diff --git a/src/Base/GeometryPyCXX.cpp b/src/Base/GeometryPyCXX.cpp index a01fe6ced8..67e9212079 100644 --- a/src/Base/GeometryPyCXX.cpp +++ b/src/Base/GeometryPyCXX.cpp @@ -86,3 +86,64 @@ Base::Vector3d Py::Vector::toVector() const return Base::getVectorFromTuple(ptr()); } } + +namespace Base { + +Vector2dPy::Vector2dPy(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds) + : Py::PythonClass::PythonClass(self, args, kwds) +{ +} + +Vector2dPy::~Vector2dPy() +{ +} + +void Vector2dPy::init_type(void) +{ + behaviors().name( "Vector2dPy" ); + behaviors().doc( "Vector2d class" ); + behaviors().supportGetattro(); + behaviors().supportSetattro(); + // Call to make the type ready for use + behaviors().readyType(); +} + +Py::Object Vector2dPy::getattro(const Py::String &name_) +{ + std::string name( name_.as_std_string( "utf-8" ) ); + + if (name == "__members__") { + Py::List attr; + attr.append(Py::String("x")); + attr.append(Py::String("y")); + return attr; + } + else if (name == "x") { + return Py::Float(v.x); + } + else if (name == "y") { + return Py::Float(v.y); + } + else { + return genericGetAttro( name_ ); + } +} + +int Vector2dPy::setattro(const Py::String &name_, const Py::Object &value) +{ + std::string name( name_.as_std_string( "utf-8" ) ); + + if (name == "x" && !value.isNull()) { + v.x = static_cast(Py::Float(value)); + return 0; + } + else if (name == "y" && !value.isNull()) { + v.y = static_cast(Py::Float(value)); + return 0; + } + else { + return genericSetAttro( name_, value ); + } +} + +} diff --git a/src/Base/GeometryPyCXX.h b/src/Base/GeometryPyCXX.h index 3938a22633..188cd7b44b 100644 --- a/src/Base/GeometryPyCXX.h +++ b/src/Base/GeometryPyCXX.h @@ -25,6 +25,7 @@ #define PY_GEOMETRYPY_H #include +#include #include #include #include @@ -33,6 +34,7 @@ #include #include #include +#include namespace Base { template @@ -44,6 +46,31 @@ inline Vector3 getVectorFromTuple(PyObject* o) T z = (T)Py::Float(tuple.getItem(2)); return Vector3(x,y,z); } + +class BaseExport Vector2dPy : public Py::PythonClass +{ +public: + Vector2dPy(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds); + virtual ~Vector2dPy(); + + static void init_type(void); + Py::Object getattro(const Py::String &name_); + int setattro(const Py::String &name_, const Py::Object &value); + inline const Vector2d& getValue() const { + return v; + } + inline void setValue(const Vector2d& n) { + v = n; + } + inline void setValue(double x, double y) { + v.x = x; + v.y = y; + } + +private: + Vector2d v; +}; + } namespace Py { diff --git a/src/CXX/Python2/Objects.hxx b/src/CXX/Python2/Objects.hxx index 5311314b7a..d7cf2f5050 100644 --- a/src/CXX/Python2/Objects.hxx +++ b/src/CXX/Python2/Objects.hxx @@ -306,6 +306,11 @@ namespace Py return p == other.p; } + bool isNull() const + { + return p == NULL; + } + bool isNone() const { return p == _None();