11"""
22Utilities for reading and writing different CAD files.
33"""
4- import numpy as np
54import os
6- import pygem .filehandler as fh
5+ import numpy as np
6+ from mpl_toolkits import mplot3d
7+ from matplotlib import pyplot
78from OCC .IGESControl import (IGESControl_Reader , IGESControl_Writer )
89from OCC .BRep import (BRep_Tool , BRep_Builder )
9- from OCC .BRepBuilderAPI import (BRepBuilderAPI_NurbsConvert , BRepBuilderAPI_MakeWire , BRepBuilderAPI_MakeEdge , BRepBuilderAPI_MakeFace )
10+ from OCC .BRepBuilderAPI import (BRepBuilderAPI_NurbsConvert , BRepBuilderAPI_MakeWire )
11+ from OCC .BRepBuilderAPI import (BRepBuilderAPI_MakeEdge , BRepBuilderAPI_MakeFace )
1012from OCC .GeomConvert import geomconvert_SurfaceToBSplineSurface
1113import OCC .TopoDS
1214from OCC .TopAbs import (TopAbs_FACE , TopAbs_EDGE )
1517from OCC .Display .SimpleGui import init_display
1618from OCC .ShapeFix import ShapeFix_ShapeTolerance
1719from OCC .StlAPI import StlAPI_Writer
18- from mpl_toolkits import mplot3d
19- from matplotlib import pyplot
2020from stl import mesh
21+ import pygem .filehandler as fh
2122
2223
2324class IgesHandler (fh .FileHandler ):
@@ -27,7 +28,8 @@ class IgesHandler(fh.FileHandler):
2728 :cvar string infile: name of the input file to be processed.
2829 :cvar string outfile: name of the output file where to write in.
2930 :cvar string extension: extension of the input/output files. It is equal to '.iges'.
30- :cvar list control_point_position: index of the first NURBS control point (or pole) of each face of the iges file.
31+ :cvar list control_point_position: index of the first NURBS control point (or pole)
32+ of each face of the iges file.
3133 """
3234 def __init__ (self ):
3335 super (IgesHandler , self ).__init__ ()
@@ -51,22 +53,21 @@ def parse(self, filename):
5153 self ._check_extension (filename )
5254
5355 self .infile = filename
54-
55- ## read in the IGES file
56+
57+ # read in the IGES file
5658 reader = IGESControl_Reader ()
5759 reader .ReadFile (self .infile )
5860 reader .TransferRoots ()
5961 shape = reader .Shape ()
6062
61- ## cycle on the faces to get the control points
63+ # cycle on the faces to get the control points
6264 # init some quantities
6365 n_faces = 0
6466 control_point_position = [0 ]
6567 faces_explorer = TopExp_Explorer (shape , TopAbs_FACE )
66- mesh_points = np .zeros (shape = (0 ,3 ))
67-
68- while faces_explorer .More ():
68+ mesh_points = np .zeros (shape = (0 , 3 ))
6969
70+ while faces_explorer .More ():
7071 # performing some conversions to get the right format (BSplineSurface)
7172 iges_face = OCC .TopoDS .topods_Face (faces_explorer .Current ())
7273 iges_nurbs_converter = BRepBuilderAPI_NurbsConvert (iges_face )
@@ -81,27 +82,28 @@ def parse(self, filename):
8182 # extract the Control Points of each face
8283 n_poles_u = occ_face .NbUPoles ()
8384 n_poles_v = occ_face .NbVPoles ()
84- control_polygon_coordinates = np .zeros (shape = (n_poles_u * n_poles_v ,3 ))
85+ control_polygon_coordinates = np .zeros (shape = (n_poles_u * n_poles_v , 3 ))
8586
8687 # cycle over the poles to get their coordinates
8788 i = 0
8889 for pole_u_direction in xrange (n_poles_u ):
8990 for pole_v_direction in xrange (n_poles_v ):
90- control_point_coordinates = occ_face .Pole (pole_u_direction + 1 ,pole_v_direction + 1 )
91- control_polygon_coordinates [i ,:] = [control_point_coordinates .X (), control_point_coordinates .Y (), control_point_coordinates .Z ()]
92- i += 1
91+ control_point_coordinates = occ_face .Pole (pole_u_direction + 1 , pole_v_direction + 1 )
92+ control_polygon_coordinates [i , :] = [control_point_coordinates .X (), \
93+ control_point_coordinates .Y (), control_point_coordinates .Z ()]
94+ i += 1
9395
9496 # pushing the control points coordinates to the mesh_points array (used for FFD)
9597 mesh_points = np .append (mesh_points , control_polygon_coordinates , axis = 0 )
96- control_point_position .append (control_point_position [- 1 ] + n_poles_u * n_poles_v )
98+ control_point_position .append (control_point_position [- 1 ] + n_poles_u * n_poles_v )
9799
98100 n_faces += 1
99101 faces_explorer .Next ()
100102
101103 self ._control_point_position = control_point_position
102104
103105 return mesh_points
104-
106+
105107
106108 def write (self , mesh_points , filename ):
107109 """
@@ -117,18 +119,18 @@ def write(self, mesh_points, filename):
117119 self ._check_extension (filename )
118120 self ._check_infile_instantiation (self .infile )
119121
120- self .outfile = filename
122+ self .outfile = filename
121123
122- ## init the ouput file writer
124+ # init the ouput file writer
123125 writer = IGESControl_Writer ()
124126
125- ## read in the IGES file
127+ # read in the IGES file
126128 reader = IGESControl_Reader ()
127129 reader .ReadFile (self .infile )
128130 reader .TransferRoots ()
129131 shape_read = reader .Shape ()
130132
131- ## cycle on the faces to update the control points position
133+ # cycle on the faces to update the control points position
132134 # init some quantities
133135 faces_explorer = TopExp_Explorer (shape_read , TopAbs_FACE )
134136 n_faces = 0
@@ -137,9 +139,8 @@ def write(self, mesh_points, filename):
137139 compound_builder = BRep_Builder ()
138140 compound = OCC .TopoDS .TopoDS_Compound ()
139141 compound_builder .MakeCompound (compound )
140-
142+
141143 while faces_explorer .More ():
142-
143144 # similar to the parser method
144145 iges_face = OCC .TopoDS .topods_Face (faces_explorer .Current ())
145146 iges_nurbs_converter = BRepBuilderAPI_NurbsConvert (iges_face )
@@ -156,18 +157,19 @@ def write(self, mesh_points, filename):
156157 i = 0
157158 for pole_u_direction in xrange (n_poles_u ):
158159 for pole_v_direction in xrange (n_poles_v ):
159- control_point_coordinates = mesh_points [i + control_point_position [n_faces ],:]
160- point_xyz = gp_XYZ (control_point_coordinates [0 ], control_point_coordinates [1 ], control_point_coordinates [2 ])
160+ control_point_coordinates = mesh_points [i + control_point_position [n_faces ], :]
161+ point_xyz = gp_XYZ (control_point_coordinates [0 ], control_point_coordinates [1 ], \
162+ control_point_coordinates [2 ])
161163 gp_point = gp_Pnt (point_xyz )
162- occ_face .SetPole (pole_u_direction + 1 ,pole_v_direction + 1 ,gp_point )
164+ occ_face .SetPole (pole_u_direction + 1 , pole_v_direction + 1 , gp_point )
163165 i += 1
164166
165- ## construct the deformed wire for the trimmed surfaces
167+ # construct the deformed wire for the trimmed surfaces
166168 wire_maker = BRepBuilderAPI_MakeWire ()
167169 tol = ShapeFix_ShapeTolerance ()
168170 brep = BRepBuilderAPI_MakeFace (occ_face .GetHandle (), 1e-4 ).Face ()
169171 brep_face = BRep_Tool .Surface (brep )
170-
172+
171173 # cycle on the edges
172174 edge_explorer = TopExp_Explorer (nurbs_face , TopAbs_EDGE )
173175 while edge_explorer .More ():
@@ -181,10 +183,10 @@ def write(self, mesh_points, filename):
181183 wire_maker .Add (edge_phis_coordinates )
182184 edge_explorer .Next ()
183185
184- #grouping the edges in a wire
186+ # grouping the edges in a wire
185187 wire = wire_maker .Wire ()
186188
187- ## trimming the surfaces
189+ # trimming the surfaces
188190 brep_surf = BRepBuilderAPI_MakeFace (occ_face .GetHandle (), wire ).Shape ()
189191 compound_builder .Add (compound , brep_surf )
190192 n_faces += 1
@@ -193,8 +195,8 @@ def write(self, mesh_points, filename):
193195 writer .AddShape (compound )
194196
195197 writer .Write (self .outfile )
196-
197-
198+
199+
198200 def plot (self , plot_file = None , save_fig = False ):
199201 """
200202 Method to plot an iges file. If `plot_file` is not given it plots `self.infile`.
@@ -212,55 +214,54 @@ def plot(self, plot_file=None, save_fig=False):
212214 reader = IGESControl_Reader ()
213215 reader .ReadFile (plot_file )
214216 reader .TransferRoots ()
215- shape = reader .Shape ()
216-
217+ shape = reader .Shape ()
218+
217219 stl_writer = StlAPI_Writer ()
218220 # Do not switch SetASCIIMode() from False to True.
219221 stl_writer .SetASCIIMode (False )
220- stl_writer .Write (shape ,'aux_figure.stl' )
221-
222+ stl_writer .Write (shape , 'aux_figure.stl' )
223+
222224 # Create a new plot
223225 figure = pyplot .figure ()
224226 axes = mplot3d .Axes3D (figure )
225227
226228 # Load the STL files and add the vectors to the plot
227229 stl_mesh = mesh .Mesh .from_file ('aux_figure.stl' )
230+ os .remove ('aux_figure.stl' )
228231 axes .add_collection3d (mplot3d .art3d .Poly3DCollection (stl_mesh .vectors ))
229232
230233 # Auto scale to the mesh size
231234 scale = stl_mesh .points .flatten (- 1 )
232235 axes .auto_scale_xyz (scale , scale , scale )
233-
236+
234237 # Show the plot to the screen
235238 if not save_fig :
236239 pyplot .show ()
237240 else :
238241 figure .savefig (plot_file .split ('.' )[0 ] + '.png' )
239-
240- os .remove ('aux_figure.stl' )
241-
242-
242+
243+
243244 def show (self , show_file = None ):
244245 """
245246 Method to show an iges file. If `show_file` is not given it plots `self.infile`.
246247
247- :param string show_file: the iges filename you want to plot .
248+ :param string show_file: the iges filename you want to show .
248249 """
249250 if show_file is None :
250251 show_file = self .infile
251252 else :
252253 self ._check_filename_type (show_file )
253254
254- ## read in the IGES file
255+ # read in the IGES file
255256 reader = IGESControl_Reader ()
256257 reader .ReadFile (show_file )
257258 reader .TransferRoots ()
258- shape = reader .Shape ()
259-
260- display , start_display , add_menu , add_function_to_menu = init_display ()
259+ shape = reader .Shape ()
260+
261+ display , start_display , __ , __ = init_display ()
261262 display .FitAll ()
262263 display .DisplayShape (shape , update = True )
263-
264+
264265 # Show the plot to the screen
265266 start_display ()
266267
0 commit comments