aboutsummaryrefslogtreecommitdiff
path: root/gerberex/composition.py
blob: b5dffb191fccf347ea0e204e510e87734a6bc727 (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
#!/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.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 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()
        self.settings.notation = 'absolute'
        self.settings.zeros = 'trailing'
        with open(path, 'w') as f:
            gerberex.rs274x.write_gerber_header(f, self.settings)
            for statement in statements():
                f.write(statement.to_gerber(self.settings) + '\n')

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

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

        for macro in file.aperture_macros:
            statement = file.aperture_macros[macro]
            name = statement.name
            newname = self._register_aperture_macro(statement)
            aperture_macro_map[name] = newname

        for statement in file.aperture_defs:
            if statement.param == 'AD':
                if statement.shape in aperture_macro_map:
                    statement.shape = aperture_macro_map[statement.shape]
                dnum = statement.d
                newdnum = self._register_aperture(statement)
                aperture_map[dnum] = newdnum

        for statement in file.main_statements:
            if statement.type == 'APERTURE':
                statement.d = aperture_map[statement.d]
            self.drawings.append(statement)
        
        if not self.settings:
            self.settings = file.context

    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


    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.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 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()

        self.settings.notation = 'absolute'
        self.settings.zeros = 'trailing'
        with open(path, 'w') as f:
            gerberex.excellon.write_excellon_header(f, self.settings, self.tools)
            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()

        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()

        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