mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-29 03:37:21 +00:00
typing
This commit is contained in:
@@ -1703,18 +1703,18 @@ class IfcImporter:
|
|||||||
self.type_collection.children.link(aggregate["collection"])
|
self.type_collection.children.link(aggregate["collection"])
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def create_materials(self):
|
def create_materials(self) -> None:
|
||||||
for material in self.file.by_type("IfcMaterial"):
|
for material in self.file.by_type("IfcMaterial"):
|
||||||
self.create_material(material)
|
self.create_material(material)
|
||||||
|
|
||||||
def create_material(self, material):
|
def create_material(self, material: ifcopenshell.entity_instance) -> bpy.types.Material:
|
||||||
blender_material = bpy.data.materials.new(material.Name)
|
blender_material = bpy.data.materials.new(material.Name)
|
||||||
self.link_element(material, blender_material)
|
self.link_element(material, blender_material)
|
||||||
self.material_creator.materials[material.id()] = blender_material
|
self.material_creator.materials[material.id()] = blender_material
|
||||||
blender_material.use_fake_user = True
|
blender_material.use_fake_user = True
|
||||||
return blender_material
|
return blender_material
|
||||||
|
|
||||||
def create_styles(self):
|
def create_styles(self) -> None:
|
||||||
parsed_styles = set()
|
parsed_styles = set()
|
||||||
|
|
||||||
for material_definition_representation in self.file.by_type("IfcMaterialDefinitionRepresentation"):
|
for material_definition_representation in self.file.by_type("IfcMaterialDefinitionRepresentation"):
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ from mathutils import Vector, Matrix
|
|||||||
from time import time
|
from time import time
|
||||||
from blenderbim.bim.ifc import IfcStore
|
from blenderbim.bim.ifc import IfcStore
|
||||||
from ifcopenshell.util.shape_builder import ShapeBuilder
|
from ifcopenshell.util.shape_builder import ShapeBuilder
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class Operator:
|
class Operator:
|
||||||
@@ -729,7 +730,7 @@ class OverrideOutlinerDelete(bpy.types.Operator):
|
|||||||
IfcStore.add_transaction_operation(self)
|
IfcStore.add_transaction_operation(self)
|
||||||
return {"FINISHED"}
|
return {"FINISHED"}
|
||||||
|
|
||||||
def get_collection_objects_and_children(self, collection):
|
def get_collection_objects_and_children(self, collection: bpy.types.Collection) -> dict[str, Any]:
|
||||||
objects = set()
|
objects = set()
|
||||||
children = set()
|
children = set()
|
||||||
queue = [collection]
|
queue = [collection]
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import bmesh
|
|||||||
import json
|
import json
|
||||||
import ifcopenshell.api
|
import ifcopenshell.api
|
||||||
import ifcopenshell.util.element
|
import ifcopenshell.util.element
|
||||||
|
import blenderbim.core.tool
|
||||||
import blenderbim.tool as tool
|
import blenderbim.tool as tool
|
||||||
import blenderbim.bim
|
import blenderbim.bim
|
||||||
import addon_utils
|
import addon_utils
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import zipfile
|
|||||||
import functools
|
import functools
|
||||||
import ifcopenshell
|
import ifcopenshell
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Any, Union, Callable
|
from typing import Optional, Any, Union, Callable, Generator
|
||||||
|
|
||||||
from . import ifcopenshell_wrapper
|
from . import ifcopenshell_wrapper
|
||||||
from .entity_instance import entity_instance
|
from .entity_instance import entity_instance
|
||||||
@@ -59,13 +59,13 @@ class Transaction:
|
|||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
|
|
||||||
def batch(self):
|
def batch(self) -> None:
|
||||||
self.is_batched = True
|
self.is_batched = True
|
||||||
self.batch_delete_index = len(self.operations)
|
self.batch_delete_index = len(self.operations)
|
||||||
self.batch_delete_ids = set()
|
self.batch_delete_ids = set()
|
||||||
self.batch_inverses = []
|
self.batch_inverses = []
|
||||||
|
|
||||||
def unbatch(self):
|
def unbatch(self) -> None:
|
||||||
for inverses in self.batch_inverses:
|
for inverses in self.batch_inverses:
|
||||||
if inverses:
|
if inverses:
|
||||||
self.operations.insert(self.batch_delete_index, {"action": "batch_delete", "inverses": inverses})
|
self.operations.insert(self.batch_delete_index, {"action": "batch_delete", "inverses": inverses})
|
||||||
@@ -74,11 +74,11 @@ class Transaction:
|
|||||||
self.batch_delete_ids = set()
|
self.batch_delete_ids = set()
|
||||||
self.batch_inverses = []
|
self.batch_inverses = []
|
||||||
|
|
||||||
def store_create(self, element):
|
def store_create(self, element: ifcopenshell.entity_instance) -> None:
|
||||||
if element.id():
|
if element.id():
|
||||||
self.operations.append({"action": "create", "value": self.serialise_entity_instance(element)})
|
self.operations.append({"action": "create", "value": self.serialise_entity_instance(element)})
|
||||||
|
|
||||||
def store_edit(self, element, index, value):
|
def store_edit(self, element: ifcopenshell.entity_instance, index: int, value: Any) -> None:
|
||||||
if element.id():
|
if element.id():
|
||||||
self.operations.append(
|
self.operations.append(
|
||||||
{
|
{
|
||||||
@@ -120,7 +120,7 @@ class Transaction:
|
|||||||
return False
|
return False
|
||||||
return value == element
|
return value == element
|
||||||
|
|
||||||
def rollback(self):
|
def rollback(self) -> None:
|
||||||
for operation in self.operations[::-1]:
|
for operation in self.operations[::-1]:
|
||||||
if operation["action"] == "create":
|
if operation["action"] == "create":
|
||||||
element = self.file.by_id(operation["value"]["id"])
|
element = self.file.by_id(operation["value"]["id"])
|
||||||
@@ -153,7 +153,7 @@ class Transaction:
|
|||||||
for index, value in data:
|
for index, value in data:
|
||||||
inverse[index] = self.unserialise_value(inverse, value)
|
inverse[index] = self.unserialise_value(inverse, value)
|
||||||
|
|
||||||
def commit(self):
|
def commit(self) -> None:
|
||||||
for operation in self.operations:
|
for operation in self.operations:
|
||||||
if operation["action"] == "create":
|
if operation["action"] == "create":
|
||||||
e = self.file.create_entity(operation["value"]["type"], id=operation["value"]["id"])
|
e = self.file.create_entity(operation["value"]["type"], id=operation["value"]["id"])
|
||||||
@@ -260,19 +260,19 @@ class file:
|
|||||||
|
|
||||||
file_dict[self.file_pointer()] = weakref.ref(self)
|
file_dict[self.file_pointer()] = weakref.ref(self)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self) -> None:
|
||||||
del file_dict[self.file_pointer()]
|
del file_dict[self.file_pointer()]
|
||||||
|
|
||||||
def set_history_size(self, size):
|
def set_history_size(self, size: int) -> None:
|
||||||
self.history_size = size
|
self.history_size = size
|
||||||
while len(self.history) > self.history_size:
|
while len(self.history) > self.history_size:
|
||||||
self.history.pop(0)
|
self.history.pop(0)
|
||||||
|
|
||||||
def begin_transaction(self):
|
def begin_transaction(self) -> None:
|
||||||
if self.history_size:
|
if self.history_size:
|
||||||
self.transaction = Transaction(self)
|
self.transaction = Transaction(self)
|
||||||
|
|
||||||
def end_transaction(self):
|
def end_transaction(self) -> None:
|
||||||
if self.transaction:
|
if self.transaction:
|
||||||
self.history.append(self.transaction)
|
self.history.append(self.transaction)
|
||||||
if len(self.history) > self.history_size:
|
if len(self.history) > self.history_size:
|
||||||
@@ -280,19 +280,19 @@ class file:
|
|||||||
self.future = []
|
self.future = []
|
||||||
self.transaction = None
|
self.transaction = None
|
||||||
|
|
||||||
def discard_transaction(self):
|
def discard_transaction(self) -> None:
|
||||||
if self.transaction:
|
if self.transaction:
|
||||||
self.transaction.rollback()
|
self.transaction.rollback()
|
||||||
self.transaction = None
|
self.transaction = None
|
||||||
|
|
||||||
def undo(self):
|
def undo(self) -> None:
|
||||||
if not self.history:
|
if not self.history:
|
||||||
return
|
return
|
||||||
transaction = self.history.pop()
|
transaction = self.history.pop()
|
||||||
transaction.rollback()
|
transaction.rollback()
|
||||||
self.future.append(transaction)
|
self.future.append(transaction)
|
||||||
|
|
||||||
def redo(self):
|
def redo(self) -> None:
|
||||||
if not self.future:
|
if not self.future:
|
||||||
return
|
return
|
||||||
transaction = self.future.pop()
|
transaction = self.future.pop()
|
||||||
@@ -405,7 +405,7 @@ class file:
|
|||||||
else:
|
else:
|
||||||
return getattr(self.wrapped_data, attr)
|
return getattr(self.wrapped_data, attr)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key: Union[numbers.Integral, str, bytes]) -> entity_instance:
|
||||||
if isinstance(key, numbers.Integral):
|
if isinstance(key, numbers.Integral):
|
||||||
return entity_instance(self.wrapped_data.by_id(key), self)
|
return entity_instance(self.wrapped_data.by_id(key), self)
|
||||||
elif isinstance(key, (str, bytes)):
|
elif isinstance(key, (str, bytes)):
|
||||||
@@ -501,7 +501,7 @@ class file:
|
|||||||
return [entity_instance(e, self) for e in fn(inst.wrapped_data, max_levels)]
|
return [entity_instance(e, self) for e in fn(inst.wrapped_data, max_levels)]
|
||||||
|
|
||||||
def get_inverse(
|
def get_inverse(
|
||||||
self, inst: ifcopenshell.entity_instance, allow_duplicate=False, with_attribute_indices=False
|
self, inst: ifcopenshell.entity_instance, allow_duplicate: bool = False, with_attribute_indices: bool = False
|
||||||
) -> list[ifcopenshell.entity_instance]:
|
) -> list[ifcopenshell.entity_instance]:
|
||||||
"""Return a list of entities that reference this entity
|
"""Return a list of entities that reference this entity
|
||||||
|
|
||||||
@@ -572,7 +572,7 @@ class file:
|
|||||||
self.transaction.unbatch()
|
self.transaction.unbatch()
|
||||||
return self.wrapped_data.unbatch()
|
return self.wrapped_data.unbatch()
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self) -> Generator[ifcopenshell.entity_instance, None, None]:
|
||||||
return iter(self[id] for id in self.wrapped_data.entity_names())
|
return iter(self[id] for id in self.wrapped_data.entity_names())
|
||||||
|
|
||||||
def write(self, path: "os.PathLike | str", format: Optional[str] = None, zipped: bool = False) -> None:
|
def write(self, path: "os.PathLike | str", format: Optional[str] = None, zipped: bool = False) -> None:
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ import time
|
|||||||
import ifcopenshell
|
import ifcopenshell
|
||||||
import ifcopenshell.util.attribute
|
import ifcopenshell.util.attribute
|
||||||
import ifcopenshell.ifcopenshell_wrapper as ifcopenshell_wrapper
|
import ifcopenshell.ifcopenshell_wrapper as ifcopenshell_wrapper
|
||||||
|
from typing import Union, Any
|
||||||
|
|
||||||
# This is highly experimental and incomplete, however, it may work for simple datasets.
|
# This is highly experimental and incomplete, however, it may work for simple datasets.
|
||||||
# In this simple implementation, we only support 2X3<->4 right now
|
|
||||||
|
|
||||||
cwd = os.path.dirname(os.path.realpath(__file__))
|
cwd = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
@@ -223,7 +223,9 @@ class Migrator:
|
|||||||
"User": None,
|
"User": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
def migrate(self, element: ifcopenshell.entity_instance, new_file: ifcopenshell.file) -> ifcopenshell.entity_instance:
|
def migrate(
|
||||||
|
self, element: ifcopenshell.entity_instance, new_file: ifcopenshell.file
|
||||||
|
) -> ifcopenshell.entity_instance:
|
||||||
if element.id() == 0:
|
if element.id() == 0:
|
||||||
return new_file.create_entity(element.is_a(), element.wrappedValue)
|
return new_file.create_entity(element.is_a(), element.wrappedValue)
|
||||||
try:
|
try:
|
||||||
@@ -241,7 +243,9 @@ class Migrator:
|
|||||||
self.migrated_ids[element.id()] = new_element.id()
|
self.migrated_ids[element.id()] = new_element.id()
|
||||||
return new_element
|
return new_element
|
||||||
|
|
||||||
def migrate_class(self, element, new_file):
|
def migrate_class(
|
||||||
|
self, element: ifcopenshell.entity_instance, new_file: ifcopenshell.file
|
||||||
|
) -> ifcopenshell.entity_instance:
|
||||||
try:
|
try:
|
||||||
new_element = new_file.create_entity(element.is_a())
|
new_element = new_file.create_entity(element.is_a())
|
||||||
except:
|
except:
|
||||||
@@ -260,7 +264,14 @@ class Migrator:
|
|||||||
self.migrate_attribute(attribute, element, new_file, new_element, new_element_schema)
|
self.migrate_attribute(attribute, element, new_file, new_element, new_element_schema)
|
||||||
return new_element
|
return new_element
|
||||||
|
|
||||||
def find_equivalent_attribute(self, new_element, attribute, element, attributes_mapping, reverse_mapping=False):
|
def find_equivalent_attribute(
|
||||||
|
self,
|
||||||
|
new_element: ifcopenshell.entity_instance,
|
||||||
|
attribute: ifcopenshell_wrapper.attribute,
|
||||||
|
element: ifcopenshell.entity_instance,
|
||||||
|
attributes_mapping: dict[str, dict[str, str]],
|
||||||
|
reverse_mapping: bool = False,
|
||||||
|
) -> Union[Any, None]:
|
||||||
# print("Searching for an equivalent", element, new_element, attribute.name())
|
# print("Searching for an equivalent", element, new_element, attribute.name())
|
||||||
try:
|
try:
|
||||||
if reverse_mapping:
|
if reverse_mapping:
|
||||||
@@ -281,7 +292,14 @@ class Migrator:
|
|||||||
)
|
)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
def migrate_attribute(self, attribute, element, new_file: ifcopenshell.file, new_element, new_element_schema):
|
def migrate_attribute(
|
||||||
|
self,
|
||||||
|
attribute: ifcopenshell_wrapper.attribute,
|
||||||
|
element: ifcopenshell.entity_instance,
|
||||||
|
new_file: ifcopenshell.file,
|
||||||
|
new_element: ifcopenshell.entity_instance,
|
||||||
|
new_element_schema: ifcopenshell_wrapper.declaration,
|
||||||
|
) -> None:
|
||||||
# NOTE: `attribute` is an attribute in new file schema
|
# NOTE: `attribute` is an attribute in new file schema
|
||||||
# print("Migrating attribute", element, new_element, attribute.name())
|
# print("Migrating attribute", element, new_element, attribute.name())
|
||||||
old_file = element.wrapped_data.file
|
old_file = element.wrapped_data.file
|
||||||
@@ -349,7 +367,7 @@ class Migrator:
|
|||||||
if value is not None:
|
if value is not None:
|
||||||
setattr(new_element, attribute.name(), value)
|
setattr(new_element, attribute.name(), value)
|
||||||
|
|
||||||
def generate_default_value(self, attribute, new_file):
|
def generate_default_value(self, attribute: ifcopenshell_wrapper.attribute, new_file: ifcopenshell.file) -> Any:
|
||||||
if attribute.name() in self.default_values:
|
if attribute.name() in self.default_values:
|
||||||
return self.default_values[attribute.name()]
|
return self.default_values[attribute.name()]
|
||||||
elif attribute.name() == "OwnerHistory":
|
elif attribute.name() == "OwnerHistory":
|
||||||
|
|||||||
Reference in New Issue
Block a user