44File handling operations (reading/writing) must be implemented in derived classes.
55"""
66import os
7-
8- import OCC .TopoDS
97import numpy as np
8+ import OCC .TopoDS
109from 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 )
1712from OCC .Display .SimpleGui import init_display
1813from OCC .GeomConvert import geomconvert_SurfaceToBSplineSurface
1914from OCC .ShapeFix import ShapeFix_ShapeTolerance
2419from matplotlib import pyplot
2520from mpl_toolkits import mplot3d
2621from stl import mesh
27-
2822import pygem .filehandler as fh
2923
3024
@@ -62,20 +56,16 @@ def _check_infile_instantiation(self):
6256
6357 """
6458 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." )
6860
6961 def load_shape_from_file (self , filename ):
7062 """
7163 Abstract method to load a specific file as a shape.
7264
7365 Not implemented, it has to be implemented in subclasses.
7466 """
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" )
7969
8070 def parse (self , filename ):
8171 """
@@ -114,30 +104,22 @@ def parse(self, filename):
114104 # extract the Control Points of each face
115105 n_poles_u = occ_face .NbUPoles ()
116106 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 ))
120109
121110 # cycle over the poles to get their coordinates
122111 i = 0
123112 for pole_u_direction in xrange (n_poles_u ):
124113 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 ()]
133119 i += 1
134120 # 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 )
141123
142124 n_faces += 1
143125 faces_explorer .Next ()
@@ -192,23 +174,17 @@ def write(self, mesh_points, filename, tolerance=None):
192174 i = 0
193175 for pole_u_direction in xrange (n_poles_u ):
194176 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 ], :]
198178 point_xyz = gp_XYZ (* control_point_coordinates )
199179
200180 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 )
204182 i += 1
205183
206184 # construct the deformed wire for the trimmed surfaces
207185 wire_maker = BRepBuilderAPI_MakeWire ()
208186 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 ()
212188 brep_face = BRep_Tool .Surface (brep )
213189
214190 # cycle on the edges
@@ -218,9 +194,8 @@ def write(self, mesh_points, filename, tolerance=None):
218194 # edge in the (u,v) coordinates
219195 edge_uv_coordinates = BRep_Tool .CurveOnSurface (edge , face_aux )
220196 # 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 )
224199 edge_phis_coordinates = edge_phis_coordinates_aux .Edge ()
225200 tol .SetTolerance (edge_phis_coordinates , self .tolerance )
226201 wire_maker .Add (edge_phis_coordinates )
@@ -230,8 +205,7 @@ def write(self, mesh_points, filename, tolerance=None):
230205 wire = wire_maker .Wire ()
231206
232207 # trimming the surfaces
233- brep_surf = BRepBuilderAPI_MakeFace (
234- occ_face .GetHandle (), wire ).Shape ()
208+ brep_surf = BRepBuilderAPI_MakeFace (occ_face .GetHandle (), wire ).Shape ()
235209 compound_builder .Add (compound , brep_surf )
236210 n_faces += 1
237211 faces_explorer .Next ()
@@ -243,10 +217,9 @@ def write_shape_to_file(self, shape, filename):
243217
244218 Not implemented, it has to be implemented in subclasses.
245219 """
246- raise NotImplementedError (
220+ raise NotImplementedError (\
247221 "Subclass must implement abstract method " + \
248- self .__class__ .__name__ + ".write_shape_to_file"
249- )
222+ self .__class__ .__name__ + ".write_shape_to_file" )
250223
251224 def plot (self , plot_file = None , save_fig = False ):
252225 """
@@ -277,35 +250,28 @@ def plot(self, plot_file=None, save_fig=False):
277250 # Load the STL files and add the vectors to the plot
278251 stl_mesh = mesh .Mesh .from_file ('aux_figure.stl' )
279252 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 ))
283254
284255 # 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 ])
295264
296265 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 )
309275
310276 # Show the plot to the screen
311277 if not save_fig :
0 commit comments