Recorded webinar exploring how artificial intelligence is transforming workflows in architecture, engineering, and construction.
Explore these practical code examples demonstrating how to implement AI techniques in AEC applications. These snippets showcase machine learning, computer vision, and natural language processing for AEC workflows.
1import numpy as np
2import pandas as pd
3from sklearn.ensemble import RandomForestClassifier
4from sklearn.model_selection import train_test_split
5from sklearn.metrics import classification_report
6import ifcopenshell
7
8# Load IFC file
9def load_ifc_data(ifc_file_path):
10 print(f"Loading IFC file: {ifc_file_path}")
11 ifc_file = ifcopenshell.open(ifc_file_path)
12 return ifc_file
13
14# Extract features from BIM elements
15def extract_element_features(ifc_file):
16 print("Extracting features from BIM elements...")
17
18 # Get all walls, slabs, columns, beams, windows, and doors
19 walls = ifc_file.by_type("IfcWall")
20 slabs = ifc_file.by_type("IfcSlab")
21 columns = ifc_file.by_type("IfcColumn")
22 beams = ifc_file.by_type("IfcBeam")
23 windows = ifc_file.by_type("IfcWindow")
24 doors = ifc_file.by_type("IfcDoor")
25
26 # Combine all elements
27 all_elements = walls + slabs + columns + beams + windows + doors
28
29 # Create feature list
30 features = []
31 labels = []
32
33 for element in all_elements:
34 # Skip elements without properties
35 if not hasattr(element, "IsDefinedBy"):
36 continue
37
38 # Get element properties
39 element_props = {}
40 element_props["GlobalId"] = element.GlobalId
41 element_props["Type"] = element.is_a()
42
43 # Extract geometric properties
44 try:
45 # Get representation
46 if hasattr(element, "Representation"):
47 rep = element.Representation
48 if rep and rep.Representations:
49 for r in rep.Representations:
50 if r.RepresentationType == 'Brep':
51 # Extract bounding box
52
53 # Get volume and surface area
54 element_props["HasBrep"] = 1
55 else:
56 element_props["HasBrep"] = 0
57 except:
58 element_props["HasBrep"] = 0
59
60 # Extract material properties if available
61 try:
62 if hasattr(element, "HasAssociations"):
63 for association in element.HasAssociations:
64 if association.is_a('IfcRelAssociatesMaterial'):
65 material = association.RelatingMaterial
66 if material.is_a('IfcMaterialLayerSetUsage'):
67 element_props["HasMaterial"] = 1
68 else:
69 element_props["HasMaterial"] = 0
70 except:
71 element_props["HasMaterial"] = 0
72
73 # Add more features as needed
74
75 # Add to feature list
76 features.append([element_props.get("HasBrep", 0), element_props.get("HasMaterial", 0)])
77 labels.append(element_props["Type"])
78
79 return features, labels
80
81# Train a machine learning model to classify BIM elements
82def train_element_classifier(features, labels):
83 print("Training element classifier...")
84
85 # Convert to numpy arrays
86 X = np.array(features)
87 y = np.array(labels)
88
89 # Split data into training and testing sets
90 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
91
92 # Train a random forest classifier
93 clf = RandomForestClassifier(n_estimators=100, random_state=42)
94 clf.fit(X_train, y_train)
95
96 # Evaluate the classifier
97 y_pred = clf.predict(X_test)
98 print(classification_report(y_test, y_pred))
99
100 return clf
101
102# Predict element types for new elements
103def predict_element_types(clf, features):
104 print("Predicting element types...")
105
106 # Convert to numpy array
107 X = np.array(features)
108
109 # Make predictions
110 predictions = clf.predict(X)
111
112 return predictions
113
114# Main function
115def main():
116 # Load IFC file
117 ifc_file = load_ifc_data("path/to/building_model.ifc")
118
119 # Extract features
120 features, labels = extract_element_features(ifc_file)
121
122 # Train classifier
123 clf = train_element_classifier(features, labels)
124
125 # Save the trained model
126 import joblib
127 joblib.dump(clf, "bim_element_classifier.joblib")
128
129 print("Model saved as 'bim_element_classifier.joblib'")
130
131if __name__ == "__main__":
132 main()
Download the complete code examples and additional resources to implement these solutions in your projects.
Download Complete Code ExamplesExperiment with the code examples in our interactive playground. Modify parameters, test different inputs, and see the results in real-time.