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
605 stars 103 forks source link

Convert 2D mesh to .stl format #171

Closed elahe-talebi closed 1 year ago

elahe-talebi commented 3 years ago

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.

wolph commented 3 years ago

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.

elahe-talebi commented 3 years ago

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')
wolph commented 2 years ago

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?