updated programs and added FileManager
This commit is contained in:
1 parent
55177b1444
commit
4c994a28f0
7 files changed
+717
-248
No files matched your search
+380
@@ -0,0 +1,380 @@
|
||||
import os
|
||||
import re
|
||||
import csv
|
||||
import hashlib
|
||||
import tkinter as tk
|
||||
from pathlib import Path
|
||||
|
||||
# ============================================================
|
||||
# GUI
|
||||
# ============================================================
|
||||
|
||||
def select_folder():
|
||||
return input("Enter folder path: ").strip().strip("'\"")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Helper Functions
|
||||
# ============================================================
|
||||
|
||||
def normalize_nc_content(text):
|
||||
|
||||
output = []
|
||||
|
||||
for line in text.splitlines():
|
||||
|
||||
line = line.upper()
|
||||
|
||||
# Remove comments
|
||||
line = re.sub(r"\(.*?\)", "", line)
|
||||
line = re.sub(r";.*", "", line)
|
||||
|
||||
# Remove tool numbers
|
||||
line = re.sub(r"\bT\d+\b", "", line)
|
||||
|
||||
# Remove spindle speeds
|
||||
line = re.sub(r"\bS[-+]?\d*\.?\d+\b", "", line)
|
||||
|
||||
line = line.strip()
|
||||
|
||||
if line:
|
||||
output.append(line)
|
||||
|
||||
return "\n".join(output)
|
||||
|
||||
|
||||
def calculate_file_hash(file_path):
|
||||
|
||||
sha = hashlib.sha256()
|
||||
|
||||
with open(file_path, "rb") as f:
|
||||
|
||||
while True:
|
||||
|
||||
chunk = f.read(8192)
|
||||
|
||||
if not chunk:
|
||||
break
|
||||
|
||||
sha.update(chunk)
|
||||
|
||||
return sha.hexdigest()
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Geometry Comparison
|
||||
# ============================================================
|
||||
|
||||
AXIS_PATTERN = re.compile(r"([XYZIJKR])([-+]?\d*\.?\d+)")
|
||||
COMMAND_PATTERN = re.compile(r"\b(G\d+|M\d+)\b")
|
||||
|
||||
|
||||
def extract_toolpath(file_path):
|
||||
|
||||
motions = []
|
||||
|
||||
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
||||
|
||||
for raw_line in f:
|
||||
|
||||
line = normalize_nc_content(raw_line)
|
||||
|
||||
if not line:
|
||||
continue
|
||||
|
||||
commands = COMMAND_PATTERN.findall(line)
|
||||
|
||||
axes = {
|
||||
axis: float(value)
|
||||
for axis, value in AXIS_PATTERN.findall(line)
|
||||
}
|
||||
|
||||
if commands or axes:
|
||||
|
||||
motions.append({
|
||||
"commands": tuple(commands),
|
||||
"axes": axes
|
||||
})
|
||||
|
||||
return motions
|
||||
|
||||
|
||||
def compare_program_structure(file1, file2, tolerance=0.005):
|
||||
|
||||
path1 = extract_toolpath(file1)
|
||||
path2 = extract_toolpath(file2)
|
||||
|
||||
if len(path1) != len(path2):
|
||||
return False, None
|
||||
|
||||
max_diff = 0
|
||||
|
||||
for move1, move2 in zip(path1, path2):
|
||||
|
||||
if move1["commands"] != move2["commands"]:
|
||||
return False, None
|
||||
|
||||
axes = set(move1["axes"]) | set(move2["axes"])
|
||||
|
||||
for axis in axes:
|
||||
|
||||
if axis not in move1["axes"]:
|
||||
return False, None
|
||||
|
||||
if axis not in move2["axes"]:
|
||||
return False, None
|
||||
|
||||
diff = abs(
|
||||
move1["axes"][axis]
|
||||
- move2["axes"][axis]
|
||||
)
|
||||
|
||||
max_diff = max(max_diff, diff)
|
||||
|
||||
if diff > tolerance:
|
||||
return False, max_diff
|
||||
|
||||
return True, max_diff
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Extension Normalization
|
||||
# ============================================================
|
||||
|
||||
def normalize_extensions(root_folder):
|
||||
|
||||
root_path = Path(root_folder)
|
||||
|
||||
if not root_path.exists():
|
||||
raise FileNotFoundError(root_folder)
|
||||
|
||||
for file in root_path.rglob("*"):
|
||||
|
||||
if not file.is_file():
|
||||
continue
|
||||
|
||||
suffix = file.suffix.lower()
|
||||
|
||||
if suffix == "":
|
||||
|
||||
new_file = file.with_name(
|
||||
file.name + ".txt"
|
||||
)
|
||||
|
||||
elif suffix == ".nc":
|
||||
|
||||
new_file = file.with_suffix(".txt")
|
||||
|
||||
elif suffix == ".txt":
|
||||
|
||||
if file.suffix == ".txt":
|
||||
continue
|
||||
|
||||
new_file = file.with_suffix(".txt")
|
||||
|
||||
else:
|
||||
continue
|
||||
|
||||
try:
|
||||
file.rename(new_file)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed: {file}")
|
||||
print(e)
|
||||
|
||||
print("Extension normalization complete.")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Program Splitting
|
||||
# ============================================================
|
||||
|
||||
def split_file_by_key(input_file, output_dir):
|
||||
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
current_output = None
|
||||
|
||||
with open(input_file, "r", encoding="utf-8", errors="ignore") as infile:
|
||||
|
||||
for line in infile:
|
||||
|
||||
if line.startswith("O"):
|
||||
|
||||
if current_output:
|
||||
|
||||
current_output.write("%\n")
|
||||
current_output.close()
|
||||
|
||||
filename = line[:6].strip()
|
||||
|
||||
output_path = os.path.join(output_dir, f"{filename}.txt")
|
||||
|
||||
current_output = open(output_path, "w", encoding="utf-8")
|
||||
|
||||
current_output.write("%\n")
|
||||
|
||||
if current_output:
|
||||
current_output.write(line)
|
||||
|
||||
if current_output:
|
||||
|
||||
current_output.write("%\n")
|
||||
current_output.close()
|
||||
|
||||
print(f"Finished: {input_file}")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Exact Duplicate Removal
|
||||
# ============================================================
|
||||
|
||||
def remove_exact_duplicates(root_dir):
|
||||
|
||||
known_hashes = {}
|
||||
deleted = []
|
||||
|
||||
for file in Path(root_dir).rglob("*.txt"):
|
||||
|
||||
file_hash = calculate_file_hash(file)
|
||||
|
||||
if file_hash in known_hashes:
|
||||
|
||||
print(f"Removing exact duplicate: {file}")
|
||||
|
||||
file.unlink()
|
||||
|
||||
deleted.append(file)
|
||||
|
||||
else:
|
||||
known_hashes[file_hash] = file
|
||||
|
||||
print(
|
||||
f"Removed {len(deleted)} exact duplicates."
|
||||
)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Geometry Duplicate Removal
|
||||
# ============================================================
|
||||
|
||||
def remove_geometry_duplicates(root_dir, tolerance=0.005):
|
||||
|
||||
files = list(Path(root_dir).rglob("*.txt"))
|
||||
|
||||
removed = set()
|
||||
|
||||
for i in range(len(files)):
|
||||
|
||||
if files[i] in removed:
|
||||
continue
|
||||
|
||||
for j in range(i + 1, len(files)):
|
||||
|
||||
if files[j] in removed:
|
||||
continue
|
||||
|
||||
duplicate, max_diff = compare_program_structure(files[i], files[j], tolerance)
|
||||
|
||||
if duplicate:
|
||||
|
||||
print(
|
||||
f"Removing geometric duplicate:\n"
|
||||
f" Keep: {files[i].name}\n"
|
||||
f" Delete: {files[j].name}\n"
|
||||
f" Max Delta: {max_diff:.5f}"
|
||||
)
|
||||
|
||||
files[j].unlink()
|
||||
|
||||
removed.add(files[j])
|
||||
|
||||
print(
|
||||
f"Removed {len(removed)} geometry duplicates."
|
||||
)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Similarity Report
|
||||
# ============================================================
|
||||
|
||||
def generate_similarity_report(
|
||||
root_dir,
|
||||
tolerance=0.005,
|
||||
output_csv="similarity_report.csv"
|
||||
):
|
||||
|
||||
files = list(Path(root_dir).rglob("*.txt"))
|
||||
|
||||
with open(output_csv, "w", newline="", encoding="utf-8") as csvfile:
|
||||
|
||||
writer = csv.writer(csvfile)
|
||||
|
||||
writer.writerow([
|
||||
"File1",
|
||||
"File2",
|
||||
"Duplicate",
|
||||
"MaxCoordinateDelta"
|
||||
])
|
||||
|
||||
for i in range(len(files)):
|
||||
|
||||
for j in range(i + 1, len(files)):
|
||||
|
||||
duplicate, delta = compare_program_structure(files[i], files[j], tolerance)
|
||||
|
||||
writer.writerow([
|
||||
files[i].name,
|
||||
files[j].name,
|
||||
duplicate,
|
||||
delta
|
||||
])
|
||||
|
||||
print(
|
||||
f"Similarity report written to {output_csv}"
|
||||
)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Main
|
||||
# ============================================================
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
input_dir = select_folder()
|
||||
|
||||
if not os.path.isdir(input_dir):
|
||||
|
||||
print("Invalid folder.")
|
||||
|
||||
raise SystemExit(1)
|
||||
|
||||
normalize_extensions(input_dir)
|
||||
|
||||
master_output_dir = os.path.join(input_dir, "Split_Output")
|
||||
|
||||
os.makedirs(master_output_dir, exist_ok=True)
|
||||
|
||||
for root, dirs, files in os.walk(input_dir):
|
||||
|
||||
if os.path.abspath(root).startswith(os.path.abspath(master_output_dir)):
|
||||
continue
|
||||
|
||||
for filename in files:
|
||||
|
||||
if not filename.lower().endswith(".pgm"):
|
||||
continue
|
||||
|
||||
input_file = os.path.join(root, filename)
|
||||
|
||||
program_output_dir = os.path.join(master_output_dir, os.path.splitext(filename)[0])
|
||||
|
||||
split_file_by_key(input_file, program_output_dir)
|
||||
|
||||
remove_exact_duplicates(master_output_dir)
|
||||
|
||||
remove_geometry_duplicates(master_output_dir, tolerance=0.005)
|
||||
|
||||
generate_similarity_report(master_output_dir, tolerance=0.005)
|
||||
|
||||
print("All files processed.")
|
||||
Reference in new issue
Block a user