wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
624 stars 105 forks source link

Create a STL file with XYZ 2D arrays #104

Closed tomyferra closed 5 years ago

tomyferra commented 5 years ago

I need help of how to create a STL file having my X Y Z 2D arrays. I saw this example but i'm having trouble finding a way out. https://github.com/WoLpH/numpy-stl/issues/89

image

This is the problem in my code, where xInterp, yInterp and zInterp are 2D arrays

Thank you!

wolph commented 5 years ago

This issue has been discussed over email a bit already but I'm not sure how to fully solve it. Here's the code for completeness though :)

import numpy as np

x_all = np.hstack(valorX)
y_all = np.hstack(valorY)
z_all = np.hstack(valorZ)

tris = mtri.Triangulation(x_all, y_all)

data = np.zeros(len(tris.triangles), dtype=mesh.Mesh.dtype)
m = mesh.Mesh(data, remove_empty_areas=False)
m.x[:] = x_all[tris.triangles]
m.y[:] = y_all[tris.triangles]
m.z[:] = z_all[tris.triangles]
m.save('model.stl')

from matplotlib import pyplot
from mpl_toolkits import mplot3d

# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)

# Render the cube
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(m.vectors))

# Auto scale to the mesh size
scale = m.points.flatten(-1)
axes.auto_scale_xyz(scale, scale, scale)

# Show the plot to the screen
pyplot.show()