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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using UVtools.Core.Extensions;
using UVtools.Core.Gerber.Apertures;
namespace UVtools.Core.Gerber
{
public class GerberDocument
{
#region Properties
public GerberPositionType PositionType { get; set; } = GerberPositionType.Absolute;
public GerberUnitType UnitType { get; set; } = GerberUnitType.Millimeter;
public GerberPolarityType Polarity { get; set; } = GerberPolarityType.Dark;
public GerberMoveType MoveType { get; set; } = GerberMoveType.Linear;
public bool LeadingZeroOmitted { get; set; } = true;
public byte CoordinateXIntegers { get; set; } = 3;
public byte CoordinateXFractionalDigits { get; set; } = 6;
public byte CoordinateYIntegers { get; set; } = 3;
public byte CoordinateYFractionalDigits { get; set; } = 6;
public Dictionary<int, Aperture> Apertures { get; } = new();
public Dictionary<string, Macro> Macros { get; } = new();
#endregion
public GerberDocument()
{
}
public GerberDocument(string filePath)
{
}
public static GerberDocument ParseAndDraw(string filePath, Mat mat, bool enableAntialiasing = false)
{
using var file = new StreamReader(filePath);
var document = new GerberDocument();
int FSlength = "%FSLAX46Y46*%".Length;
int MOlength = "%MOMM*%".Length;
int LPlength = "%LPD*%".Length;
SizeF xyPpmm = new SizeF(20, 20);
double currentX = 0;
double currentY = 0;
Aperture? currentAperture = null;
Macro? currentMacro = null;
var regionPoints = new List<Point>();
bool insideRegion = false;
string? line;
while ((line = file.ReadLine()) is not null)
{
line = line.Trim();
if(line == string.Empty) continue;
if (line.StartsWith("M02")) break;
var accumulatedLine = line;
while (!accumulatedLine.Contains('*') && (line = file.ReadLine()) is not null)
{
line = line.Trim();
if (line == string.Empty) continue;
accumulatedLine += line;
}
line = accumulatedLine;
if(currentMacro is not null)
{
currentMacro.ParsePrimitive(line);
if (line[^1] == '%') currentMacro = null;
continue;
}
if (line.StartsWith("%MO") && line.Length >= MOlength)
{
if(line[3] == 'M' && line[4] == 'M') document.UnitType = GerberUnitType.Millimeter;
else if(line[3] == 'I' && line[4] == 'N') document.UnitType = GerberUnitType.Inch;
continue;
}
if (line.StartsWith("%FS") && line.Length >= FSlength)
{
// %FSLAX34Y34*%
// 0123456789
document.LeadingZeroOmitted = line[3] switch
{
'L' => true,
'T' => false,
_ => document.LeadingZeroOmitted
};
document.PositionType = line[4] switch
{
'A' => GerberPositionType.Absolute,
'I' => GerberPositionType.Relative,
_ => document.PositionType
};
if (line[5] != 'X') continue;
if (byte.TryParse(line[6].ToString(), out var x1)) document.CoordinateXIntegers = x1;
if (byte.TryParse(line[7].ToString(), out var x2)) document.CoordinateXFractionalDigits = x2;
if (line[8] != 'Y') continue;
if (byte.TryParse(line[9].ToString(), out var y1)) document.CoordinateYIntegers = y1;
if (byte.TryParse(line[10].ToString(), out var y2)) document.CoordinateYFractionalDigits = y2;
continue;
}
if (line.StartsWith("%LP") && line.Length >= LPlength)
{
document.Polarity = line[3] switch
{
'D' => GerberPolarityType.Dark,
'C' => GerberPolarityType.Clear,
_ => document.Polarity
};
continue;
}
if (line.StartsWith("G01"))
{
document.MoveType = GerberMoveType.Linear;
continue;
}
if (line.StartsWith("G02"))
{
document.MoveType = GerberMoveType.Arc;
continue;
}
if (line.StartsWith("G03"))
{
document.MoveType = GerberMoveType.ArcCounterClockwise;
continue;
}
if (line.StartsWith("G36"))
{
insideRegion = true;
regionPoints.Clear();
continue;
}
if (line.StartsWith("G37"))
{
insideRegion = false;
if (regionPoints.Count > 0)
{
using var vec = new VectorOfPoint(regionPoints.ToArray());
CvInvoke.FillPoly(mat, vec, document.Polarity == GerberPolarityType.Dark ? EmguExtensions.WhiteColor : EmguExtensions.BlackColor, enableAntialiasing ? LineType.AntiAlias : LineType.EightConnected);
}
regionPoints.Clear();
continue;
}
if (line.StartsWith("%AM"))
{
currentMacro = Macro.Parse(line);
if (currentMacro is null) continue;
document.Macros.Add(currentMacro.Name, currentMacro);
//document.Apertures.Add(aperture.Index, aperture);
continue;
}
if (line.StartsWith("%ADD"))
{
var aperture = Aperture.Parse(line, document);
if (aperture is null) continue;
currentAperture = aperture;
document.Apertures.Add(aperture.Index, aperture);
continue;
}
if (line[0] == 'D')
{
var matchD = Regex.Match(line, @"D(\d+)");
if (!matchD.Success || matchD.Groups.Count < 2) continue;
if (!int.TryParse(matchD.Groups[1].Value, out var d)) continue;
if (!document.Apertures.TryGetValue(d, out currentAperture)) continue;
continue;
}
if (line[0] == 'X' || line[0] == 'Y')
{
var matchX = Regex.Match(line, @"X-?(\d+)");
var matchY = Regex.Match(line, @"Y-?(\d+)");
var matchD = Regex.Match(line, @"D(\d+)");
double nowX = 0;
double nowY = 0;
if (!matchD.Success || matchD.Groups.Count < 2) continue;
if (!int.TryParse(matchD.Groups[1].Value, out var d)) continue;
if (matchX.Success && matchX.Groups.Count >= 2)
{
if (double.TryParse(matchX.Groups[1].Value, out nowX))
{
if (nowX != 0)
{
var integers = matchX.Groups[1].Value[..^document.CoordinateXFractionalDigits];
var fraction = matchX.Groups[1].Value.Substring(matchX.Groups[1].Value.Length - document.CoordinateXFractionalDigits, document.CoordinateXFractionalDigits);
double.TryParse($"{integers}.{fraction}", out nowX);
}
}
}
if (matchY.Success && matchY.Groups.Count >= 2)
{
if (double.TryParse(matchY.Groups[1].Value, out nowY))
{
if (nowY != 0)
{
var integers = matchY.Groups[1].Value[..^document.CoordinateYFractionalDigits];
var fraction = matchY.Groups[1].Value.Substring(matchY.Groups[1].Value.Length - document.CoordinateYFractionalDigits, document.CoordinateYFractionalDigits);
double.TryParse($"{integers}.{fraction}", out nowY);
}
}
}
if (insideRegion)
{
if (d == 2)
{
if (regionPoints.Count > 0)
{
using var vec = new VectorOfPoint(regionPoints.ToArray());
CvInvoke.FillPoly(mat, vec, document.Polarity == GerberPolarityType.Dark ? EmguExtensions.WhiteColor : EmguExtensions.BlackColor, enableAntialiasing ? LineType.AntiAlias : LineType.EightConnected);
}
regionPoints.Clear();
}
var pt = new Point((int) (nowX * xyPpmm.Width), (int) (nowY * xyPpmm.Height));
if (regionPoints.Count == 0 || (regionPoints.Count > 0 && regionPoints[^1] != pt)) regionPoints.Add(pt);
}
else if(currentAperture is not null)
{
if (d == 1)
{
if (currentAperture is CircleAperture circleAperture)
{
if (document.MoveType is GerberMoveType.Arc or GerberMoveType.ArcCounterClockwise)
{
var matchI = Regex.Match(line, @"I(-?\d+)");
var matchJ = Regex.Match(line, @"J(-?\d+)");
if(!matchI.Success || !matchJ.Success || matchI.Groups.Count < 2 || matchJ.Groups.Count < 2) continue;
if (double.TryParse(matchI.Groups[1].Value, out var xOffset))
{
if (xOffset != 0)
{
var integers = matchI.Groups[1].Value[..^document.CoordinateXFractionalDigits];
var fraction = matchI.Groups[1].Value.Substring(matchI.Groups[1].Value.Length - document.CoordinateXFractionalDigits, document.CoordinateXFractionalDigits);
double.TryParse($"{integers}.{fraction}", out xOffset);
}
}
if (double.TryParse(matchJ.Groups[1].Value, out var yOffset))
{
if(yOffset != 0)
{
var integers = matchJ.Groups[1].Value[..^document.CoordinateYFractionalDigits];
var fraction = matchJ.Groups[1].Value.Substring(matchJ.Groups[1].Value.Length - document.CoordinateYFractionalDigits, document.CoordinateYFractionalDigits);
double.TryParse($"{integers}.{fraction}", out yOffset);
}
}
if (currentX == nowX && currentY == nowY) // Closed circle
{
CvInvoke.Ellipse(mat, new Point((int)((nowX + xOffset) * xyPpmm.Width), (int)((nowY + yOffset) * xyPpmm.Height)),
new Size((int)(Math.Abs(xOffset) * xyPpmm.Width), (int)(Math.Abs(xOffset) * xyPpmm.Width)),
0, 0, 360.0, document.Polarity == GerberPolarityType.Dark ? EmguExtensions.WhiteColor : EmguExtensions.BlackColor,
(int)(circleAperture.Diameter * xyPpmm.Max()),
enableAntialiasing ? LineType.AntiAlias : LineType.EightConnected
);
}
else
{
// TODO: Fix this
throw new NotImplementedException("Partial arcs are not yet implemented (G03)\nTo be fixed in the future...");
CvInvoke.Ellipse(mat, new Point((int)((nowX + xOffset) * xyPpmm.Width), (int)((currentY) * xyPpmm.Height)),
new Size((int)(Math.Abs(xOffset) * xyPpmm.Width), (int)(Math.Abs(yOffset) * xyPpmm.Width)),
0, Math.Abs(currentY - nowY), 360.0 / Math.Abs(currentX - nowX), document.Polarity == GerberPolarityType.Dark ? EmguExtensions.WhiteColor : EmguExtensions.BlackColor,
(int)(circleAperture.Diameter * xyPpmm.Max()),
enableAntialiasing ? LineType.AntiAlias : LineType.EightConnected
);
}
}
else
{
CvInvoke.Line(mat,
new Point((int)(currentX * xyPpmm.Width), (int)(currentY * xyPpmm.Height)),
new Point((int)(nowX * xyPpmm.Width), (int)(nowY * xyPpmm.Height)),
document.Polarity == GerberPolarityType.Dark ? EmguExtensions.WhiteColor : EmguExtensions.BlackColor,
(int)(circleAperture.Diameter * xyPpmm.Max()),
enableAntialiasing ? LineType.AntiAlias : LineType.EightConnected);
}
}
}
else if (d == 3)
{
currentAperture.DrawFlashD3(mat, xyPpmm, new Point((int)(nowX * xyPpmm.Width), (int)(nowY * xyPpmm.Height)), document.Polarity == GerberPolarityType.Dark ? EmguExtensions.WhiteColor : EmguExtensions.BlackColor, enableAntialiasing ? LineType.AntiAlias : LineType.EightConnected);
}
}
currentX = nowX;
currentY = nowY;
continue;
}
}
return document;
}
}
}
/* KIDCAD
var document = File.ReadAllLines(@"D:\Tiago\Desktop\kisample\kisample.kicad_pcb");
System.Drawing.PointF location = PointF.Empty;
using var mat = EmguExtensions.InitMat(new System.Drawing.Size(2440, 1440));
const byte pixelsPerMm = 20;
var drillPoints = new List<KeyValuePair<Point, int>>();
foreach (var line in document)
{
var parseLine = line.Trim();
if (parseLine.StartsWith("(footprint "))
{
location = PointF.Empty;
continue;
}
if (location.IsEmpty && parseLine.StartsWith("(at "))
{
parseLine = parseLine.Substring(4, parseLine.Length-5);
var split = parseLine.Split(' ');
location = new PointF(float.Parse(split[0]), float.Parse(split[1]));
continue;
}
if (parseLine.StartsWith("(segment ") || parseLine.StartsWith("(gr_line "))
{
var layerMatch = Regex.Match(parseLine, @"\S.Cu");
if (!layerMatch.Success || layerMatch.Groups.Count < 1) continue;
var startMatch = Regex.Match(parseLine, @"\(start\s+(\S+)\s+(\S+)\)");
if(!startMatch.Success || startMatch.Groups.Count < 3) continue;
var endMatch = Regex.Match(parseLine, @"\(end\s+(\S+)\s+(\S+)\)");
if (!endMatch.Success || endMatch.Groups.Count < 3) continue;
var widthMatch = Regex.Match(parseLine, @"\(width\s+(\S+)\)");
if (!widthMatch.Success || widthMatch.Groups.Count < 2) continue;
var startXf = new PointF(float.Parse(startMatch.Groups[1].Value), float.Parse(startMatch.Groups[2].Value));
var endXf = new PointF(float.Parse(endMatch.Groups[1].Value), float.Parse(endMatch.Groups[2].Value));
var widthf = float.Parse(widthMatch.Groups[1].Value);
var startX = new System.Drawing.Point((int)(startXf.X * pixelsPerMm), (int)(startXf.Y * pixelsPerMm));
var endX = new System.Drawing.Point((int)(endXf.X * pixelsPerMm), (int)(endXf.Y * pixelsPerMm));
var width = (int) (widthf * pixelsPerMm);
CvInvoke.Line(mat, startX, endX, EmguExtensions.WhiteColor, width);
continue;
}
if (parseLine.StartsWith("(via "))
{
var layerMatches = Regex.Matches(parseLine, @"\S.Cu");
if (layerMatches.Count < 1) continue;
var atMatch = Regex.Match(parseLine, @"\(at\s+(\S+)\s+(\S+)\)");
if (!atMatch.Success || atMatch.Groups.Count < 3) continue;
var drillMatch = Regex.Match(parseLine, @"\(drill\s+(\S+)\)");
var atf = new PointF(float.Parse(atMatch.Groups[1].Value), float.Parse(atMatch.Groups[2].Value));
//var sizef = new SizeF(float.Parse(sizeMatch.Groups[1].Value), float.Parse(sizeMatch.Groups[2].Value));
var at = new System.Drawing.Point((int)(atf.X * pixelsPerMm), (int)(atf.Y * pixelsPerMm));
if (!drillMatch.Success || drillMatch.Groups.Count < 2) continue;
var drillf = float.Parse(drillMatch.Groups[1].Value);
var drill = (int) (drillf * pixelsPerMm / 2);
CvInvoke.Circle(mat, at, drill, EmguExtensions.WhiteColor, -1);
continue;
}
if (parseLine.StartsWith("(gr_circle "))
{
var layerMatch = Regex.Match(parseLine, @"\S.Cu");
if (!layerMatch.Success || layerMatch.Groups.Count < 1) continue;
var atMatch = Regex.Match(parseLine, @"\(center\s+(\S+)\s+(\S+)\)");
if (!atMatch.Success || atMatch.Groups.Count < 3) continue;
var endMatch = Regex.Match(parseLine, @"\(end\s+(\S+)\s+(\S+)\)");
if (!endMatch.Success || endMatch.Groups.Count < 3) continue;
var widthMatch = Regex.Match(parseLine, @"\(width\s+(\S+)\)");
if (!widthMatch.Success || widthMatch.Groups.Count < 2) continue;
var atf = new PointF(float.Parse(atMatch.Groups[1].Value), float.Parse(atMatch.Groups[2].Value));
var at = new System.Drawing.Point((int)(atf.X * pixelsPerMm), (int)(atf.Y * pixelsPerMm));
var endf = new PointF(float.Parse(endMatch.Groups[1].Value), float.Parse(endMatch.Groups[2].Value));
var radius = (int)(Math.Max(Math.Abs(atf.X - endf.X), Math.Abs(atf.Y - endf.Y)) * pixelsPerMm);
var widthf = float.Parse(widthMatch.Groups[1].Value);
var width = (int)(widthf * pixelsPerMm);
CvInvoke.Circle(mat, at, radius, EmguExtensions.WhiteColor, width);
if (parseLine.Contains("fill solid"))
{
CvInvoke.Circle(mat, at, radius, EmguExtensions.WhiteColor, -1);
}
continue;
}
if (parseLine.StartsWith("(gr_rect "))
{
var layerMatch = Regex.Match(parseLine, @"\S.Cu");
if (!layerMatch.Success || layerMatch.Groups.Count < 1) continue;
var startMatch = Regex.Match(parseLine, @"\(start\s+(\S+)\s+(\S+)\)");
if (!startMatch.Success || startMatch.Groups.Count < 3) continue;
var endMatch = Regex.Match(parseLine, @"\(end\s+(\S+)\s+(\S+)\)");
if (!endMatch.Success || endMatch.Groups.Count < 3) continue;
var widthMatch = Regex.Match(parseLine, @"\(width\s+(\S+)\)");
if (!widthMatch.Success || widthMatch.Groups.Count < 2) continue;
var startf = new PointF(float.Parse(startMatch.Groups[1].Value), float.Parse(startMatch.Groups[2].Value));
var endf = new PointF(float.Parse(endMatch.Groups[1].Value), float.Parse(endMatch.Groups[2].Value));
var widthf = float.Parse(widthMatch.Groups[1].Value);
var start = new System.Drawing.Point((int)(startf.X * pixelsPerMm), (int)(startf.Y * pixelsPerMm));
var end = new System.Drawing.Point((int)(endf.X * pixelsPerMm), (int)(endf.Y * pixelsPerMm));
var width = (int)(widthf * pixelsPerMm);
CvInvoke.Rectangle(mat, new Rectangle(start, new System.Drawing.Size(end.X - start.X, end.Y - start.Y)), EmguExtensions.WhiteColor, width);
if (parseLine.Contains("fill solid"))
{
CvInvoke.Rectangle(mat, new Rectangle(start, new System.Drawing.Size(end.X - start.X, end.Y - start.Y)), EmguExtensions.WhiteColor, -1);
}
continue;
}
if (location.IsEmpty) continue;
if (parseLine.StartsWith("(pad "))
{
var layerMatch = Regex.Match(parseLine, @"\S.Cu");
if (!layerMatch.Success || layerMatch.Groups.Count < 1) continue;
var atMatch = Regex.Match(parseLine, @"\(at\s+(\S+)\s+(\S+)\)");
if (!atMatch.Success || atMatch.Groups.Count < 3) continue;
var sizeMatch = Regex.Match(parseLine, @"\(size\s+(\S+)\s+(\S+)\)");
if (!sizeMatch.Success || sizeMatch.Groups.Count < 3) continue;
var drillMatch = Regex.Match(parseLine, @"\(drill\s+(\S+)\)");
var atf = new PointF(float.Parse(atMatch.Groups[1].Value), float.Parse(atMatch.Groups[2].Value));
var sizef = new SizeF(float.Parse(sizeMatch.Groups[1].Value), float.Parse(sizeMatch.Groups[2].Value));
var at = new System.Drawing.Point((int)(location.X * pixelsPerMm + atf.X * pixelsPerMm), (int)(location.Y * pixelsPerMm + atf.Y * pixelsPerMm));
if (parseLine.Contains(" rect ") || parseLine.Contains(" roundrect "))
{
var size = new System.Drawing.Size((int)(sizef.Width * pixelsPerMm), (int)(sizef.Height * pixelsPerMm));
var rect = new Rectangle(at, size);
rect.Offset(-size.Width / 2, -size.Height / 2);
CvInvoke.Rectangle(mat, rect, EmguExtensions.WhiteColor, -1);
}
else if (parseLine.Contains(" oval ") || parseLine.Contains(" circle "))
{
var size = new System.Drawing.Size((int)(sizef.Width / 2 * pixelsPerMm), (int)(sizef.Height / 2 * pixelsPerMm));
CvInvoke.Ellipse(mat, at, size, 0, 0, 360, EmguExtensions.WhiteColor, -1);
}
if (drillMatch.Success && drillMatch.Groups.Count >= 2)
{
var drillf = float.Parse(drillMatch.Groups[1].Value);
var drill = (int)(drillf * pixelsPerMm / 2);
drillPoints.Add(new KeyValuePair<Point, int>(at, drill));
}
continue;
}
}
foreach (var pair in drillPoints)
{
CvInvoke.Circle(mat, pair.Key, pair.Value, EmguExtensions.BlackColor, -1);
}
CvInvoke.Imshow("asd", mat);
CvInvoke.WaitKey();
return;
*/
|