forked from tpaviot/pythonocc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerManager.py
More file actions
196 lines (174 loc) · 5.44 KB
/
LayerManager.py
File metadata and controls
196 lines (174 loc) · 5.44 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
##Copyright 2021 Tanneguy de Villemagne (tanneguydv@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC 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.
##
##pythonOCC 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 pythonOCC. If not, see <http://www.gnu.org/licenses/>.
from OCC.Core.Graphic3d import Graphic3d_NOM_DEFAULT
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform
class Layer:
def __init__(
self,
from_display,
shape=None,
color=0,
transparency=0.0,
material=Graphic3d_NOM_DEFAULT,
):
r"""
Parameters
----------
from_display: the display from the main code
shape: TopoDS_Shape
color: Quantity color
transparency: from 0.0 to 1.0
Returns
-------
None
"""
self.clear()
self.color = color
self.display = from_display
self.transparency = transparency
self.material = material
if shape is not None:
self.add_shape(shape)
def add_shape(self, shape):
r"""
Parameters
----------
shape: TopoDS_Shape
Returns
-------
None
"""
to_display = self.display.DisplayShape(
shape, color=self.color, material=self.material
)[0]
self.display.Context.SetTransparency(to_display, self.transparency, True)
self.element_to_display[self.count] = (shape, to_display)
self.count += 1
self.display.Context.Erase(to_display, False)
def replace_shape(self, shape, index):
r"""
Parameters
----------
shape: TopoDS_Shape
index: The index of the shape to replace
Returns
-------
None
"""
self.display.Context.Erase(self.element_to_display[index][1], False)
self.element_to_display.pop(index)
to_display = self.display.DisplayShape(
shape, color=self.color, material=self.material
)[0]
self.display.Context.SetTransparency(to_display, self.transparency, True)
self.element_to_display[index] = (shape, to_display)
# self.display.Context.Erase(to_display, False)
def update_trsf_shape(self, shape, index, transformations):
r"""
Parameters
----------
shape: TopoDS_Shape
index: The index of the shape to update and replace
transformations: gp_Trsf
Returns
-------
None
"""
shape_moved = BRepBuilderAPI_Transform(shape, transformations, True).Shape()
self.replace_shape(shape_moved, index)
def merge(self, layer, clear=False):
r"""
Parameters
----------
layer: name of the layer to merge to the main one
clear: bool to clear the layer
Returns
-------
None
"""
for shape in layer.get_shapes():
self.add_shape(shape)
if clear is True:
layer.clear()
def delete_shape_with_index(self, index):
r"""
Parameters
----------
index: index of the shape to delete from layer
Returns
-------
None
"""
self.element_to_display.pop(index)
def delete_shape(self, shape_to_del):
r"""
Parameters
----------
shape: the TopoDS_Shape to delete from layer
Returns
-------
None
"""
for index, element in self.element_to_display.items():
shape, ais_shape = element
if shape_to_del == shape:
self.element_to_display.pop(index)
def clear(self):
r"""
Clear the layer from its shapes
"""
self.element_to_display = {}
self.count = 0
def get_shapes(self):
r"""
Returns
-------
List of TopoDS_Shape
"""
topods_shapes = []
for index, element in self.element_to_display.items():
shape, ais_shape = element
topods_shapes.append(shape)
return topods_shapes
def get_aisshape_from_topodsshape(self, topshape):
r"""
Parameters
----------
topshape: the TopoDS_Shape linked to the AIS_Shape to retrieve
Returns
-------
AIS_Shape, index of shape
"""
for index, element in self.element_to_display.items():
shape, ais_shape = element
if shape == topshape:
return ais_shape, index
def hide(self):
r"""
hide the layer from display
"""
for index, element in self.element_to_display.items():
shape, ais_shape = element
self.display.Context.Erase(ais_shape, False)
self.display.View.Redraw()
def show(self):
r"""
Show the layer to display
"""
for index, element in self.element_to_display.items():
shape, ais = element
self.display.Context.Display(ais, True)