11import random
2- import os
3- import pathlib
42from pathlib import Path
53from random import randint
64from tempfile import NamedTemporaryFile
7- from typing import List , Any
5+ from typing import Any , List
86
97import ifcopenshell
10- import ifcopenshell .geom as geom
11- from shapely import affinity , Point
12- from viktor import ViktorController , File , Color , geometry , UserException
13- from viktor .geometry import Triangle , TriangleAssembly , Material
14- from viktor .parametrization import ViktorParametrization , FileField , MultiSelectField , DownloadButton , \
15- LineBreak , Text , Lookup , IsFalse , BooleanField , ActionButton
16- from viktor .views import GeometryView , GeometryResult
8+ from shapely import Point , affinity
9+ from viktor import Color , File , UserException , ViktorController , geometry
10+ from viktor .geometry import Material , Triangle , TriangleAssembly
11+ from viktor .parametrization import (
12+ BooleanField ,
13+ DownloadButton ,
14+ FileField ,
15+ LineBreak ,
16+ MultiSelectField ,
17+ Text ,
18+ ViktorParametrization ,
19+ )
1720from viktor .result import DownloadResult
21+ from viktor .views import GeometryResult , GeometryView
1822
1923
2024def get_element_options (params , ** kwargs ) -> List [str ]:
2125 """Get all existing geometry element types from ifc file."""
22- if not params .ifc_upload and params .get_sample_ifc_toggle == False :
26+ if not params .ifc_upload and not params .get_sample_ifc_toggle :
2327 return []
24- elif params .get_sample_ifc_toggle == True :
25- params .sample_ifc = File .from_path (Path (__file__ ).parent / "AC20-FZK-Haus (Sample IFC).ifc" )
28+ if params .get_sample_ifc_toggle :
29+ params .sample_ifc = File .from_path (
30+ Path (__file__ ).parent / "AC20-FZK-Haus (Sample IFC).ifc"
31+ )
2632 model = load_ifc_file_into_model (params .sample_ifc )
2733 else :
2834 model = load_ifc_file_into_model (params .ifc_upload .file )
@@ -36,6 +42,7 @@ def get_element_options(params, **kwargs) -> List[str]:
3642
3743class Parametrization (ViktorParametrization ):
3844 """Viktor parametrization."""
45+
3946 text1 = Text (
4047 """
4148# Welcome to the ifc-viewer app!
@@ -57,18 +64,17 @@ class Parametrization(ViktorParametrization):
5764 )
5865
5966 ifc_upload = FileField (
60- "Upload model" ,
61- file_types = [".ifc" ],
67+ "Upload model" ,
68+ file_types = [".ifc" ],
6269 max_size = 20_000_000 ,
6370 )
64-
71+
6572 get_sample_ifc_toggle = BooleanField (
66- "Use sample IFC File" ,
67- default = False ,
73+ "Use sample IFC File" ,
74+ default = False ,
6875 flex = 30 ,
6976 )
7077
71-
7278 lb = LineBreak ()
7379 text2 = Text (
7480 """
@@ -79,8 +85,8 @@ class Parametrization(ViktorParametrization):
7985 """
8086 )
8187 element_filter = MultiSelectField (
82- "Filter elements" ,
83- options = get_element_options ,
88+ "Filter elements" ,
89+ options = get_element_options ,
8490 )
8591
8692 lb = LineBreak ()
@@ -91,29 +97,29 @@ class Parametrization(ViktorParametrization):
9197 """
9298 )
9399 download = DownloadButton (
94- ' Download' ,
95- method = ' download_file' ,
100+ " Download" ,
101+ method = " download_file" ,
96102 )
97103
98104
99105class Controller (ViktorController ):
100106 """Viktor Controller."""
101107
102- label = ' My Entity Type'
108+ label = " My Entity Type"
103109 parametrization = Parametrization
104110
105111 def download_file (self , params , ** kwargs ):
106112 model = load_ifc_file_into_model (params .ifc_upload .file )
107- #remove all other parts from the ifc file which are not viewed
113+ # remove all other parts from the ifc file which are not viewed
108114 for element in model .by_type ("IfcElement" ):
109115 if element .get_info ()["type" ] not in params .element_filter :
110116 model .remove (element )
111- #part where we save the model as seen in the viewer
117+ # part where we save the model as seen in the viewer
112118 temp_file = NamedTemporaryFile (suffix = ".ifc" , delete = False , mode = "wb" )
113119 model .write (str (Path (temp_file .name )))
114120 temp_file .close ()
115121 path_out = Path (temp_file .name )
116- return DownloadResult (path_out .read_bytes (), ' filtered_elements.ifc' )
122+ return DownloadResult (path_out .read_bytes (), " filtered_elements.ifc" )
117123
118124 @staticmethod
119125 @GeometryView ("3D model of filtered elements" , duration_guess = 12 )
@@ -124,7 +130,9 @@ def ifc_view(params, **kwargs):
124130 if not params .element_filter and params .get_sample_ifc_toggle == False :
125131 raise UserException ("Upload ifc file and select elements." )
126132 if params .get_sample_ifc_toggle == True :
127- params .sample_ifc = File .from_path (Path (__file__ ).parent / "AC20-FZK-Haus (Sample IFC).ifc" )
133+ params .sample_ifc = File .from_path (
134+ Path (__file__ ).parent / "AC20-FZK-Haus (Sample IFC).ifc"
135+ )
128136 model = load_ifc_file_into_model (params .sample_ifc )
129137 else :
130138 model = load_ifc_file_into_model (params .ifc_upload .file )
@@ -134,17 +142,14 @@ def ifc_view(params, **kwargs):
134142 geometry_groups = []
135143 selected_elements = params .element_filter
136144 for element_type in selected_elements :
137- material = Material (
138- name = element_type ,
139- color = get_random_color (element_type )
140- )
145+ material = Material (name = element_type , color = get_random_color (element_type ))
141146 elements = model .by_type (element_type )
142147 for element in elements :
143148
144149 # Create triangle assembly and assign material
145150 triangle_assembly = TriangleAssembly (
146151 triangles = get_faces_from_ifc_element (element , settings ),
147- material = material
152+ material = material ,
148153 )
149154 geometry_groups .append (triangle_assembly )
150155
@@ -161,10 +166,18 @@ def get_faces_from_ifc_element(element, settings) -> List[Triangle]:
161166 # Convert IfcOpenShell matrix to Shapely matrix
162167 matrix = shape .transformation .matrix .data
163168 shapely_matrix = [
164- matrix [0 ], matrix [3 ], matrix [6 ],
165- matrix [1 ], matrix [4 ], matrix [7 ],
166- matrix [2 ], matrix [5 ], matrix [8 ],
167- matrix [9 ], matrix [10 ], matrix [11 ],
169+ matrix [0 ],
170+ matrix [3 ],
171+ matrix [6 ],
172+ matrix [1 ],
173+ matrix [4 ],
174+ matrix [7 ],
175+ matrix [2 ],
176+ matrix [5 ],
177+ matrix [8 ],
178+ matrix [9 ],
179+ matrix [10 ],
180+ matrix [11 ],
168181 ]
169182
170183 # Transform all vertices with transformation matrix
@@ -181,7 +194,7 @@ def get_faces_from_ifc_element(element, settings) -> List[Triangle]:
181194 triangle = Triangle (
182195 grouped_verts [faces [i ]],
183196 grouped_verts [faces [i + 1 ]],
184- grouped_verts [faces [i + 2 ]]
197+ grouped_verts [faces [i + 2 ]],
185198 )
186199 grouped_faces .append (triangle )
187200
@@ -191,7 +204,7 @@ def get_faces_from_ifc_element(element, settings) -> List[Triangle]:
191204def load_ifc_file_into_model (file : File ) -> Any :
192205 """Load ifc file into ifc model object."""
193206 ifc_upload : File = file
194- temp_file = NamedTemporaryFile (suffix = ' .sld' , delete = False , mode = 'wb' )
207+ temp_file = NamedTemporaryFile (suffix = " .sld" , delete = False , mode = "wb" )
195208 temp_file .write (ifc_upload .getvalue_binary ())
196209 temp_file .close ()
197210 path = Path (temp_file .name )
0 commit comments