-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGOperation.cs
183 lines (154 loc) · 4.95 KB
/
GOperation.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
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
using System;
using System.Collections.Generic;
using System.Numerics;
namespace GCEd
{
class GOperation
{
public readonly static IEqualityComparer<GOperation> ByGLineEqualityComparer = new ByGLineEqualityComparerImpl();
public GLine Line { get; set; }
public float AbsXStart { get; set; }
public float AbsYStart { get; set; }
public float AbsZStart { get; set; }
public float AbsXEnd { get; set; }
public float AbsYEnd { get; set; }
public float AbsZEnd { get; set; }
public float AbsI { get; set; }
public float AbsJ { get; set; }
public float AbsK { get; set; }
public decimal F { get; set; }
public decimal S { get; set; }
public bool Active { get; set; }
public bool Absolute { get; set; }
public TimeSpan TimeStart { get; set; }
public TimeSpan TimeEnd { get; set; }
public Vector2 AbsStart { get => new Vector2(AbsXStart, AbsYStart); set => (AbsXStart, AbsYStart) = (value.X, value.Y); }
public Vector2 AbsEnd { get => new Vector2(AbsXEnd, AbsYEnd); set => (AbsXEnd, AbsYEnd) = (value.X, value.Y); }
public Vector2 AbsOffset { get => new Vector2(AbsI, AbsJ); set => (AbsI, AbsJ) = (value.X, value.Y); }
public TimeSpan TimeDuration => TimeEnd - TimeStart;
public GOperation? OriginalValues { get; private set; }
public GOperation(GLine line)
{
Line = line;
}
public void SaveOriginalValues()
{
OriginalValues = new GOperation(Line)
{
AbsXStart = AbsXStart,
AbsYStart = AbsYStart,
AbsZStart = AbsZStart,
AbsXEnd = AbsXEnd,
AbsYEnd = AbsYEnd,
AbsZEnd = AbsZEnd,
AbsI = AbsI,
AbsJ = AbsJ,
AbsK = AbsK,
F = F,
S = S,
Active = Active,
Absolute = Absolute
};
}
public void RestoreOriginalValues()
{
if (OriginalValues == null) return;
AbsXStart = OriginalValues.AbsXStart;
AbsYStart = OriginalValues.AbsYStart;
AbsZStart = OriginalValues.AbsZStart;
AbsXEnd = OriginalValues.AbsXEnd;
AbsYEnd = OriginalValues.AbsYEnd;
AbsZEnd = OriginalValues.AbsZEnd;
AbsI = OriginalValues.AbsI;
AbsJ = OriginalValues.AbsJ;
AbsK = OriginalValues.AbsK;
F = OriginalValues.F;
S = OriginalValues.S;
Active = OriginalValues.Active;
Absolute = OriginalValues.Absolute;
}
public void Execute(GContext context)
{
AbsXStart = (float)context.X;
AbsYStart = (float)context.Y;
AbsZStart = (float)context.Z;
AbsI = AbsXStart + (float)context.ToMM(Line.I.GetValueOrDefault());
AbsJ = AbsYStart + (float)context.ToMM(Line.J.GetValueOrDefault());
AbsK = AbsZStart + (float)context.ToMM(Line.K.GetValueOrDefault());
if (Line.Instruction == GInstruction.G0
|| Line.Instruction == GInstruction.G1
|| Line.Instruction == GInstruction.G2
|| Line.Instruction == GInstruction.G3)
{
if (Line.F.HasValue) context.F = Line.F.Value;
if (Line.S.HasValue) context.S = Line.S.Value;
F = context.F;
S = context.S;
if (context.Absolute)
{
if (Line.X.HasValue) context.X = context.ToMM(Line.X.Value);
if (Line.Y.HasValue) context.Y = context.ToMM(Line.Y.Value);
if (Line.Z.HasValue) context.Z = context.ToMM(Line.Z.Value);
}
else
{
if (Line.X.HasValue) context.X += context.ToMM(Line.X.Value);
if (Line.Y.HasValue) context.Y += context.ToMM(Line.Y.Value);
if (Line.Z.HasValue) context.Z += context.ToMM(Line.Z.Value);
}
}
else if (Line.Instruction == GInstruction.G20)
{
context.Inches = true;
}
else if (Line.Instruction == GInstruction.G21)
{
context.Inches = false;
}
else if (Line.Instruction == GInstruction.G90)
{
context.Absolute = true;
}
else if (Line.Instruction == GInstruction.G91)
{
context.Absolute = false;
}
else if (Line.Instruction == GInstruction.M3)
{
context.Active = true;
context.Dynamic = false;
}
else if (Line.Instruction == GInstruction.M4)
{
context.Active = true;
context.Dynamic = true;
}
else if (Line.Instruction == GInstruction.M5)
{
context.Active = false;
context.Dynamic = false;
}
Active = context.Active;
Absolute = context.Absolute;
AbsXEnd = (float)context.X;
AbsYEnd = (float)context.Y;
AbsZEnd = (float)context.Z;
TimeStart = context.Time;
var length = 0f;
if (Line.IsLine) length = Geometry.LineLength(AbsStart, AbsEnd);
else if (Line.IsArc) length = Geometry.ArcLength(AbsStart, AbsEnd, AbsOffset, Line.Instruction == GInstruction.G2);
if (F > 0) TimeEnd = context.Time + TimeSpan.FromSeconds(length * 60 / (double)F);
context.Time = TimeEnd;
if (Line.Instruction == GInstruction.Directive)
{
AbsStart = Line.XY;
if (Line.Directive == Directive.Line || Line.Directive == Directive.Circle) AbsEnd = Line.XY + Line.IJ;
}
}
private class ByGLineEqualityComparerImpl : IEqualityComparer<GOperation>
{
public bool Equals(GOperation? x, GOperation? y) => Object.ReferenceEquals(x?.Line, y?.Line);
public int GetHashCode(GOperation obj) => obj.Line == null ? 0 : obj.Line.GetHashCode();
}
}
}