From 0037195543b8bd30d003947d0151402f00e0a8fa Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 24 Mar 2023 00:12:50 +0100 Subject: Dedup both Excellon and Gerber tools during write --- gerbonara/excellon.py | 29 +++++++++++++++++++---------- gerbonara/rs274x.py | 14 +++++++++++--- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/gerbonara/excellon.py b/gerbonara/excellon.py index 657012b..fbbeb50 100755 --- a/gerbonara/excellon.py +++ b/gerbonara/excellon.py @@ -359,25 +359,34 @@ class ExcellonFile(CamFile): # Build tool index tool_map = { id(obj.tool): obj.tool for obj in self.objects } tools = sorted(tool_map.items(), key=lambda id_tool: (id_tool[1].plated, id_tool[1].diameter)) - tools = { tool_id: index for index, (tool_id, _tool) in enumerate(tools, start=1) } - # FIXME dedup tools mixed_plating = (len({ tool.plated for tool in tool_map.values() }) > 1) if mixed_plating: warnings.warn('Multiple plating values in same file. Will use non-standard Altium comment syntax to indicate hole plating.') - if tools and max(tools.values()) >= 100: - warnings.warn('More than 99 tools defined. Some programs may not like three-digit tool indices.', SyntaxWarning) + defined_tools = {} + tool_indices = {} + index = 1 + for tool_id, tool in tools: + xnc = tool.to_xnc(settings) + if (tool.plated, xnc) in defined_tools: + tool_indices[tool_id] = defined_tools[(tool.plated, xnc)] - for tool_id, index in tools.items(): - tool = tool_map[tool_id] - if mixed_plating: - yield ';TYPE=PLATED' if tool.plated else ';TYPE=NON_PLATED' - yield f'T{index:02d}' + tool.to_xnc(settings) + else: + if mixed_plating: + yield ';TYPE=PLATED' if tool.plated else ';TYPE=NON_PLATED' + + yield f'T{index:02d}' + xnc + + tool_indices[tool_id] = defined_tools[(tool.plated, xnc)] = index + index += 1 + + if index >= 100: + warnings.warn('More than 99 tools defined. Some programs may not like three-digit tool indices.', SyntaxWarning) yield '%' - ctx = ExcellonContext(settings, tools) + ctx = ExcellonContext(settings, tool_indices) # Export objects for obj in self.objects: diff --git a/gerbonara/rs274x.py b/gerbonara/rs274x.py index 0c50991..5956b53 100644 --- a/gerbonara/rs274x.py +++ b/gerbonara/rs274x.py @@ -247,7 +247,9 @@ class GerberFile(CamFile): processed_macros = set() aperture_map = {} - for number, aperture in enumerate(self.apertures, start=10): + defined_apertures = {} + number = 10 + for aperture in self.apertures: if isinstance(aperture, apertures.ApertureMacroInstance): macro_def = am_stmt(aperture._rotated().macro) @@ -255,9 +257,15 @@ class GerberFile(CamFile): processed_macros.add(macro_def) yield macro_def - yield f'%ADD{number}{aperture.to_gerber(settings)}*%' + ap_def = aperture.to_gerber(settings) + if ap_def in defined_apertures: + aperture_map[id(aperture)] = defined_apertures[ap_def] - aperture_map[id(aperture)] = number + else: + yield f'%ADD{number}{ap_def}*%' + defined_apertures[ap_def] = number + aperture_map[id(aperture)] = number + number += 1 def warn(msg, kls=SyntaxWarning): warnings.warn(msg, kls) -- cgit