Coverage for bim2sim/tasks/common/load_ifc.py: 89%
44 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-01 10:24 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-01 10:24 +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_dict = {k: [] for k in ['arch', 'hydraulic', 'ventilation']}
40 ifc_files_unsorted = []
41 ifc_files = []
42 ifc_files_paths = list(base_path.glob("**/*.ifc")) + list(
43 base_path.glob("**/*.ifcxml")) + list(
44 base_path.glob("**/*.ifczip"))
45 self.logger.info(f"Found {len(ifc_files_paths)} IFC files in project "
46 f"directory.")
47 for i, total_ifc_path in enumerate(ifc_files_paths, start=1):
48 self.logger.info(
49 f"Loading IFC file {total_ifc_path.name} {i}/{len(ifc_files_paths)}.")
50 ifc_domain = total_ifc_path.parent.name
51 reset_guids = self.playground.sim_settings.reset_guids
52 ifc_domain = IFCDomain[ifc_domain]
53 t_load_start = time.time()
54 ifc_file_cls = IfcFileClass(
55 total_ifc_path,
56 ifc_domain=ifc_domain,
57 reset_guids=reset_guids)
58 yield from ifc_file_cls.initialize_finder(self.paths.finder)
59 ifc_files_unsorted.append(ifc_file_cls)
60 t_load_end = time.time()
61 t_loading = round(t_load_end - t_load_start, 2)
62 self.logger.info(f"Loaded {total_ifc_path.name} for Domain "
63 f"{ifc_domain.name}. "
64 f"This took {t_loading} seconds")
65 for file in ifc_files_unsorted:
66 ifc_files_dict[file.domain.name].append(file)
67 for domain in ('arch', 'hydraulic', 'ventilation'):
68 ifc_files.extend(ifc_files_dict[domain])
69 if not ifc_files:
70 self.logger.error("No ifc found in project folder.")
71 raise AssertionError("No ifc found. Check '%s'" % base_path)
72 elif len(ifc_files) < len(ifc_files_unsorted):
73 self.logger.warning("Not all ifc files were added for further "
74 "processing. IFCDomain may not be recognized.")
75 raise AssertionError("Not all ifc processed. Check '%s'" %
76 base_path)
77 self.logger.info(f"Loaded {len(ifc_files)} IFC-files.")
78 return ifc_files