Skip to content

Commit 210903c

Browse files
fsalmoirmtezzele
authored andcommitted
added show function to igeshandler + plot now with matlplotlib and sa… (#56)
* added show function to igeshandler + plot now with matlplotlib and save stl of iges in png format * added show function to igeshandler + plot now with matlplotlib and save stl of iges in png format * added show function to igeshandler + plot now with matlplotlib and save stl of iges in png format * added show function to igeshandler + plot now with matlplotlib and save stl of iges in png format
1 parent d30b0a3 commit 210903c

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

pygem/igeshandler.py

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Utilities for reading and writing different CAD files.
33
"""
44
import numpy as np
5-
import sys
5+
import os
66
import pygem.filehandler as fh
77
from OCC.IGESControl import (IGESControl_Reader, IGESControl_Writer)
88
from OCC.BRep import (BRep_Tool, BRep_Builder)
@@ -14,6 +14,10 @@
1414
from OCC.gp import (gp_Pnt, gp_XYZ)
1515
from OCC.Display.SimpleGui import init_display
1616
from 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

1923
class 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

tests/test_igeshandler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ def test_iges_write_comparison(self):
138138

139139
iges_handler.write(mesh_points, outfilename)
140140
os.remove(outfilename)
141+
142+
143+
def test_iges_plot_save_fig(self):
144+
iges_handler = ih.IgesHandler()
145+
mesh_points = iges_handler.parse('tests/test_datasets/test_pipe.iges')
146+
iges_handler.plot(save_fig=True)
147+
self.assertTrue(os.path.isfile('tests/test_datasets/test_pipe.png'))
148+
os.remove('tests/test_datasets/test_pipe.png')
141149

142150

143151
def test_iges_plot_failing_outfile_type(self):

0 commit comments

Comments
 (0)