Skip to content

Commit b7230f9

Browse files
committed
stl_from_vtk
1 parent d945615 commit b7230f9

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

post/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import vtk
2+
import os
3+
4+
# PARAMETERS
5+
data_dir = "/leonardo_scratch/large/userexternal/aroccon0/VANDYKE/post/paraview_hit/output"
6+
file_pattern = "OUTPAR_*.vtk"
7+
iso_value = 0.5
8+
output_dir = "stl"
9+
array_name = "phi"
10+
11+
os.makedirs(output_dir, exist_ok=True)
12+
files = sorted(f for f in os.listdir(data_dir) if f.endswith(".vtk"))
13+
14+
for fname in files:
15+
step = fname.split("_")[-1].replace(".vtk", "")
16+
filepath = os.path.join(data_dir, fname)
17+
18+
print(f"Processing step {step}")
19+
20+
# -----------------------------
21+
# READ VTK FILE
22+
# -----------------------------
23+
reader = vtk.vtkGenericDataObjectReader()
24+
reader.SetFileName(filepath)
25+
reader.Update()
26+
27+
data = reader.GetOutput()
28+
29+
# -----------------------------
30+
# SELECT SCALAR ARRAY
31+
# -----------------------------
32+
if data.GetPointData().HasArray(array_name):
33+
data.GetPointData().SetActiveScalars(array_name)
34+
print(" phi on POINTS")
35+
elif data.GetCellData().HasArray(array_name):
36+
data.GetCellData().SetActiveScalars(array_name)
37+
print(" phi on CELLS")
38+
else:
39+
raise RuntimeError(f"{array_name} not found in {fname}")
40+
41+
# -----------------------------
42+
# ISO-SURFACE (ParaView Contour)
43+
# -----------------------------
44+
contour = vtk.vtkContourFilter()
45+
contour.SetInputData(data)
46+
contour.SetValue(0, iso_value)
47+
contour.Update()
48+
49+
poly = contour.GetOutput()
50+
n_cells = poly.GetNumberOfCells()
51+
print(f" contour cells: {n_cells}")
52+
53+
if n_cells == 0:
54+
print(" ⚠️ empty iso-surface, skipping")
55+
continue
56+
57+
# -----------------------------
58+
# WRITE STL
59+
# -----------------------------
60+
outname = os.path.join(output_dir, f"phi_iso_{step}.stl")
61+
62+
writer = vtk.vtkSTLWriter()
63+
writer.SetFileName(outname)
64+
writer.SetInputData(poly)
65+
writer.SetFileTypeToBinary()
66+
writer.Write()
67+
68+
print("✅ All STL files written.")

0 commit comments

Comments
 (0)