forked from erichVK5/BXL2text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFootprint.java
354 lines (297 loc) · 13.8 KB
/
Footprint.java
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
// KicadModuleToGEDA - a utility for turning kicad modules to gEDA PCB footprints
// Footprint.java v1.0
// Copyright (C) 2015 Erich S. Heinzle, [email protected]
// see LICENSE-gpl-v2.txt for software license
// see README.txt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// KicadModuleToGEDA Copyright (C) 2015 Erich S. Heinzle [email protected]
/**
*
* This object coordinates the header, text descriptors,
* drawn lines, drawn arcs, drawn circles
* and also the pads for a given Kicad module,
* passed as a string array of the form (String[] args)
* and is then able to produce a gEDA footprint
*
*/
import java.util.Scanner;
public class Footprint
{
String footprintName = "newFootprint";
String assembledGEDAelement = "";
String passedString = "";
FootprintHeader[] moduleTextDescriptors = new FootprintHeader[200];
FootprintElementArchetype footprintElements[] = new FootprintElementArchetype[2000];
int FPFeatureCount = 0;
Boolean metricSystem = false; // this will be changed to "true", if needed,
// with parsing in due course
int moduleLineCountTotal = 0;
int padDefinitionLineCount = 0;
String padDefinitionLines;
int lineCount = 0;
int padCount = 0;
int drawSegmentCount = 0;
int drawArcCount = 0;
int drawCircleCount = 0;
int textDescriptorCount = 0;
String licenceText1 = null;
String licenceText2 = "\n# dist-license: GPL\n# use-license: unlimited\n"; //# unless specified otherwise in source Kicad module\n";
String clearanceWarningNotice1 = "# Users of the foot print must ensure that the solder mask reliefs and clearances\n# are compatible with the PCB manufacturer's process tolerances\n";
String clearanceWarningNotice2 = null;
long xOffset = 0; // in case we wish to translate the footprint silkscreen in x or y plane
long yOffset = 0; // useful for building complex multiple device footprints
float parsedValue = 0;
String reconstructedKicadModuleAsString = ""; // we'll return this from the toString() method
public Footprint(String args, Boolean MmMetricUnits, long minimumViaAndDrillSizeNM)
{
metricSystem = MmMetricUnits;
boolean moduleFinished = false;
Scanner moduleDefinition = new Scanner(args);
String parseString = "";
String trimmedString = "";
String[] tokens;
// System.out.println(args);
licenceText1 = "# Footprint converted from Kicad Module ";
clearanceWarningNotice2 = "# Pins and SMD pads have been converted from Kicad foot prints which\n# do not have solder mask relief or clearances specified.\n# Fairly sane default values have been used for solder mask relief and clearances.\n";
while (moduleDefinition.hasNext() && !moduleFinished)
{
parseString = moduleDefinition.nextLine();
trimmedString = parseString.trim();
tokens = trimmedString.split(" ");
// we now move into the main parsing section
// which decides what each line is all about and then deploys
// it to construct the relevant footprint element object
if (tokens[0].startsWith("$INDEX"))
{
// System.out.println(footprintName); // we don't care about the index
}
else if (tokens[0].startsWith("$MODULE") || tokens[0].startsWith("module"))
{ // it all starts here, with the module header
// first we look for double quotes inserted by the likes of madparts
footprintName = tokens[1];
if (footprintName.length() > 2) {
if (footprintName.charAt(0) == '"') {
footprintName = footprintName.substring(1);
}
if (footprintName.charAt(footprintName.length()-1) == '"') {
footprintName = footprintName.substring(0,footprintName.length()-1);
}
}
// now we get rid of characters ![a-zA-Z0-9.-] which
// may be unacceptable for a filename
footprintName = footprintName.replaceAll("[^a-zA-Z0-9.-]", "_");
// System.out.println(footprintName + " is the footprint name found.");
// we now step through the module line by line
while (moduleDefinition.hasNext() && !moduleFinished)
{
parseString = moduleDefinition.nextLine();
trimmedString = parseString.trim();
// System.out.println("Current footprint def line:" +
// trimmedString);
// we tokenize the line
tokens = trimmedString.split(" ");
// and we then decide what to do with the tokenized lines
// we ignore "attr" lines at this stage
if (tokens[0].startsWith("Po") || tokens[0].startsWith("at "))
{
// we find the xOffset in the position, and remember
// it is passed in Nm units
parsedValue = Float.parseFloat(tokens[1]);
xOffset = convertToNanometres(parsedValue, metricSystem);
parsedValue = Float.parseFloat(tokens[2]);
yOffset = convertToNanometres(parsedValue, metricSystem);
}
else if (tokens[0].startsWith("T0"))
{
moduleTextDescriptors[textDescriptorCount] = new FootprintHeader();
moduleTextDescriptors[textDescriptorCount].populateHeader(trimmedString, metricSystem);
textDescriptorCount++;
}
else if (tokens[0].startsWith("T") && !tokens[0].startsWith("T1"))
{
// we exclude the component value T1, and look for other text to render
footprintElements[FPFeatureCount] = new FootprintText();
footprintElements[FPFeatureCount].populateElement(trimmedString, metricSystem);
FPFeatureCount++;
}
else if (tokens[0].startsWith("fp_text") && tokens[1].startsWith("reference"))
{
moduleTextDescriptors[textDescriptorCount] = new FootprintHeader();
moduleTextDescriptors[textDescriptorCount].populateHeader(trimmedString, metricSystem);
textDescriptorCount++;
// we only want to know what the reference text is
}
else if (tokens[0].startsWith("DS"))
{
footprintElements[FPFeatureCount] = new DrawnElement();
footprintElements[FPFeatureCount].populateElement(trimmedString, metricSystem);
FPFeatureCount++;
drawSegmentCount++;
}
else if (tokens[0].startsWith("fp_line"))
{
footprintElements[FPFeatureCount] = new DrawnElement();
footprintElements[FPFeatureCount].populateElement(trimmedString, metricSystem);
FPFeatureCount++;
drawSegmentCount++;
}
else if (tokens[0].startsWith("DC"))
{
footprintElements[FPFeatureCount] = new Circle();
footprintElements[FPFeatureCount].populateElement(trimmedString, metricSystem);
FPFeatureCount++;
drawCircleCount++;
}
else if (tokens[0].startsWith("fp_circle"))
{
footprintElements[FPFeatureCount] = new Circle();
footprintElements[FPFeatureCount].populateElement(trimmedString, metricSystem);
FPFeatureCount++;
drawCircleCount++;
}
else if (tokens[0].startsWith("DA"))
{
footprintElements[FPFeatureCount] = new Arc();
footprintElements[FPFeatureCount].populateElement(trimmedString, metricSystem);
FPFeatureCount++;
drawArcCount++;
}
else if (tokens[0].startsWith("fp_arc"))
{
footprintElements[FPFeatureCount] = new Arc();
footprintElements[FPFeatureCount].populateElement(trimmedString, metricSystem);
FPFeatureCount++;
drawArcCount++;
}
else if (tokens[0].startsWith("pad"))
{ // we have identified a pad definition in the module
padDefinitionLines = trimmedString;
padCount++;
footprintElements[FPFeatureCount] = new Pad();
footprintElements[FPFeatureCount].populateElement(padDefinitionLines, metricSystem, minimumViaAndDrillSizeNM);
FPFeatureCount++;
}
else if (tokens[0].startsWith("$PAD"))
{ // we have identified a $PAD definition in the module
padDefinitionLines = "";
padDefinitionLineCount = 0;
padCount++;
while (!trimmedString.startsWith("$EndPAD") && moduleDefinition.hasNext())
{ // we now turn the multiline $PAD definition
// into a single string
padDefinitionLines = padDefinitionLines +
trimmedString + " \n" ;
parseString = moduleDefinition.nextLine();
trimmedString = parseString.trim();
padDefinitionLineCount++;
}
padDefinitionLines = padDefinitionLines + "$EndPad \n";
// having created a single string containing the
// $PAD definition
// we can now pass it to a Pad constructor
footprintElements[FPFeatureCount] = new Pad();
footprintElements[FPFeatureCount].populateElement(padDefinitionLines, metricSystem, minimumViaAndDrillSizeNM);
FPFeatureCount++;
}
if (tokens[0].startsWith("$EndMOD"))
{
moduleFinished = true;
}
}
}
else {
}
}
// we also create a single string version of the module for
// use by the toString() method
reconstructedKicadModuleAsString = args;
}
public String generateGEDAfootprintFilename()
{
return footprintName + ".fp";
}
public String generateGEDAfootprint(float magnificationRatio)
{
assembledGEDAelement = licenceText1 + footprintName + licenceText2;
if (padCount > 0)
{
assembledGEDAelement = assembledGEDAelement +
clearanceWarningNotice1 + clearanceWarningNotice2 ;
}
if (metricSystem)
{
assembledGEDAelement = assembledGEDAelement +
"# Kicad module units: mm\n";
}
else
{
assembledGEDAelement = assembledGEDAelement +
"# Kicad module units: 0.1 mil\n";
}
// we start by generating a generic gEDA footprint Element[...] field
// in case the Kicad module failed to have a text field "T0 .... "
// to base the gEDA element field on
// We use the file name for the description, default text orientation
// of 0, with an offset 250 mils to the right of centre, and
// text scaling of 100
String gEDAfootprintElementField =
"# Since the Kicad Module did not have a 'T0' field,\n" +
"# The module name has been used for the Element description field\n" +
"Element[\"\" \"" + footprintName +
"\" \"\" \"\" 0 0 0 25000 0 100 \"\"]\n(\n";
if (textDescriptorCount > 0)
{
gEDAfootprintElementField = moduleTextDescriptors[0].generateGEDAtextField(0,0);
}
assembledGEDAelement = assembledGEDAelement +
("# Footprint = module name: " + footprintName + "\n" +
"# Text descriptor count: " + textDescriptorCount + "\n" +
"# Draw segment object count: " + drawSegmentCount + "\n" +
"# Draw circle object count: " + drawCircleCount + "\n" +
"# Draw arc object count: " + drawArcCount + "\n" +
"# Pad count: " + padCount + "\n#\n" +
gEDAfootprintElementField );
// System.out.println(assembledGEDAelement);
for (int counter = 0; counter < FPFeatureCount; counter++)
{
assembledGEDAelement = assembledGEDAelement +
footprintElements[counter].generateGEDAelement(xOffset,yOffset,magnificationRatio);
}
return assembledGEDAelement;
}
public String getKicadModuleName()
{
return footprintName;
}
public String toString()
{
return reconstructedKicadModuleAsString;
}
private long convertToNanometres(float rawValue, boolean metricSystem)
{
if (metricSystem) // metricSystem = units mm
{
return (long)(1000000 * rawValue);
// multiply mm by 1000000 to turn into nm
}
else
{
return (long)(2540 * rawValue);
// multiply 0.1 mil units by 2540 to turn into nm
}
}
}