Created
October 19, 2025 12:19
-
-
Save catbraincell/74dfc0c24331339393d6ad213331a443 to your computer and use it in GitHub Desktop.
Remove tool information and file path from gerber & nc drill files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import re | |
| import fnmatch | |
| # Regex replacements for .g* files | |
| g_patterns = [ | |
| (re.compile(r'^(G04\s+#@!\s+TF\.GenerationSoftware)[^*]*\*$', re.MULTILINE), | |
| r'\1*'), | |
| (re.compile(r'^(G04\s+#@!\s+TF\.SameCoordinates)[^*]*\*$', re.MULTILINE), | |
| r'\1*') | |
| ] | |
| # Walk recursively | |
| for root, _, files in os.walk("."): | |
| for filename in files: | |
| filepath = os.path.join(root, filename) | |
| lowername = filename.lower() | |
| # Handle .g* files | |
| if fnmatch.fnmatch(lowername, "*.g*"): | |
| try: | |
| with open(filepath, "r", encoding="utf-8", errors="ignore") as f: | |
| content = f.read() | |
| new_content = content | |
| for pattern, replacement in g_patterns: | |
| new_content = pattern.sub(replacement, new_content) | |
| if new_content != content: | |
| with open(filepath, "w", encoding="utf-8") as f: | |
| f.write(new_content) | |
| print(f"Updated .g*: {filepath}") | |
| except Exception as e: | |
| print(f"Error processing {filepath}: {e}") | |
| # Handle .ldp files | |
| elif lowername.endswith(".ldp"): | |
| try: | |
| with open(filepath, "r", encoding="utf-8", errors="ignore") as f: | |
| lines = f.readlines() | |
| new_lines = [] | |
| modified = False | |
| for line in lines: | |
| if line.startswith("Layer Pairs Export File for PCB:"): | |
| prefix, path_part = line.split(":", 1) | |
| # strip whitespace and extract only filename | |
| pcb_path = path_part.strip() | |
| filename_only = os.path.basename(pcb_path) | |
| new_line = f"{prefix}: {filename_only}\n" | |
| new_lines.append(new_line) | |
| modified = True | |
| else: | |
| new_lines.append(line) | |
| if modified: | |
| with open(filepath, "w", encoding="utf-8") as f: | |
| f.writelines(new_lines) | |
| print(f"Updated .ldp: {filepath}") | |
| except Exception as e: | |
| print(f"Error processing {filepath}: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment