forked from tpaviot/pythonocc-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
233 lines (192 loc) · 6.8 KB
/
base.py
File metadata and controls
233 lines (192 loc) · 6.8 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
##Copyright 2008-2013 Jelle Feringa (jelleferinga@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/>
'''
Sketch for a high-level API for pythonOCC
Please note the following;
@readonly
means that the decorated method is a readonly descriptor
@property
means that the decorated method can be set / get using the descriptor
( irony is that just using @property *would*
result in a readonly descriptor :")
Sometimes a set of methods should be contained in another module or class,
or simply grouped.
For instance the set of methods after:
#===========================================================================
# Curve.local_properties
#===========================================================================
Can be a module, class or namespace.
'''
# occ
from OCC.BRepBuilderAPI import BRepBuilderAPI_Copy
from OCC.BRepGProp import brepgprop_VolumeProperties, brepgprop_LinearProperties, brepgprop_SurfaceProperties
from OCC.BRepCheck import *
# occ high level
from OCC.Display.SimpleGui import init_display
from Construct import *
# KBE
from types_lut import shape_lut, topo_lut, orient_lut, state_lut, curve_lut, surface_lut
# stdlib
import functools
#===========================================================================
# DISPLAY
#===========================================================================
global display
class singleton(object):
def __init__(self, cls):
self.cls = cls
self.instance_container = []
def __call__(self, *args, **kwargs):
if not len(self.instance_container):
cls = functools.partial(self.cls, *args, **kwargs)
self.instance_container.append(cls())
return self.instance_container[0]
@singleton
class Display(object):
def __init__(self):
self.display, self.start_display, self.add_menu, self.add_function_to_menu = init_display()
def __call__(self, *args, **kwargs):
return self.display.DisplayShape(*args, **kwargs)
#============
# base class
#============
class KbeObject(object):
"""base class for all KBE objects"""
def __init__(self, name=None):
"""Constructor for KbeObject"""
self.GlobalProperties = GlobalProperties(self)
self.name = name
self._dirty = False
self.tolerance = TOLERANCE
self.display_set = False
@property
def is_dirty(self):
'''when an object is dirty, its topology will be
rebuild when update is called'''
return self._dirty
@is_dirty.setter
def is_dirty(self, _bool):
self._dirty = bool(_bool)
@property
def topo_type(self):
return topo_lut[self.ShapeType()]
@property
def geom_type(self):
if self.topo_type == 'edge':
return curve_lut[self.ShapeType()]
if self.topo_type == 'face':
return surface_lut[self.adaptor.GetType()]
else:
raise ValueError('geom_type works only for edges and faces...')
def set_display(self, display):
if hasattr(display, 'DisplayShape'):
self.display_set = True
self.display = display
else:
raise ValueError('not a display')
def check(self):
"""
"""
_check = dict(vertex=BRepCheck_Vertex, edge=BRepCheck_Edge,
wire=BRepCheck_Wire, face=BRepCheck_Face,
shell=BRepCheck_Shell)
_check[self.topo_type]
# TODO: BRepCheck will be able to inform *what* actually is the matter,
# though implementing this still is a bit of work...
raise NotImplementedError
def is_valid(self):
analyse = BRepCheck_Analyzer(self)
ok = analyse.IsValid()
if ok:
return True
else:
return False
def copy(self):
"""
:return:
"""
cp = BRepBuilderAPI_Copy(self)
cp.Perform(self)
# get the class, construct a new instance
# cast the cp.Shape() to its specific TopoDS topology
_copy = self.__class__(shape_lut(cp.Shape()))
return _copy
def distance(self, other):
'''
return the minimum distance
:return: minimum distance,
minimum distance points on shp1
minimum distance points on shp2
'''
return minimum_distance(self, other)
def show(self, *args, **kwargs):
"""
renders the topological entity in the viewer
:param update: redraw the scene or not
"""
if not self.display_set:
Display()(self, *args, **kwargs)
else:
self.disp.DisplayShape(*args, **kwargs)
def build(self):
if self.name.startswith('Vertex'):
self = make_vertex(self)
def __eq__(self, other):
return self.IsEqual(other)
def __ne__(self, other):
return not(self.__eq__(other))
class GlobalProperties(object):
'''
global properties for all topologies
'''
def __init__(self, instance):
self.instance = instance
@property
def system(self):
self._system = GProp_GProps()
# todo, type should be abstracted with TopoDS...
_topo_type = self.instance.topo_type
if _topo_type == 'face' or _topo_type == 'shell':
brepgprop_SurfaceProperties(self.instance, self._system)
elif _topo_type == 'edge':
brepgprop_LinearProperties(self.instance, self._system)
elif _topo_type == 'solid':
brepgprop_VolumeProperties(self.instance, self._system)
return self._system
def centre(self):
"""
:return: centre of the entity
"""
return self.system.CentreOfMass()
def inertia(self):
'''returns the inertia matrix'''
return self.system.MatrixOfInertia(), self.system.MomentOfInertia()
def area(self):
'''returns the area of the surface'''
return self.system.Mass()
def bbox(self):
'''
returns the bounding box of the face
'''
return get_boundingbox(self.instance)
def oriented_bbox(self):
"""
return the minimal bounding box
has dependencies with scipy.spatial
[ has an implementation at hb-robo-code ]
"""
pass