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import defaultdict
|
| 3 |
+
|
| 4 |
+
def count_part_frequencies(ldr_content):
|
| 5 |
+
"""
|
| 6 |
+
精确统计.ldr文件中零件使用频率(基于1开头的行)
|
| 7 |
+
|
| 8 |
+
参数:
|
| 9 |
+
ldr_content: .ldr文件的内容字符串
|
| 10 |
+
|
| 11 |
+
返回:
|
| 12 |
+
defaultdict: 零件文件名 -> 出现次数
|
| 13 |
+
"""
|
| 14 |
+
freq = defaultdict(int)
|
| 15 |
+
for line in ldr_content.splitlines():
|
| 16 |
+
line = line.strip()
|
| 17 |
+
if line.startswith('1 '):
|
| 18 |
+
parts = line.split()
|
| 19 |
+
if len(parts) >= 15: # 确保是完整的零件定义行
|
| 20 |
+
part_filename = parts[-1].lower() # 统一小写处理
|
| 21 |
+
freq[part_filename] += 1
|
| 22 |
+
return freq
|
| 23 |
+
|
| 24 |
+
def build_frequency_index(folder_path):
|
| 25 |
+
"""
|
| 26 |
+
为文件夹中的所有.ldr文件建立频率索引
|
| 27 |
+
|
| 28 |
+
参数:
|
| 29 |
+
folder_path: 文件夹路径
|
| 30 |
+
|
| 31 |
+
返回:
|
| 32 |
+
dict: {文件名: 频率字典}
|
| 33 |
+
"""
|
| 34 |
+
freq_index = {}
|
| 35 |
+
for filename in os.listdir(folder_path):
|
| 36 |
+
if filename.lower().endswith('.ldr'):
|
| 37 |
+
file_path = os.path.join(folder_path, filename)
|
| 38 |
+
with open(file_path, 'r') as f:
|
| 39 |
+
freq_index[filename] = count_part_frequencies(f.read())
|
| 40 |
+
return freq_index
|
| 41 |
+
|
| 42 |
+
def compare_folders(folder1, folder2):
|
| 43 |
+
"""
|
| 44 |
+
比较两个文件夹中的.ldr文件,找出没有匹配的文件
|
| 45 |
+
|
| 46 |
+
参数:
|
| 47 |
+
folder1: 第一个文件夹路径
|
| 48 |
+
folder2: 第二个文件夹路径
|
| 49 |
+
"""
|
| 50 |
+
# 为两个文件夹建立频率索引
|
| 51 |
+
freq_index1 = build_frequency_index(folder1)
|
| 52 |
+
freq_index2 = build_frequency_index(folder2)
|
| 53 |
+
|
| 54 |
+
# 转换第二个文件夹的索引为频率字典到文件名的映射
|
| 55 |
+
freq_to_files2 = defaultdict(list)
|
| 56 |
+
for filename, freq in freq_index2.items():
|
| 57 |
+
freq_key = frozenset(freq.items()) # 将字典转换为可哈希的frozenset
|
| 58 |
+
freq_to_files2[freq_key].append(filename)
|
| 59 |
+
|
| 60 |
+
# 检查folder1中的每个文件是否在folder2中有匹配
|
| 61 |
+
unmatched_files = []
|
| 62 |
+
for filename1, freq1 in freq_index1.items():
|
| 63 |
+
freq_key = frozenset(freq1.items())
|
| 64 |
+
if freq_key not in freq_to_files2:
|
| 65 |
+
unmatched_files.append(filename1)
|
| 66 |
+
|
| 67 |
+
# 输出结果
|
| 68 |
+
if unmatched_files:
|
| 69 |
+
print("以下文件在目标文件夹中没有找到匹配项:")
|
| 70 |
+
for filename in unmatched_files:
|
| 71 |
+
print(f" - {filename}")
|
| 72 |
+
else:
|
| 73 |
+
print("所有文件都在目标文件夹中找到了匹配项")
|
| 74 |
+
|
| 75 |
+
def compare_two_ldr_files(file1, file2):
|
| 76 |
+
"""
|
| 77 |
+
详细比较两个.ldr文件的零件使用差异
|
| 78 |
+
|
| 79 |
+
参数:
|
| 80 |
+
file1: 第一个.ldr文件路径
|
| 81 |
+
file2: 第二个.ldr文件路径
|
| 82 |
+
|
| 83 |
+
返回:
|
| 84 |
+
dict: 差异报告字典
|
| 85 |
+
"""
|
| 86 |
+
with open(file1, 'r') as f1, open(file2, 'r') as f2:
|
| 87 |
+
freq1 = count_part_frequencies(f1.read())
|
| 88 |
+
freq2 = count_part_frequencies(f2.read())
|
| 89 |
+
|
| 90 |
+
# 获取所有零件类别的并集
|
| 91 |
+
all_parts = set(freq1.keys()).union(set(freq2.keys()))
|
| 92 |
+
|
| 93 |
+
differences = {
|
| 94 |
+
'only_in_file1': {},
|
| 95 |
+
'only_in_file2': {},
|
| 96 |
+
'count_differences': {}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
for part in sorted(all_parts):
|
| 100 |
+
count1 = freq1.get(part, 0)
|
| 101 |
+
count2 = freq2.get(part, 0)
|
| 102 |
+
|
| 103 |
+
if count1 == 0 and count2 > 0:
|
| 104 |
+
differences['only_in_file2'][part] = count2
|
| 105 |
+
elif count1 > 0 and count2 == 0:
|
| 106 |
+
differences['only_in_file1'][part] = count1
|
| 107 |
+
elif count1 != count2:
|
| 108 |
+
differences['count_differences'][part] = (count1, count2)
|
| 109 |
+
|
| 110 |
+
return differences
|
| 111 |
+
|
| 112 |
+
def print_differences(differences, file1_name, file2_name):
|
| 113 |
+
"""
|
| 114 |
+
打印两个文件的比较结果
|
| 115 |
+
|
| 116 |
+
参数:
|
| 117 |
+
differences: compare_two_ldr_files()的返回结果
|
| 118 |
+
file1_name: 第一个文件名(用于显示)
|
| 119 |
+
file2_name: 第二个文件名(用于显示)
|
| 120 |
+
"""
|
| 121 |
+
print(f"\n详细比较结果 ({file1_name} vs {file2_name}):")
|
| 122 |
+
|
| 123 |
+
if differences['only_in_file1']:
|
| 124 |
+
print("\n只在第一个文件中出现的零件:")
|
| 125 |
+
for part, count in differences['only_in_file1'].items():
|
| 126 |
+
print(f" - {part}: {count}次")
|
| 127 |
+
|
| 128 |
+
if differences['only_in_file2']:
|
| 129 |
+
print("\n只在第二个文件中出现的零件:")
|
| 130 |
+
for part, count in differences['only_in_file2'].items():
|
| 131 |
+
print(f" - {part}: {count}次")
|
| 132 |
+
|
| 133 |
+
if differences['count_differences']:
|
| 134 |
+
print("\n出现次数不同的零件:")
|
| 135 |
+
for part, (count1, count2) in differences['count_differences'].items():
|
| 136 |
+
print(f" - {part}: {file1_name}={count1}次, {file2_name}={count2}次")
|
| 137 |
+
|
| 138 |
+
if not any(differences.values()):
|
| 139 |
+
print("两个文件使用的零件完全相同")
|
| 140 |
+
|
| 141 |
+
# 使用示例
|
| 142 |
+
folder1 = '/public/home/wangshuo/gap/assembly/cubedit/outputs/sample/'
|
| 143 |
+
folder2 = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/ldr_l30_rotrans_expand_wom'
|
| 144 |
+
|
| 145 |
+
# 比较两个文件夹
|
| 146 |
+
#compare_folders(folder1, folder2)
|
| 147 |
+
|
| 148 |
+
# 比较两个具体文件
|
| 149 |
+
file1 = '/public/home/wangshuo/gap/assembly/cubedit/outputs/sample/test_0_drp_101_r512_uncond.ldr'
|
| 150 |
+
file2 = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/ldr_l30_rotrans_expand_wom/modified_car_121_rot.ldr'
|
| 151 |
+
diff_result = compare_two_ldr_files(file1, file2)
|
| 152 |
+
print_differences(diff_result, os.path.basename(file1), os.path.basename(file2))
|