mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-22 17:11:02 +00:00
Attribute.data_type -> EnumProperty (+typing)
This commit is contained in:
@@ -48,6 +48,8 @@ from bpy.props import (
|
|||||||
FloatVectorProperty,
|
FloatVectorProperty,
|
||||||
CollectionProperty,
|
CollectionProperty,
|
||||||
)
|
)
|
||||||
|
from typing import Any, Union, Literal, get_args, TYPE_CHECKING
|
||||||
|
from typing_extensions import assert_never
|
||||||
|
|
||||||
cwd = os.path.dirname(os.path.realpath(__file__))
|
cwd = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
@@ -248,14 +250,19 @@ def get_display_name(self):
|
|||||||
return f"{name}, {unit_symbol}"
|
return f"{name}, {unit_symbol}"
|
||||||
|
|
||||||
|
|
||||||
|
AttributeDataType = Literal["string", "integer", "float", "boolean", "enum", "file"]
|
||||||
|
|
||||||
|
|
||||||
class Attribute(PropertyGroup):
|
class Attribute(PropertyGroup):
|
||||||
tooltip = "`Right Click > IFC Description` to read the attribute description and online documentation"
|
tooltip = "`Right Click > IFC Description` to read the attribute description and online documentation"
|
||||||
name: StringProperty(name="Name")
|
name: StringProperty(name="Name")
|
||||||
display_name: StringProperty(name="Display Name", get=get_display_name)
|
display_name: StringProperty(name="Display Name", get=get_display_name)
|
||||||
description: StringProperty(name="Description")
|
description: StringProperty(name="Description")
|
||||||
ifc_class: StringProperty(name="Ifc Class")
|
ifc_class: StringProperty(name="Ifc Class")
|
||||||
# TODO: make it enum
|
data_type: EnumProperty( # type: ignore [reportRedeclaration]
|
||||||
data_type: StringProperty(name="Data Type")
|
name="Data Type",
|
||||||
|
items=[(i, i, "") for i in get_args(AttributeDataType)],
|
||||||
|
)
|
||||||
string_value: StringProperty(name="Value", update=update_attribute_value, description=tooltip)
|
string_value: StringProperty(name="Value", update=update_attribute_value, description=tooltip)
|
||||||
bool_value: BoolProperty(name="Value", update=update_attribute_value, description=tooltip)
|
bool_value: BoolProperty(name="Value", update=update_attribute_value, description=tooltip)
|
||||||
int_value: IntProperty(
|
int_value: IntProperty(
|
||||||
@@ -291,7 +298,10 @@ class Attribute(PropertyGroup):
|
|||||||
special_type: StringProperty(name="Special Value Type", default="")
|
special_type: StringProperty(name="Special Value Type", default="")
|
||||||
metadata: StringProperty(name="Metadata", description="For storing some additional information about the attribute")
|
metadata: StringProperty(name="Metadata", description="For storing some additional information about the attribute")
|
||||||
|
|
||||||
def get_value(self):
|
if TYPE_CHECKING:
|
||||||
|
data_type: AttributeDataType
|
||||||
|
|
||||||
|
def get_value(self) -> Union[str, float, int, bool, None]:
|
||||||
if self.is_optional and self.is_null:
|
if self.is_optional and self.is_null:
|
||||||
return None
|
return None
|
||||||
if self.data_type == "string":
|
if self.data_type == "string":
|
||||||
@@ -300,37 +310,43 @@ class Attribute(PropertyGroup):
|
|||||||
return [f.name for f in self.filepath_value.file_list]
|
return [f.name for f in self.filepath_value.file_list]
|
||||||
return getattr(self, str(self.get_value_name()), None)
|
return getattr(self, str(self.get_value_name()), None)
|
||||||
|
|
||||||
def get_value_default(self):
|
def get_value_default(self) -> Union[str, float, int, bool]:
|
||||||
if self.data_type == "string":
|
data_type = self.data_type
|
||||||
|
if data_type == "string":
|
||||||
return ""
|
return ""
|
||||||
elif self.data_type == "integer":
|
elif data_type == "integer":
|
||||||
return 0
|
return 0
|
||||||
elif self.data_type == "float":
|
elif data_type == "float":
|
||||||
return 0.0
|
return 0.0
|
||||||
elif self.data_type == "boolean":
|
elif data_type == "boolean":
|
||||||
return False
|
return False
|
||||||
elif self.data_type == "enum":
|
elif data_type == "enum":
|
||||||
return "0"
|
return "0"
|
||||||
elif self.data_type == "file":
|
elif data_type == "file":
|
||||||
return ""
|
return ""
|
||||||
|
else:
|
||||||
|
assert_never(data_type)
|
||||||
|
|
||||||
def get_value_name(self, display_only=False):
|
def get_value_name(self, display_only: bool = False) -> str:
|
||||||
if self.data_type == "string":
|
data_type = self.data_type
|
||||||
|
if data_type == "string":
|
||||||
return "string_value"
|
return "string_value"
|
||||||
elif self.data_type == "boolean":
|
elif data_type == "boolean":
|
||||||
return "bool_value"
|
return "bool_value"
|
||||||
elif self.data_type == "integer":
|
elif data_type == "integer":
|
||||||
return "int_value"
|
return "int_value"
|
||||||
elif self.data_type == "float":
|
elif data_type == "float":
|
||||||
if display_only and self.special_type == "LENGTH":
|
if display_only and self.special_type == "LENGTH":
|
||||||
return "length_value"
|
return "length_value"
|
||||||
return "float_value"
|
return "float_value"
|
||||||
elif self.data_type == "enum":
|
elif data_type == "enum":
|
||||||
return "enum_value"
|
return "enum_value"
|
||||||
elif self.data_type == "file":
|
elif data_type == "file":
|
||||||
return "filepath_value"
|
return "filepath_value"
|
||||||
|
else:
|
||||||
|
assert_never(data_type)
|
||||||
|
|
||||||
def set_value(self, value):
|
def set_value(self, value: Union[Any, str, float, bool, int]) -> None:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
self.data_type = "string"
|
self.data_type = "string"
|
||||||
elif isinstance(value, float):
|
elif isinstance(value, float):
|
||||||
|
|||||||
Reference in New Issue
Block a user