Created
February 22, 2023 21:10
-
-
Save tfmoraes/b668b14de38d883c2a79dc82ea66a319 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
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