From d69f50e0f62570a4c327cb8fe4f886f439196010 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Wed, 2 Dec 2015 12:44:30 +0800 Subject: Make the hit accessible from the drawable Hit, fix crash with cario drawing rect --- gerber/excellon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 708f50b..4ff2161 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -126,7 +126,7 @@ class ExcellonFile(CamFile): @property def primitives(self): - return [Drill(hit.position, hit.tool.diameter,units=self.settings.units) for hit in self.hits] + return [Drill(hit.position, hit.tool.diameter, hit, units=self.settings.units) for hit in self.hits] @property -- cgit From 206f4c57ab66f8a6753015340315991b40178c9b Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Wed, 16 Dec 2015 18:59:25 +0800 Subject: Fix drawing arcs. Dont crash for arcs with rectangular apertures. Fix crash with board size of zero for only one drill --- gerber/excellon.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 4ff2161..85821e5 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -634,7 +634,11 @@ def _layer_size_score(size, hole_count, hole_area): Lower is better. """ board_area = size[0] * size[1] + if board_area == 0: + return 0 + hole_percentage = hole_area / board_area hole_score = (hole_percentage - 0.25) ** 2 size_score = (board_area - 8) **2 return hole_score * size_score + \ No newline at end of file -- cgit From 4e838df32ac6d283429e30d2a3151b7d7e8e82b2 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sat, 19 Dec 2015 11:44:12 +0800 Subject: Parse misc nc drill files --- gerber/excellon.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 85821e5..3fb813f 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -32,6 +32,7 @@ except(ImportError): from io import StringIO from .excellon_statements import * +from .excellon_tool import ExcellonToolDefinitionParser from .cam import CamFile, FileSettings from .primitives import Drill from .utils import inch, metric @@ -56,12 +57,15 @@ def read(filename): settings = FileSettings(**detect_excellon_format(data)) return ExcellonParser(settings).parse(filename) -def loads(data): +def loads(data, settings = None, tools = None): """ Read data from string and return an ExcellonFile Parameters ---------- data : string string containing Excellon file contents + + tools: dict (optional) + externally defined tools Returns ------- @@ -70,8 +74,9 @@ def loads(data): """ # File object should use settings from source file by default. - settings = FileSettings(**detect_excellon_format(data)) - return ExcellonParser(settings).parse_raw(data) + if not settings: + settings = FileSettings(**detect_excellon_format(data)) + return ExcellonParser(settings, tools).parse_raw(data) class DrillHit(object): @@ -199,7 +204,7 @@ class ExcellonFile(CamFile): for primitive in self.primitives: primitive.to_inch() for hit in self.hits: - hit.position = tuple(map(inch, hit,position)) + hit.position = tuple(map(inch, hit.position)) def to_metric(self): @@ -282,7 +287,7 @@ class ExcellonParser(object): settings : FileSettings or dict-like Excellon file settings to use when interpreting the excellon file. """ - def __init__(self, settings=None): + def __init__(self, settings=None, ext_tools=None): self.notation = 'absolute' self.units = 'inch' self.zeros = 'leading' @@ -290,6 +295,8 @@ class ExcellonParser(object): self.state = 'INIT' self.statements = [] self.tools = {} + self.ext_tools = ext_tools or {} + self.comment_tools = {} self.hits = [] self.active_tool = None self.pos = [0., 0.] @@ -352,6 +359,18 @@ class ExcellonParser(object): detected_format = tuple([int(x) for x in comment_stmt.comment.split('=')[1].split(":")]) if detected_format: self.format = detected_format + + if "HEADER:" in comment_stmt.comment: + self.state = "HEADER" + + if " Holesize " in comment_stmt.comment: + self.state = "HEADER" + + # Parse this as a hole definition + tools = ExcellonToolDefinitionParser(self._settings()).parse_raw(comment_stmt.comment) + if len(tools) == 1: + tool = tools[tools.keys()[0]] + self.comment_tools[tool.number] = tool elif line[:3] == 'M48': self.statements.append(HeaderBeginStmt()) @@ -363,6 +382,16 @@ class ExcellonParser(object): self.state = 'DRILL' elif self.state == 'INIT': self.state = 'HEADER' + + elif line[:3] == 'M00' and self.state == 'DRILL': + if self.active_tool: + cur_tool_number = self.active_tool.number + next_tool = self._get_tool(cur_tool_number + 1) + + self.statements.append(NextToolSelectionStmt(self.active_tool, next_tool)) + self.active_tool = next_tool + else: + raise Exception('Invalid state exception') elif line[:3] == 'M95': self.statements.append(HeaderEndStmt()) @@ -480,8 +509,10 @@ class ExcellonParser(object): # T0 is used as END marker, just ignore if stmt.tool != 0: - # FIXME: for weird files with no tools defined, original calc from gerbv - if stmt.tool not in self.tools: + tool = self._get_tool(stmt.tool) + + if not tool: + # FIXME: for weird files with no tools defined, original calc from gerbv if self._settings().units == "inch": diameter = (16 + 8 * stmt.tool) / 1000.0; else: @@ -496,7 +527,7 @@ class ExcellonParser(object): self.statements.insert(i, tool) break - self.active_tool = self.tools[stmt.tool] + self.active_tool = tool elif line[0] == 'R' and self.state != 'HEADER': stmt = RepeatHoleStmt.from_excellon(line, self._settings()) @@ -523,6 +554,9 @@ class ExcellonParser(object): if y is not None: self.pos[1] += y if self.state == 'DRILL': + if not self.active_tool: + self.active_tool = self._get_tool(1) + self.hits.append(DrillHit(self.active_tool, tuple(self.pos))) self.active_tool._hit() else: @@ -531,7 +565,23 @@ class ExcellonParser(object): def _settings(self): return FileSettings(units=self.units, format=self.format, zeros=self.zeros, notation=self.notation) - + + def _get_tool(self, toolid): + + tool = self.tools.get(toolid) + if not tool: + tool = self.comment_tools.get(toolid) + if tool: + tool.settings = self._settings() + self.tools[toolid] = tool + + if not tool: + tool = self.ext_tools.get(toolid) + if tool: + tool.settings = self._settings() + self.tools[toolid] = tool + + return tool def detect_excellon_format(data=None, filename=None): """ Detect excellon file decimal format and zero-suppression settings. -- cgit From 2e42d1a4705f8cf30a9ae1f987567ce97a39ae11 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Wed, 30 Dec 2015 16:11:25 +0800 Subject: Support KiCad format statement where FMAT,2 is 2:4 with inch --- gerber/excellon.py | 1 + 1 file changed, 1 insertion(+) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 3fb813f..cdd6d8d 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -480,6 +480,7 @@ class ExcellonParser(object): elif line[:4] == 'FMAT': stmt = FormatStmt.from_excellon(line) self.statements.append(stmt) + self.format = stmt.format_tuple elif line[:3] == 'G40': self.statements.append(CutterCompensationOffStmt()) -- cgit From 60784dfa2107f72fcaeed739b835d647e4c3a7a9 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sat, 16 Jan 2016 18:33:40 +0800 Subject: Skip over a strange excellon statement --- gerber/excellon.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index cdd6d8d..4317e41 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -500,9 +500,12 @@ class ExcellonParser(object): self.statements.append(infeed_rate_stmt) elif line[0] == 'T' and self.state == 'HEADER': - tool = ExcellonTool.from_excellon(line, self._settings()) - self.tools[tool.number] = tool - self.statements.append(tool) + if not ',OFF' in line and not ',ON' in line: + tool = ExcellonTool.from_excellon(line, self._settings()) + self.tools[tool.number] = tool + self.statements.append(tool) + else: + self.statements.append(UnknownStmt.from_excellon(line)) elif line[0] == 'T' and self.state != 'HEADER': stmt = ToolSelectionStmt.from_excellon(line) -- cgit From e84f131720e5952ba0dc20de8729bfd1d7aa0fe7 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sun, 31 Jan 2016 14:17:35 +0800 Subject: Add support for more excellon formats. Dont consider line width when determinging region bounding box --- gerber/excellon.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 4317e41..4456329 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -461,6 +461,8 @@ class ExcellonParser(object): stmt = UnitStmt.from_excellon(line) self.units = stmt.units self.zeros = stmt.zeros + if stmt.format: + self.format = stmt.format self.statements.append(stmt) elif line[:3] == 'M71' or line [:3] == 'M72': -- cgit From 7053d320f0b3e9404edb4c05710001ea58d44995 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sun, 13 Mar 2016 14:27:09 +0800 Subject: Better detection of plated tools --- gerber/excellon.py | 61 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 17 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 4456329..0637b23 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -175,21 +175,12 @@ class ExcellonFile(CamFile): def write(self, filename=None): filename = filename if filename is not None else self.filename with open(filename, 'w') as f: - - # Copy the header verbatim - for statement in self.statements: - if not isinstance(statement, ToolSelectionStmt): - f.write(statement.to_excellon(self.settings) + '\n') - else: - break - - # Write out coordinates for drill hits by tool - for tool in iter(self.tools.values()): - f.write(ToolSelectionStmt(tool.number).to_excellon(self.settings) + '\n') - for hit in self.hits: - if hit.tool.number == tool.number: - f.write(CoordinateStmt(*hit.position).to_excellon(self.settings) + '\n') - f.write(EndOfProgramStmt().to_excellon() + '\n') + self.writes(f) + + def writes(self, f): + # Copy the header verbatim + for statement in self.statements: + f.write(statement.to_excellon(self.settings) + '\n') def to_inch(self): """ @@ -300,6 +291,8 @@ class ExcellonParser(object): self.hits = [] self.active_tool = None self.pos = [0., 0.] + # Default for lated is None, which means we don't know + self.plated = ExcellonTool.PLATED_UNKNOWN if settings is not None: self.units = settings.units self.zeros = settings.zeros @@ -360,6 +353,12 @@ class ExcellonParser(object): if detected_format: self.format = detected_format + if "TYPE=PLATED" in comment_stmt.comment: + self.plated = ExcellonTool.PLATED_YES + + if "TYPE=NON_PLATED" in comment_stmt.comment: + self.plated = ExcellonTool.PLATED_NO + if "HEADER:" in comment_stmt.comment: self.state = "HEADER" @@ -370,7 +369,7 @@ class ExcellonParser(object): tools = ExcellonToolDefinitionParser(self._settings()).parse_raw(comment_stmt.comment) if len(tools) == 1: tool = tools[tools.keys()[0]] - self.comment_tools[tool.number] = tool + self._add_comment_tool(tool) elif line[:3] == 'M48': self.statements.append(HeaderBeginStmt()) @@ -503,7 +502,8 @@ class ExcellonParser(object): elif line[0] == 'T' and self.state == 'HEADER': if not ',OFF' in line and not ',ON' in line: - tool = ExcellonTool.from_excellon(line, self._settings()) + tool = ExcellonTool.from_excellon(line, self._settings(), None, self.plated) + self._merge_properties(tool) self.tools[tool.number] = tool self.statements.append(tool) else: @@ -572,6 +572,33 @@ class ExcellonParser(object): return FileSettings(units=self.units, format=self.format, zeros=self.zeros, notation=self.notation) + def _add_comment_tool(self, tool): + """ + Add a tool that was defined in the comments to this file. + + If we have already found this tool, then we will merge this comment tool definition into + the information for the tool + """ + + existing = self.tools.get(tool.number) + if existing and existing.plated == None: + existing.plated = tool.plated + + self.comment_tools[tool.number] = tool + + def _merge_properties(self, tool): + """ + When we have externally defined tools, merge the properties of that tool into this one + + For now, this is only plated + """ + + if tool.plated == ExcellonTool.PLATED_UNKNOWN: + ext_tool = self.ext_tools.get(tool.number) + + if ext_tool: + tool.plated = ext_tool.plated + def _get_tool(self, toolid): tool = self.tools.get(toolid) -- cgit From acde19f205898188c03a46e5d8a7a6a4d4637a2d Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sat, 26 Mar 2016 15:59:42 +0800 Subject: Support for the G85 slot statement --- gerber/excellon.py | 136 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 109 insertions(+), 27 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 0637b23..f9bb18a 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -34,7 +34,7 @@ except(ImportError): from .excellon_statements import * from .excellon_tool import ExcellonToolDefinitionParser from .cam import CamFile, FileSettings -from .primitives import Drill +from .primitives import Drill, Slot from .utils import inch, metric @@ -93,6 +93,51 @@ class DrillHit(object): if self.tool.units == 'inch': self.tool.to_metric() self.position = tuple(map(metric, self.position)) + + @property + def bounding_box(self): + position = self.position + radius = self.tool.diameter / 2. + + min_x = position[0] - radius + max_x = position[0] + radius + min_y = position[1] - radius + max_y = position[1] + radius + return ((min_x, max_x), (min_y, max_y)) + + +class DrillSlot(object): + """ + A slot is created between two points. The way the slot is created depends on the statement used to create it + """ + + def __init__(self, tool, start, end): + self.tool = tool + self.start = start + self.end = end + + def to_inch(self): + if self.tool.units == 'metric': + self.tool.to_inch() + self.start = tuple(map(inch, self.start)) + self.end = tuple(map(inch, self.end)) + + def to_metric(self): + if self.tool.units == 'inch': + self.tool.to_metric() + self.start = tuple(map(metric, self.start)) + self.end = tuple(map(metric, self.end)) + + @property + def bounding_box(self): + start = self.start + end = self.end + radius = self.tool.diameter / 2. + min_x = min(start[0], end[0]) - radius + max_x = max(start[0], end[0]) + radius + min_y = min(start[1], end[1]) - radius + max_y = max(start[1], end[1]) + radius + return ((min_x, max_x), (min_y, max_y)) class ExcellonFile(CamFile): @@ -131,7 +176,17 @@ class ExcellonFile(CamFile): @property def primitives(self): - return [Drill(hit.position, hit.tool.diameter, hit, units=self.settings.units) for hit in self.hits] + + primitives = [] + for hit in self.hits: + if isinstance(hit, DrillHit): + primitives.append(Drill(hit.position, hit.tool.diameter, hit, units=self.settings.units)) + elif isinstance(hit, DrillSlot): + primitives.append(Slot(hit.start, hit.end, hit.tool.diameter, hit, units=self.settings.units)) + else: + raise ValueError('Unknown hit type') + + return primitives @property @@ -139,12 +194,11 @@ class ExcellonFile(CamFile): xmin = ymin = 100000000000 xmax = ymax = -100000000000 for hit in self.hits: - radius = hit.tool.diameter / 2. - x, y = hit.position - xmin = min(x - radius, xmin) - xmax = max(x + radius, xmax) - ymin = min(y - radius, ymin) - ymax = max(y + radius, ymax) + bbox = hit.bounding_box + xmin = min(bbox[0][0], xmin) + xmax = max(bbox[0][1], xmax) + ymin = min(bbox[1][0], ymin) + ymax = max(bbox[1][1], ymax) return ((xmin, xmax), (ymin, ymax)) def report(self, filename=None): @@ -545,26 +599,54 @@ class ExcellonParser(object): self.active_tool._hit() elif line[0] in ['X', 'Y']: - stmt = CoordinateStmt.from_excellon(line, self._settings()) - x = stmt.x - y = stmt.y - self.statements.append(stmt) - if self.notation == 'absolute': - if x is not None: - self.pos[0] = x - if y is not None: - self.pos[1] = y - else: - if x is not None: - self.pos[0] += x - if y is not None: - self.pos[1] += y - if self.state == 'DRILL': - if not self.active_tool: - self.active_tool = self._get_tool(1) + if 'G85' in line: + stmt = SlotStmt.from_excellon(line, self._settings()) - self.hits.append(DrillHit(self.active_tool, tuple(self.pos))) - self.active_tool._hit() + # I don't know if this is actually correct, but it makes sense that this is where the tool would end + x = stmt.x_end + y = stmt.y_end + + self.statements.append(stmt) + + if self.notation == 'absolute': + if x is not None: + self.pos[0] = x + if y is not None: + self.pos[1] = y + else: + if x is not None: + self.pos[0] += x + if y is not None: + self.pos[1] += y + + if self.state == 'DRILL': + if not self.active_tool: + self.active_tool = self._get_tool(1) + + self.hits.append(DrillSlot(self.active_tool, (stmt.x_start, stmt.y_start), (stmt.x_end, stmt.y_end))) + self.active_tool._hit() + else: + stmt = CoordinateStmt.from_excellon(line, self._settings()) + x = stmt.x + y = stmt.y + self.statements.append(stmt) + if self.notation == 'absolute': + if x is not None: + self.pos[0] = x + if y is not None: + self.pos[1] = y + else: + if x is not None: + self.pos[0] += x + if y is not None: + self.pos[1] += y + + if self.state == 'DRILL': + if not self.active_tool: + self.active_tool = self._get_tool(1) + + self.hits.append(DrillHit(self.active_tool, tuple(self.pos))) + self.active_tool._hit() else: self.statements.append(UnknownStmt.from_excellon(line)) -- cgit From 25515b8ec7016698431b74e5beac8ff2d6691f0b Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sat, 26 Mar 2016 18:18:16 +0800 Subject: Correctly render M15 slot holes --- gerber/excellon.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index f9bb18a..02709fd 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -345,6 +345,7 @@ class ExcellonParser(object): self.hits = [] self.active_tool = None self.pos = [0., 0.] + self.drill_down = False # Default for lated is None, which means we don't know self.plated = ExcellonTool.PLATED_UNKNOWN if settings is not None: @@ -453,12 +454,15 @@ class ExcellonParser(object): elif line[:3] == 'M15': self.statements.append(ZAxisRoutPositionStmt()) + self.drill_down = True elif line[:3] == 'M16': self.statements.append(RetractWithClampingStmt()) + self.drill_down = False elif line[:3] == 'M17': self.statements.append(RetractWithoutClampingStmt()) + self.drill_down = False elif line[:3] == 'M30': stmt = EndOfProgramStmt.from_excellon(line, self._settings()) @@ -491,6 +495,9 @@ class ExcellonParser(object): stmt = CoordinateStmt.from_excellon(line[3:], self._settings()) stmt.mode = self.state + + # The start position is where we were before the rout command + start = (self.pos[0], self.pos[1]) x = stmt.x y = stmt.y @@ -505,9 +512,20 @@ class ExcellonParser(object): self.pos[0] += x if y is not None: self.pos[1] += y - + + # Our ending position + end = (self.pos[0], self.pos[1]) + + if self.drill_down: + if not self.active_tool: + self.active_tool = self._get_tool(1) + + self.hits.append(DrillSlot(self.active_tool, start, end)) + self.active_tool._hit() + elif line[:3] == 'G05': self.statements.append(DrillModeStmt()) + self.drill_down = False self.state = 'DRILL' elif 'INCH' in line or 'METRIC' in line: -- cgit From 288f49955ecc1a811752aa4b1e713f9954e3033b Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sun, 27 Mar 2016 14:24:11 +0800 Subject: Actually fix the rout rendering to be correct --- gerber/excellon.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 02709fd..72cf75c 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -111,10 +111,14 @@ class DrillSlot(object): A slot is created between two points. The way the slot is created depends on the statement used to create it """ - def __init__(self, tool, start, end): + TYPE_ROUT = 1 + TYPE_G85 = 2 + + def __init__(self, tool, start, end, slot_type): self.tool = tool self.start = start self.end = end + self.slot_type = slot_type def to_inch(self): if self.tool.units == 'metric': @@ -520,7 +524,7 @@ class ExcellonParser(object): if not self.active_tool: self.active_tool = self._get_tool(1) - self.hits.append(DrillSlot(self.active_tool, start, end)) + self.hits.append(DrillSlot(self.active_tool, start, end, DrillSlot.TYPE_ROUT)) self.active_tool._hit() elif line[:3] == 'G05': @@ -641,7 +645,7 @@ class ExcellonParser(object): if not self.active_tool: self.active_tool = self._get_tool(1) - self.hits.append(DrillSlot(self.active_tool, (stmt.x_start, stmt.y_start), (stmt.x_end, stmt.y_end))) + self.hits.append(DrillSlot(self.active_tool, (stmt.x_start, stmt.y_start), (stmt.x_end, stmt.y_end), DrillSlot.TYPE_G85)) self.active_tool._hit() else: stmt = CoordinateStmt.from_excellon(line, self._settings()) -- cgit From 2eac1e427ca3264cb6dc36e0712020c1ca73fa9c Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Tue, 5 Apr 2016 22:40:12 +0800 Subject: Fix converting values for excellon files. Give error for incremental mode --- gerber/excellon.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 72cf75c..09636aa 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -85,14 +85,10 @@ class DrillHit(object): self.position = position def to_inch(self): - if self.tool.units == 'metric': - self.tool.to_inch() - self.position = tuple(map(inch, self.position)) + self.position = tuple(map(inch, self.position)) def to_metric(self): - if self.tool.units == 'inch': - self.tool.to_metric() - self.position = tuple(map(metric, self.position)) + self.position = tuple(map(metric, self.position)) @property def bounding_box(self): @@ -121,16 +117,12 @@ class DrillSlot(object): self.slot_type = slot_type def to_inch(self): - if self.tool.units == 'metric': - self.tool.to_inch() - self.start = tuple(map(inch, self.start)) - self.end = tuple(map(inch, self.end)) + self.start = tuple(map(inch, self.start)) + self.end = tuple(map(inch, self.end)) def to_metric(self): - if self.tool.units == 'inch': - self.tool.to_metric() - self.start = tuple(map(metric, self.start)) - self.end = tuple(map(metric, self.end)) + self.start = tuple(map(metric, self.start)) + self.end = tuple(map(metric, self.end)) @property def bounding_box(self): @@ -253,7 +245,7 @@ class ExcellonFile(CamFile): for primitive in self.primitives: primitive.to_inch() for hit in self.hits: - hit.position = tuple(map(inch, hit.position)) + hit.to_inch() def to_metric(self): @@ -268,7 +260,7 @@ class ExcellonFile(CamFile): for primitive in self.primitives: primitive.to_metric() for hit in self.hits: - hit.position = tuple(map(metric, hit.position)) + hit.to_metric() def offset(self, x_offset=0, y_offset=0): for statement in self.statements: -- cgit From f1f07d74c41ad74be2b0bbad4cfcd1c6e5923678 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Tue, 10 May 2016 23:16:51 +0800 Subject: Offset of drill hit and slots --- gerber/excellon.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 09636aa..9a69042 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -100,7 +100,9 @@ class DrillHit(object): min_y = position[1] - radius max_y = position[1] + radius return ((min_x, max_x), (min_y, max_y)) - + + def offset(self, x_offset, y_offset): + self.position = tuple(map(operator.add, self.position, (x_offset, y_offset))) class DrillSlot(object): """ @@ -134,6 +136,10 @@ class DrillSlot(object): min_y = min(start[1], end[1]) - radius max_y = max(start[1], end[1]) + radius return ((min_x, max_x), (min_y, max_y)) + + def offset(self, x_offset, y_offset): + self.start = tuple(map(operator.add, self.start, (x_offset, y_offset))) + self.end = tuple(map(operator.add, self.end, (x_offset, y_offset))) class ExcellonFile(CamFile): @@ -268,7 +274,7 @@ class ExcellonFile(CamFile): for primitive in self.primitives: primitive.offset(x_offset, y_offset) for hit in self. hits: - hit.position = tuple(map(operator.add, hit.position, (x_offset, y_offset))) + hit.offset(x_offset, y_offset) def path_length(self, tool_number=None): """ Return the path length for a given tool -- cgit From 8f4b439efcc4dccd327a8fb95ce3bbb6d16adbcf Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Mon, 6 Jun 2016 22:26:06 +0800 Subject: Rout mode doesn't need to specify G01 every time --- gerber/excellon.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 9a69042..a0a639e 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -498,7 +498,7 @@ class ExcellonParser(object): stmt = CoordinateStmt.from_excellon(line[3:], self._settings()) stmt.mode = self.state - # The start position is where we were before the rout command + # The start position is where we were before the rout command start = (self.pos[0], self.pos[1]) x = stmt.x @@ -647,6 +647,10 @@ class ExcellonParser(object): self.active_tool._hit() else: stmt = CoordinateStmt.from_excellon(line, self._settings()) + + # We need this in case we are in rout mode + start = (self.pos[0], self.pos[1]) + x = stmt.x y = stmt.y self.statements.append(stmt) @@ -667,6 +671,13 @@ class ExcellonParser(object): self.hits.append(DrillHit(self.active_tool, tuple(self.pos))) self.active_tool._hit() + + elif self.state == 'LINEAR' and self.drill_down: + if not self.active_tool: + self.active_tool = self._get_tool(1) + + self.hits.append(DrillSlot(self.active_tool, start, tuple(self.pos), DrillSlot.TYPE_ROUT)) + else: self.statements.append(UnknownStmt.from_excellon(line)) -- cgit From 7e06f3a2f5870d4878f25e391372285263fe5ac6 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sun, 10 Jul 2016 15:41:31 +0800 Subject: Workaround for bad excellon files that don't correctly set the mode --- gerber/excellon.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index a0a639e..becf82d 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -665,19 +665,21 @@ class ExcellonParser(object): if y is not None: self.pos[1] += y - if self.state == 'DRILL': + if self.state == 'LINEAR' and self.drill_down: + if not self.active_tool: + self.active_tool = self._get_tool(1) + + self.hits.append(DrillSlot(self.active_tool, start, tuple(self.pos), DrillSlot.TYPE_ROUT)) + + elif self.state == 'DRILL' or self.state == 'HEADER': + # Yes, drills in the header doesn't follow the specification, but it there are many + # files like this if not self.active_tool: self.active_tool = self._get_tool(1) self.hits.append(DrillHit(self.active_tool, tuple(self.pos))) self.active_tool._hit() - elif self.state == 'LINEAR' and self.drill_down: - if not self.active_tool: - self.active_tool = self._get_tool(1) - - self.hits.append(DrillSlot(self.active_tool, start, tuple(self.pos), DrillSlot.TYPE_ROUT)) - else: self.statements.append(UnknownStmt.from_excellon(line)) -- cgit From 10c7075ad5fc05907e53036b2e308cfc372476c7 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Mon, 11 Jul 2016 23:18:15 +0800 Subject: Allow G85 for invalid files --- gerber/excellon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index becf82d..65e676b 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -639,7 +639,7 @@ class ExcellonParser(object): if y is not None: self.pos[1] += y - if self.state == 'DRILL': + if self.state == 'DRILL' or self.state == 'HEADER': if not self.active_tool: self.active_tool = self._get_tool(1) -- cgit From 7a79d1504e348251740efe622b4018cc26ffcd59 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sat, 16 Jul 2016 14:22:38 +0800 Subject: Setup .gitignore for Eclipse. Start creating doc strings --- gerber/excellon.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index 65e676b..bcd136e 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -80,6 +80,16 @@ def loads(data, settings = None, tools = None): class DrillHit(object): + """Drill feature that is a single drill hole. + + Attributes + ---------- + tool : ExcellonTool + Tool to drill the hole. Defines the size of the hole that is generated. + position : tuple(float, float) + Center position of the drill. + + """ def __init__(self, tool, position): self.tool = tool self.position = position -- cgit From 52c6d4928a1b5fc65b95cf5b0784a560cec2ca1d Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sat, 16 Jul 2016 15:49:48 +0800 Subject: Fix most broken tests so that I can safely merge into changes with known expected test result --- gerber/excellon.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index bcd136e..430ee7d 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -113,6 +113,9 @@ class DrillHit(object): def offset(self, x_offset, y_offset): self.position = tuple(map(operator.add, self.position, (x_offset, y_offset))) + + def __str__(self): + return 'Hit (%f, %f) {%s}' % (self.position[0], self.position[1], self.tool) class DrillSlot(object): """ -- cgit From 8cd842a41a55ab3d8f558a2e3e198beba7da58a1 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Thu, 21 Jan 2016 03:57:44 -0500 Subject: Manually mere rendering changes --- gerber/excellon.py | 94 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 40 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index a0bad4f..a5da42a 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -81,7 +81,7 @@ def loads(data, settings = None, tools = None): return ExcellonParser(settings, tools).parse_raw(data) -class DrillHit(object): +class DrillHit(object): """Drill feature that is a single drill hole. Attributes @@ -92,6 +92,7 @@ class DrillHit(object): Center position of the drill. """ + def __init__(self, tool, position): self.tool = tool self.position = position @@ -184,6 +185,7 @@ class ExcellonFile(CamFile): either 'inch' or 'metric'. """ + def __init__(self, statements, tools, hits, settings, filename=None): super(ExcellonFile, self).__init__(statements=statements, settings=settings, @@ -193,7 +195,9 @@ class ExcellonFile(CamFile): @property def primitives(self): - + """ + Gets the primitives. Note that unlike Gerber, this generates new objects + """ primitives = [] for hit in self.hits: if isinstance(hit, DrillHit): @@ -203,8 +207,7 @@ class ExcellonFile(CamFile): else: raise ValueError('Unknown hit type') - return primitives - + return primitives @property def bounds(self): @@ -237,7 +240,8 @@ class ExcellonFile(CamFile): rprt += ' Code Size Hits Path Length\n' rprt += ' --------------------------------------\n' for tool in iter(self.tools.values()): - rprt += toolfmt.format(tool.number, tool.diameter, tool.hit_count, self.path_length(tool.number)) + rprt += toolfmt.format(tool.number, tool.diameter, + tool.hit_count, self.path_length(tool.number)) if filename is not None: with open(filename, 'w') as f: f.write(rprt) @@ -245,13 +249,21 @@ class ExcellonFile(CamFile): def write(self, filename=None): filename = filename if filename is not None else self.filename - with open(filename, 'w') as f: - self.writes(f) - - def writes(self, f): - # Copy the header verbatim - for statement in self.statements: - f.write(statement.to_excellon(self.settings) + '\n') + with open(filename, 'w') as f: + for statement in self.statements: + if not isinstance(statement, ToolSelectionStmt): + f.write(statement.to_excellon(self.settings) + '\n') + else: + break + + # Write out coordinates for drill hits by tool + for tool in iter(self.tools.values()): + f.write(ToolSelectionStmt(tool.number).to_excellon(self.settings) + '\n') + for hit in self.hits: + if hit.tool.number == tool.number: + f.write(CoordinateStmt( + *hit.position).to_excellon(self.settings) + '\n') + f.write(EndOfProgramStmt().to_excellon() + '\n') def to_inch(self): """ @@ -265,9 +277,8 @@ class ExcellonFile(CamFile): tool.to_inch() for primitive in self.primitives: primitive.to_inch() - for hit in self.hits: - hit.to_inch() - + for hit in self.hits: + hit.to_inch() def to_metric(self): """ Convert units to metric @@ -288,8 +299,8 @@ class ExcellonFile(CamFile): statement.offset(x_offset, y_offset) for primitive in self.primitives: primitive.offset(x_offset, y_offset) - for hit in self. hits: - hit.offset(x_offset, y_offset) + for hit in self. hits: + hit.offset(x_offset, y_offset) def path_length(self, tool_number=None): """ Return the path length for a given tool @@ -299,9 +310,11 @@ class ExcellonFile(CamFile): for hit in self.hits: tool = hit.tool num = tool.number - positions[num] = (0, 0) if positions.get(num) is None else positions[num] + positions[num] = (0, 0) if positions.get( + num) is None else positions[num] lengths[num] = 0.0 if lengths.get(num) is None else lengths[num] - lengths[num] = lengths[num] + math.hypot(*tuple(map(operator.sub, positions[num], hit.position))) + lengths[num] = lengths[ + num] + math.hypot(*tuple(map(operator.sub, positions[num], hit.position))) positions[num] = hit.position if tool_number is None: @@ -310,13 +323,13 @@ class ExcellonFile(CamFile): return lengths.get(tool_number) def hit_count(self, tool_number=None): - counts = {} - for tool in iter(self.tools.values()): - counts[tool.number] = tool.hit_count - if tool_number is None: - return counts - else: - return counts.get(tool_number) + counts = {} + for tool in iter(self.tools.values()): + counts[tool.number] = tool.hit_count + if tool_number is None: + return counts + else: + return counts.get(tool_number) def update_tool(self, tool_number, **kwargs): """ Change parameters of a tool @@ -340,7 +353,6 @@ class ExcellonFile(CamFile): hit.tool = newtool - class ExcellonParser(object): """ Excellon File Parser @@ -348,8 +360,8 @@ class ExcellonParser(object): ---------- settings : FileSettings or dict-like Excellon file settings to use when interpreting the excellon file. - """ - def __init__(self, settings=None, ext_tools=None): + """ + def __init__(self, settings=None, ext_tools=None): self.notation = 'absolute' self.units = 'inch' self.zeros = 'leading' @@ -371,7 +383,6 @@ class ExcellonParser(object): self.notation = settings.notation self.format = settings.format - @property def coordinates(self): return [(stmt.x, stmt.y) for stmt in self.statements if isinstance(stmt, CoordinateStmt)] @@ -421,7 +432,8 @@ class ExcellonParser(object): # get format from altium comment if "FILE_FORMAT" in comment_stmt.comment: - detected_format = tuple([int(x) for x in comment_stmt.comment.split('=')[1].split(":")]) + detected_format = tuple( + [int(x) for x in comment_stmt.comment.split('=')[1].split(":")]) if detected_format: self.format = detected_format @@ -553,7 +565,7 @@ class ExcellonParser(object): self.format = stmt.format self.statements.append(stmt) - elif line[:3] == 'M71' or line [:3] == 'M72': + elif line[:3] == 'M71' or line[:3] == 'M72': stmt = MeasuringModeStmt.from_excellon(line) self.units = stmt.units self.statements.append(stmt) @@ -603,20 +615,22 @@ class ExcellonParser(object): self.statements.append(stmt) # T0 is used as END marker, just ignore - if stmt.tool != 0: + if stmt.tool != 0: tool = self._get_tool(stmt.tool) if not tool: - # FIXME: for weird files with no tools defined, original calc from gerbv + # FIXME: for weird files with no tools defined, original calc from gerbv if self._settings().units == "inch": - diameter = (16 + 8 * stmt.tool) / 1000.0; + diameter = (16 + 8 * stmt.tool) / 1000.0 else: - diameter = metric((16 + 8 * stmt.tool) / 1000.0); + diameter = metric((16 + 8 * stmt.tool) / 1000.0) - tool = ExcellonTool(self._settings(), number=stmt.tool, diameter=diameter) + tool = ExcellonTool( + self._settings(), number=stmt.tool, diameter=diameter) self.tools[tool.number] = tool - # FIXME: need to add this tool definition inside header to make sure it is properly written + # FIXME: need to add this tool definition inside header to + # make sure it is properly written for i, s in enumerate(self.statements): if isinstance(s, ToolSelectionStmt) or isinstance(s, ExcellonTool): self.statements.insert(i, tool) @@ -787,7 +801,7 @@ def detect_excellon_format(data=None, filename=None): and 'FILE_FORMAT' in stmt.comment] detected_format = (tuple([int(val) for val in - format_comment[0].split('=')[1].split(':')]) + format_comment[0].split('=')[1].split(':')]) if len(format_comment) == 1 else None) detected_zeros = zero_statements[0] if len(zero_statements) == 1 else None @@ -852,6 +866,6 @@ def _layer_size_score(size, hole_count, hole_area): hole_percentage = hole_area / board_area hole_score = (hole_percentage - 0.25) ** 2 - size_score = (board_area - 8) **2 + size_score = (board_area - 8) ** 2 return hole_score * size_score \ No newline at end of file -- cgit From 5af19af190c1fb0f0c5be029d46d63e657dde4d9 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Thu, 21 Jan 2016 03:57:44 -0500 Subject: Commit partial merge so I can work on the plane --- gerber/excellon.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'gerber/excellon.py') diff --git a/gerber/excellon.py b/gerber/excellon.py index a5da42a..0626819 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -80,7 +80,7 @@ def loads(data, settings = None, tools = None): settings = FileSettings(**detect_excellon_format(data)) return ExcellonParser(settings, tools).parse_raw(data) - + class DrillHit(object): """Drill feature that is a single drill hole. @@ -91,8 +91,7 @@ class DrillHit(object): position : tuple(float, float) Center position of the drill. - """ - + """ def __init__(self, tool, position): self.tool = tool self.position = position @@ -194,7 +193,7 @@ class ExcellonFile(CamFile): self.hits = hits @property - def primitives(self): + def primitives(self): """ Gets the primitives. Note that unlike Gerber, this generates new objects """ @@ -262,7 +261,7 @@ class ExcellonFile(CamFile): for hit in self.hits: if hit.tool.number == tool.number: f.write(CoordinateStmt( - *hit.position).to_excellon(self.settings) + '\n') + *hit.position).to_excellon(self.settings) + '\n') f.write(EndOfProgramStmt().to_excellon() + '\n') def to_inch(self): @@ -276,7 +275,7 @@ class ExcellonFile(CamFile): for tool in iter(self.tools.values()): tool.to_inch() for primitive in self.primitives: - primitive.to_inch() + primitive.to_inch() for hit in self.hits: hit.to_inch() @@ -298,7 +297,7 @@ class ExcellonFile(CamFile): for statement in self.statements: statement.offset(x_offset, y_offset) for primitive in self.primitives: - primitive.offset(x_offset, y_offset) + primitive.offset(x_offset, y_offset) for hit in self. hits: hit.offset(x_offset, y_offset) @@ -359,7 +358,7 @@ class ExcellonParser(object): Parameters ---------- settings : FileSettings or dict-like - Excellon file settings to use when interpreting the excellon file. + Excellon file settings to use when interpreting the excellon file. """ def __init__(self, settings=None, ext_tools=None): self.notation = 'absolute' @@ -614,12 +613,12 @@ class ExcellonParser(object): stmt = ToolSelectionStmt.from_excellon(line) self.statements.append(stmt) - # T0 is used as END marker, just ignore + # T0 is used as END marker, just ignore if stmt.tool != 0: tool = self._get_tool(stmt.tool) if not tool: - # FIXME: for weird files with no tools defined, original calc from gerbv + # FIXME: for weird files with no tools defined, original calc from gerb if self._settings().units == "inch": diameter = (16 + 8 * stmt.tool) / 1000.0 else: -- cgit