Last active
July 18, 2018 06:54
-
-
Save ngunhaSO/f3902fd34172a161b678b2a05a5c91fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# retrieve the original stl file | |
stl_reader = vtk.vtkSTLReader() | |
stl_reader.SetFileName('path_to_stl') | |
stl_reader.Update() | |
stl_poly_data = stl_reader.GetOutput() | |
# triangulate the stl | |
triangleFilter = vtk.vtkTriangleFilter() | |
triangleFilter.SetInputConnection(stl_reader.GetOutputPort()) | |
triangleFilter.Update() | |
# extract the boundary edge | |
featureEdges = vtk.vtkFeatureEdges() | |
featureEdges.SetInputData(triangleFilter.GetOutput()) | |
featureEdges.SetBoundaryEdges(1) | |
featureEdges.SetFeatureEdges(0) | |
featureEdges.SetNonManifoldEdges(0) | |
featureEdges.SetManifoldEdges(0) | |
featureEdges.Update() | |
boundaryMesh = featureEdges.GetOutput() | |
print('get boundary mesh: ', boundaryMesh.GetNumberOfLines()) | |
# init all the container holders for the stiching data | |
trianglePolyData = vtk.vtkPolyData() | |
triangles = vtk.vtkCellArray() | |
points = vtk.vtkPoints() | |
triangle = vtk.vtkTriangle() | |
totalLines = boundaryMesh.GetNumberOfLines() | |
# Start loop thru each edge line from the boundary edge | |
for i in range(0, totalLines): | |
cell = boundaryMesh.GetCell(i) | |
pointIds = cell.GetPointIds() | |
pointsPerEdge = pointIds.GetNumberOfIds() # expect each edge line has 2 points | |
p = boundaryMesh.GetPoint(pointIds.GetId(0)) | |
nextPoint = boundaryMesh.GetPoint(pointIds.GetId(1)) | |
# first triangle: | |
# (xa,ya,za), | |
# (xb,yb,zb) | |
# (xb,yb,0) | |
points.InsertNextPoint(p[0], p[1], p[2]) | |
points.InsertNextPoint(nextPoint[0], nextPoint[1], nextPoint[2]) | |
points.InsertNextPoint(nextPoint[0], nextPoint[1], 1100) | |
triangle.GetPointIds().SetId(0, i) | |
triangle.GetPointIds().SetId(1, i + 1) | |
triangle.GetPointIds().SetId(2, i + 2) | |
triangles.InsertNextCell(triangle) | |
# second triangle: | |
#(xb,yb,0) | |
#(xa,ya,0) | |
#(xa,ya,za) | |
points.InsertNextPoint(nextPoint[0], nextPoint[1], 1100) | |
points.InsertNextPoint(p[0], p[1], 1100) | |
points.InsertNextPoint(p[0], p[1], p[2]) | |
triangle.GetPointIds().SetId(0, i + 3) | |
triangle.GetPointIds().SetId(1, i + 4) | |
triangle.GetPointIds().SetId(2, i + 5) | |
triangles.InsertNextCell(triangle) | |
# clean up the data and ready for writing data into file | |
trianglePolyData.SetPoints(points) | |
trianglePolyData.SetPolys(triangles) | |
trianglePolyData.BuildLinks() | |
appendPoly = vtk.vtkAppendPolyData() | |
appendPoly.AddInputData(trianglePolyData) | |
appendPoly.AddInputData(stl_poly_data) | |
appendPoly.Update() | |
cleanPoly = vtk.vtkCleanPolyData() | |
cleanPoly.SetInputData(appendPoly.GetOutput()) | |
cleanPoly.Update() | |
writerFinal = vtk.vtkSTLWriter() | |
writerFinal.SetFileTypeToBinary() | |
writerFinal.SetInputData(cleanPoly.GetOutput()) | |
writerFinal.SetFileName('data/3D_data/demoparts/test2.stl') | |
writerFinal.Update() | |
writerFinal.Write() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment