MicroStation Python provides the
ECValue class.
It is a
variant data type.
It wraps the class having the same name in the C++ MicroStationAPI.
It is used to set and retrieve property values in
ECInstances.
The data types that a Python ECValue can store include …
int — integers
float — double-precision floating point
bool — Boolean
str — strings using various encodings: Python string,
Bentley WString, Utf8 string
DPoint2d — Bentley 2D point
DPoint3d — Bentley 3D point
DateTime — Bentley Date-Time
The ECValue can also store complex data types, including structures and arrays.
However, the available documentation does not explain those.
An ECValue variable is intended to be initialised using one of the supported data types.
With C++ that idiom is easy, because C++ lets you write several overloaded constructors …
# C++ ECValue initialision
intVal = ECValue(123);
floatVal = ECValue(1.234);
strVal = ECValue(L"string");
ptVal = ECValue(DPoint3d(1.0, 2.0, 3.0));
With Python, that's not so straightforward — you can't simply translate the above. There are several reasons …
Here's my solution to initialising an ECValue instance with one of several possible data types.
I use a
Python match
to test the data type of the passed variable …
def set_ec_value(val)->ECValue:
'''
The original C++ ECValue class has several constructors that accept different data types.
A Python class can have only one constructor. This function offers functionality similar
to C++ by testing the data type of the argument and setting the ECValue data type explicitly.
'''
ecv = ECValue()
match val:
case IntEnum():
ecv.SetInteger(val)
case float():
ecv.SetDouble(val)
case DPoint3d():
ecv.SetPoint3D(val)
case int():
ecv.SetInteger(val)
case str():
ecv.SetString(val)
case WString():
ecv.SetString(val)
case _ :
msg = f"set_ec_value: unable to categorise '{val}'"
print(msg)
return ecv
set_ec_value
The functon is simple to use.
Pass it a variable or literal of one of the supported types.
The function creates an uninitialised ECValue,
then assigns the value according to its type …
ival = 11 ecv1 = set_ec_value(ival) assert ecv1.IsInteger() fval = 99.876 ecv2 = set_ec_value(fval) assert ecv2.IsDouble() strval = "String Value" ecv3 = set_ec_value(strval) assert ecv3.IsString() pt = DPoint3d(1, 2, 3) ecv4 = set_ec_value(pt) assert ecv4.IsPoint3D()
Post questions about MicroStation Python programming to the MicroStation Programming Forum.