Spaces:
Paused
Paused
| import os | |
| from collections import defaultdict | |
| def count_part_frequencies(ldr_content): | |
| """ | |
| 精确统计.ldr文件中零件使用频率(基于1开头的行) | |
| 参数: | |
| ldr_content: .ldr文件的内容字符串 | |
| 返回: | |
| defaultdict: 零件文件名 -> 出现次数 | |
| """ | |
| freq = defaultdict(int) | |
| for line in ldr_content.splitlines(): | |
| line = line.strip() | |
| if line.startswith('1 '): | |
| parts = line.split() | |
| if len(parts) >= 15: # 确保是完整的零件定义行 | |
| part_filename = parts[-1].lower() # 统一小写处理 | |
| freq[part_filename] += 1 | |
| return freq | |
| def build_frequency_index(folder_path): | |
| """ | |
| 为文件夹中的所有.ldr文件建立频率索引 | |
| 参数: | |
| folder_path: 文件夹路径 | |
| 返回: | |
| dict: {文件名: 频率字典} | |
| """ | |
| freq_index = {} | |
| for filename in os.listdir(folder_path): | |
| if filename.lower().endswith('.ldr'): | |
| file_path = os.path.join(folder_path, filename) | |
| with open(file_path, 'r') as f: | |
| freq_index[filename] = count_part_frequencies(f.read()) | |
| return freq_index | |
| def compare_folders(folder1, folder2): | |
| """ | |
| 比较两个文件夹中的.ldr文件,找出没有匹配的文件 | |
| 参数: | |
| folder1: 第一个文件夹路径 | |
| folder2: 第二个文件夹路径 | |
| """ | |
| # 为两个文件夹建立频率索引 | |
| freq_index1 = build_frequency_index(folder1) | |
| freq_index2 = build_frequency_index(folder2) | |
| # 转换第二个文件夹的索引为频率字典到文件名的映射 | |
| freq_to_files2 = defaultdict(list) | |
| for filename, freq in freq_index2.items(): | |
| freq_key = frozenset(freq.items()) # 将字典转换为可哈希的frozenset | |
| freq_to_files2[freq_key].append(filename) | |
| # 检查folder1中的每个文件是否在folder2中有匹配 | |
| unmatched_files = [] | |
| for filename1, freq1 in freq_index1.items(): | |
| freq_key = frozenset(freq1.items()) | |
| if freq_key not in freq_to_files2: | |
| unmatched_files.append(filename1) | |
| # 输出结果 | |
| if unmatched_files: | |
| print("以下文件在目标文件夹中没有找到匹配项:") | |
| for filename in unmatched_files: | |
| print(f" - {filename}") | |
| else: | |
| print("所有文件都在目标文件夹中找到了匹配项") | |
| def compare_two_ldr_files(file1, file2): | |
| """ | |
| 详细比较两个.ldr文件的零件使用差异 | |
| 参数: | |
| file1: 第一个.ldr文件路径 | |
| file2: 第二个.ldr文件路径 | |
| 返回: | |
| dict: 差异报告字典 | |
| """ | |
| with open(file1, 'r') as f1, open(file2, 'r') as f2: | |
| freq1 = count_part_frequencies(f1.read()) | |
| freq2 = count_part_frequencies(f2.read()) | |
| # 获取所有零件类别的并集 | |
| all_parts = set(freq1.keys()).union(set(freq2.keys())) | |
| differences = { | |
| 'only_in_file1': {}, | |
| 'only_in_file2': {}, | |
| 'count_differences': {} | |
| } | |
| for part in sorted(all_parts): | |
| count1 = freq1.get(part, 0) | |
| count2 = freq2.get(part, 0) | |
| if count1 == 0 and count2 > 0: | |
| differences['only_in_file2'][part] = count2 | |
| elif count1 > 0 and count2 == 0: | |
| differences['only_in_file1'][part] = count1 | |
| elif count1 != count2: | |
| differences['count_differences'][part] = (count1, count2) | |
| return differences | |
| def print_differences(differences, file1_name, file2_name): | |
| """ | |
| 打印两个文件的比较结果 | |
| 参数: | |
| differences: compare_two_ldr_files()的返回结果 | |
| file1_name: 第一个文件名(用于显示) | |
| file2_name: 第二个文件名(用于显示) | |
| """ | |
| print(f"\n详细比较结果 ({file1_name} vs {file2_name}):") | |
| if differences['only_in_file1']: | |
| print("\n只在第一个文件中出现的零件:") | |
| for part, count in differences['only_in_file1'].items(): | |
| print(f" - {part}: {count}次") | |
| if differences['only_in_file2']: | |
| print("\n只在第二个文件中出现的零件:") | |
| for part, count in differences['only_in_file2'].items(): | |
| print(f" - {part}: {count}次") | |
| if differences['count_differences']: | |
| print("\n出现次数不同的零件:") | |
| for part, (count1, count2) in differences['count_differences'].items(): | |
| print(f" - {part}: {file1_name}={count1}次, {file2_name}={count2}次") | |
| if not any(differences.values()): | |
| print("两个文件使用的零件完全相同") | |
| # 使用示例 | |
| folder1 = '/public/home/wangshuo/gap/assembly/cubedit/outputs/sample/' | |
| folder2 = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/ldr_l30_rotrans_expand_wom' | |
| # 比较两个文件夹 | |
| #compare_folders(folder1, folder2) | |
| # 比较两个具体文件 | |
| file1 = '/public/home/wangshuo/gap/assembly/cubedit/outputs/sample/test_0_drp_101_r512_uncond.ldr' | |
| file2 = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/ldr_l30_rotrans_expand_wom/modified_car_121_rot.ldr' | |
| diff_result = compare_two_ldr_files(file1, file2) | |
| print_differences(diff_result, os.path.basename(file1), os.path.basename(file2)) |