Skip to content

Instantly share code, notes, and snippets.

@tfmoraes
Created February 22, 2023 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfmoraes/b668b14de38d883c2a79dc82ea66a319 to your computer and use it in GitHub Desktop.
Save tfmoraes/b668b14de38d883c2a79dc82ea66a319 to your computer and use it in GitHub Desktop.
import sys
import vtk
def getInlet_and_Outlets(mesh):
fillHoles = vtk.vtkFillHolesFilter()
fillHoles.SetInputData(mesh)
fillHoles.SetHoleSize(1000.0)
fillHoles.Update()
# Make the triangle winding order consistent
normals = vtk.vtkPolyDataNormals()
normals.SetInputConnection(fillHoles.GetOutputPort())
normals.ConsistencyOn()
normals.SplittingOff()
normals.Update()
normals.GetOutput().GetPointData().SetNormals(mesh.GetPointData().GetNormals())
# How many added cells
numOriginalCells = mesh.GetNumberOfCells()
numNewCells = normals.GetOutput().GetNumberOfCells()
numCells = 0
it = normals.GetOutput().NewCellIterator()
while not it.IsDoneWithTraversal() and numCells < numOriginalCells:
it.GoToNextCell()
numCells += 1
print(
f"Num original: {numOriginalCells}\nNum new: {numNewCells}\nNum added {numNewCells - numOriginalCells}"
)
holePolyData = vtk.vtkPolyData()
holePolyData.Allocate(normals.GetOutput(), numNewCells - numOriginalCells)
holePolyData.SetPoints(normals.GetOutput().GetPoints())
cell = vtk.vtkGenericCell()
# The remaining cells are the new ones from the hole filler
while not it.IsDoneWithTraversal():
it.GetCell(cell)
holePolyData.InsertNextCell(it.GetCellType(), cell.GetPointIds())
it.GoToNextCell()
conn = vtk.vtkPolyDataConnectivityFilter()
conn.SetInputData(holePolyData)
conn.SetExtractionModeToAllRegions()
conn.Update()
nregions = conn.GetNumberOfExtractedRegions()
conn.SetExtractionModeToSpecifiedRegions()
conn.Update()
for region in range(nregions):
print(region)
conn.InitializeSpecifiedRegionList()
conn.AddSpecifiedRegion(region)
conn.Update()
p = vtk.vtkPolyData()
p.DeepCopy(conn.GetOutput())
writer = vtk.vtkSTLWriter()
writer.SetInputData(p)
writer.SetFileName(f"saida_{region:02}.stl")
writer.Write()
if __name__ == "__main__":
reader = vtk.vtkSTLReader()
reader.SetFileName(sys.argv[1])
reader.Update()
getInlet_and_Outlets(reader.GetOutput())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment