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
|
/*
* 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;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UVtools.Core.Gerber.Primitives;
namespace UVtools.Core.Gerber;
public class Macro : IReadOnlyList<Primitive>
{
#region Properties
/// <summary>
/// Gets the macro name
/// </summary>
public string Name { get; set; } = string.Empty;
public List<Primitive> Primitives { get; } = new();
#endregion
public Macro() { }
public Macro(string name)
{
Name = name;
}
public void ParsePrimitive(string line)
{
line = line.TrimEnd('%', '*');
// 0 Comment: A comment string
if (line[0] == '0')
{
if(line.Length > 2) Primitives.Add(new CommentPrimitive(line[2..]));
return;
}
var commaSplit = line.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if(!byte.TryParse(commaSplit[0], out var code)) return;
switch (code)
{
// 1 Circle: Exposure, Diameter, Center X, Center Y[, Rotation]
case CirclePrimitive.Code:
{
var primitive = new CirclePrimitive(commaSplit[1], commaSplit[2], commaSplit[3], commaSplit[4]);
if (commaSplit.Length > 5) primitive.RotationExpression = commaSplit[5];
Primitives.Add(primitive);
break;
}
// 20 Vector Line: Exposure, Width, Start X, Start Y, End X, End Y, Rotation
case VectorLinePrimitive.Code:
{
var primitive = new VectorLinePrimitive(commaSplit[1], commaSplit[2], commaSplit[3], commaSplit[4], commaSplit[5], commaSplit[6]);
if (commaSplit.Length > 7) primitive.RotationExpression = commaSplit[7];
Primitives.Add(primitive);
break;
}
// 21 Center Line: Exposure, Width, Hight, Center X, Center Y, Rotation
case CenterLinePrimitive.Code:
{
var primitive = new CenterLinePrimitive(commaSplit[1], commaSplit[2], commaSplit[3], commaSplit[4], commaSplit[5]);
if (commaSplit.Length > 6) primitive.RotationExpression = commaSplit[6];
Primitives.Add(primitive);
break;
}
// 4 Outline: Exposure, # vertices, Start X, Start Y, Subsequent points..., Rotation
case OutlinePrimitive.Code:
{
Primitives.Add(new OutlinePrimitive(commaSplit[1], commaSplit[3..^1], commaSplit[^1]));
break;
}
// 5 Outline: Exposure, # vertices, Start X, Start Y, Subsequent points..., Rotation
case PolygonPrimitive.Code:
{
var primitive = new PolygonPrimitive(commaSplit[1], commaSplit[2], commaSplit[3], commaSplit[4], commaSplit[5]);
if (commaSplit.Length > 6) primitive.RotationExpression = commaSplit[6];
Primitives.Add(primitive);
break;
}
}
}
public static Macro? Parse(string line)
{
var match = Regex.Match(line, @"\%AM(\S+)\*");
if (!match.Success || match.Groups.Count < 2) return null;
return new Macro(match.Groups[1].Value);
}
public IEnumerator<Primitive> GetEnumerator()
{
return Primitives.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) Primitives).GetEnumerator();
}
public int Count => Primitives.Count;
public Primitive this[int index] => Primitives[index];
}
|