aboutsummaryrefslogtreecommitdiff
path: root/gerberex/composition.py
blob: 7f691f5d35db590a62edcdd034dc010466a35dec (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com>
import os
from functools import reduce
from gerber.cam import FileSettings
from gerber.gerber_statements import EofStmt
from gerber.excellon_statements import *
from gerber.excellon import DrillSlot, DrillHit
import gerberex.rs274x
import gerberex.excellon
import gerberex.dxf

class Composition(object):
    def __init__(self, settings = None, comments = None):
        self.settings = settings
        self.comments = comments if comments != None else []

class GerberComposition(Composition):
    APERTURE_ID_BIAS = 10

    def __init__(self, settings=None, comments=None):
        super(GerberComposition, self).__init__(settings, comments)
        self.param_statements = []
        self.aperture_macros = {}
        self.apertures = []
        self.drawings = []

    def merge(self, file):
        if isinstance(file, gerberex.rs274x.GerberFile):
            self._merge_gerber(file)
        elif isinstance(file, gerberex.dxf.DxfFile):
            self._merge_dxf(file)
        else:
            raise Exception('unsupported file type')

    def dump(self, path):
        def statements():
            for s in self.param_statements:
                yield s
            for k in self.aperture_macros:
                yield self.aperture_macros[k]
            for s in self.apertures:
                yield s
            for s in self.drawings:
                yield s
            yield EofStmt()
        with open(path, 'w') as f:
            for statement in statements():
                f.write(statement.to_gerber(self.settings) + '\n')

    def _merge_gerber(self, file):
        param_statements = []
        aperture_macro_map = {}
        aperture_map = {}

        if self.settings:
            if self.settings.units == 'metric':
                file.to_metric()
            else:
                file.to_inch()

        for statement in file.statements:
            if statement.type == 'COMMENT':
                self.comments.append(statement.comment)
            elif statement.type == 'PARAM':
                if statement.param == 'AM':
                    name = statement.name
                    newname = self._register_aperture_macro(statement)
                    aperture_macro_map[name] = newname
                elif statement.param == 'AD':
                    if not statement.shape in ['C', 'R', 'O']:
                        statement.shape = aperture_macro_map[statement.shape]
                    dnum = statement.d
                    newdnum = self._register_aperture(statement)
                    aperture_map[dnum] = newdnum
                elif statement.param == 'LP':
                    self.drawings.append(statement)
                else:
                    param_statements.append(statement)
            elif statement.type in ['EOF', "DEPRECATED"]:
                pass
            else:
                if statement.type == 'APERTURE':
                    statement.d = aperture_map[statement.d]
                self.drawings.append(statement)
        
        if not self.settings:
            self.settings = file.settings
            self.param_statements = param_statements

    def _merge_dxf(self, file):
        if self.settings:
            if self.settings.units == 'metric':
                file.to_metric()
            else:
                file.to_inch()

        file.dcode = self._register_aperture(file.aperture)
        self.drawings.append(file.statements)

        if not self.settings:
            self.settings = file.settings
            self.param_statements = [file.header]


    def _register_aperture_macro(self, statement):
        name = statement.name
        newname = name
        offset = 0
        while newname in self.aperture_macros:
            offset += 1
            newname = '%s_%d' % (name, offset)
        statement.name = newname
        self.aperture_macros[newname] = statement
        return newname

    def _register_aperture(self, statement):
        statement.d = len(self.apertures) + self.APERTURE_ID_BIAS
        self.apertures.append(statement)
        return statement.d

class DrillComposition(Composition):
    def __init__(self, settings=None, comments=None):
        super(DrillComposition, self).__init__(settings, comments)
        self.header1_statements = []
        self.header2_statements = []
        self.tools = []
        self.hits = []
        self.dxf_statements = []
    
    def merge(self, file):
        if isinstance(file, gerberex.excellon.ExcellonFileEx):
            self._merge_excellon(file)
        elif isinstance(file, gerberex.DxfFile):
            self._merge_dxf(file)
        else:
            raise Exception('unsupported file type')

    def dump(self, path):
        def statements():
            for s in self.header1_statements:
                yield s.to_excellon(self.settings)
            for t in self.tools:
                yield t.to_excellon(self.settings)
            for s in self.header2_statements:
                yield s.to_excellon(self.settings)
            for t in self.tools:
                yield ToolSelectionStmt(t.number).to_excellon(self.settings)
                for h in self.hits:
                    if h.tool.number == t.number:
                        yield h.to_excellon(self.settings)
                for num, statement in self.dxf_statements:
                    if num == t.number:
                        yield statement.to_excellon(self.settings)
            yield EndOfProgramStmt().to_excellon()
        
        with open(path, 'w') as f:
            for statement in statements():
                f.write(statement + '\n')

    def _merge_excellon(self, file):
        tool_map = {}

        if not self.settings:
            self.settings = file.settings
        else:
            if self.settings.units == 'metric':
                file.to_metric()
            else:
                file.to_inch()

        if not self.header1_statements:
            in_header1 = True
            for statement in file.statements:
                if not isinstance(statement, ToolSelectionStmt):
                    if isinstance(statement, ExcellonTool):
                        in_header1 = False
                    else:
                        if in_header1:
                            self.header1_statements.append(statement)
                        else:
                            self.header2_statements.append(statement)
                else:
                    break
        
        for tool in iter(file.tools.values()):
            num = tool.number
            tool_map[num] = self._register_tool(tool)
        
        for hit in file.hits:
            hit.tool = tool_map[hit.tool.number]
            self.hits.append(hit)
    
    def _merge_dxf(self, file):
        if not self.settings:
            self.settings = file.settings
        else:
            if self.settings.units == 'metric':
                file.to_metric()
            else:
                file.to_inch()

        if not self.header1_statements:
            self.header1_statements = [file.header]
            self.header2_statements = [file.header2]
        
        tool = self._register_tool(ExcellonTool(self.settings, number=1, diameter=file.width))
        self.dxf_statements.append((tool.number, file.statements))

    def _register_tool(self, tool):
        for existing in self.tools:
            if existing.equivalent(tool):
                return existing
        new_tool = ExcellonTool.from_tool(tool)
        new_tool.settings = self.settings
        def toolnums():
            for tool in self.tools:
                yield tool.number
        max_num = reduce(lambda x, y: x if x > y else y, toolnums(), 0)
        new_tool.number = max_num + 1
        self.tools.append(new_tool)
        return new_tool