Files
IfcOpenShell/src/ifcopenshell-python/ifcopenshell/api/geometry/add_window_representation.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

415 lines
19 KiB
Python
Raw Normal View History

2023-01-11 14:33:56 +05:00
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2022 @Andrej730
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell.util.unit
from math import sin, cos
from ifcopenshell.util.shape_builder import ShapeBuilder, V
from mathutils import Vector
# SCHEMAS describe panels setup
# where:
# - schema rows represent window X axis
# - schema columns represent window Y axis
# - order of rows is from top of the window to bottom
DEFAULT_PANEL_SCHEMAS = {
2023-01-25 10:49:03 +05:00
"SINGLE_PANEL": [[0]],
"DOUBLE_PANEL_HORIZONTAL": [[0], [1]],
"DOUBLE_PANEL_VERTICAL": [[0, 1]],
"TRIPLE_PANEL_BOTTOM": [[0, 1], [2, 2]],
"TRIPLE_PANEL_TOP": [[0, 0], [1, 2]],
"TRIPLE_PANEL_LEFT": [[0, 1], [0, 2]],
"TRIPLE_PANEL_RIGHT": [[0, 1], [2, 1]],
"TRIPLE_PANEL_HORIZONTAL": [[0], [1], [2]],
"TRIPLE_PANEL_VERTICAL": [[0, 1, 2]],
2023-01-11 14:33:56 +05:00
}
2023-01-25 10:49:03 +05:00
2023-01-11 14:33:56 +05:00
class Usecase:
def __init__(self, file, **settings):
self.file = file
# http://ifc43-docs.standards.buildingsmart.org/IFC/RELEASE/IFC4x3/HTML/lexical/IfcWindow.htm
# http://ifc43-docs.standards.buildingsmart.org/IFC/RELEASE/IFC4x3/HTML/lexical/IfcWindowTypePartitioningEnum.htm
2023-01-25 10:49:03 +05:00
# http://ifc43-docs.standards.buildingsmart.org/IFC/RELEASE/IFC4x3/HTML/lexical/IfcWindowLiningProperties.htm
2023-01-11 14:33:56 +05:00
# http://ifc43-docs.standards.buildingsmart.org/IFC/RELEASE/IFC4x3/HTML/lexical/IfcWindowPanelProperties.htm
self.settings = {
"context": None, # IfcGeometricRepresentationContext
2023-01-25 10:49:03 +05:00
# SINGLE_PANEL, DOUBLE_PANEL_HORIZONTAL, DOUBLE_PANEL_VERTICAL,
2023-01-11 14:33:56 +05:00
# TRIPLE_PANEL_BOTTOM, TRIPLE_PANEL_HORIZONTAL, TRIPLE_PANEL_LEFT, TRIPLE_PANEL_RIGHT, TRIPLE_PANEL_TOP, TRIPLE_PANEL_VERTICAL
2023-01-25 10:49:03 +05:00
"partition_type": "SINGLE_PANEL",
2023-01-11 14:33:56 +05:00
"overall_height": 900,
2023-01-25 10:49:03 +05:00
"overall_width": 600,
2023-01-11 14:33:56 +05:00
"lining_properties": {
2023-01-25 10:49:03 +05:00
"LiningDepth": 50,
"LiningThickness": 50,
"LiningOffset": 50, # offset to the wall
"LiningToPanelOffsetX": 25,
"LiningToPanelOffsetY": 25,
2023-01-11 14:33:56 +05:00
# applies to DoublePanelVertical, TriplePanelBottom, TriplePanelTop, TriplePanelLeft, TriplePanelRight
# mullion - distance between panels
2023-01-25 10:49:03 +05:00
"FirstMullionOffset": ..., # distance from the first lining to the mullion
2023-01-11 14:33:56 +05:00
# TODO: take mullion thickness into account
2023-01-25 10:49:03 +05:00
"MullionThickness": ...,
2023-01-11 14:33:56 +05:00
# applies to DoublePanelHorizontal, TriplePanelBottom, TriplePanelTop, TriplePanelLeft, TriplePanelRight
# works similar way to mullion
2023-01-25 10:49:03 +05:00
"FirstTransomOffset": ...,
"TransomThickness": ...,
2023-01-11 14:33:56 +05:00
# applies to TriplePanelVertical
2023-01-25 10:49:03 +05:00
"SecondMullionOffset": ..., # distance from the first lining to the second mullion
2023-01-11 14:33:56 +05:00
# applies to TriplePanelHorizontal
2023-01-25 10:49:03 +05:00
"SecondTransomOffset": ...,
"ShapeAspectStyle": None, # DEPRECATED
},
2023-01-11 14:33:56 +05:00
"panel_properties": [
{
2023-01-25 10:49:03 +05:00
"FrameDepth": 35, # by Y
"FrameThickness": 35, # by X
2023-01-11 14:33:56 +05:00
# BOTTOM, LEFT, MIDDLE, RIGHT, TOP
2023-01-25 10:49:03 +05:00
"PanelPosition": ...,
2023-01-11 14:33:56 +05:00
# defines the basic ways to describe how window panels operate
# how it's hanged, how it opens
2023-01-25 10:49:03 +05:00
"OperationType": None,
"ShapeAspectStyle": None, # DEPRECATED
2023-01-11 14:33:56 +05:00
# Custom Parameter not available in IFC
# dimensions of the panel relative to overall window dimensions
2023-01-25 10:49:03 +05:00
"RelativeWidth": 1.0,
"RelativeHeight": 1.0,
2023-01-11 14:33:56 +05:00
},
2023-01-25 10:49:03 +05:00
],
2023-01-11 14:33:56 +05:00
}
for key, value in settings.items():
self.settings[key] = value
2023-01-25 10:49:03 +05:00
self.settings["panel_schema"] = DEFAULT_PANEL_SCHEMAS[self.settings["partition_type"]]
2023-01-11 14:33:56 +05:00
# recalculate relative width and height to avoid errors
# TODO: rework or remove
# panels_data = self.settings['panel_properties']
# current_height = sum(p['RelativeHeight'] for p in panels_data)
# current_width = sum(p['RelativeWidth'] for p in panels_data)
2023-01-25 10:49:03 +05:00
2023-01-11 14:33:56 +05:00
# for p in panels_data:
# if current_height != 1.0:
# p['RelativeHeight'] = p['RelativeHeight'] / current_height
# if current_width != 1.0:
# p['RelativeWidth'] = p['RelativeWidth'] / current_width
def execute(self):
self.settings["unit_scale"] = ifcopenshell.util.unit.calculate_unit_scale(self.file)
builder = ShapeBuilder(self.file)
2023-01-25 10:49:03 +05:00
overall_height = self.convert_si_to_unit(self.settings["overall_height"])
overall_width = self.convert_si_to_unit(self.settings["overall_width"])
2023-01-11 14:33:56 +05:00
2023-01-25 10:49:03 +05:00
if self.settings["context"].TargetView == "ELEVATION_VIEW":
2023-01-11 14:33:56 +05:00
rect = builder.rectangle(V(overall_width, 0, overall_height))
2023-01-25 10:49:03 +05:00
representation_evelevation = builder.get_representation(self.settings["context"], rect)
2023-01-11 14:33:56 +05:00
return representation_evelevation
2023-01-25 10:49:03 +05:00
panel_schema = self.settings["panel_schema"]
panels = self.settings["panel_properties"]
2023-01-11 14:33:56 +05:00
accumulated_height = [0] * len(panel_schema[0])
built_panels = []
window_items = []
2023-01-25 10:49:03 +05:00
lining_thickness = self.convert_si_to_unit(self.settings["lining_properties"]["LiningThickness"])
lining_depth = self.convert_si_to_unit(self.settings["lining_properties"]["LiningDepth"])
lining_offset = self.convert_si_to_unit(self.settings["lining_properties"]["LiningOffset"])
lining_panel_offset_x = self.convert_si_to_unit(self.settings["lining_properties"]["LiningToPanelOffsetX"])
lining_panel_offset_y = self.convert_si_to_unit(self.settings["lining_properties"]["LiningToPanelOffsetY"])
2023-01-11 14:33:56 +05:00
glass_thickness = self.convert_si_to_unit(10)
for row_i, panel_row in enumerate(reversed(panel_schema)):
accumulated_width = 0
for column_i, panel_i in enumerate(panel_row):
if panel_i in built_panels:
2023-01-25 10:49:03 +05:00
accumulated_height[column_i] += cur_panel["RelativeHeight"]
accumulated_width += cur_panel["RelativeWidth"]
2023-01-11 14:33:56 +05:00
continue
cur_panel = panels[panel_i]
current_items = []
2023-01-25 10:49:03 +05:00
panel_depth = self.convert_si_to_unit(cur_panel["FrameDepth"])
panel_thickness = self.convert_si_to_unit(cur_panel["FrameThickness"])
panel_height = cur_panel["RelativeHeight"] * overall_height
panel_width = cur_panel["RelativeWidth"] * overall_width
2023-01-11 14:33:56 +05:00
2023-01-25 10:49:03 +05:00
panel_actual_width = panel_width - lining_panel_offset_x * 2
panel_actual_height = panel_height - lining_panel_offset_x * 2
2023-01-11 14:33:56 +05:00
2023-01-25 10:49:03 +05:00
glass_width = panel_actual_width - panel_thickness * 2
glass_height = panel_actual_height - panel_thickness * 2
2023-01-11 14:33:56 +05:00
# build lining
lining_items_vertical = []
lining_items = []
# lining is calculated on panel level because
# panel depth is used
2023-01-25 10:49:03 +05:00
lining_rectangle = builder.rectangle(size=V(lining_thickness, lining_depth))
2023-01-11 14:33:56 +05:00
# need to check offsets to decide whether lining should be rectangle
# or L shaped
2023-01-25 10:49:03 +05:00
if lining_panel_offset_x >= lining_thickness or lining_panel_offset_y >= lining_depth:
2023-01-11 14:33:56 +05:00
lining_vertical_polyline = ifcopenshell.util.element.copy_deep(self.file, lining_rectangle)
lining_vertical_height = panel_height
else:
lining_points = [
2023-01-25 10:49:03 +05:00
V(0, 0),
2023-01-11 14:33:56 +05:00
V(0, lining_depth),
V(lining_panel_offset_x, lining_depth),
2023-01-25 10:49:03 +05:00
V(lining_panel_offset_x, lining_depth - (panel_depth - lining_panel_offset_y)),
V(lining_thickness, lining_depth - (panel_depth - lining_panel_offset_y)),
2023-01-11 14:33:56 +05:00
V(lining_thickness, 0),
]
# lining vertical
lining_vertical_polyline = builder.polyline(lining_points, closed=True)
lining_vertical_height = panel_height - lining_panel_offset_x * 2
2023-01-25 10:49:03 +05:00
# if lining panel X offset is present
2023-01-11 14:33:56 +05:00
# then we also need to add two more box shapes
2023-01-25 10:49:03 +05:00
# to finish the lining after the panel ends
2023-01-11 14:33:56 +05:00
if lining_panel_offset_x > 0:
2023-01-25 10:49:03 +05:00
lining_vertical_addition = builder.extrude(
builder.deep_copy(lining_rectangle), lining_panel_offset_x
)
lining_items_vertical.extend(
[
lining_vertical_addition,
builder.translate(
lining_vertical_addition,
V(0, 0, panel_height - lining_panel_offset_x),
create_copy=True,
),
]
)
2023-01-11 14:33:56 +05:00
# horizontal lining
lining_horizontal_polyline = builder.deep_copy(lining_vertical_polyline)
lining_horizontal_extruded = builder.extrude(
lining_horizontal_polyline,
2023-01-25 10:49:03 +05:00
magnitude=panel_width - 2 * lining_thickness,
extrusion_vector=V(0, 0, -1),
position_z_axis=V(-1, 0, 0),
position_x_axis=V(0, 0, 1),
position=V(lining_thickness, 0, 0),
2023-01-11 14:33:56 +05:00
)
# TODO: should implement mirror by Z for more readability
# TODO: investigate meaning of mirror axes in case of custom x/y/z space
# lining_horizontal_mirrored = builder.mirror(
2023-01-25 10:49:03 +05:00
# lining_horizontal_extruded,
2023-01-11 14:33:56 +05:00
# mirror_point=V(0, panel_height/2),
# mirror_axes=V(0,1),
# create_copy=True
# )
2023-01-25 10:49:03 +05:00
2023-01-11 14:33:56 +05:00
lining_horizontal_polyline_mirrored = builder.mirror(
lining_horizontal_polyline,
2023-01-25 10:49:03 +05:00
mirror_axes=V(1, 0),
mirror_point=V(lining_thickness, 0),
create_copy=True,
2023-01-11 14:33:56 +05:00
)
lining_horizontal_mirrored = builder.extrude(
lining_horizontal_polyline_mirrored,
2023-01-25 10:49:03 +05:00
magnitude=panel_width - 2 * lining_thickness,
extrusion_vector=V(0, 0, -1),
position_z_axis=V(-1, 0, 0),
position_x_axis=V(0, 0, 1),
position=V(lining_thickness, 0, panel_height - lining_thickness * 2),
2023-01-11 14:33:56 +05:00
)
lining_items.extend([lining_horizontal_extruded, lining_horizontal_mirrored])
2023-01-25 10:49:03 +05:00
extrusion_position = V(0, 0, lining_panel_offset_x)
lining_vertical_extruded = builder.extrude(
lining_vertical_polyline, lining_vertical_height, position=extrusion_position
)
2023-01-11 14:33:56 +05:00
lining_items_vertical.append(lining_vertical_extruded)
lining_items_vertical_mirrored = builder.mirror(
2023-01-25 10:49:03 +05:00
lining_items_vertical, mirror_point=V(panel_width / 2, 0), mirror_axes=V(1, 0), create_copy=True
)
2023-01-11 14:33:56 +05:00
lining_items.extend(lining_items_vertical)
lining_items.extend(lining_items_vertical_mirrored)
current_items.extend(lining_items)
# PANEL
panel_items = []
panel_position = V(
2023-01-25 10:49:03 +05:00
lining_panel_offset_x, (lining_depth - panel_depth) + lining_panel_offset_y, lining_panel_offset_x
)
2023-01-11 14:33:56 +05:00
panel_rect = builder.rectangle(size=V(panel_actual_width, 0, panel_actual_height))
glass_rect = builder.rectangle(
2023-01-25 10:49:03 +05:00
size=V(glass_width, 0, glass_height), position=V(panel_thickness, 0, panel_thickness)
)
2023-01-11 14:33:56 +05:00
panel_profile = builder.profile(panel_rect, inner_curves=glass_rect)
panel_extruded = builder.extrude(
2023-01-25 10:49:03 +05:00
panel_profile, panel_depth, extrusion_vector=V(0, 1, 0), position=panel_position
)
2023-01-11 14:33:56 +05:00
panel_items.append(panel_extruded)
current_items.extend(panel_items)
# add glass
2023-01-25 10:49:03 +05:00
glass_position = panel_position + V(0, panel_depth / 2 - glass_thickness / 2, 0)
2023-01-11 14:33:56 +05:00
glass_rect = builder.deep_copy(glass_rect)
glass = builder.extrude(
2023-01-25 10:49:03 +05:00
glass_rect, glass_thickness, extrusion_vector=V(0, 1, 0), position=glass_position
2023-01-11 14:33:56 +05:00
)
current_items.append(glass)
# translate panel
2023-01-25 10:49:03 +05:00
accumulated_offset = V(accumulated_width, 0, accumulated_height[column_i]) * V(
overall_width, 0, overall_height
)
2023-01-11 14:33:56 +05:00
builder.translate(current_items, accumulated_offset)
built_panels.append(panel_i)
window_items.extend(current_items)
2023-01-25 10:49:03 +05:00
accumulated_height[column_i] += cur_panel["RelativeHeight"]
accumulated_width += cur_panel["RelativeWidth"]
builder.translate(window_items, V(0, lining_offset, 0)) # wall offset
representation = builder.get_representation(self.settings["context"], window_items)
2023-01-11 14:33:56 +05:00
return representation
def convert_si_to_unit(self, value):
return value * 0.001 / self.settings["unit_scale"]
2023-01-25 10:49:03 +05:00
2023-01-11 14:33:56 +05:00
# TODO: remove test at the end
if __name__ == "__main__":
ifc_file = ifcopenshell.file()
project = ifcopenshell.api.run(
"root.create_entity", ifc_file, ifc_class="IfcProject", name=f"Non-structural assets library"
)
library = ifcopenshell.api.run(
"root.create_entity", ifc_file, ifc_class="IfcProjectLibrary", name=f"Non-structural assets library"
)
ifcopenshell.api.run("project.assign_declaration", ifc_file, definition=library, relating_context=project)
unit = ifcopenshell.api.run("unit.add_si_unit", ifc_file, unit_type="LENGTHUNIT", name="METRE", prefix="MILLI")
ifcopenshell.api.run("unit.assign_unit", ifc_file, units=[unit])
model = ifcopenshell.api.run("context.add_context", ifc_file, context_type="Model")
plan = ifcopenshell.api.run("context.add_context", ifc_file, context_type="Plan")
representations = {
"body": ifcopenshell.api.run(
"context.add_context",
ifc_file,
context_type="Model",
context_identifier="Body",
target_view="MODEL_VIEW",
parent=model,
),
"elevation": ifcopenshell.api.run(
"context.add_context",
ifc_file,
context_type="Model",
context_identifier="Profile",
target_view="ELEVATION_VIEW",
parent=model,
),
"annotation": ifcopenshell.api.run(
"context.add_context",
ifc_file,
context_type="Plan",
context_identifier="Annotation",
target_view="PLAN_VIEW",
parent=plan,
),
}
settings = {
2023-01-25 10:49:03 +05:00
"context": representations["body"],
# SINGLE_PANEL, DOUBLE_PANEL_HORIZONTAL, DOUBLE_PANEL_VERTICAL,
2023-01-11 14:33:56 +05:00
# TRIPLE_PANEL_BOTTOM, TRIPLE_PANEL_HORIZONTAL, TRIPLE_PANEL_LEFT, TRIPLE_PANEL_RIGHT, TRIPLE_PANEL_TOP, TRIPLE_PANEL_VERTICAL
2023-01-25 10:49:03 +05:00
"partition_type": "TRIPLE_PANEL_RIGHT",
2023-01-11 14:33:56 +05:00
"overall_height": 900,
2023-01-25 10:49:03 +05:00
"overall_width": 600 * 3,
2023-01-11 14:33:56 +05:00
# "lining_properties": {
# 'LiningDepth': 50,
# 'LiningThickness': 50,
# 'LiningOffset': 50, # offset to the wall
# 'LiningToPanelOffsetX': 25,
# 'LiningToPanelOffsetY': 25,
# # applies to DoublePanelVertical, TriplePanelBottom, TriplePanelTop, TriplePanelLeft, TriplePanelRight
# # mullion - distance between panels
# 'FirstMullionOffset': ..., # distance from the first lining to the mullion
# # TODO: take mullion thickness into account
# 'MullionThickness': ...,
# # applies to DoublePanelHorizontal, TriplePanelBottom, TriplePanelTop, TriplePanelLeft, TriplePanelRight
# # works similar way to mullion
2023-01-25 10:49:03 +05:00
# 'FirstTransomOffset': ...,
2023-01-11 14:33:56 +05:00
# 'TransomThickness': ...,
# # applies to TriplePanelVertical
# 'SecondMullionOffset': ..., # distance from the first lining to the second mullion
# # applies to TriplePanelHorizontal
# 'SecondTransomOffset': ...,
# 'ShapeAspectStyle': None, # DEPRECATED
2023-01-25 10:49:03 +05:00
# },
2023-01-11 14:33:56 +05:00
"panel_properties": [
{
2023-01-25 10:49:03 +05:00
"FrameDepth": 35, # by Y
"FrameThickness": 35, # by X
"RelativeWidth": 1.0 / 2,
"RelativeHeight": 1.0 / 2,
2023-01-11 14:33:56 +05:00
},
{
2023-01-25 10:49:03 +05:00
"FrameDepth": 35, # by Y
"FrameThickness": 35, # by X
"RelativeWidth": 1.0 / 2,
"RelativeHeight": 1.0,
2023-01-11 14:33:56 +05:00
},
{
2023-01-25 10:49:03 +05:00
"FrameDepth": 35, # by Y
"FrameThickness": 35, # by X
"RelativeWidth": 1.0 / 2,
"RelativeHeight": 1.0 / 2,
2023-01-11 14:33:56 +05:00
},
2023-01-25 10:49:03 +05:00
],
2023-01-11 14:33:56 +05:00
}
# builder = ShapeBuilder(ifc_file)
# points = [V(0,0), V(5,1)]
# print(points)
# base_point = V(2,2)
# points = [p + base_point for p in points]
# points = [builder.mirror_2d_point(p, mirror_axes=V(0,1), mirror_point=V(3,3)) for p in points]
# print('mirrored')
# print(points)
# print('mirrored base point')
# base_point = builder.mirror_2d_point(base_point, mirror_axes=V(0,1), mirror_point=V(3,3))
# print(base_point)
# print('base line')
# print([p - base_point for p in points])
use_case = Usecase(ifc_file, **settings)
representation = use_case.execute()
print(representation)
2023-01-25 10:49:03 +05:00
settings["context"] = representations["elevation"]
2023-01-11 14:33:56 +05:00
use_case = Usecase(ifc_file, **settings)
representation_2d = use_case.execute()
print(representation_2d)
2023-01-25 10:49:03 +05:00
ifc_file.write("tmp.ifc")