File size: 4,569 Bytes
19c667c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os

max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf')

# 设置根目录路径,所有文件夹都位于此目录下
root_dir = '/public/home/wangshuo/gap/assembly/data'  # 替换为你的根目录路径

# 遍历根目录下的每个文件夹
for folder_name in os.listdir(root_dir):
    folder_path = os.path.join(root_dir, folder_name)
    
    if os.path.isdir(folder_path):
        # 设置原始 LDR 文件路径
        original_file_path = os.path.join(folder_path, f'{folder_name}.ldr')
        
        # 检查文件是否存在
        if os.path.exists(original_file_path):
            # 打开并读取原始 LDR 文件
            with open(original_file_path, "r") as file:
                lines = file.readlines()

            if len(lines) > 310:
                continue
            # 初始化最小坐标值
            min_x, min_y, min_z = float('inf'), float('inf'), float('inf')

            # 第一遍遍历文件找到最小坐标值
            for line in lines:
                parts = line.split()  # 按空格分割行数据
                if len(parts) == 15:  # 检查是否为零件数据行
                    # 提取 x, y, z 坐标
                    x, y, z = float(parts[2]), float(parts[3]), float(parts[4])

                    # 更新最小坐标值
                    min_x = min(min_x, x)
                    min_y = min(min_y, y)
                    min_z = min(min_z, z)

            # 新的 LDR 文件内容
            new_lines = []

            # 第二遍遍历文件,更新坐标
            for line in lines:
                parts = line.split()  # 按空格分割行数据
                if len(parts) == 15:  # 检查是否为零件数据行
                    # 提取 x, y, z 坐标
                    x, y, z = float(parts[2]), float(parts[3]), float(parts[4])

                    # 计算新的坐标
                    x_new = round(x - min_x)
                    y_new = round(y - min_y)
                    z_new = round(z - min_z)

                    # 更新坐标
                    parts[2] = str(x_new)
                    parts[3] = str(y_new)
                    parts[4] = str(z_new)

                    max_x = max(max_x, x_new)
                    max_y = max(max_y, y_new)
                    max_z = max(max_z, z_new)

                    # 将修改后的行添加到新文件内容中
                    new_lines.append(" ".join(parts) + "\n")
                else:
                    # 如果是非零件行,直接添加到新文件中
                    new_lines.append(line)

            # 设置新的 LDR 文件路径
            modified_file_path = os.path.join(folder_path, f'modified_{folder_name}.ldr')

            # 将修改后的内容写入新的 LDR 文件
            with open(modified_file_path, "w") as file:
                file.writelines(new_lines)

            print(f"LDR file '{original_file_path}' has been modified and saved as '{modified_file_path}'")

print(f"Maximum coordinates found across all LDR files:")
print(f"Max X: {max_x}")
print(f"Max Y: {max_y}")
print(f"Max Z: {max_z}")

# import os

# # 设置data目录路径
# data_dir = '/public/home/wangshuo/gap/assembly/data/car_1k/ldr/car_81'  # 替换为你的data目录路径

# # 设置原始LDR文件和保存的新LDR文件的路径
# original_file_path = os.path.join(data_dir, 'car_81.ldr')
# modified_file_path = os.path.join(data_dir, 'modified_model.ldr')

# # 打开并读取原始 LDR 文件
# with open(original_file_path, "r") as file:
#     lines = file.readlines()

# # 新的 LDR 文件内容
# new_lines = []

# # 处理每一行
# for line in lines:
#     parts = line.split()  # 按空格分割行数据
#     if len(parts) == 15:  # 检查是否为零件数据行
#         # 提取 x, y, z 坐标
#         x, y, z = float(parts[2]), float(parts[3]), float(parts[4])

#         # 对坐标进行加法操作
#         x_new = x + 6000
#         y_new = y + 150
#         z_new = z + 2000

#         # 更新坐标
#         parts[2] = str(x_new)
#         parts[3] = str(y_new)
#         parts[4] = str(z_new)

#         # 将修改后的行添加到新文件内容中
#         new_lines.append(" ".join(parts) + "\n")
#     else:
#         # 如果是非零件行,直接添加到新文件中
#         new_lines.append(line)

# # 将修改后的内容写入新的 LDR 文件
# with open(modified_file_path, "w") as file:
#     file.writelines(new_lines)

# print(f"LDR file has been modified and saved as '{modified_file_path}'")