Skip to content

Commit

Permalink
add simple Python wrapper for Vector2d class
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 21, 2016
1 parent d63d7ae commit 14594dd
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/App/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
#include "Expression.h"
#include "Transactions.h"
#include <App/MaterialPy.h>
#include <Base/GeometryPyCXX.h>

// If you stumble here, run the target "BuildExtractRevision" on Windows systems
// or the Python script "SubWCRev.py" on Linux based systems which builds
Expand Down Expand Up @@ -245,6 +246,10 @@ Application::Application(std::map<std::string,std::string> &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()
Expand Down
61 changes: 61 additions & 0 deletions src/Base/GeometryPyCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,64 @@ Base::Vector3d Py::Vector::toVector() const
return Base::getVectorFromTuple<double>(ptr());
}
}

namespace Base {

Vector2dPy::Vector2dPy(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds)
: Py::PythonClass<Vector2dPy>::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<double>(Py::Float(value));
return 0;
}
else if (name == "y" && !value.isNull()) {
v.y = static_cast<double>(Py::Float(value));
return 0;
}
else {
return genericSetAttro( name_, value );
}
}

}
27 changes: 27 additions & 0 deletions src/Base/GeometryPyCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define PY_GEOMETRYPY_H

#include <CXX/Objects.hxx>
#include <CXX/Extensions.hxx>
#include <Base/Vector3D.h>
#include <Base/Matrix.h>
#include <Base/MatrixPy.h>
Expand All @@ -33,6 +34,7 @@
#include <Base/Placement.h>
#include <Base/PlacementPy.h>
#include <Base/BoundBoxPy.h>
#include <Base/Tools2D.h>

namespace Base {
template <typename T>
Expand All @@ -44,6 +46,31 @@ inline Vector3<T> getVectorFromTuple(PyObject* o)
T z = (T)Py::Float(tuple.getItem(2));
return Vector3<T>(x,y,z);
}

class BaseExport Vector2dPy : public Py::PythonClass<Vector2dPy>
{
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 {
Expand Down
5 changes: 5 additions & 0 deletions src/CXX/Python2/Objects.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ namespace Py
return p == other.p;
}

bool isNull() const
{
return p == NULL;
}

bool isNone() const
{
return p == _None();
Expand Down

0 comments on commit 14594dd

Please sign in to comment.