summaryrefslogtreecommitdiff
path: root/gerber/render/excellon_backend.py
blob: eb79f1b24a0dcd44127bad7471226dd778060455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from .render import GerberContext
from ..excellon_statements import *

class ExcellonContext(GerberContext):
    
    def __init__(self, settings):
        GerberContext.__init__(self)
        self.comments = []
        self.header = []
        self.tool_def = []
        self.body_start = [RewindStopStmt()]
        self.body = []
        self.start = [HeaderBeginStmt()]
        self.end = [EndOfProgramStmt()]
        
        self.handled_tools = set()
        self.cur_tool = None
        self._pos = (None, None)
        
        self.settings = settings

        self._start_header()
        self._start_comments()
        
    def _start_header(self):
        """Create the header from the settings"""
        
        self.header.append(UnitStmt.from_settings(self.settings))
        
    def _start_comments(self):
        
        # Write the digits used - this isn't valid Excellon statement, so we write as a comment
        self.comments.append(CommentStmt('FILE_FORMAT=%d:%d' % (self.settings.format[0], self.settings.format[1])))
        
    @property
    def statements(self):
        return self.start + self.comments + self.header + self.body_start + self.body + self.end
        
    def set_bounds(self, bounds):
        pass
    
    def _paint_background(self):
        pass
        
    def _render_line(self, line, color):
        raise ValueError('Invalid Excellon object')
    def _render_arc(self, arc, color):
        raise ValueError('Invalid Excellon object')

    def _render_region(self, region, color):
        raise ValueError('Invalid Excellon object')
        
    def _render_level_polarity(self, region):
        raise ValueError('Invalid Excellon object')

    def _render_circle(self, circle, color):
        raise ValueError('Invalid Excellon object')

    def _render_rectangle(self, rectangle, color):
        raise ValueError('Invalid Excellon object')
        
    def _render_obround(self, obround, color):
        raise ValueError('Invalid Excellon object')
        
    def _render_polygon(self, polygon, color):
        raise ValueError('Invalid Excellon object')
    
    def _simplify_point(self, point):
        return (point[0] if point[0] != self._pos[0] else None, point[1] if point[1] != self._pos[1] else None)

    def _render_drill(self, drill, color):
        
        tool = drill.hit.tool
        if not tool in self.handled_tools:
            self.handled_tools.add(tool)
            self.header.append(ExcellonTool.from_tool(tool))
    
        if tool != self.cur_tool:
            self.body.append(ToolSelectionStmt(tool.number))
            self.cur_tool = tool
            
        point = self._simplify_point(drill.position)
        self._pos = drill.position
        self.body.append(CoordinateStmt.from_point(point))
        
    def _render_slot(self, slot, color):
        
        tool = slot.hit.tool
        if not tool in self.handled_tools:
            self.handled_tools.add(tool)
            self.header.append(ExcellonTool.from_tool(tool))
    
        if tool != self.cur_tool:
            self.body.append(ToolSelectionStmt(tool.number))
            self.cur_tool = tool
            
        # Slots don't use simplified points
        self._pos = slot.end
        self.body.append(SlotStmt.from_points(slot.start, slot.end))

    def _render_inverted_layer(self):
        pass