StorePosControl.cs
9.7 KB
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
using DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TheMachine
{
using crc = OnlineStore.CodeResourceControl;
public partial class StorePosControl : UserControl
{
private Store_ConfigBase _Config;
public Store_ConfigBase Config
{
get { return _Config; }
set
{
if (value == null)
return;
_Config = value;
}
}
public StoreMachine Machine { get; set; }
public StorePosControl()
{
InitializeComponent();
this.Tag = "not";
crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
tableLayoutPanel1.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.SetValue(tableLayoutPanel1, true, null);
}
private void Crc_LanguageChangeEvent(object sender, EventArgs e)
{
if (!this.Created)
return;
this.Invoke((EventHandler)delegate
{
try
{
LoadPos(_aCStorePosition);
}
catch (Exception ee)
{
MessageBox.Show("Crc_LanguageChangeEvent LoadPos:" + ee.ToString());
}
});
}
private ACStorePosition _aCStorePosition;
public void LoadPos(ACStorePosition aCStorePosition)
{
if (aCStorePosition == null)
return;
_aCStorePosition = aCStorePosition;
tableLayoutPanel1.SuspendLayout();
int maxrow = tableLayoutPanel1.Height / 37;
tableLayoutPanel1.Controls.Clear();
this.tableLayoutPanel1.RowStyles.Clear();
//this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType., 26));
int r = 0;
int c = 0;
int lastSubType = 0;
Random random = new Random(1);
Color color = Color.Black;
var type = typeof(ACStorePosition);
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo prop in props)
{
var editable = prop.GetCustomAttributes(typeof(EditableAttribute));
if (editable.Count() == 0)
continue;
var asixno = ((EditableAttribute)editable.First()).Axisno+(Machine.MachineSide==MachineSideE.Left?5:0);
var attrib = prop.GetCustomAttributes(typeof(CSVAttribute));
if (attrib.Count() == 0)
continue;
var FieldName = ((CSVAttribute)attrib.First()).FieldName;
var ProValue = prop.GetValue(aCStorePosition).ToString();
if (asixno != lastSubType)
{
lastSubType = asixno;
color = Color.FromArgb(random.Next(30, 150), random.Next(30, 150), random.Next(30, 150));
}
Button button = new Button();
button.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
button.Name = prop.Name;
button.Size = new System.Drawing.Size(225, 27);
button.Text = crc.GetString(prop.Name, FieldName);
button.Click += Button_Click;
button.ForeColor = color;
button.Tag = asixno;
tableLayoutPanel1.Controls.Add(button, c, r);
TextBox textBox = new TextBox();
textBox.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
textBox.Name = prop.Name;
textBox.Margin = new Padding(4, 8, 0, 0);
textBox.Size = new System.Drawing.Size(180, 23);
textBox.Text = ProValue;
textBox.KeyPress += TextBox_KeyPress;
textBox.TextChanged += TextBox_TextChanged;
setAxisPopup(textBox, prop.Name, getConfigMoveAxis(asixno).Config);
tableLayoutPanel1.Controls.Add(textBox, c + 1, r);
textBox.Tag = prop.PropertyType.Name;
tableLayoutPanel1.SetColumnSpan(textBox, 2);
r++;
if (r > maxrow)
{
r = 0;
c += 3;
}
}
if (r <= maxrow + 1)
{
Label button1 = new Label();
tableLayoutPanel1.Controls.Add(button1, c, r++);
}
tableLayoutPanel1.ResumeLayout(true);
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
var lastlen = lastvalue.Length;
var s = (sender as TextBox);
if (s.Tag.ToString() == "Int32")
{
if (!int.TryParse(s.Text, out _))
s.Text = lastvalue;
//s.SelectionStart = lastselectindex;
}
else if (s.Tag.ToString() == "Double")
{
if (!double.TryParse(s.Text, out _))
s.Text = lastvalue;
//s.SelectionStart = lastselectindex;
}
}
string lastvalue = "";
int lastselectindex = 0;
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
var s = (sender as TextBox);
if (s.Tag.ToString() == "Int32")
{
if (int.TryParse(s.Text, out _))
lastvalue = s.Text;
}
else if (s.Tag.ToString() == "Double")
{
if (double.TryParse(s.Text, out _))
lastvalue = s.Text;
}
lastselectindex = s.SelectionStart;
}
private void Button_Click(object sender, EventArgs e)
{
var cc = tableLayoutPanel1.Controls.Find(((Button)sender).Name, false);
var speed = tableLayoutPanel1.Controls.Find(((Button)sender).Name+ "_speed", false);
var axis = getConfigMoveAxis((int)((Button)sender).Tag);
ConfigMoveAxis configMoveAxis = axis.Config;
var targetpos = int.Parse(cc[1].Text);
if (!axis.IsSafe(targetpos, out string msg))
{
MessageBox.Show(msg);
return;
}
var targetSpeed = configMoveAxis.TargetSpeed;
var AddSpeed = configMoveAxis.AddSpeed > 0 ? configMoveAxis.AddSpeed : targetSpeed * 4;
var DelSpeed = configMoveAxis.DelSpeed > 0 ? configMoveAxis.DelSpeed : targetSpeed * 4;
AxisManager.AbsMove("", configMoveAxis.GetAxisValue(), int.Parse(cc[1].Text), targetSpeed, AddSpeed, DelSpeed);//, configMoveAxis.AddSpeed, configMoveAxis.DelSpeed);
}
void setAxisPopup(Control c, string title, ConfigMoveAxis configMoveAxis)
{
c.Tag = "A";
c.Click += (cc, e) =>
{
AxisTipControl axisTipControl = new AxisTipControl();
axisTipControl.ShowTips((Control)cc, title, configMoveAxis.GetAxisValue(), configMoveAxis.TargetSpeed);
};
}
AxisBean getConfigMoveAxis(int Axisid)
{
foreach (AxisBean configMoveAxis in AxisBean.List[Machine.MachineSide])
{
if (configMoveAxis.Config.GetAxisValue() == Axisid)
return configMoveAxis;
}
return null;
}
private void ConfigControl_Load(object sender, EventArgs e)
{
OnlineStore.CodeResourceControl.LanguageChangeEvent += CodeResourceControl_LanguageChange;
CodeResourceControl_LanguageChange(null,null);
}
private void CodeResourceControl_LanguageChange(object sender, EventArgs e)
{
OnlineStore.CodeResourceControl.LanguageProcess(this);
}
private void btnSavePos_Click(object sender, EventArgs e)
{
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i].GetType().Name != "TextBox")
continue;
TextBox textBox = (TextBox)tableLayoutPanel1.Controls[i];
PropertyInfo pi = _aCStorePosition.GetType().GetProperty(textBox.Name);
if (pi != null)
{
//var cc = Config.configList.Find(new Predicate<ConfigBase>((c) => { return c.ProName == textBox.Name; }));
//if (cc != null)
// cc.ProValue = textBox.Text;
if (pi.PropertyType.Name == "Int32")
pi.SetValue(_aCStorePosition, int.Parse(textBox.Text));
else if (pi.PropertyType.Name == "Double")
pi.SetValue(_aCStorePosition, double.Parse(textBox.Text));
else
pi.SetValue(_aCStorePosition, textBox.Text);
}
}
var positionConfigFile = Path.Combine(Application.StartupPath, "StoreConfig\\"+ Machine.MachineSide.ToString()+ "\\linePositions.csv");
bool result = CSVPositionReader<ACStorePosition>.SavePostion(positionConfigFile, _aCStorePosition);
}
}
}