Clean and test ComplexGeoData python interface

This commit is contained in:
bgbsww
2024-03-21 06:59:21 -04:00
parent 84407e36b8
commit 07180280a0
5 changed files with 548 additions and 72 deletions

View File

@@ -37,6 +37,7 @@
#include <Base/BoundBoxPy.h>
#include <Base/MatrixPy.h>
#include <Base/PlacementPy.h>
#include "Base/PyWrapParseTupleAndKeywords.h"
#include <Base/VectorPy.h>
#include <Base/GeometryPyCXX.h>
@@ -295,153 +296,194 @@ PyObject* ComplexGeoDataPy::transformGeometry(PyObject *args)
}
}
PyObject* ComplexGeoDataPy::getElementName(PyObject *args)
PyObject* ComplexGeoDataPy::getElementName(PyObject* args)
{
char* input;
int direction = 0;
if (!PyArg_ParseTuple(args, "s|i", &input,&direction))
if (!PyArg_ParseTuple(args, "s|i", &input, &direction)) {
return NULL;
}
Data::MappedElement res = getComplexGeoDataPtr()->getElementName(input);
std::string s;
if (direction == 1)
if (direction == 1) {
return Py::new_reference_to(Py::String(res.name.appendToBuffer(s)));
else if (direction == 0)
}
else if (direction == 0) {
return Py::new_reference_to(Py::String(res.index.appendToStringBuffer(s)));
else if (Data::IndexedName(input))
}
else if (Data::IndexedName(input)) {
return Py::new_reference_to(Py::String(res.name.appendToBuffer(s)));
else
}
else {
return Py::new_reference_to(Py::String(res.index.appendToStringBuffer(s)));
}
}
PyObject* ComplexGeoDataPy::getElementIndexedName(PyObject *args)
PyObject* ComplexGeoDataPy::getElementIndexedName(PyObject* args)
{
char* input;
PyObject *returnID = Py_False;
if (!PyArg_ParseTuple(args, "s|O", &input,&returnID))
PyObject* returnID = Py_False;
if (!PyArg_ParseTuple(args, "s|O", &input, &returnID)) {
return NULL;
}
ElementIDRefs ids;
Data::MappedElement res = getComplexGeoDataPtr()->getElementName(
input, PyObject_IsTrue(returnID)?&ids:nullptr);
Data::MappedElement res =
getComplexGeoDataPtr()->getElementName(input, PyObject_IsTrue(returnID) ? &ids : nullptr);
std::string s;
Py::String name(res.index.appendToStringBuffer(s));
if (!PyObject_IsTrue(returnID))
if (!PyObject_IsTrue(returnID)) {
return Py::new_reference_to(name);
}
Py::List list;
for (auto &id : ids)
for (auto& id : ids) {
list.append(Py::Long(id.value()));
}
return Py::new_reference_to(Py::TupleN(name, list));
}
PyObject* ComplexGeoDataPy::getElementMappedName(PyObject *args)
PyObject* ComplexGeoDataPy::getElementMappedName(PyObject* args)
{
char* input;
PyObject *returnID = Py_False;
if (!PyArg_ParseTuple(args, "s|O", &input,&returnID))
PyObject* returnID = Py_False;
if (!PyArg_ParseTuple(args, "s|O", &input, &returnID)) {
return NULL;
}
ElementIDRefs ids;
Data::MappedElement res = getComplexGeoDataPtr()->getElementName(
input, PyObject_IsTrue(returnID)?&ids:nullptr);
Data::MappedElement res =
getComplexGeoDataPtr()->getElementName(input, PyObject_IsTrue(returnID) ? &ids : nullptr);
std::string s;
Py::String name(res.name.appendToBuffer(s));
if (!PyObject_IsTrue(returnID))
if (!PyObject_IsTrue(returnID)) {
return Py::new_reference_to(name);
}
Py::List list;
for (auto &id : ids)
for (auto& id : ids) {
list.append(Py::Long(id.value()));
}
return Py::new_reference_to(Py::TupleN(name, list));
}
PyObject *ComplexGeoDataPy::setElementName(PyObject *args, PyObject *kwds) {
const char *element;
const char *name = 0;
const char *postfix = 0;
PyObject* ComplexGeoDataPy::setElementName(PyObject* args, PyObject* kwds)
{
const char* element;
const char* name = 0;
const char* postfix = 0;
int tag = 0;
PyObject *pySid = Py_None;
PyObject *overwrite = Py_False;
PyObject* pySid = Py_None;
PyObject* overwrite = Py_False;
static char *kwlist[] = {"element", "name", "postfix", "overwrite", "sid", "tag", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|sssOOi", kwlist,
&element,&name,&postfix,&overwrite,&pySid,&tag))
const std::array<const char *,7> kwlist = {"element", "name", "postfix", "overwrite", "sid", "tag", nullptr};
if (!Wrapped_ParseTupleAndKeywords(args,
kwds,
"s|sssOOi",
kwlist,
&element,
&name,
&postfix,
&overwrite,
&pySid,
&tag)) {
return NULL;
ElementIDRefs sids;
if(pySid != Py_None) {
if(PyObject_TypeCheck(pySid,&App::StringIDPy::Type))
sids.push_back(static_cast<App::StringIDPy*>(pySid)->getStringIDPtr());
else if(PySequence_Check(pySid)) {
Py::Sequence seq(pySid);
for(auto it=seq.begin();it!=seq.end();++it) {
auto ptr = (*it).ptr();
if(PyObject_TypeCheck(ptr,&App::StringIDPy::Type))
sids.push_back(static_cast<App::StringIDPy*>(ptr)->getStringIDPtr());
else
throw Py::TypeError("expect StringID in sid sequence");
}
} else
throw Py::TypeError("expect sid to contain either StringID or sequence of StringID");
}
PY_TRY {
ElementIDRefs sids;
if (pySid != Py_None) {
if (PyObject_TypeCheck(pySid, &App::StringIDPy::Type)) {
sids.push_back(static_cast<App::StringIDPy*>(pySid)->getStringIDPtr());
}
else if (PySequence_Check(pySid)) {
Py::Sequence seq(pySid);
for (auto it = seq.begin(); it != seq.end(); ++it) {
auto ptr = (*it).ptr();
if (PyObject_TypeCheck(ptr, &App::StringIDPy::Type)) {
sids.push_back(static_cast<App::StringIDPy*>(ptr)->getStringIDPtr());
}
else {
throw Py::TypeError("expect StringID in sid sequence");
}
}
}
else {
throw Py::TypeError("expect sid to contain either StringID or sequence of StringID");
}
}
PY_TRY
{
Data::IndexedName index(element, getComplexGeoDataPtr()->getElementTypes());
Data::MappedName mapped = Data::MappedName::fromRawData(name);
std::ostringstream ss;
ElementMapPtr map = getComplexGeoDataPtr()->resetElementMap();
map->encodeElementName(getComplexGeoDataPtr()->elementType(index),
mapped, ss, &sids, tag, postfix, tag);
Data::MappedName res = map->setElementName(
index, mapped, tag, &sids, PyObject_IsTrue(overwrite));
mapped,
ss,
&sids,
tag,
postfix,
tag);
Data::MappedName res =
map->setElementName(index, mapped, tag, &sids, PyObject_IsTrue(overwrite));
return Py::new_reference_to(Py::String(res.toString(0)));
}PY_CATCH
}
PY_CATCH
}
Py::Object ComplexGeoDataPy::getHasher() const {
Py::Object ComplexGeoDataPy::getHasher() const
{
auto self = getComplexGeoDataPtr();
if(!self->Hasher)
if (!self->Hasher) {
return Py::None();
return Py::Object(self->Hasher->getPyObject(),true);
}
return Py::Object(self->Hasher->getPyObject(), true);
}
Py::Dict ComplexGeoDataPy::getElementMap() const {
Py::Dict ComplexGeoDataPy::getElementMap() const
{
Py::Dict ret;
std::string s;
for(auto &v : getComplexGeoDataPtr()->getElementMap()) {
for (auto& v : getComplexGeoDataPtr()->getElementMap()) {
s.clear();
ret.setItem(v.name.toString(0), Py::String(v.index.appendToStringBuffer(s)));
}
return ret;
}
void ComplexGeoDataPy::setElementMap(Py::Dict dict) {
void ComplexGeoDataPy::setElementMap(Py::Dict dict)
{
std::vector<Data::MappedElement> map;
const auto & types = getComplexGeoDataPtr()->getElementTypes();
for(auto it=dict.begin();it!=dict.end();++it) {
const auto &value = *it;
if(!value.first.isString() || !value.second.isString())
const auto& types = getComplexGeoDataPtr()->getElementTypes();
for (auto it = dict.begin(); it != dict.end(); ++it) {
const auto& value = *it;
if (!value.first.isString() || !value.second.isString()) {
throw Py::TypeError("expect only strings in the dict");
}
map.emplace_back(Data::MappedName(value.first.as_string().c_str()),
Data::IndexedName(Py::Object(value.second).as_string().c_str(), types));
}
getComplexGeoDataPtr()->setElementMap(map);
}
Py::Dict ComplexGeoDataPy::getElementReverseMap() const {
Py::Dict ComplexGeoDataPy::getElementReverseMap() const
{
Py::Dict ret;
std::string s;
for(auto &v : getComplexGeoDataPtr()->getElementMap()) {
for (auto& v : getComplexGeoDataPtr()->getElementMap()) {
s.clear();
auto value = ret[Py::String(v.index.appendToStringBuffer(s))];
Py::Object item(value);
if(item.isNone()) {
if (item.isNone()) {
s.clear();
value = Py::String(v.name.appendToBuffer(s));
} else if(item.isList()) {
}
else if (item.isList()) {
Py::List list(item);
s.clear();
list.append(Py::String(v.name.appendToBuffer(s)));
} else {
}
else {
Py::List list;
list.append(item);
s.clear();
@@ -452,25 +494,31 @@ Py::Dict ComplexGeoDataPy::getElementReverseMap() const {
return ret;
}
Py::Int ComplexGeoDataPy::getElementMapSize() const {
Py::Int ComplexGeoDataPy::getElementMapSize() const
{
return Py::Int((long)getComplexGeoDataPtr()->getElementMapSize());
}
void ComplexGeoDataPy::setHasher(Py::Object obj) {
void ComplexGeoDataPy::setHasher(Py::Object obj)
{
auto self = getComplexGeoDataPtr();
if(obj.isNone()) {
if(self->Hasher) {
if (obj.isNone()) {
if (self->Hasher) {
self->Hasher = App::StringHasherRef();
self->resetElementMap();
}
}else if(PyObject_TypeCheck(obj.ptr(),&App::StringHasherPy::Type)) {
App::StringHasherRef ref(static_cast<App::StringHasherPy*>(obj.ptr())->getStringHasherPtr());
if(self->Hasher != ref) {
}
else if (PyObject_TypeCheck(obj.ptr(), &App::StringHasherPy::Type)) {
App::StringHasherRef ref(
static_cast<App::StringHasherPy*>(obj.ptr())->getStringHasherPtr());
if (self->Hasher != ref) {
self->Hasher = ref;
self->resetElementMap();
}
}else
}
else {
throw Py::TypeError("invalid type");
}
}
Py::Object ComplexGeoDataPy::getBoundBox() const
@@ -507,7 +555,14 @@ void ComplexGeoDataPy::setPlacement(Py::Object arg)
Py::String ComplexGeoDataPy::getElementMapVersion() const
{
#ifdef FC_USE_TNP_FIX
return Py::String(getComplexGeoDataPtr()->getElementMapVersion());
#else
// This is to allow python level tests visibility into whether element maps are in use, so that
// expectations can be adjusted. Eventually this ifdef and clause should be removed, and at the
// same time all python tests checking for ElementMapVersion != '' should also be removed.
return Py::String();
#endif
}