-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_layers.py
152 lines (125 loc) · 5.43 KB
/
ifc_layers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# ***************************************************************************
# * *
# * Copyright (c) 2023 Yorik van Havre <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License (GPL) *
# * as published by the Free Software Foundation; either version 3 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program 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 General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""This NativeIFC module deals with layers"""
import ifc_tools
import ifcopenshell
from ifcopenshell import util
def load_layers(obj):
"""Loads all the layers of an IFC file"""
proj = ifc_tools.get_project(obj)
ifcfile = ifc_tools.get_ifcfile(obj)
layers = ifcfile.by_type("IfcPresentationLayerAssignment")
for layer in layers:
obj = get_layer(layer, proj)
populate_layer(obj)
def has_layers(obj):
"""Returns true if the given project has layers"""
ifcfile = ifc_tools.get_ifcfile(obj)
layers = ifcfile.by_type("IfcPresentationLayerAssignment")
if layers:
return True
return False
def get_layer(layer, project):
"""Returns (creates if necessary) a layer object in the given project"""
group = ifc_tools.get_group(project, "IfcLayersGroup")
if not group:
return None
if hasattr(project, "Document"):
doc = project.Document
else:
doc = project
exobj = ifc_tools.get_object(layer, doc)
if exobj:
return exobj
obj = ifc_tools.add_object(doc, otype="layer")
ifcfile = ifc_tools.get_ifcfile(project)
ifc_tools.add_properties(obj, ifcfile, layer)
group.addObject(obj)
return obj
def populate_layer(obj):
"""Attaches all the possible objects to this layer"""
g = []
element = ifc_tools.get_ifc_element(obj)
for shape in getattr(element, "AssignedItems", []):
rep = getattr(shape, "OfProductRepresentation", None)
for prod in getattr(rep, "ShapeOfProduct", []):
obj = ifc_tools.get_object(prod)
if obj:
g.append(obj)
obj.Group = g
def add_layers(obj, element=None, ifcfile=None, proj=None):
"""Creates necessary layers for the given object"""
if not ifcfile:
ifcfile = ifc_tools.get_ifcfile(obj)
if not element:
element = ifc_tools.get_ifc_element(obj, ifcfile)
if not proj:
proj = ifc_tools.get_project(obj)
layers = ifcopenshell.util.element.get_layers(ifcfile, element)
for layer in layers:
lay = get_layer(layer, proj)
if lay and not obj in lay.Group:
lay.Proxy.addObject(lay, obj)
def add_to_layer(obj, layer):
"""Adds the given object to the given layer"""
if hasattr(obj, "StepId"):
obj_element = ifc_tools.get_ifc_element(obj)
elif hasattr(obj, "id"):
obj_element = obj
obj = ifc_tools.get_object(obj_element)
else:
return
if hasattr(layer, "StepId"):
layer_element = ifc_tools.get_ifc_element(layer)
elif hasattr(layer, "id"):
layer_element = layer
layer = ifc_tools.get_object(layer_element)
else:
return
ifcfile = ifc_tools.get_ifcfile(obj)
if not ifcfile:
return
items = ()
if layer_element.AssignedItems:
items = layer_element.AssignedItems
if not obj_element in items:
cmd = "attribute.edit_attributes"
attribs = {"AssignedItems": items + (obj_element,)}
ifc_tools.api_run(cmd, ifcfile, product=layer_element, attributes=attribs)
if not obj in layer.Group:
layer.Proxy.addObject(layer, obj)
def create_layer(name, project):
"""Adds a new layer to the given project"""
group = ifc_tools.get_group(project, "IfcLayersGroup")
ifcfile = ifc_tools.get_ifcfile(project)
layer = ifc_tools.api_run("layer.add_layer", ifcfile, Name=name)
return get_layer(layer, project)
def transfer_layer(layer, project):
"""Transfer a non-NativeIFC layer to a project"""
label = layer.Label
ifclayer = create_layer(label, project)
delete = not (ifc_tools.PARAMS.GetBool("KeepAggregated", False))
# delete the old one if empty and delete param allows
if delete and not layer.Group:
layer.Document.removeObject(layer.Name)
ifclayer.Label = label # to avoid 001-ing the Label...
return ifclayer