add simple Python wrapper for Vector2d class
This commit is contained in:
@@ -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
|
||||
@@ -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()
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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 {
|
||||
|
||||
@@ -306,6 +306,11 @@ namespace Py
|
||||
return p == other.p;
|
||||
}
|
||||
|
||||
bool isNull() const
|
||||
{
|
||||
return p == NULL;
|
||||
}
|
||||
|
||||
bool isNone() const
|
||||
{
|
||||
return p == _None();
|
||||
|
||||
Reference in New Issue
Block a user