forked from LibrePCB/librepcb-parts-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_capacitor_radial_tht.py
379 lines (345 loc) · 14.3 KB
/
generate_capacitor_radial_tht.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
"""
Generate THT polarized radial electrolytic capacitors (CAPPRD).
"""
from os import path
from uuid import uuid4
from typing import Optional
from common import format_ipc_dimension, init_cache, now, save_cache
from entities.common import (
Align, Angle, Author, Category, Circle, Created, Deprecated, Description, Diameter, Fill, GeneratedBy, GrabArea,
Height, Keywords, Layer, Name, Polygon, Position, Position3D, Rotation, Rotation3D, Value, Version, Vertex, Width
)
from entities.component import SignalUUID
from entities.device import ComponentPad, ComponentUUID, Device, PackageUUID
from entities.package import (
AssemblyType, AutoRotate, ComponentSide, CopperClearance, DrillDiameter, Footprint, FootprintPad, LetterSpacing,
LineSpacing, Mirror, Package, PackagePad, PackagePadUuid, PadFunction, PadHole, Shape, ShapeRadius, Size,
SolderPasteConfig, StopMaskConfig, StrokeText, StrokeWidth
)
generator = 'librepcb-parts-generator (generate_capacitor_radial_tht.py)'
# Lookup table to get the drill diameter from a given lead diameter.
LEAD_WIDTH_TO_DRILL = {
0.4: 0.5,
0.45: 0.6,
0.5: 0.7,
0.6: 0.8,
0.8: 1.0,
}
# Initialize UUID cache
uuid_cache_file = 'uuid_cache_capacitors_radial_tht.csv'
uuid_cache = init_cache(uuid_cache_file)
def uuid(category: str, full_name: str, identifier: str) -> str:
"""
Return a uuid for the specified element.
Params:
category:
For example 'cmp' or 'pkg'.
full_name:
For example "SOIC127P762X120-16".
identifier:
For example 'pad-1' or 'pin-13'.
"""
key = '{}-{}-{}'.format(category, full_name, identifier).lower().replace(' ', '~')
if key not in uuid_cache:
uuid_cache[key] = str(uuid4())
return uuid_cache[key]
def get_variant(
diameter: float,
height: float,
pitch: float,
lead_width: float,
) -> str:
return 'D{}-H{}-P{}-W{}'.format(diameter, height, pitch, lead_width)
def generate_pkg(
library: str,
diameter: float,
height: float,
pitch: float,
lead_width: float,
author: str,
version: str,
create_date: Optional[str],
) -> None:
# Name according IPC-7351 "Capacitor, Polarized Radial Diameter":
# CAPPRD + Lead Spacing + W Lead Width + D Body Diameter + H Body Height
name = 'CAPPRD{}W{}D{}H{}'.format(
format_ipc_dimension(pitch), format_ipc_dimension(lead_width),
format_ipc_dimension(diameter), format_ipc_dimension(height))
variant = get_variant(diameter, height, pitch, lead_width)
def _pkg_uuid(identifier: str) -> str:
return uuid('pkg', variant, identifier)
def _create_footprint(footprint_identifier: str, name: str) -> Footprint:
def _fpt_uuid(identifier: str) -> str:
return _pkg_uuid(footprint_identifier + '-' + identifier)
drill = LEAD_WIDTH_TO_DRILL[lead_width]
restring = min((0.4 if diameter >= 6.0 else 0.3), # preferred restring
(pitch - drill - 0.25) / 2) # minimum required restring
pad_diameter = drill + (2 * restring) # outer diameter of pad
courtyard_diameter = diameter + (1.0 if diameter >= 10.0 else 0.8)
def _generate_fill_polygon(identifier: str, layer: str) -> Polygon:
polygon = Polygon(
uuid=_fpt_uuid(identifier),
layer=Layer(layer),
width=Width(0.0),
fill=Fill(True),
grab_area=GrabArea(False),
)
if ((pitch - pad_diameter) < 0.6):
# not enough space, use a simplified polygon
vertices = [
(0.0, (diameter / 2) - 0.2, 0.0),
(0.0, (pad_diameter / 2) + 0.2, 0.0),
(pitch / 2, (pad_diameter / 2) + 0.2, -180.0),
(pitch / 2, -(pad_diameter / 2) - 0.2, 0.0),
(0.0, -(pad_diameter / 2) - 0.2, 0.0),
(0.0, -(diameter / 2) + 0.2, 180.0),
(0.0, (diameter / 2) - 0.2, 0.0),
]
else:
vertices = [
(0.0, (diameter / 2) - 0.2, 0.0),
(0.0, 0.0, 0.0),
((pitch / 2) - (pad_diameter / 2) - 0.2, 0.0, -180.0),
((pitch / 2) + (pad_diameter / 2) + 0.2, 0.0, -180.0),
((pitch / 2) - (pad_diameter / 2) - 0.2, 0.0, 0.0),
(0.0, 0.0, 0.0),
(0.0, -(diameter / 2) + 0.2, 180.0),
(0.0, (diameter / 2) - 0.2, 0.0),
]
for vertex in vertices:
polygon.add_vertex(Vertex(Position(vertex[0], vertex[1]), Angle(vertex[2])))
return polygon
footprint = Footprint(
uuid=_fpt_uuid('footprint'),
name=Name(name),
description=Description(''),
position_3d=Position3D.zero(),
rotation_3d=Rotation3D.zero(),
)
pad_hole_path = [Vertex(Position(0.0, 0.0), Angle(0.0))]
uuid_plus = _pkg_uuid('pad-plus')
footprint.add_pad(FootprintPad(
uuid=uuid_plus,
side=ComponentSide.TOP,
shape=Shape.ROUNDED_RECT,
position=Position(-pitch / 2, 0),
rotation=Rotation(0),
size=Size(pad_diameter, pad_diameter),
radius=ShapeRadius(0.0),
stop_mask=StopMaskConfig.AUTO,
solder_paste=SolderPasteConfig.OFF,
copper_clearance=CopperClearance(0),
function=PadFunction.UNSPECIFIED,
package_pad=PackagePadUuid(uuid_plus),
holes=[PadHole(uuid_plus, DrillDiameter(drill), pad_hole_path)],
))
uuid_minus = _pkg_uuid('pad-minus')
footprint.add_pad(FootprintPad(
uuid=uuid_minus,
side=ComponentSide.TOP,
shape=Shape.ROUNDED_RECT,
position=Position(pitch / 2, 0),
rotation=Rotation(0),
size=Size(pad_diameter, pad_diameter),
radius=ShapeRadius(1.0),
stop_mask=StopMaskConfig.AUTO,
solder_paste=SolderPasteConfig.OFF,
copper_clearance=CopperClearance(0),
function=PadFunction.UNSPECIFIED,
package_pad=PackagePadUuid(uuid_minus),
holes=[PadHole(uuid_minus, DrillDiameter(drill), pad_hole_path)],
))
# placement
footprint.add_circle(Circle(
uuid=_fpt_uuid('circle-placement'),
layer=Layer('top_legend'),
width=Width(0.2),
fill=Fill(False),
grab_area=GrabArea(False),
diameter=Diameter(diameter + 0.2),
position=Position(0.0, 0.0),
))
footprint.add_polygon(_generate_fill_polygon(
identifier='polygon-placement-fill',
layer='top_legend',
))
# documentation
footprint.add_circle(Circle(
uuid=_fpt_uuid('circle-documentation'),
layer=Layer('top_documentation'),
width=Width(0.2),
fill=Fill(False),
grab_area=GrabArea(False),
diameter=Diameter(diameter - 0.2),
position=Position(0.0, 0.0),
))
footprint.add_polygon(_generate_fill_polygon(
identifier='polygon-documentation-fill',
layer='top_documentation',
))
# courtyard
footprint.add_circle(Circle(
uuid=_fpt_uuid('circle-courtyard'),
layer=Layer('top_courtyard'),
width=Width(0.0),
fill=Fill(False),
grab_area=GrabArea(False),
diameter=Diameter(courtyard_diameter),
position=Position(0.0, 0.0),
))
# texts
footprint.add_text(StrokeText(
uuid=_fpt_uuid('text-name'),
layer=Layer('top_names'),
height=Height(1.0),
stroke_width=StrokeWidth(0.2),
letter_spacing=LetterSpacing.AUTO,
line_spacing=LineSpacing.AUTO,
align=Align('center bottom'),
position=Position(0.0, (diameter / 2) + 0.8),
rotation=Rotation(0.0),
auto_rotate=AutoRotate(True),
mirror=Mirror(False),
value=Value('{{NAME}}'),
))
footprint.add_text(StrokeText(
uuid=_fpt_uuid('text-value'),
layer=Layer('top_values'),
height=Height(1.0),
stroke_width=StrokeWidth(0.2),
letter_spacing=LetterSpacing.AUTO,
line_spacing=LineSpacing.AUTO,
align=Align('center top'),
position=Position(0.0, -(diameter / 2) - 0.8),
rotation=Rotation(0.0),
auto_rotate=AutoRotate(True),
mirror=Mirror(False),
value=Value('{{VALUE}}'),
))
return footprint
# package
package = Package(
uuid=_pkg_uuid('pkg'),
name=Name(name),
description=Description(
'Polarized radial electrolytic capacitor.\n\n' +
'Diameter: {} mm\n'.format(diameter) +
'Height: {} mm\n'.format(height) +
'Lead Spacing: {} mm\n'.format(pitch) +
'Max. Lead Diameter: {} mm\n\n'.format(lead_width) +
'Generated with {}'.format(generator)
),
keywords=Keywords('electrolytic,capacitor,polarized,radial,c,cap,cpol'),
author=Author(author),
version=Version(version),
created=Created(create_date or now()),
deprecated=Deprecated(False),
generated_by=GeneratedBy(''),
categories=[Category('ee75e31d-f231-41d9-8a3b-bea5114f41e3')],
assembly_type=AssemblyType.AUTO,
)
package.add_pad(PackagePad(uuid=_pkg_uuid('pad-plus'), name=Name('+')))
package.add_pad(PackagePad(uuid=_pkg_uuid('pad-minus'), name=Name('-')))
package.add_footprint(_create_footprint(
footprint_identifier='default',
name='default',
))
# write files
package.serialize(path.join('out', library, 'pkg'))
print('Wrote package {}'.format(name))
def generate_dev(
library: str,
diameter: float,
height: float,
pitch: float,
lead_width: float,
author: str,
version: str,
create_date: Optional[str],
) -> None:
name = 'Capacitor Radial ⌀{}x{}/{}mm'.format(diameter, height, pitch)
variant = get_variant(diameter, height, pitch, lead_width)
def _uuid(identifier: str) -> str:
return uuid('dev', variant, identifier)
device = Device(
uuid=_uuid('dev'),
name=Name(name),
description=Description(
'Generic polarized radial electrolytic capacitor.\n\n' +
'Diameter: {} mm\n'.format(diameter) +
'Height: {} mm\n'.format(height) +
'Lead Spacing: {} mm\n'.format(pitch) +
'Max. Lead Diameter: {} mm\n\n'.format(lead_width) +
'Generated with {}'.format(generator)
),
keywords=Keywords('electrolytic,capacitor,polarized,radial,c,cap,cpol'),
author=Author(author),
version=Version(version),
created=Created(create_date or now()),
deprecated=Deprecated(False),
generated_by=GeneratedBy(''),
categories=[Category('c011cc6b-b762-498e-8494-d1994f3043cf')],
component_uuid=ComponentUUID('c54375c5-7149-4ded-95c5-7462f7301ee7'),
package_uuid=PackageUUID(uuid('pkg', variant, 'pkg')),
)
device.add_pad(ComponentPad(
pad_uuid=uuid('pkg', variant, 'pad-plus'),
signal=SignalUUID('e010ecbb-6210-4da3-9270-ebd58656dbf0'),
))
device.add_pad(ComponentPad(
pad_uuid=uuid('pkg', variant, 'pad-minus'),
signal=SignalUUID('af3ffca8-0085-4edb-a775-fcb759f63411'),
))
# write files
device.serialize(path.join('out', library, 'dev'))
print('Wrote device {}'.format(name))
if __name__ == '__main__':
CONFIGS = [
# Some typical, frequently used configurations. The lead width depends
# from package to package, thus choosing the highest value to ensure
# compatibility with all variants (models with thinner leads can
# still be mount).
{'diameter': 3.0, 'height': 5.0, 'pitch': 1.0, 'lead_width': 0.4},
{'diameter': 4.0, 'height': 5.0, 'pitch': 1.5, 'lead_width': 0.45},
{'diameter': 4.0, 'height': 7.0, 'pitch': 1.5, 'lead_width': 0.45},
{'diameter': 4.0, 'height': 11.0, 'pitch': 1.5, 'lead_width': 0.45},
{'diameter': 5.0, 'height': 5.0, 'pitch': 2.0, 'lead_width': 0.5},
{'diameter': 5.0, 'height': 7.0, 'pitch': 2.0, 'lead_width': 0.5},
{'diameter': 5.0, 'height': 11.0, 'pitch': 2.0, 'lead_width': 0.5},
{'diameter': 6.3, 'height': 5.0, 'pitch': 2.5, 'lead_width': 0.5},
{'diameter': 6.3, 'height': 7.0, 'pitch': 2.5, 'lead_width': 0.5},
{'diameter': 6.3, 'height': 11.0, 'pitch': 2.5, 'lead_width': 0.5},
{'diameter': 8.0, 'height': 5.0, 'pitch': 2.5, 'lead_width': 0.6},
{'diameter': 8.0, 'height': 7.0, 'pitch': 3.5, 'lead_width': 0.6},
{'diameter': 8.0, 'height': 11.5, 'pitch': 3.5, 'lead_width': 0.6},
{'diameter': 10.0, 'height': 12.5, 'pitch': 5.0, 'lead_width': 0.6},
{'diameter': 10.0, 'height': 16.0, 'pitch': 5.0, 'lead_width': 0.6},
{'diameter': 10.0, 'height': 20.0, 'pitch': 5.0, 'lead_width': 0.6},
{'diameter': 12.5, 'height': 20.0, 'pitch': 5.0, 'lead_width': 0.8},
{'diameter': 12.5, 'height': 25.0, 'pitch': 5.0, 'lead_width': 0.8},
{'diameter': 16.0, 'height': 25.0, 'pitch': 7.5, 'lead_width': 0.8},
{'diameter': 16.0, 'height': 31.5, 'pitch': 7.5, 'lead_width': 0.8},
{'diameter': 18.0, 'height': 35.5, 'pitch': 7.5, 'lead_width': 0.8},
]
for config in CONFIGS:
generate_pkg(
library='LibrePCB_Base.lplib',
diameter=config['diameter'],
height=config['height'],
pitch=config['pitch'],
lead_width=config['lead_width'],
author='U. Bruhin',
version='0.1',
create_date='2019-12-29T14:14:11Z',
)
generate_dev(
library='LibrePCB_Base.lplib',
diameter=config['diameter'],
height=config['height'],
pitch=config['pitch'],
lead_width=config['lead_width'],
author='U. Bruhin',
version='0.1',
create_date='2019-12-29T14:14:11Z',
)
save_cache(uuid_cache_file, uuid_cache)