-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathprocess.py
474 lines (390 loc) · 20.5 KB
/
process.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# For better annotation.
from __future__ import annotations
# System base libraries
import os
import re
import csv
import math
import shutil
from collections import defaultdict
from typing import Tuple
# Interaction with KiCad.
import pcbnew # type: ignore
from .utils import footprint_has_field, footprint_get_field, get_plot_plan
# Application definitions.
from .config import *
class ProcessManager:
def __init__(self):
self.board = pcbnew.GetBoard()
self.bom = []
self.components = []
self.__rotation_db = self.__read_rotation_db()
@staticmethod
def normalize_filename(filename):
return re.sub(r'[^\w\s\.\-]', '', filename)
def update_zone_fills(self):
'''Verify all zones have up-to-date fills.'''
filler = pcbnew.ZONE_FILLER(self.board)
zones = self.board.Zones()
# Fill returns true/false if a refill was made
# We cant use aCheck = True as that would require a rollback on the commit object if
# user decided to not perform the zone fill and the commit object is not exposed to python API
filler.Fill(zones, False)
# Finally rebuild the connectivity db
self.board.BuildConnectivity()
def generate_gerber(self, temp_dir, extra_layers, extend_edge_cuts):
'''Generate the Gerber files.'''
settings = self.board.GetDesignSettings()
settings.m_SolderMaskMargin = 50000
settings.m_SolderMaskToCopperClearance = 5000
settings.m_SolderMaskMinWidth = 0
plot_controller = pcbnew.PLOT_CONTROLLER(self.board)
plot_options = plot_controller.GetPlotOptions()
plot_options.SetOutputDirectory(temp_dir)
plot_options.SetPlotFrameRef(False)
plot_options.SetSketchPadLineWidth(pcbnew.FromMM(0.1))
plot_options.SetAutoScale(False)
plot_options.SetScale(1)
plot_options.SetMirror(False)
plot_options.SetUseGerberAttributes(True)
plot_options.SetUseGerberProtelExtensions(True)
plot_options.SetUseAuxOrigin(True)
plot_options.SetSubtractMaskFromSilk(True)
plot_options.SetUseGerberX2format(False)
plot_options.SetDrillMarksType(0) # NO_DRILL_SHAPE
if hasattr(plot_options, "SetExcludeEdgeLayer"):
plot_options.SetExcludeEdgeLayer(True)
if extra_layers is not None:
extra_layers = [element.strip() for element in extra_layers.strip().split(',') if element.strip()]
else:
extra_layers = []
for layer_info in get_plot_plan(self.board):
if self.board.IsLayerEnabled(layer_info[1]) or layer_info[0] in extra_layers:
plot_controller.SetLayer(layer_info[1])
plot_controller.OpenPlotfile(layer_info[0], pcbnew.PLOT_FORMAT_GERBER, layer_info[2])
if layer_info[1] == pcbnew.Edge_Cuts and hasattr(plot_controller, 'PlotLayers') and extend_edge_cuts:
# includes User_1 layer with Edge_Cuts layer to allow V Cuts to be defined as User_1 layer
# available for KiCad 7.0.1+
seq = pcbnew.LSEQ()
seq.push_back(layer_info[1])
seq.push_back(pcbnew.User_1)
plot_controller.PlotLayers(seq)
else:
plot_controller.PlotLayer()
plot_controller.ClosePlot()
def generate_drills(self, temp_dir):
'''Generate the drill file.'''
drill_writer = pcbnew.EXCELLON_WRITER(self.board)
drill_writer.SetOptions(
False,
False,
self.board.GetDesignSettings().GetAuxOrigin(),
False)
drill_writer.SetFormat(True)
drill_writer.SetMapFileFormat(pcbnew.PLOT_FORMAT_GERBER)
drill_writer.CreateDrillandMapFilesSet(temp_dir, True, True)
def generate_netlist(self, temp_dir):
'''Generate the connection netlist.'''
netlist_writer = pcbnew.IPC356D_WRITER(self.board)
netlist_writer.Write(os.path.join(temp_dir, netlistFileName))
def _get_footprint_position(self, footprint):
"""Calculate position based on center of bounding box."""
position = footprint.GetPosition()
attributes = footprint.GetAttributes()
if attributes & pcbnew.FP_SMD:
position = footprint.GetPosition()
elif attributes & pcbnew.FP_THROUGH_HOLE:
position = footprint.GetBoundingBox(False, False).GetCenter()
else: # handle Unspecified footprint type
pads = footprint.Pads()
if len(pads) > 0:
# get bounding box based on pads only to ignore non-copper layers, e.g. silkscreen
bbox = pads[0].GetBoundingBox() # start with small bounding box
for pad in pads:
bbox.Merge(pad.GetBoundingBox()) # expand bounding box
position = bbox.GetCenter()
return position
def generate_tables(self, temp_dir, auto_translate, exclude_dnp):
'''Generate the data tables.'''
if hasattr(self.board, 'GetModules'):
footprints = list(self.board.GetModules())
else:
footprints = list(self.board.GetFootprints())
# sort footprint after designator
footprints.sort(key=lambda x: x.GetReference())
# unique designator dictionary
footprint_designators = defaultdict(int)
for i, footprint in enumerate(footprints):
# count unique designators
footprint_designators[footprint.GetReference()] += 1
bom_designators = footprint_designators.copy()
if len(footprint_designators.items()) > 0:
with open((os.path.join(temp_dir, designatorsFileName)), 'w', encoding='utf-8-sig') as f:
for key, value in footprint_designators.items():
f.write('%s:%s\n' % (key, value))
for i, footprint in enumerate(footprints):
try:
footprint_name = str(footprint.GetFPID().GetFootprintName())
except AttributeError:
footprint_name = str(footprint.GetFPID().GetLibItemName())
layer = self._get_top_or_bottom_side_override_from_footprint(footprint)
# mount_type = {
# 0: 'smt',
# 1: 'tht',
# 2: 'unspecified'
# }.get(footprint.GetAttributes())
is_dnp = (footprint_has_field(footprint, 'dnp')
or (footprint.GetValue().upper() == 'DNP')
or getattr(footprint, 'IsDNP', bool)())
skip_dnp = exclude_dnp and is_dnp
skip_footprint = footprint.GetPadCount() == 0
if not (footprint.GetAttributes() & pcbnew.FP_EXCLUDE_FROM_POS_FILES) and not skip_footprint and not is_dnp:
# append unique ID if duplicate footprint designator
unique_id = ""
if footprint_designators[footprint.GetReference()] > 1:
unique_id = str(footprint_designators[footprint.GetReference()])
footprint_designators[footprint.GetReference()] -= 1
designator = "{}{}{}".format(footprint.GetReference(), "" if unique_id == "" else "_", unique_id)
position = self._get_footprint_position(footprint)
mid_x = (position[0] - self.board.GetDesignSettings().GetAuxOrigin()[0]) / 1000000.0
mid_y = (position[1] - self.board.GetDesignSettings().GetAuxOrigin()[1]) * -1.0 / 1000000.0
rotation = footprint.GetOrientation().AsDegrees() if hasattr(footprint.GetOrientation(), 'AsDegrees') else footprint.GetOrientation() / 10.0
rotation_offset_db = self._get_rotation_from_db(footprint_name) # internal database offset
rotation_offset_manual = self._get_rotation_offset_from_footprint(footprint) # explicated offset by the designer
# position offset needs to take rotation into account
pos_offset = self._get_position_offset_from_footprint(footprint)
rsin = math.sin(rotation / 180 * math.pi)
rcos = math.cos(rotation / 180 * math.pi)
if layer == 'bottom':
pos_offset = ( pos_offset[0] * rcos + pos_offset[1] * rsin, pos_offset[0] * rsin - pos_offset[1] * rcos )
else:
pos_offset = ( pos_offset[0] * rcos - pos_offset[1] * rsin, pos_offset[0] * rsin + pos_offset[1] * rcos )
mid_x, mid_y = tuple(map(sum,zip((mid_x, mid_y), pos_offset)))
# JLC expect 'Rotation' to be 'as viewed from above component', so bottom needs inverting, and ends up 180 degrees out as well
if layer == 'bottom':
rotation = (180.0 - rotation)
if auto_translate:
rotation += rotation_offset_db
rotation = (rotation + rotation_offset_manual) % 360.0
self.components.append({
'Designator': designator,
'Mid X': mid_x,
'Mid Y': mid_y,
'Rotation': rotation,
'Layer': layer,
})
if not (footprint.GetAttributes() & pcbnew.FP_EXCLUDE_FROM_BOM) and not skip_footprint and not skip_dnp:
# append unique ID if we are dealing with duplicate bom designator
unique_id = ""
if bom_designators[footprint.GetReference()] > 1:
unique_id = str(bom_designators[footprint.GetReference()])
bom_designators[footprint.GetReference()] -= 1
# merge similar parts into single entry
insert = True
for component in self.bom:
same_footprint = component['Footprint'] == self._normalize_footprint_name(footprint_name)
same_value = component['Value'].upper() == footprint.GetValue().upper()
same_lcsc = component['LCSC Part #'] == self._get_mpn_from_footprint(footprint)
under_limit = component['Quantity'] < bomRowLimit
if same_footprint and same_value and same_lcsc and under_limit:
component['Designator'] += ", " + "{}{}{}".format(footprint.GetReference(), "" if unique_id == "" else "_", unique_id)
component['Quantity'] += 1
insert = False
break
# add component to BOM
if insert:
self.bom.append({
'Designator': "{}{}{}".format(footprint.GetReference(), "" if unique_id == "" else "_", unique_id),
'Footprint': self._normalize_footprint_name(footprint_name),
'Quantity': 1,
'Value': footprint.GetValue(),
# 'Mount': mount_type,
'LCSC Part #': self._get_mpn_from_footprint(footprint),
})
def generate_positions(self, temp_dir):
'''Generate the position file.'''
if len(self.components) > 0:
with open((os.path.join(temp_dir, placementFileName)), 'w', newline='', encoding='utf-8-sig') as outfile:
csv_writer = csv.writer(outfile)
# writing headers of CSV file
csv_writer.writerow(self.components[0].keys())
for component in self.components:
# writing data of CSV file
if ('**' not in component['Designator']):
csv_writer.writerow(component.values())
def generate_bom(self, temp_dir):
'''Generate the bom file.'''
if len(self.bom) > 0:
with open((os.path.join(temp_dir, bomFileName)), 'w', newline='', encoding='utf-8-sig') as outfile:
csv_writer = csv.writer(outfile)
# writing headers of CSV file
csv_writer.writerow(self.bom[0].keys())
# Output all of the component information
for component in self.bom:
# writing data of CSV file
if ('**' not in component['Designator']):
csv_writer.writerow(component.values())
def generate_archive(self, temp_dir, temp_file):
'''Generate the archive file.'''
temp_file = shutil.make_archive(temp_file, 'zip', temp_dir)
temp_file = shutil.move(temp_file, temp_dir)
# remove non essential files
for item in os.listdir(temp_dir):
if not item.endswith(".zip") and not item.endswith(".csv") and not item.endswith(".ipc"):
os.remove(os.path.join(temp_dir, item))
return temp_file
""" Private """
def __read_rotation_db(self, filename: str = os.path.join(os.path.dirname(__file__), 'transformations.csv')) -> dict[str, float]:
'''Read the rotations.cf config file so we know what rotations
to apply later.
'''
db = {}
with open(filename, newline='') as csvfile:
csvDialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
csvData = csv.DictReader(csvfile, fieldnames=["footprint", "rotation", "x", "y"],
restkey="extra", restval="0", dialect=csvDialect)
rowNum = 0
for row in csvData:
rowNum = rowNum + 1
# First row is header row, skip.
if rowNum == 1:
skipFirst = False
continue
# If there was too many fields, throw an exception.
if len(row) > 4:
raise RuntimeError("{}: Too many fields found in row {}: {}".format(filename, rowNum, row))
# See if the values we expect to be floating point numbers
# can be converted to floating point, if not throw an exception.
if row['rotation'] == "":
rotation = 0.0
else:
try:
rotation = float(row['rotation'])
except ValueError:
raise RuntimeError("{}: Non-numeric rotation value found in row {}".format(filename, rowNum))
if row['x'] == "":
delta_x = 0.0
else:
try:
delta_x = float(row['x'])
except ValueError:
raise RuntimeError("{}: Non-numeric translation value found in row {}".format(filename, rowNum))
if row['y'] == "":
delta_y = 0.0
else:
try:
delta_y = float(row['y'])
except ValueError:
raise RuntimeError("{}: Non-numeric translation value found in row {}".format(filename, rowNum))
# Add the entry to the database in the format we expect.
db[rowNum] = {}
db[rowNum]['name'] = row['footprint']
db[rowNum]['rotation'] = rotation
db[rowNum]['x'] = delta_x
db[rowNum]['y'] = delta_y
return db
def _get_rotation_from_db(self, footprint: str) -> float:
'''Get the rotation to be added from the database file.'''
# Look for regular expression math of the footprint name and not its root library.
for entry in self.__rotation_db.items():
# If the expression in the DB contains a :, search for it literally.
if (re.search(':', entry[1]['name'])):
if (re.search(entry[1]['name'], footprint)):
return float(entry[1]['rotation'])
# There is no : in the expression, so only search the right side of the :
else:
footprint_segments = footprint.split(':')
# Only one means there was no :, just check the short.
if (len(footprint_segments) == 1):
check = footprint_segments[0]
# More means there was a :, check the right side.
else:
check = footprint_segments[1]
if (re.search(entry[1]['name'], check)):
return float(entry[1]['rotation'])
# Not found, no rotation.
return 0.0
def _get_position_offset_from_db(self, footprint: str) -> Tuple[float, float]:
'''Get the rotation to be added from the database file.'''
# Look for regular expression math of the footprint name and not its root library.
for entry in self.__rotation_db.items():
# If the expression in the DB contains a :, search for it literally.
if (re.search(':', entry[1]['name'])):
if (re.search(entry[1]['name'], footprint)):
return ( float(entry[1]['x']), float(entry[1]['y']) )
# There is no : in the expression, so only search the right side of the :
else:
footprint_segments = footprint.split(':')
# Only one means there was no :, just check the short.
if (len(footprint_segments) == 1):
check = footprint_segments[0]
# More means there was a :, check the right side.
else:
check = footprint_segments[1]
if (re.search(entry[1]['name'], check)):
return ( float(entry[1]['x']), float(entry[1]['y']) )
# Not found, no delta.
return (0.0, 0.0)
def _get_mpn_from_footprint(self, footprint) -> str:
''''Get the MPN/LCSC stock code from standard symbol fields.'''
keys = ['LCSC Part #', 'JLCPCB Part #']
fallback_keys = ['LCSC Part', 'JLC Part', 'LCSC', 'JLC', 'MPN', 'Mpn', 'mpn']
if footprint_has_field(footprint, 'dnp'):
return 'DNP'
for key in keys + fallback_keys:
if footprint_has_field(footprint, key):
return footprint_get_field(footprint, key)
def _get_top_or_bottom_side_override_from_footprint(self, footprint) -> str:
keys = ['JLCPCB Layer Override']
fallback_keys = ['JlcLayerOverride', 'JLCLayerOverride']
layer = {
pcbnew.F_Cu: 'top',
pcbnew.B_Cu: 'bottom',
}.get(footprint.GetLayer())
for key in keys + fallback_keys:
if footprint_has_field(footprint, key):
temp_layer = footprint_get_field(footprint, key)
if (temp_layer[0] == 'b' or temp_layer[0] == 'B'):
layer = "bottom"
break
elif (temp_layer[0] == 't' or temp_layer[0] == 'T'):
layer = "top"
break
return layer
def _get_rotation_offset_from_footprint(self, footprint) -> float:
'''Get the rotation from standard symbol fields.'''
keys = ['JLCPCB Rotation Offset']
fallback_keys = ['JlcRotOffset', 'JLCRotOffset']
offset = ""
for key in keys + fallback_keys:
if footprint_has_field(footprint, key):
offset = footprint_get_field(footprint, key)
break
if offset is None or offset == "":
return 0.0
else:
try:
return float(offset)
except ValueError:
raise RuntimeError("Rotation offset of {} is not a valid number".format(footprint.GetReference()))
def _get_position_offset_from_footprint(self, footprint) -> Tuple[float, float]:
keys = ['JLCPCB Position Offset']
fallback_keys = ['JlcPosOffset', 'JLCPosOffset']
offset = ""
for key in keys + fallback_keys:
if footprint_has_field(footprint, key):
offset = footprint_get_field(footprint, key)
break
if offset == "":
return (0.0, 0.0)
else:
try:
offset = offset.split(",")
return (float(offset[0]), float(offset[1]))
except Exception as e:
raise RuntimeError("Position offset of {} is not a valid pair of numbers".format(footprint.GetReference()))
def _normalize_footprint_name(self, footprint) -> str:
# replace footprint names of resistors, capacitors, inductors, diodes, LEDs, fuses etc, with the footprint size only
pattern = re.compile(r'^(\w*_SMD:)?\w{1,4}_(\d+)_\d+Metric.*$')
return pattern.sub(r'\2', footprint)