Reimplement sort / reverse / join function to format language, simplify text annotation variables, add tests

Previously, sort, reverse list, and join functionality was implemented
as special cases in Bonsai itself. Given that it has usecases
(especially in material lists, but any sort of list applies) I've moved
this function into the IOS formatting language.

The IOS formatting language previously wasn't capable of this, but the
awesome addition by @falken10vdl made the formatting language accept
queries inline, so that means it can handle lists. I also added tests
for all the new functions and expression syntax (+-*/ operators).

I simplified the code that gets the evaluated text literal - previously
it seems to call format() multiple times.
This commit is contained in:
Dion Moult
2026-02-08 19:17:12 +11:00
parent 1d5108f934
commit dffa3515c0
5 changed files with 62 additions and 44 deletions
+1 -21
View File
@@ -365,15 +365,10 @@ class DecoratorData:
for literal in literals:
literal_value = literal.Literal
try:
eval_value = cls.evaluate_formatting_expressions(literal_value, product)
current_value = tool.Drawing.replace_text_literal_variables(eval_value, product)
except Exception:
current_value = literal_value
literal_data = {
"Literal": literal_value,
"BoxAlignment": literal.BoxAlignment,
"CurrentValue": current_value,
"CurrentValue": tool.Drawing.replace_text_literal_variables(literal_value, product),
}
literals_data.append(literal_data)
@@ -399,21 +394,6 @@ class DecoratorData:
return element
@classmethod
def evaluate_formatting_expressions(cls, text: str, element=None) -> str:
"""Evaluate formatting expressions wrapped in backticks using ifcopenshell.util.selector.format, always passing element context"""
import re
def evaluate_expression(match):
try:
expression = match.group(1)
result = ifcopenshell.util.selector.format(expression, element)
return str(result)
except Exception as e:
return match.group(0)
return re.sub(r"``([^`]+)``", evaluate_expression, text)
@classmethod
def get_element_value_by_key(cls, element: ifcopenshell.entity_instance, key: str):
"""Get element value by its key using IfcOpenShell selector syntax"""
+4 -9
View File
@@ -1306,9 +1306,7 @@ class Drawing(bonsai.core.tool.Drawing):
element = tool.Ifc.get_entity(obj)
assert element
# updating text font size in EPset_Annotation.Classes
print("we got", font_size, repr(font_size))
font_size_str = next((key for key in FONT_SIZES if FONT_SIZES[key] == font_size), None)
print("so", font_size_str)
classes = ifcopenshell.util.element.get_pset(element, "EPset_Annotation", "Classes")
assert isinstance(classes, Union[str, None])
classes_split = classes.split() if classes else []
@@ -2101,16 +2099,13 @@ class Drawing(bonsai.core.tool.Drawing):
if not product:
return text
for command in re.findall("``.*?``", text):
for command in re.findall("``.+?``", text):
original_command = command
command_content = command[2:-2]
if command_content is None or str(command_content).strip().lower() == "none":
try:
text = text.replace(original_command, ifcopenshell.util.selector.format(command_content, product))
except Exception:
text = text.replace(original_command, "")
else:
try:
text = text.replace(original_command, ifcopenshell.util.selector.format(command_content, product))
except Exception:
text = text.replace(original_command, "")
for variable in re.findall("{{.*?}}", text):
value = ifcopenshell.util.selector.get_element_value(product, variable[2:-2])
if isinstance(value, (list, tuple)):