Python wrapper improvements

This commit is contained in:
Thomas Krijnen
2023-12-11 21:06:25 +01:00
parent 9c82b752b6
commit 1e83e5b018
4 changed files with 107 additions and 6 deletions
+3 -1
View File
@@ -138,7 +138,9 @@
PyObject* pythonize(const IfcParse::inverse_attribute* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcParse__inverse_attribute, 0); }
PyObject* pythonize(const IfcParse::entity* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcParse__entity, 0); }
PyObject* pythonize(const IfcParse::declaration* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), declaration_type_to_swig(t), 0); }
PyObject* pythonize(const IfcGeom::ConversionResultShape* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcGeom__ConversionResultShape, 0); }
// @nb ownership
PyObject* pythonize(const IfcGeom::ConversionResultShape* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcGeom__ConversionResultShape, SWIG_POINTER_OWN); }
// PyObject* pythonize(const IfcGeom::ConversionResultShape* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcGeom__ConversionResultShape, 0); }
// NB: This cannot be temporary as a Python object is constructed from a pointer to the address of this object
// PyObject* pythonize(const IfcGeom::Material& t) { return SWIG_NewPointerObj(SWIG_as_voidptr(&t), SWIGTYPE_p_IfcGeom__Material, 0); }
+55 -1
View File
@@ -317,4 +317,58 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
CREATE_OPTIONAL_TYPEMAP_IN(int, integer, int)
CREATE_OPTIONAL_TYPEMAP_IN(double, real, float)
CREATE_OPTIONAL_TYPEMAP_IN(std::string, string, str)
CREATE_OPTIONAL_TYPEMAP_IN(std::string, string, str)
%{
bool check_aggregate_of_swig_type(PyObject* aggregate, swig_type_info* type_obj) {
if (!PySequence_Check(aggregate)) return false;
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
PyObject* element = PySequence_GetItem(aggregate, i);
bool b = true;
void* argp1 = nullptr;
auto res1 = SWIG_ConvertPtr(element, &argp1, type_obj, 0);
if (!SWIG_IsOK(res1)) {
b = false;
}
Py_DECREF(element);
if (!b) {
return false;
}
}
return true;
}
template <typename T>
std::vector<T> python_sequence_as_swig_object_vector(PyObject* aggregate, swig_type_info* type_obj) {
std::vector<T> result_vector;
result_vector.reserve(PySequence_Size(aggregate));
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
PyObject* element = PySequence_GetItem(aggregate, i);
void* argp1 = nullptr;
auto res1 = SWIG_ConvertPtr(element, &argp1, type_obj, 0);
if (SWIG_IsOK(res1)) {
auto arg1 = reinterpret_cast<IfcGeom::OpaqueCoordinate<4>*>(argp1);
result_vector.push_back(*arg1);
}
}
return result_vector;
}
%}
%typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) const std::vector<IfcGeom::OpaqueCoordinate<4>>& {
$1 = check_aggregate_of_swig_type($input, SWIGTYPE_p_IfcGeom__OpaqueCoordinateT_4_t) ? 1 : 0;
}
%typemap(arginit) const std::vector<IfcGeom::OpaqueCoordinate<4>>& {
$1 = new std::vector<IfcGeom::OpaqueCoordinate<4>>();
}
%typemap(in) const std::vector<IfcGeom::OpaqueCoordinate<4>>& {
if (!check_aggregate_of_swig_type($input, SWIGTYPE_p_IfcGeom__OpaqueCoordinateT_4_t)) {
SWIG_exception(SWIG_TypeError, "");
}
*$1 = python_sequence_as_swig_object_vector<IfcGeom::OpaqueCoordinate<4>>($input, SWIGTYPE_p_IfcGeom__OpaqueCoordinateT_4_t);
}
%typemap(freearg) const std::vector<IfcGeom::OpaqueCoordinate<4>>& {
delete $1;
}