4
4
File handling operations (reading/writing) must be implemented in derived classes.
5
5
"""
6
6
import os
7
-
8
- import OCC .TopoDS
9
7
import numpy as np
8
+ import OCC .TopoDS
10
9
from OCC .BRep import (BRep_Tool , BRep_Builder )
11
- from OCC .BRepBuilderAPI import (
12
- BRepBuilderAPI_MakeEdge , BRepBuilderAPI_MakeFace
13
- )
14
- from OCC .BRepBuilderAPI import (
15
- BRepBuilderAPI_NurbsConvert , BRepBuilderAPI_MakeWire
16
- )
10
+ from OCC .BRepBuilderAPI import (BRepBuilderAPI_MakeEdge , BRepBuilderAPI_MakeFace , \
11
+ BRepBuilderAPI_NurbsConvert , BRepBuilderAPI_MakeWire )
17
12
from OCC .Display .SimpleGui import init_display
18
13
from OCC .GeomConvert import geomconvert_SurfaceToBSplineSurface
19
14
from OCC .ShapeFix import ShapeFix_ShapeTolerance
24
19
from matplotlib import pyplot
25
20
from mpl_toolkits import mplot3d
26
21
from stl import mesh
27
-
28
22
import pygem .filehandler as fh
29
23
30
24
@@ -62,20 +56,16 @@ def _check_infile_instantiation(self):
62
56
63
57
"""
64
58
if not self .shape or not self .infile :
65
- raise RuntimeError (
66
- "You can not write a file without having parsed one."
67
- )
59
+ raise RuntimeError ("You can not write a file without having parsed one." )
68
60
69
61
def load_shape_from_file (self , filename ):
70
62
"""
71
63
Abstract method to load a specific file as a shape.
72
64
73
65
Not implemented, it has to be implemented in subclasses.
74
66
"""
75
- raise NotImplementedError (
76
- "Subclass must implement abstract method " + \
77
- self .__class__ .__name__ + ".load_shape_from_file"
78
- )
67
+ raise NotImplementedError ("Subclass must implement abstract method " + \
68
+ self .__class__ .__name__ + ".load_shape_from_file" )
79
69
80
70
def parse (self , filename ):
81
71
"""
@@ -114,30 +104,22 @@ def parse(self, filename):
114
104
# extract the Control Points of each face
115
105
n_poles_u = occ_face .NbUPoles ()
116
106
n_poles_v = occ_face .NbVPoles ()
117
- control_polygon_coordinates = np .zeros (
118
- shape = (n_poles_u * n_poles_v , 3 )
119
- )
107
+ control_polygon_coordinates = np .zeros (\
108
+ shape = (n_poles_u * n_poles_v , 3 ))
120
109
121
110
# cycle over the poles to get their coordinates
122
111
i = 0
123
112
for pole_u_direction in xrange (n_poles_u ):
124
113
for pole_v_direction in xrange (n_poles_v ):
125
- control_point_coordinates = occ_face .Pole (
126
- pole_u_direction + 1 , pole_v_direction + 1
127
- )
128
- control_polygon_coordinates [i , :] = [
129
- control_point_coordinates .X (),
130
- control_point_coordinates .Y (),
131
- control_point_coordinates .Z ()
132
- ]
114
+ control_point_coordinates = occ_face .Pole (\
115
+ pole_u_direction + 1 , pole_v_direction + 1 )
116
+ control_polygon_coordinates [i , :] = [control_point_coordinates .X (),\
117
+ control_point_coordinates .Y (),\
118
+ control_point_coordinates .Z ()]
133
119
i += 1
134
120
# pushing the control points coordinates to the mesh_points array (used for FFD)
135
- mesh_points = np .append (
136
- mesh_points , control_polygon_coordinates , axis = 0
137
- )
138
- control_point_position .append (
139
- control_point_position [- 1 ] + n_poles_u * n_poles_v
140
- )
121
+ mesh_points = np .append (mesh_points , control_polygon_coordinates , axis = 0 )
122
+ control_point_position .append (control_point_position [- 1 ] + n_poles_u * n_poles_v )
141
123
142
124
n_faces += 1
143
125
faces_explorer .Next ()
@@ -192,23 +174,17 @@ def write(self, mesh_points, filename, tolerance=None):
192
174
i = 0
193
175
for pole_u_direction in xrange (n_poles_u ):
194
176
for pole_v_direction in xrange (n_poles_v ):
195
- control_point_coordinates = mesh_points [
196
- i + control_point_position [n_faces ], :
197
- ]
177
+ control_point_coordinates = mesh_points [i + control_point_position [n_faces ], :]
198
178
point_xyz = gp_XYZ (* control_point_coordinates )
199
179
200
180
gp_point = gp_Pnt (point_xyz )
201
- occ_face .SetPole (
202
- pole_u_direction + 1 , pole_v_direction + 1 , gp_point
203
- )
181
+ occ_face .SetPole (pole_u_direction + 1 , pole_v_direction + 1 , gp_point )
204
182
i += 1
205
183
206
184
# construct the deformed wire for the trimmed surfaces
207
185
wire_maker = BRepBuilderAPI_MakeWire ()
208
186
tol = ShapeFix_ShapeTolerance ()
209
- brep = BRepBuilderAPI_MakeFace (
210
- occ_face .GetHandle (), self .tolerance
211
- ).Face ()
187
+ brep = BRepBuilderAPI_MakeFace (occ_face .GetHandle (), self .tolerance ).Face ()
212
188
brep_face = BRep_Tool .Surface (brep )
213
189
214
190
# cycle on the edges
@@ -218,9 +194,8 @@ def write(self, mesh_points, filename, tolerance=None):
218
194
# edge in the (u,v) coordinates
219
195
edge_uv_coordinates = BRep_Tool .CurveOnSurface (edge , face_aux )
220
196
# evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones
221
- edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge (
222
- edge_uv_coordinates [0 ], brep_face
223
- )
197
+ edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge (\
198
+ edge_uv_coordinates [0 ], brep_face )
224
199
edge_phis_coordinates = edge_phis_coordinates_aux .Edge ()
225
200
tol .SetTolerance (edge_phis_coordinates , self .tolerance )
226
201
wire_maker .Add (edge_phis_coordinates )
@@ -230,8 +205,7 @@ def write(self, mesh_points, filename, tolerance=None):
230
205
wire = wire_maker .Wire ()
231
206
232
207
# trimming the surfaces
233
- brep_surf = BRepBuilderAPI_MakeFace (
234
- occ_face .GetHandle (), wire ).Shape ()
208
+ brep_surf = BRepBuilderAPI_MakeFace (occ_face .GetHandle (), wire ).Shape ()
235
209
compound_builder .Add (compound , brep_surf )
236
210
n_faces += 1
237
211
faces_explorer .Next ()
@@ -243,10 +217,9 @@ def write_shape_to_file(self, shape, filename):
243
217
244
218
Not implemented, it has to be implemented in subclasses.
245
219
"""
246
- raise NotImplementedError (
220
+ raise NotImplementedError (\
247
221
"Subclass must implement abstract method " + \
248
- self .__class__ .__name__ + ".write_shape_to_file"
249
- )
222
+ self .__class__ .__name__ + ".write_shape_to_file" )
250
223
251
224
def plot (self , plot_file = None , save_fig = False ):
252
225
"""
@@ -277,35 +250,28 @@ def plot(self, plot_file=None, save_fig=False):
277
250
# Load the STL files and add the vectors to the plot
278
251
stl_mesh = mesh .Mesh .from_file ('aux_figure.stl' )
279
252
os .remove ('aux_figure.stl' )
280
- axes .add_collection3d (
281
- mplot3d .art3d .Poly3DCollection (stl_mesh .vectors / 1000 )
282
- )
253
+ axes .add_collection3d (mplot3d .art3d .Poly3DCollection (stl_mesh .vectors / 1000 ))
283
254
284
255
# Get the limits of the axis and center the geometry
285
- max_dim = np .array ([
286
- np .max (stl_mesh .vectors [:, :, 0 ]) / 1000 ,
287
- np .max (stl_mesh .vectors [:, :, 1 ]) / 1000 ,
288
- np .max (stl_mesh .vectors [:, :, 2 ]) / 1000
289
- ])
290
- min_dim = np .array ([
291
- np .min (stl_mesh .vectors [:, :, 0 ]) / 1000 ,
292
- np .min (stl_mesh .vectors [:, :, 1 ]) / 1000 ,
293
- np .min (stl_mesh .vectors [:, :, 2 ]) / 1000
294
- ])
256
+ max_dim = np .array ([\
257
+ np .max (stl_mesh .vectors [:, :, 0 ]) / 1000 ,\
258
+ np .max (stl_mesh .vectors [:, :, 1 ]) / 1000 ,\
259
+ np .max (stl_mesh .vectors [:, :, 2 ]) / 1000 ])
260
+ min_dim = np .array ([\
261
+ np .min (stl_mesh .vectors [:, :, 0 ]) / 1000 ,\
262
+ np .min (stl_mesh .vectors [:, :, 1 ]) / 1000 ,\
263
+ np .min (stl_mesh .vectors [:, :, 2 ]) / 1000 ])
295
264
296
265
max_lenght = np .max (max_dim - min_dim )
297
- axes .set_xlim (
298
- - .6 * max_lenght + (max_dim [0 ] + min_dim [0 ]) / 2 ,
299
- .6 * max_lenght + (max_dim [0 ] + min_dim [0 ]) / 2
300
- )
301
- axes .set_ylim (
302
- - .6 * max_lenght + (max_dim [1 ] + min_dim [1 ]) / 2 ,
303
- .6 * max_lenght + (max_dim [1 ] + min_dim [1 ]) / 2
304
- )
305
- axes .set_zlim (
306
- - .6 * max_lenght + (max_dim [2 ] + min_dim [2 ]) / 2 ,
307
- .6 * max_lenght + (max_dim [2 ] + min_dim [2 ]) / 2
308
- )
266
+ axes .set_xlim (\
267
+ - .6 * max_lenght + (max_dim [0 ] + min_dim [0 ]) / 2 ,\
268
+ .6 * max_lenght + (max_dim [0 ] + min_dim [0 ]) / 2 )
269
+ axes .set_ylim (\
270
+ - .6 * max_lenght + (max_dim [1 ] + min_dim [1 ]) / 2 ,\
271
+ .6 * max_lenght + (max_dim [1 ] + min_dim [1 ]) / 2 )
272
+ axes .set_zlim (\
273
+ - .6 * max_lenght + (max_dim [2 ] + min_dim [2 ]) / 2 ,\
274
+ .6 * max_lenght + (max_dim [2 ] + min_dim [2 ]) / 2 )
309
275
310
276
# Show the plot to the screen
311
277
if not save_fig :
0 commit comments