def plot(self, plot_file=None, save_fig=False):
"""
Method to plot a file. If `plot_file` is not given it plots `self.shape`.
:param string plot_file: the filename you want to plot.
:param bool save_fig: a flag to save the figure in png or not. If True the
plot is not shown.
:return: figure: matlplotlib structure for the figure of the chosen geometry
:rtype: matplotlib.pyplot.figure
"""
if plot_file is None:
shape = self.shape
plot_file = self.infile
else:
shape = self.load_shape_from_file(plot_file)
stl_writer = StlAPI_Writer()
# Do not switch SetASCIIMode() from False to True.
stl_writer.SetASCIIMode(False)
stl_writer.Write(shape, 'aux_figure.stl')
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Load the STL files and add the vectors to the plot
stl_mesh = mesh.Mesh.from_file('aux_figure.stl')
os.remove('aux_figure.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors / 1000))
# Get the limits of the axis and center the geometry
max_dim = np.array([\
np.max(stl_mesh.vectors[:, :, 0]) / 1000,\
np.max(stl_mesh.vectors[:, :, 1]) / 1000,\
np.max(stl_mesh.vectors[:, :, 2]) / 1000])
min_dim = np.array([\
np.min(stl_mesh.vectors[:, :, 0]) / 1000,\
np.min(stl_mesh.vectors[:, :, 1]) / 1000,\
np.min(stl_mesh.vectors[:, :, 2]) / 1000])
max_lenght = np.max(max_dim - min_dim)
axes.set_xlim(\
-.6 * max_lenght + (max_dim[0] + min_dim[0]) / 2,\
.6 * max_lenght + (max_dim[0] + min_dim[0]) / 2)
axes.set_ylim(\
-.6 * max_lenght + (max_dim[1] + min_dim[1]) / 2,\
.6 * max_lenght + (max_dim[1] + min_dim[1]) / 2)
axes.set_zlim(\
-.6 * max_lenght + (max_dim[2] + min_dim[2]) / 2,\
.6 * max_lenght + (max_dim[2] + min_dim[2]) / 2)
# Show the plot to the screen
if not save_fig:
pyplot.show()
else:
figure.savefig(plot_file.split('.')[0] + '.png')
return figure
评论列表
文章目录