-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxtShow.py
More file actions
102 lines (88 loc) · 3.8 KB
/
txtShow.py
File metadata and controls
102 lines (88 loc) · 3.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
import matplotlib.pyplot as plt
def node_num_display(nodes, faces=None):
""" 可视化节点编号及面连接 """
x = np.array([n[0] for n in nodes.values()])
y = np.array([n[1] for n in nodes.values()])
plt.figure(figsize=(12, 12))
plt.scatter(x, y, color='r', s=20) # 绘制节点
# 标注节点编号
for node_id, (xi, yi) in nodes.items():
plt.annotate(f'{node_id}', (xi, yi), fontsize=6, color='b',
xytext=(2, 2), textcoords='offset points')
# 绘制面连接(若提供面数据)
if faces:
for face in faces:
start_node = face['nodes'][0]
end_node = face['nodes'][1]
x_coords = [nodes[start_node][0], nodes[end_node][0]]
y_coords = [nodes[start_node][1], nodes[end_node][1]]
plt.plot(x_coords, y_coords, 'g-', linewidth=2) # 绿色线条表示面
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Node and Face Visualization')
plt.grid(True)
plt.show()
def read_text_semi(file_path):
""" 读取文件并分离节点和面数据 """
nodes = {} # 格式:{节点ID: (x, y)}
faces = [] # 格式:[{'id': 3467, 'tag': 0, 'nodes': (10,9)}, ...]
current_zone = None
try:
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if not line:
continue
# 处理Zone声明行
if line.startswith("Zone"):
parts = line.split()
zone_type = int(parts[1]) # Zone 1或Zone 2
current_zone = zone_type
print(f"读取Zone {zone_type}数据...")
continue
# 根据当前Zone类型处理数据
if current_zone == 1: # 节点坐标Zone
parts = line.split()
if len(parts) == 2:
try:
x = float(parts[0])
y = float(parts[1])
# 自动生成节点ID(假设按顺序排列)
node_id = len(nodes) + 1
nodes[node_id] = (x, y)
except ValueError:
print(f"忽略无效节点行: {line}")
elif current_zone == 2: # 面连接关系Zone
parts = line.split()
if len(parts) == 4:
try:
face_id = int(parts[0])
tag = int(parts[1])
start_node = int(parts[2])
end_node = int(parts[3])
faces.append({
'id': face_id,
'tag': tag,
'nodes': (start_node, end_node)
})
except (ValueError, IndexError):
print(f"忽略无效面行: {line}")
except FileNotFoundError:
print(f"错误:文件 {file_path} 不存在!")
return None, None
except Exception as e:
print(f"读取错误: {str(e)}")
return None, None
return nodes, faces
if __name__ == "__main__":
file_path = "TEXT_20210516.txt"
nodes, faces = read_text_semi(file_path)
if nodes and faces:
print(f"成功读取 {len(nodes)} 个节点,{len(faces)} 条面连接")
node_num_display(nodes, faces) # 同时显示节点和面
elif nodes:
print(f"仅读取到节点数据({len(nodes)} 个节点),无面数据")
node_num_display(nodes)
else:
print("数据读取失败")