-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.cs
84 lines (74 loc) · 1.99 KB
/
Worker.cs
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
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace GCodeAdjust
{
public class Worker
{
Adjuster[] Adjusters;
readonly Regex RexG1;
readonly string Text;
public Worker(
string _Text)
{
Text = _Text;
RexG1 = new Regex(
@"(^\s*G1)(\s+X\S+)*(\s+Y\S+)*(\s+Z\S+)*",
RegexOptions.Multiline);
Adjusters = new[] { Adjuster.Null, Adjuster.Null, Adjuster.Null };
}
public string Adjust(
decimal diffX,
decimal diffY,
decimal diffZ)
{
Adjusters = new[] { new Adjuster(diffX), new Adjuster(diffY), new Adjuster(diffZ) };
return RexG1
.Replace(
Text,
MatchEval);
}
string MatchEval(
Match m)
{
var replacement =
(m.Groups[1].Success
? m.Groups[1].Value
: string.Empty)
+ string.Join(
string.Empty,
Adjusters
.Select((s, i) =>
m.Groups[i + 2].Success
? " "
+ m.Groups[i + 2].Value.TrimStart()[0]
+ s.Do(m.Groups[i + 2].Value.TrimStart().Substring(1))
: string.Empty));
return replacement;
}
}
public class Adjuster
{
readonly decimal Diff;
public Adjuster(
decimal diff)
{
Diff = diff;
}
public string Do(
string text)
{
return (decimal
.Parse(text)
+ Diff)
.ToString();
}
public static Adjuster Null
{
get
{
return new Adjuster(0M);
}
}
}
}