Coverage for bim2sim/tasks/common/load_ifc.py: 91%
35 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 17:09 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 17:09 +0000
1from __future__ import annotations
3import time
4from pathlib import Path
6from bim2sim.kernel.ifc_file import IfcFileClass
7from bim2sim.tasks.base import ITask
8from bim2sim.utilities.types import IFCDomain
11class LoadIFC(ITask):
12 """Load all IFC files from PROJECT.ifc_base path.
14 This tasks reads the IFC files of one or multiple domains inside bim2sim.
16 Returns:
17 ifc: list of one or multiple IfcFileClass elements
18 """
19 touches = ('ifc_files', )
21 def run(self):
22 self.logger.info("Loading IFC files")
23 ifc_files = yield from self.load_ifc_files(self.paths.ifc_base)
24 return ifc_files,
26 def load_ifc_files(self, base_path: Path):
27 """Load all ifc files in given base_path or a specific file in this path
29 Loads the ifc files inside the different domain folders in the base
30 path, and initializes the bim2sim ifc file classes.
32 Args:
33 base_path: Pathlib path that holds the different domain folders,
34 which hold the ifc files.
35 """
36 if not base_path.is_dir():
37 raise AssertionError(f"Given base_path {base_path} is not a"
38 f" directory. Please provide a directory.")
39 ifc_files = []
40 ifc_files_paths = list(base_path.glob("**/*.ifc")) + list(
41 base_path.glob("**/*.ifcxml")) + list(
42 base_path.glob("**/*.ifczip"))
43 self.logger.info(f"Found {len(ifc_files_paths)} IFC files in project "
44 f"directory.")
45 for i, total_ifc_path in enumerate(ifc_files_paths, start=1):
46 self.logger.info(
47 f"Loading IFC file {total_ifc_path.name} {i}/{len(ifc_files_paths)}.")
48 ifc_domain = total_ifc_path.parent.name
49 reset_guids = self.playground.sim_settings.reset_guids
50 ifc_domain = IFCDomain[ifc_domain]
51 t_load_start = time.time()
52 ifc_file_cls = IfcFileClass(
53 total_ifc_path,
54 ifc_domain=ifc_domain,
55 reset_guids=reset_guids)
56 yield from ifc_file_cls.initialize_finder(self.paths.finder)
57 ifc_files.append(ifc_file_cls)
58 t_load_end = time.time()
59 t_loading = round(t_load_end - t_load_start, 2)
60 self.logger.info(f"Loaded {total_ifc_path.name} for Domain "
61 f"{ifc_domain.name}. "
62 f"This took {t_loading} seconds")
63 if not ifc_files:
64 self.logger.error("No ifc found in project folder.")
65 raise AssertionError("No ifc found. Check '%s'" % base_path)
66 self.logger.info(f"Loaded {len(ifc_files)} IFC-files.")
67 return ifc_files