From e1f85cd091ae13f30b80a92e263b9f3ba99bbac6 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 13 Nov 2019 00:43:23 +0100 Subject: [PATCH] export typeid system to Python --- src/App/Application.cpp | 2 + src/Base/CMakeLists.txt | 3 + src/Base/TypePy.xml | 81 +++++++++++++++++++++ src/Base/TypePyImp.cpp | 151 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 237 insertions(+) create mode 100644 src/Base/TypePy.xml create mode 100644 src/Base/TypePyImp.cpp diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 46b81602df61..3fdf30eecdfa 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -81,6 +81,7 @@ #include #include #include +#include #include "GeoFeature.h" #include "FeatureTest.h" @@ -303,6 +304,7 @@ Application::Application(std::map &mConfig) Base::Interpreter().addType(&Base::RotationPy ::Type,pBaseModule,"Rotation"); Base::Interpreter().addType(&Base::AxisPy ::Type,pBaseModule,"Axis"); Base::Interpreter().addType(&Base::CoordinateSystemPy::Type,pBaseModule,"CoordinateSystem"); + Base::Interpreter().addType(&Base::TypePy ::Type,pBaseModule,"TypeId"); Base::Interpreter().addType(&App::MaterialPy::Type, pAppModule, "Material"); diff --git a/src/Base/CMakeLists.txt b/src/Base/CMakeLists.txt index be5bbc494409..0654f581decd 100644 --- a/src/Base/CMakeLists.txt +++ b/src/Base/CMakeLists.txt @@ -78,6 +78,7 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") list(APPEND FreeCADBase_LIBS -lutil -ldl) endif() +generate_from_xml(TypePy) generate_from_xml(BaseClassPy) generate_from_xml(BoundBoxPy) generate_from_xml(CoordinateSystemPy) @@ -173,6 +174,7 @@ SET(FreeCADBase_XML_SRCS RotationPy.xml VectorPy.xml QuantityPy.xml + TypePy.xml UnitPy.xml ) SOURCE_GROUP("XML" FILES ${FreeCADBase_XML_SRCS}) @@ -276,6 +278,7 @@ SET(FreeCADBase_CPP_SRCS Tools2D.cpp Translate.cpp Type.cpp + TypePyImp.cpp Uuid.cpp Vector3D.cpp VectorPyImp.cpp diff --git a/src/Base/TypePy.xml b/src/Base/TypePy.xml new file mode 100644 index 000000000000..b1408e2e6d6e --- /dev/null +++ b/src/Base/TypePy.xml @@ -0,0 +1,81 @@ + + + + +namespace Base { + typedef Type BaseType; +} + + + This is the Type class + This is the Type class + + + + Returns a type object by name + + + + + Returns a type object by key + + + + + Returns the number of type ids + + + + + Returns an invalid type id + + + + + Returns the parent type id + + + + + Checks if the type id is invalid + + + + + Returns true if given type is a father + + + + + Returns all descendants + + + + + The name of the type id + + + + + + The key of the type id + + + + + + Module in which this class is defined + + + + + diff --git a/src/Base/TypePyImp.cpp b/src/Base/TypePyImp.cpp new file mode 100644 index 000000000000..af9b2b2a77bd --- /dev/null +++ b/src/Base/TypePyImp.cpp @@ -0,0 +1,151 @@ +/*************************************************************************** + * Copyright (c) 2019 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 51 Franklin Street, * + * Fifth Floor, Boston, MA 02110-1301, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#include "Type.h" +#include "TypePy.h" +#include "TypePy.cpp" + +using namespace Base; + +// returns a string which represent the object e.g. when printed in python +std::string TypePy::representation(void) const +{ + std::stringstream str; + str << "getName() << "'>"; + return str.str(); +} + +PyObject* TypePy::staticCallback_fromName (PyObject * /*self*/, PyObject *args) +{ + const char *name; + if (!PyArg_ParseTuple(args, "s", &name)) + return NULL; + + Base::Type type = Base::Type::fromName(name); + return new TypePy(new Base::Type(type)); +} + +PyObject* TypePy::staticCallback_fromKey (PyObject * /*self*/, PyObject *args) +{ + unsigned int index; + if (!PyArg_ParseTuple(args, "I", &index)) + return NULL; + + Base::Type type = Base::Type::fromKey(index); + return new TypePy(new Base::Type(type)); +} + +PyObject* TypePy::staticCallback_getNumTypes (PyObject * /*self*/, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + + int num = Base::Type::getNumTypes(); + return PyLong_FromLong(num); +} + +PyObject* TypePy::staticCallback_getBadType (PyObject * /*self*/, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + + Base::Type type = Base::Type::badType(); + return new TypePy(new Base::Type(type)); +} + +PyObject* TypePy::getParent(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + + Base::Type type = getBaseTypePtr()->getParent(); + return new TypePy(new Base::Type(type)); +} + +PyObject* TypePy::isBad(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + + bool v = getBaseTypePtr()->isBad(); + return PyBool_FromLong(v ? 1 : 0); +} + +PyObject* TypePy::isDerivedFrom(PyObject *args) +{ + char *name; + if (!PyArg_ParseTuple(args, "s", &name)) // convert args: Python->C + return NULL; // NULL triggers exception + + Base::Type type = Base::Type::fromName(name); + bool v = (type != Base::Type::badType() && getBaseTypePtr()->isDerivedFrom(type)); + return PyBool_FromLong(v ? 1 : 0); +} + +PyObject* TypePy::getAllDerivedFrom(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) // convert args: Python->C + return NULL; // NULL triggers exception + + std::vector ary; + Base::Type::getAllDerivedFrom(*getBaseTypePtr(), ary); + Py::List res; + for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) + res.append(Py::String(it->getName())); + return Py::new_reference_to(res); +} + +Py::String TypePy::getName(void) const +{ + return Py::String(std::string(getBaseTypePtr()->getName())); +} + +Py::Long TypePy::getKey(void) const +{ + return Py::Long(static_cast(getBaseTypePtr()->getKey())); +} + +Py::String TypePy::getModule(void) const +{ + std::string module(getBaseTypePtr()->getName()); + std::string::size_type pos = module.find_first_of("::"); + + if (pos != std::string::npos) + module = std::string(module, 0, pos); + else + module.clear(); + + return Py::String(module); +} + +PyObject *TypePy::getCustomAttributes(const char* /*attr*/) const +{ + return 0; +} + +int TypePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +}