22Utilities for reading and writing different CAD files.
33"""
44import numpy as np
5- import sys
5+ import os
66import pygem .filehandler as fh
77from OCC .IGESControl import (IGESControl_Reader , IGESControl_Writer )
88from OCC .BRep import (BRep_Tool , BRep_Builder )
1414from OCC .gp import (gp_Pnt , gp_XYZ )
1515from OCC .Display .SimpleGui import init_display
1616from OCC .ShapeFix import ShapeFix_ShapeTolerance
17+ from OCC .StlAPI import StlAPI_Writer
18+ from mpl_toolkits import mplot3d
19+ from matplotlib import pyplot
20+ from stl import mesh
1721
1822
1923class IgesHandler (fh .FileHandler ):
@@ -198,9 +202,6 @@ def plot(self, plot_file=None, save_fig=False):
198202 :param string plot_file: the iges filename you want to plot.
199203 :param bool save_fig: a flag to save the figure in png or not. If True the
200204 plot is not shown.
201-
202- .. warning::
203- It does not work well up to now
204205 """
205206 if plot_file is None :
206207 plot_file = self .infile
@@ -213,40 +214,53 @@ def plot(self, plot_file=None, save_fig=False):
213214 reader .TransferRoots ()
214215 shape = reader .Shape ()
215216
216- display , start_display , add_menu , add_function_to_menu = init_display ()
217- display .FitAll ()
218- display .DisplayShape (shape , update = True )
217+ stl_writer = StlAPI_Writer ()
218+ # Do not switch SetASCIIMode() from False to True.
219+ stl_writer .SetASCIIMode (False )
220+ stl_writer .Write (shape ,'aux_figure.stl' )
219221
220- def export_to_BMP (event = None ):
221- display .View .Dump ('./capture_bmp.bmp' )
222-
223-
224- def export_to_PNG (event = None ):
225- display .View .Dump ('./capture_png.png' )
226-
222+ # Create a new plot
223+ figure = pyplot .figure ()
224+ axes = mplot3d .Axes3D (figure )
227225
228- def export_to_JPEG (event = None ):
229- display .View .Dump ('./capture_jpeg.jpeg' )
226+ # Load the STL files and add the vectors to the plot
227+ stl_mesh = mesh .Mesh .from_file ('aux_figure.stl' )
228+ axes .add_collection3d (mplot3d .art3d .Poly3DCollection (stl_mesh .vectors ))
230229
230+ # Auto scale to the mesh size
231+ scale = stl_mesh .points .flatten (- 1 )
232+ axes .auto_scale_xyz (scale , scale , scale )
233+
234+ # Show the plot to the screen
235+ if not save_fig :
236+ pyplot .show ()
237+ else :
238+ figure .savefig (plot_file .split ('.' )[0 ] + '.png' )
239+
240+ os .remove ('aux_figure.stl' )
241+
242+
243+ def show (self , show_file = None ):
244+ """
245+ Method to show an iges file. If `show_file` is not given it plots `self.infile`.
231246
232- def export_to_TIFF (event = None ):
233- display .View .Dump ('./capture_tiff.tiff' )
234-
247+ :param string show_file: the iges filename you want to plot.
248+ """
249+ if show_file is None :
250+ show_file = self .infile
251+ else :
252+ self ._check_filename_type (show_file )
235253
236- def exit (event = None ):
237- sys .exit ()
254+ ## read in the IGES file
255+ reader = IGESControl_Reader ()
256+ reader .ReadFile (show_file )
257+ reader .TransferRoots ()
258+ shape = reader .Shape ()
238259
239- add_menu ('screencapture' )
240- add_function_to_menu ('screencapture' , export_to_BMP )
241- add_function_to_menu ('screencapture' , export_to_PNG )
242- add_function_to_menu ('screencapture' , export_to_JPEG )
243- add_function_to_menu ('screencapture' , export_to_TIFF )
244- add_function_to_menu ('screencapture' , exit )
260+ display , start_display , add_menu , add_function_to_menu = init_display ()
261+ display .FitAll ()
262+ display .DisplayShape (shape , update = True )
245263
246264 # Show the plot to the screen
247- if not save_fig :
248- start_display ()
249- else :
250- f = display .View .View ().GetObject ()
251- display .View .Dump (plot_file .split ('.' )[0 ] + '.ppm' )
265+ start_display ()
252266
0 commit comments