tree and document plug-ins

This commit is contained in:
Thomas Krijnen
2026-04-17 11:24:09 +02:00
parent 3824e7b449
commit d2cc66fdf0
60 changed files with 2207 additions and 766 deletions
+83 -3
View File
@@ -29,6 +29,29 @@
template <> void* get_python_type<std::string>() { return &PyString_Type; }
#endif
template <typename T>
bool check_python_type(PyObject* element) {
return element->ob_type == get_python_type<T>();
}
inline bool convert_pyobject_to_base(PyObject* element, express::Base& value) {
void* arg = nullptr;
int result = SWIG_ConvertPtr(element, &arg, SWIGTYPE_p_express__Base, 0);
if (SWIG_IsOK(result) && arg) {
value = *static_cast<express::Base*>(arg);
return true;
}
value = express::Base{};
return false;
}
template <>
bool check_python_type<express::Base>(PyObject* element) {
express::Base value;
return convert_pyobject_to_base(element, value);
}
bool check_aggregate_of_type(PyObject* aggregate, void* type_obj) {
if (!PySequence_Check(aggregate)) return false;
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
@@ -57,6 +80,63 @@
return true;
}
template <typename T>
bool check_aggregate_of_type(PyObject* aggregate) {
if (!PySequence_Check(aggregate)) return false;
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
PyObject* element = PySequence_GetItem(aggregate, i);
bool valid = check_python_type<T>(element);
Py_DECREF(element);
if (!valid) {
return false;
}
}
return true;
}
template <>
bool check_aggregate_of_type<express::Base>(PyObject* aggregate) {
if (!PySequence_Check(aggregate)) return false;
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
PyObject* element = PySequence_GetItem(aggregate, i);
express::Base value;
bool valid = convert_pyobject_to_base(element, value);
Py_DECREF(element);
if (!valid) {
return false;
}
}
return true;
}
template <typename T>
bool check_aggregate_of_aggregate_of_type(PyObject* aggregate) {
if (!PySequence_Check(aggregate)) return false;
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
PyObject* element = PySequence_GetItem(aggregate, i);
bool valid = check_aggregate_of_type<T>(element);
Py_DECREF(element);
if (!valid) {
return false;
}
}
return true;
}
template <>
bool check_aggregate_of_aggregate_of_type<express::Base>(PyObject* aggregate) {
if (!PySequence_Check(aggregate)) return false;
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
PyObject* element = PySequence_GetItem(aggregate, i);
bool valid = check_aggregate_of_type<express::Base>(element);
Py_DECREF(element);
if (!valid) {
return false;
}
}
return true;
}
template <typename T>
T cast_pyobject(PyObject* element);
@@ -87,9 +167,8 @@
template <>
express::Base cast_pyobject(PyObject* element) {
void *arg = 0;
int res = SWIG_ConvertPtr(element, &arg, SWIGTYPE_p_express__Base, 0);
return SWIG_IsOK(res) ? *reinterpret_cast<express::Base*>(arg) : express::Base{};
express::Base value;
return convert_pyobject_to_base(element, value) ? value : express::Base{};
}
template<typename T>
@@ -112,6 +191,7 @@
PyObject* element = PySequence_GetItem(aggregate, i);
T t = cast_pyobject<T>(element);
add_to_container(result_vector, t);
Py_DECREF(element);
}
return result_vector;
}
+7 -57
View File
@@ -20,23 +20,23 @@
%define CREATE_VECTOR_TYPEMAP_IN(template_type, express_name, python_name)
%typemap(in) std::vector<template_type> {
if (!check_aggregate_of_type($input, get_python_type<template_type>())) {
if (!check_aggregate_of_type<template_type>($input)) {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF " #express_name " needs a python sequence of " #python_name "s");
}
$1 = python_sequence_as_vector<template_type>($input);
}
%typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) std::vector<template_type> {
$1 = check_aggregate_of_type($input, get_python_type<template_type>()) ? 1 : 0;
$1 = check_aggregate_of_type<template_type>($input) ? 1 : 0;
}
%typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) const std::vector<template_type>& {
$1 = check_aggregate_of_type($input, get_python_type<template_type>()) ? 1 : 0;
$1 = check_aggregate_of_type<template_type>($input) ? 1 : 0;
}
%typemap(arginit) const std::vector<template_type>& {
$1 = new std::vector<template_type>();
}
%typemap(in) const std::vector<template_type>& {
if (!check_aggregate_of_type($input, get_python_type<template_type>())) {
if (!check_aggregate_of_type<template_type>($input)) {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF " #express_name " needs a python sequence of " #python_name "s");
}
*$1 = python_sequence_as_vector<template_type>($input);
@@ -46,7 +46,7 @@
}
%typemap(in) std::vector< std::vector<template_type> > {
if (!check_aggregate_of_aggregate_of_type($input, get_python_type<template_type>())) {
if (!check_aggregate_of_aggregate_of_type<template_type>($input)) {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF " #express_name " needs a python sequence of sequence of " #python_name "s");
}
$1 = python_sequence_as_vector_of_vector<template_type>($input);
@@ -56,7 +56,7 @@
$1 = new std::vector< std::vector<template_type> >();
}
%typemap(in) const std::vector< std::vector<template_type> >& {
if (!check_aggregate_of_aggregate_of_type($input, get_python_type<template_type>())) {
if (!check_aggregate_of_aggregate_of_type<template_type>($input)) {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF " #express_name " needs a python sequence of sequence of " #python_name "s");
}
*$1 = python_sequence_as_vector_of_vector<template_type>($input);
@@ -70,6 +70,7 @@
CREATE_VECTOR_TYPEMAP_IN(int, INTEGER, int)
CREATE_VECTOR_TYPEMAP_IN(double, REAL, float)
CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
CREATE_VECTOR_TYPEMAP_IN(express::Base, ENTITY INSTANCE, entity instance)
// @todo use macros.
@@ -166,57 +167,6 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
}
}
%typemap(in) aggregate_of_instance::ptr {
if (PySequence_Check($input)) {
$1 = aggregate_of_instance::ptr(new aggregate_of_instance());
for(Py_ssize_t i = 0; i < PySequence_Size($input); ++i) {
PyObject* element = PySequence_GetItem($input, i);
express::Base inst = cast_pyobject<express::Base>(element);
Py_DECREF(element);
if (inst) {
$1->push(inst);
} else {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF ENTITY INSTANCE needs a python sequence of entity instances");
}
}
} else {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF ENTITY INSTANCE needs a python sequence of entity instances");
}
}
%typemap(in) aggregate_of_aggregate_of_instance::ptr {
if (PySequence_Check($input)) {
$1 = aggregate_of_aggregate_of_instance::ptr(new aggregate_of_aggregate_of_instance());
for(Py_ssize_t i = 0; i < PySequence_Size($input); ++i) {
PyObject* element = PySequence_GetItem($input, i);
bool b = false;
if (PySequence_Check(element)) {
b = true;
std::vector<express::Base> vector;
vector.reserve(PySequence_Size(element));
for(Py_ssize_t j = 0; j < PySequence_Size(element); ++j) {
PyObject* element_element = PySequence_GetItem(element, j);
express::Base inst = cast_pyobject<express::Base>(element_element);
Py_DECREF(element_element);
if (inst) {
vector.push_back(inst);
} else {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF ENTITY INSTANCE needs a python sequence of sequence of entity instances");
}
}
$1->push(vector);
}
Py_DECREF(element);
if (!b) {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF ENTITY INSTANCE needs a python sequence of sequence of entity instances");
break;
}
}
} else {
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF ENTITY INSTANCE needs a python sequence of sequence of entity instances");
}
}
%typemap(in) const gp_Pnt& {
if (!check_aggregate_of_type($input, get_python_type<double>())) {
SWIG_exception(SWIG_TypeError, "<Point> type needs a python sequence of 3 floats");