-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcas_visualizer.py
More file actions
72 lines (59 loc) · 2.27 KB
/
cas_visualizer.py
File metadata and controls
72 lines (59 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import matplotlib.pyplot as plt
def read_cas_file(file_path):
""" 读取CAS文件并解析节点和面数据 """
nodes = []
faces = []
with open(file_path, 'r') as f:
lines = f.readlines()
reading_nodes = False
reading_faces = False
for line in lines:
# 解析节点数据
if "Zone 1 Number of Nodes" in line:
reading_nodes = True
reading_faces = False
continue
# 解析面数据
if "Zone 2 Number of Face" in line:
reading_faces = True
reading_nodes = False
continue
if reading_nodes:
if line.strip() and '(' not in line: # 跳过非数据行
x, y = map(float, line.strip().split())
nodes.append((x, y))
if reading_faces:
if line.strip() and '(' not in line: # 跳过非数据行
parts = list(map(int, line.strip().split()))
if len(parts) >= 2: # 只需要前两个节点索引
faces.append((parts[0]-1, parts[1]-1)) # 转换为0-based索引
return nodes, faces
def plot_mesh(nodes, faces):
""" 绘制网格 """
plt.figure(figsize=(10, 10))
# 绘制所有面
for face in faces:
n1, n2 = face
x = [nodes[n1][0], nodes[n2][0]]
y = [nodes[n1][1], nodes[n2][1]]
plt.plot(x, y, 'b-', linewidth=1)
# 绘制节点并标注编号
for i, (x, y) in enumerate(nodes):
plt.scatter(x, y, c='r', s=50)
plt.text(x+0.02, y+0.02, str(i+1), fontsize=8, color='black') # 显示1-based编号
# 设置图形属性
plt.title("Mesh Visualization")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.axis('equal')
plt.show()
if __name__ == "__main__":
# 文件路径需要根据实际情况修改
cas_file = "datainput/semicircle_holes_new.cas"
try:
nodes, faces = read_cas_file(cas_file)
print(f"成功读取 {len(nodes)} 个节点和 {len(faces)} 个面")
plot_mesh(nodes, faces)
except Exception as e:
print(f"读取文件出错: {e}")