numpy-stl
numpy-stl copied to clipboard
Convert 2D mesh to .stl format
My geometry is 2D and I tried both Delaunay from scipy.spatial and Triangle to mesh my geometry and then, I added the z coordination as 0 to all the vectors. I thought this can solve the problem but it seems that there is something wrong with my .stl file. when I import it to another software, I get this error: Edge with ID 2 does not share an Point with another Edge.
Can you show some of the code you used perhaps? I'm guessing that some of the points are singular and not part of a triangle.
Hi this is the coordination of my points. aggregates - 1.txt and the following is my code.
import meshpy.triangle as triangle
import numpy as np
vertices = np.loadtxt(aggregates - 1.txt, delimiter="\t")
points = []
length=(vertices.shape[0])
for i in range(length):
points.append(vertices[i,:])
def round_trip_connect(start, end):
result = []
for i in range(start, end):
result.append((i, i + 1))
result.append((end, start))
return result
info = triangle.MeshInfo()
info.set_points(vertices)
info.set_facets(round_trip_connect(0, 189))
mesh = triangle.build(info, allow_volume_steiner=False, allow_boundary_steiner=False)
triangle.write_gnuplot_mesh("triangl.dat", mesh)
from stl import mesh
new = np.loadtxt(triangl.dat, dtype=np.str)
new= new.astype(np.float)
length2=(new.shape[0])
print("length2 is equal to", length2)
rangeof=length2-1
print("rangeof is equal to", rangeof)
facessize=np.int(length2/4*3)
print("facessize is equal to", facessize)
faces=np.zeros((facessize,3),dtype=np.float)
j=0
for i in range(rangeof):
if np.remainder(i+1,4)!=0:
faces[j,0:2]=new[i,:]
j=j+1
length3=length4/3
length3=np.int(length3)
np.reshape(faces, (length3,3,3))
cube = mesh.Mesh(np.zeros((length3), dtype=mesh.Mesh.dtype))
cube.vectors=np.reshape(faces, (length3,3,3))
cube.vectors
cube.save('cube.stl')
This issue slipped my mind unfortunately. It seems that there are several things wrong with your code however. I suppose at line 3 and line 20 there should be strings.
But beyond that, length4 at line 34 is never defined. What is it supposed to be?