forked from tpaviot/pythonocc-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoccutils_test.py
More file actions
228 lines (189 loc) · 7.92 KB
/
occutils_test.py
File metadata and controls
228 lines (189 loc) · 7.92 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
#!/usr/bin/env python
##Copyright 2009-2015 Thomas Paviot (tpaviot@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/>.
import unittest
import sys
sys.path.append('../')
sys.path.append('../OCCUtils')
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere
from OCC.Core.TopoDS import TopoDS_Face, TopoDS_Edge
from Topology import Topo, WireExplorer
from edge import Edge
from face import Face
from wire import Wire
from vertex import Vertex
from shell import Shell
from solid import Solid
def get_test_box_shape():
return BRepPrimAPI_MakeBox(10, 20, 30).Shape()
def get_test_sphere_shape():
return BRepPrimAPI_MakeSphere(10.).Shape()
class TestTopo(unittest.TestCase):
def setUp(self):
self.topo = Topo(get_test_box_shape())
assert self.topo
def test_loop_faces(self):
i = 0
for face in self.topo.faces():
i += 1
assert(isinstance(face, TopoDS_Face))
assert(i == 6)
def test_loop_edges(self):
i = 0
for face in self.topo.edges():
i += 1
assert(isinstance(face, TopoDS_Edge))
assert(i == 12)
def number_of_topological_entities(self):
assert(self.topo.number_of_faces() == 6)
assert(self.topo.number_of_edges() == 12)
assert(self.topo.number_of_vertices() == 8)
assert(self.topo.number_of_wires() == 6)
assert(self.topo.number_of_solids() == 1)
assert(self.topo.number_of_shells() == 1)
assert(self.topo.number_of_compounds() == 0)
assert(self.topo.number_of_comp_solids() == 0)
def test_nested_iteration(self):
'''check nested looping'''
for f in self.topo.faces():
for e in self.topo.edges():
assert isinstance(f, TopoDS_Face)
assert isinstance(e, TopoDS_Edge)
def test_kept_reference(self):
'''did we keep a reference after looping several time through a list
of topological entities?'''
_tmp = []
_faces = [i for i in self.topo.faces()]
for f in _faces:
_tmp.append(0 == f.IsNull())
for f in _faces:
_tmp.append(0 == f.IsNull())
self.assertTrue(all(_tmp))
def test_edge_face(self):
edg = next(self.topo.edges())
face = next(self.topo.faces())
faces_from_edge = [i for i in self.topo.faces_from_edge(edg)]
self.assertTrue(len(faces_from_edge) == self.topo.number_of_faces_from_edge(edg))
edges_from_face = [i for i in self.topo.edges_from_face(face)]
self.assertTrue(len(edges_from_face) == self.topo.number_of_edges_from_face(face))
def test_edge_wire(self):
edg = next(self.topo.edges())
wire = next(self.topo.wires())
wires_from_edge = [i for i in self.topo.wires_from_edge(edg)]
self.assertTrue(len(wires_from_edge) == self.topo.number_of_wires_from_edge(edg))
edges_from_wire = [i for i in self.topo.edges_from_wire(wire)]
self.assertTrue(len(edges_from_wire) == self.topo.number_of_edges_from_wire(wire))
def test_vertex_edge(self):
vert = next(self.topo.vertices())
edge = next(self.topo.edges())
verts_from_edge = [i for i in self.topo.vertices_from_edge(edge)]
self.assertTrue(len(verts_from_edge) == self.topo.number_of_vertices_from_edge(edge))
edges_from_vert = [i for i in self.topo.edges_from_vertex(vert)]
self.assertTrue(len(edges_from_vert) == self.topo.number_of_edges_from_vertex(vert))
def test_vertex_face(self):
vert = next(self.topo.vertices())
face = next(self.topo.faces())
faces_from_vertex = [i for i in self.topo.faces_from_vertex(vert)]
self.assertTrue(len(faces_from_vertex) == self.topo.number_of_faces_from_vertex(vert))
verts_from_face = [i for i in self.topo.vertices_from_face(face)]
self.assertTrue(len(verts_from_face) == self.topo.number_of_vertices_from_face(face))
def test_face_solid(self):
face = next(self.topo.faces())
solid = next(self.topo.solids())
faces_from_solid = [i for i in self.topo.faces_from_solids(solid)]
self.assertTrue(len(faces_from_solid) == self.topo.number_of_faces_from_solids(solid))
solids_from_face = [i for i in self.topo.solids_from_face(face)]
self.assertTrue(len(solids_from_face) == self.topo.number_of_solids_from_face(face))
def test_wire_face(self):
wire = next(self.topo.wires())
face = next(self.topo.faces())
faces_from_wire = [i for i in self.topo.faces_from_wire(wire)]
self.assertTrue(len(faces_from_wire) == self.topo.number_of_faces_from_wires(wire))
wires_from_face = [i for i in self.topo.wires_from_face(face)]
self.assertTrue(len(wires_from_face) == self.topo.number_of_wires_from_face(face))
def test_edges_out_of_scope(self):
# check pointers going out of scope
face = next(self.topo.faces())
_edges = []
for edg in Topo(face).edges():
_edges.append(edg)
for edg in _edges:
assert not edg.IsNull()
def test_wires_out_of_scope(self):
# check pointers going out of scope
wire = next(self.topo.wires())
_edges, _vertices = [], []
for edg in WireExplorer(wire).ordered_edges():
_edges.append(edg)
for edg in _edges:
assert not edg.IsNull()
for vert in WireExplorer(wire).ordered_vertices():
_vertices.append(vert)
for v in _vertices:
assert not v.IsNull()
class TestEdge(unittest.TestCase):
def test_creat_edge(self):
# create a box
b = get_test_box_shape()
# take the first edge
t = Topo(b)
edge_0 = next(t.edges()) # it's a TopoDS_Edge
assert not edge_0.IsNull()
# then create an edge
my_edge = Edge(edge_0)
assert not my_edge.IsNull()
assert my_edge.tolerance == 1e-06
assert my_edge.type == 'line'
assert my_edge.length() == 30.
class TestFace(unittest.TestCase):
def test_creat_face(self):
# create a box
my_face = Face(BRepPrimAPI_MakeSphere(1., 1.).Face())
assert not my_face.IsNull()
assert my_face.tolerance == 1e-06
assert not my_face.is_planar()
assert my_face.is_trimmed()
class TestWire(unittest.TestCase):
def test_creat_face(self):
# create a box
b = get_test_box_shape()
# take the first edge
t = Topo(b)
wire = next(t.wires())
my_wire = Wire(wire)
assert not my_wire.IsNull()
assert my_wire.tolerance == 1e-06
class TestVertex(unittest.TestCase):
def test_creat_vertex(self):
my_vertex = Vertex(1., 2., -2.6)
assert my_vertex.tolerance == 1e-06
assert my_vertex.x == 1.
assert my_vertex.y == 2.
assert my_vertex.z == -2.6
class TestShell(unittest.TestCase):
def test_creat_shell(self):
my_shell = Shell(BRepPrimAPI_MakeBox(10, 20, 30).Shell())
assert my_shell.tolerance == 1e-06
class TestSolid(unittest.TestCase):
def test_creat_solid(self):
my_solid = Solid(BRepPrimAPI_MakeBox(10, 20, 30).Solid())
assert my_solid.tolerance == 1e-06
def suite():
test_suite = unittest.TestSuite()
return test_suite
if __name__ == "__main__":
unittest.main()