Skip to content

Commit 0d49c89

Browse files
committed
logical handling in python and small fixes
1 parent 7c3fcc2 commit 0d49c89

File tree

8 files changed

+48
-0
lines changed

8 files changed

+48
-0
lines changed

src/ifcopenshell-python/ifcopenshell/entity_instance.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def __setitem__(self, idx, value):
152152
idx, entity_instance.unwrap_value(value)
153153
)
154154
except BaseException as e:
155+
import traceback
156+
traceback.print_exc()
155157
valid = False
156158

157159
if not valid:

src/ifcparse/IfcParse.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,11 @@ void IfcEntityInstanceData::setArgument(size_t i, Argument* a, IfcUtil::Argument
12121212
case IfcUtil::Argument_BOOL:
12131213
copy->set(static_cast<bool>(*a));
12141214
break;
1215+
case IfcUtil::Argument_LOGICAL: {
1216+
boost::logic::tribool tb = *a;
1217+
copy->set(tb);
1218+
break;
1219+
}
12151220
case IfcUtil::Argument_DOUBLE:
12161221
copy->set(static_cast<double>(*a));
12171222
break;

src/ifcparse/IfcWrite.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ void StringBuilderVisitor::operator()(const std::vector< std::vector<double> >&
229229

230230
IfcWriteArgument::operator int() const { return as<int>(); }
231231
IfcWriteArgument::operator bool() const { return as<bool>(); }
232+
IfcWriteArgument::operator boost::logic::tribool() const { return as<boost::logic::tribool>(); }
232233
IfcWriteArgument::operator double() const { return as<double>(); }
233234
IfcWriteArgument::operator std::string() const {
234235
if (type() == IfcUtil::Argument_ENUMERATION) {

src/ifcparse/IfcWrite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ namespace IfcWrite {
145145

146146
operator int() const;
147147
operator bool() const;
148+
operator boost::logic::tribool() const;
149+
148150
operator double() const;
149151
operator std::string() const;
150152
operator boost::dynamic_bitset<>() const;

src/ifcwrap/IfcParseWrapper.i

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ static IfcUtil::ArgumentType helper_fn_attribute_type(const IfcUtil::IfcBaseClas
326326
}
327327
}
328328

329+
void setArgumentAsLogical(unsigned int i, boost::logic::tribool v) {
330+
IfcUtil::ArgumentType arg_type = helper_fn_attribute_type($self, i);
331+
if (arg_type == IfcUtil::Argument_LOGICAL) {
332+
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
333+
arg->set(v);
334+
self->data().setArgument(i, arg);
335+
} else {
336+
throw IfcParse::IfcException("Attribute not set");
337+
}
338+
}
339+
329340
void setArgumentAsDouble(unsigned int i, double v) {
330341
IfcUtil::ArgumentType arg_type = helper_fn_attribute_type($self, i);
331342
if (arg_type == IfcUtil::Argument_DOUBLE) {

src/ifcwrap/utils/type_conversion.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
PyObject* pythonize(const int& t) { return PyInt_FromLong(t); }
124124
PyObject* pythonize(const unsigned int& t) { return PyInt_FromLong(t); }
125125
PyObject* pythonize(const bool& t) { return PyBool_FromLong(t); }
126+
PyObject* pythonize(const boost::logic::tribool& t) { return boost::logic::indeterminate(t) ? PyUnicode_FromString("UNKNOWN") : PyBool_FromLong((bool)t) ;}
126127
PyObject* pythonize(const double& t) { return PyFloat_FromDouble(t); }
127128
PyObject* pythonize(const std::string& t) { return PyUnicode_FromString(t.c_str()); }
128129
PyObject* pythonize(const IfcUtil::IfcBaseClass* t) { return SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_IfcUtil__IfcBaseClass, 0); }

src/ifcwrap/utils/typemaps_in.i

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,28 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
241241
$1 = check_aggregate_of_aggregate_of_type($input, get_python_type<double>());
242242
}
243243

244+
%typemap(in) boost::logic::tribool {
245+
if (PyBool_Check($input)) {
246+
$1 = $input == Py_True;
247+
} else if (PyUnicode_Check($input)) {
248+
// we already checked the value of the string in the typecheck typemap
249+
$1 = boost::logic::indeterminate;
250+
} else {
251+
SWIG_exception(SWIG_TypeError, "Logical needs boolean or \"UNKOWN\"");
252+
}
253+
}
254+
255+
%typemap(typecheck, precedence=SWIG_TYPECHECK_INTEGER) boost::logic::tribool {
256+
$1 = PyBool_Check($input);
257+
if (!$1 && PyUnicode_Check($input)) {
258+
PyObject * ascii = PyUnicode_AsEncodedString(result, "UTF-8", "strict");
259+
if (ascii) {
260+
$1 = strcmp(PyBytes_AS_STRING(ascii), "UNKNOWN") == 0;
261+
Py_DECREF(ascii);
262+
}
263+
}
264+
}
265+
244266
%define CREATE_OPTIONAL_TYPEMAP_IN(template_type, express_name, python_name)
245267

246268
%typemap(in) const boost::optional<template_type>& {

src/ifcwrap/utils/typemaps_out.i

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
bool v = arg;
5252
$result = pythonize(v);
5353
break; }
54+
case IfcUtil::Argument_LOGICAL: {
55+
boost::logic::tribool v = arg;
56+
$result = pythonize(v);
57+
break; }
5458
case IfcUtil::Argument_DOUBLE: {
5559
double v = arg;
5660
$result = pythonize(v);

0 commit comments

Comments
 (0)