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
185 lines (159 loc) · 6.72 KB
/
LayerManager.py
File metadata and controls
185 lines (159 loc) · 6.72 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
##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 typing import Dict, List, Tuple, Optional
from OCC.Core.AIS import AIS_InteractiveContext, AIS_Shape
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform
from OCC.Core.gp import gp_Trsf
from OCC.Core.Graphic3d import Graphic3d_NameOfMaterial
from OCC.Core.TopoDS import TopoDS_Shape
class Layer:
"""
Manages a collection of shapes as a layer in a 3D viewer.
A layer holds a set of TopoDS_Shape objects and controls their visual
properties like color, transparency, and material. It provides methods
to add, remove, and manipulate these shapes.
"""
def __init__(
self,
from_display: AIS_InteractiveContext,
shape: Optional[TopoDS_Shape] = None,
color: int = 0,
transparency: float = 0.0,
material: Graphic3d_NameOfMaterial = Graphic3d_NameOfMaterial.Graphic3d_NOM_DEFAULT,
) -> None:
"""
Initializes a new Layer.
:param from_display: The display object from the main application.
:param shape: A shape to add to the layer upon creation. Defaults to None.
:param color: The color of the shapes in the layer. Defaults to 0 (black).
:param transparency: The transparency of the shapes, from 0.0 (opaque) to 1.0 (fully transparent). Defaults to 0.0.
:param material: The material of the shapes. Defaults to Graphic3d_NOM_DEFAULT.
"""
self.element_to_display: Dict[int, Tuple[TopoDS_Shape, AIS_Shape]] = {}
self.count: int = 0
self.color: int = color
self.display: AIS_InteractiveContext = from_display
self.transparency: float = transparency
self.material: Graphic3d_NameOfMaterial = material
if shape is not None:
self.add_shape(shape)
def add_shape(self, shape: TopoDS_Shape) -> None:
"""
Adds a shape to the layer.
:param shape: The TopoDS_Shape to add.
"""
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: TopoDS_Shape, index: int) -> None:
"""
Replaces a shape in the layer at a specific index.
:param shape: The new TopoDS_Shape.
:param index: The index of the shape to replace.
"""
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: TopoDS_Shape, index: int, transformations: gp_Trsf
) -> None:
"""
Applies a transformation to a shape and updates it in the layer.
:param shape: The TopoDS_Shape to transform.
:param index: The index of the shape to update.
:param transformations: The gp_Trsf transformation to apply.
"""
shape_moved = BRepBuilderAPI_Transform(shape, transformations, True).Shape()
self.replace_shape(shape_moved, index)
def merge(self, layer: "Layer", clear: bool = False) -> None:
"""
Merges another layer into this one.
:param layer: The Layer to merge from.
:param clear: If True, the source layer is cleared after merging. Defaults to False.
"""
for shape in layer.get_shapes():
self.add_shape(shape)
if clear is True:
layer.clear()
def delete_shape_with_index(self, index: int) -> None:
"""
Deletes a shape from the layer by its index.
:param index: The index of the shape to delete.
"""
self.element_to_display.pop(index)
def delete_shape(self, shape_to_del: TopoDS_Shape) -> None:
"""
Deletes a shape from the layer.
:param shape_to_del: The TopoDS_Shape to delete.
"""
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) -> None:
"""
Removes all shapes from the layer.
"""
self.element_to_display = {}
self.count = 0
def get_shapes(self) -> List[TopoDS_Shape]:
"""
Gets all the shapes in the layer.
:return: A list of TopoDS_Shape objects.
"""
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: TopoDS_Shape
) -> Optional[Tuple[AIS_Shape, int]]:
"""
Gets the displayed AIS_Shape corresponding to a TopoDS_Shape.
:param topshape: The TopoDS_Shape to find the AIS_Shape for.
:return: A tuple containing the AIS_Shape and its index, or None if not found.
"""
for index, element in self.element_to_display.items():
shape, ais_shape = element
if shape == topshape:
return ais_shape, index
return None
def hide(self) -> None:
"""
Hides the layer from the 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) -> None:
"""
Shows the layer in the display.
"""
for index, element in self.element_to_display.items():
shape, ais = element
self.display.Context.Display(ais, True)