hytos / DTI_PID / APIDConverter / Form / APIDConverter.cs @ 98aa8635
이력 | 보기 | 이력해설 | 다운로드 (13.3 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Data; |
5 |
using System.Drawing; |
6 |
using System.Linq; |
7 |
using System.Text; |
8 |
using System.Threading.Tasks; |
9 |
using System.Windows.Forms; |
10 |
using DevExpress.XtraEditors.Repository; |
11 |
using DevExpress.XtraEditors.Controls; |
12 |
using DevExpress.XtraEditors; |
13 |
using System.Globalization; |
14 |
using System.Threading; |
15 |
using System.IO; |
16 |
using AVEVA.PID.CustomizationUtility.DB; |
17 |
using AVEVA.PID.CustomizationUtility.Model; |
18 |
using AVEVA.PID.CustomizationUtility.Properties; |
19 |
|
20 |
namespace AVEVA.PID.CustomizationUtility |
21 |
{ |
22 |
public partial class APIDConverter : DevExpress.XtraBars.Ribbon.RibbonForm |
23 |
{ |
24 |
public APIDConverter() |
25 |
{ |
26 |
InitializeComponent(); |
27 |
InitUsedDataTable(); |
28 |
InitGridControl(); |
29 |
|
30 |
layoutControlItem4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; |
31 |
} |
32 |
|
33 |
DataTable ID2SymbolTypeTable = null; |
34 |
private DataTable _ConverterDT = new DataTable(); |
35 |
public List<Document> Documents = new List<Document>(); |
36 |
private Dictionary<string, Document> _DicDocuments = new Dictionary<string, Document>(); |
37 |
|
38 |
private void InitUsedDataTable() |
39 |
{ |
40 |
// Converter Table |
41 |
DataColumn col = _ConverterDT.Columns.Add("colDrawingFileName"); |
42 |
col.Caption = "Drawing File Name"; |
43 |
col = _ConverterDT.Columns.Add("colDrawingFilePath"); |
44 |
col.Caption = "DrawingFilePath"; |
45 |
col = _ConverterDT.Columns.Add("colTemplate"); |
46 |
col.Caption = "Template"; |
47 |
col = _ConverterDT.Columns.Add("colDrawingNumber"); |
48 |
col.Caption = "Drawing Number"; |
49 |
col = _ConverterDT.Columns.Add("colSheetNumber"); |
50 |
col.Caption = "Sheet Number"; |
51 |
col = _ConverterDT.Columns.Add("colStatus"); |
52 |
col.Caption = "Status"; |
53 |
col = _ConverterDT.Columns.Add("colUID"); |
54 |
} |
55 |
|
56 |
private void InitGridControl() |
57 |
{ |
58 |
#region Converter Page |
59 |
gridViewConverter.OptionsSelection.MultiSelect = true; |
60 |
gridViewConverter.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect; |
61 |
|
62 |
gridControlConverter.DataSource = _ConverterDT; |
63 |
|
64 |
gridViewConverter.Columns["colDrawingFileName"].OptionsColumn.AllowEdit = false; |
65 |
gridViewConverter.Columns["colStatus"].OptionsColumn.AllowEdit = false; |
66 |
gridViewConverter.Columns["colDrawingFilePath"].Visible = false; |
67 |
gridViewConverter.Columns["colUID"].Visible = false; |
68 |
|
69 |
gridViewConverter.BestFitColumns(); |
70 |
#endregion |
71 |
|
72 |
#region set lookUpEdit |
73 |
DataTable templateTable = Project_DB.GetDrawingTemplate(); |
74 |
RepositoryItemGridLookUpEdit lookUpEdit = new RepositoryItemGridLookUpEdit(); |
75 |
lookUpEdit.BeginInit(); |
76 |
lookUpEdit.DataSource = templateTable; |
77 |
lookUpEdit.DisplayMember = "Name"; |
78 |
lookUpEdit.ValueMember = "ID"; |
79 |
lookUpEdit.NullText = ""; |
80 |
lookUpEdit.BestFitMode = BestFitMode.BestFitResizePopup; |
81 |
lookUpEdit.EndInit(); |
82 |
|
83 |
lookUpEdit.View.OptionsBehavior.AutoPopulateColumns = false; |
84 |
DevExpress.XtraGrid.Columns.GridColumn colName = lookUpEdit.View.Columns.AddField("Name"); |
85 |
colName.VisibleIndex = 0; |
86 |
DevExpress.XtraGrid.Columns.GridColumn colDate = lookUpEdit.View.Columns.AddField("DateCreation"); |
87 |
colDate.VisibleIndex = 1; |
88 |
DevExpress.XtraGrid.Columns.GridColumn colMachineName = lookUpEdit.View.Columns.AddField("MachineName"); |
89 |
colMachineName.VisibleIndex = 2; |
90 |
|
91 |
gridControlConverter.RepositoryItems.Add(lookUpEdit); |
92 |
gridViewConverter.Columns["colTemplate"].ColumnEdit = lookUpEdit; |
93 |
#endregion |
94 |
} |
95 |
|
96 |
private void btnLoadFile_Click(object sender, EventArgs e) |
97 |
{ |
98 |
Project_Info _ProjectInfo = Project_Info.GetInstance(); |
99 |
|
100 |
OpenFileDialog dia = new OpenFileDialog(); |
101 |
dia.Filter = "Xml Files(*.xml)|*.xml"; |
102 |
dia.InitialDirectory = _ProjectInfo.TempDirPath; |
103 |
dia.Multiselect = true; |
104 |
|
105 |
if (dia.ShowDialog() == DialogResult.OK) |
106 |
{ |
107 |
DataTable drawingTable = Project_DB.SelectDrawingTable(); |
108 |
List<Model.Document> validationFailDocs = new List<Model.Document>(); |
109 |
DataTable symbolMappingTable = Project_DB.GetSymbolMappingTable(); |
110 |
DataTable lineMappingTable = Project_DB.GetLineMappingTable(); |
111 |
DataTable opcMappingTable = Project_DB.GetOPCMappingTable(); |
112 |
|
113 |
foreach (var fileName in dia.FileNames) |
114 |
{ |
115 |
Document document = new Document(fileName, ID2SymbolTypeTable); |
116 |
if (!document.SetAvevaInfo(symbolMappingTable, lineMappingTable, opcMappingTable)) |
117 |
{ |
118 |
validationFailDocs.Add(document); |
119 |
document.Enable = false; |
120 |
} |
121 |
|
122 |
DataRow[] rows = _ConverterDT.Select(string.Format("colDrawingFilePath = '{0}'", fileName)); |
123 |
if (rows.Length == 0) |
124 |
{ |
125 |
DataRow row = _ConverterDT.NewRow(); |
126 |
string drawingName = Path.GetFileNameWithoutExtension(fileName); |
127 |
row["colDrawingFileName"] = drawingName; |
128 |
row["colDrawingFilePath"] = fileName; |
129 |
if (document.Enable) |
130 |
row["colStatus"] = "Ready"; |
131 |
else |
132 |
row["colStatus"] = "Error"; |
133 |
row["colUID"] = ""; |
134 |
|
135 |
row["colDrawingNumber"] = drawingName; |
136 |
row["colSheetNumber"] = GetSheetNumber(drawingTable, drawingName); |
137 |
|
138 |
_ConverterDT.Rows.Add(row); |
139 |
|
140 |
if (document.Enable) |
141 |
gridViewConverter.SelectRow(gridViewConverter.GetRowHandle(_ConverterDT.Rows.IndexOf(row))); |
142 |
} |
143 |
else |
144 |
{ |
145 |
foreach (DataRow row in rows) |
146 |
{ |
147 |
if (document.Enable) |
148 |
row["colStatus"] = "Ready"; |
149 |
else |
150 |
row["colStatus"] = "Error"; |
151 |
|
152 |
if (document.Enable) |
153 |
gridViewConverter.SelectRow(gridViewConverter.GetRowHandle(_ConverterDT.Rows.IndexOf(row))); |
154 |
} |
155 |
} |
156 |
|
157 |
// validation check |
158 |
if (!_DicDocuments.ContainsKey(fileName)) |
159 |
_DicDocuments.Add(fileName, null); |
160 |
|
161 |
_DicDocuments[fileName] = document; |
162 |
} |
163 |
|
164 |
symbolMappingTable.Dispose(); |
165 |
lineMappingTable.Dispose(); |
166 |
opcMappingTable.Dispose(); |
167 |
|
168 |
if (validationFailDocs.Count > 0) |
169 |
{ |
170 |
MessageForm messageForm = new MessageForm(validationFailDocs); |
171 |
messageForm.ShowDialog(); |
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
private string GetSheetNumber(DataTable drawingTable, string drawingName) |
177 |
{ |
178 |
string result = string.Empty; |
179 |
List<DataRow> rows = drawingTable.Select(string.Format("XDNLABEL = '{0}'", drawingName)).ToList(); |
180 |
if (rows.Count != 0) |
181 |
{ |
182 |
int index = 1; |
183 |
while (true) |
184 |
{ |
185 |
DataRow row = rows.Find(x => !DBNull.Value.Equals(x["XDSLABEL"]) && x["XDSLABEL"].ToString() == index.ToString()); |
186 |
if (row == null) |
187 |
{ |
188 |
result = index.ToString(); |
189 |
break; |
190 |
} |
191 |
index++; |
192 |
} |
193 |
} |
194 |
return result; |
195 |
} |
196 |
|
197 |
private void APIDConverter_Load(object sender, EventArgs e) |
198 |
{ |
199 |
Project_Info project = Project_Info.GetInstance(); |
200 |
if (!project.Enable) |
201 |
{ |
202 |
MessageBox.Show("Check Project Setting!", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
203 |
//DialogResult = DialogResult.Cancel; |
204 |
} |
205 |
else |
206 |
{ |
207 |
ID2SymbolTypeTable = Project_DB.GetSymbolType(); |
208 |
} |
209 |
} |
210 |
|
211 |
private void btnRun_Click(object sender, EventArgs e) |
212 |
{ |
213 |
if (gridViewConverter.GetSelectedRows().Length == 0) |
214 |
{ |
215 |
MessageBox.Show("Select Document", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
216 |
return; |
217 |
} |
218 |
List<Model.Document> validationFailDocs = new List<Model.Document>(); |
219 |
DataTable symbolMappingTable = Project_DB.GetSymbolMappingTable(); |
220 |
DataTable lineMappingTable = Project_DB.GetLineMappingTable(); |
221 |
DataTable opcMappingTable = Project_DB.GetOPCMappingTable(); |
222 |
DataTable tDrawing = Project_DB.GetDrawings(); |
223 |
Documents.Clear(); |
224 |
foreach (int rowHandle in gridViewConverter.GetSelectedRows()) |
225 |
{ |
226 |
string _FilePath = gridViewConverter.GetRowCellDisplayText(rowHandle, "colDrawingFilePath"); |
227 |
string _ID = gridViewConverter.GetRowCellValue(rowHandle, "colTemplate").ToString(); |
228 |
string _TemplateName = gridViewConverter.GetRowCellDisplayText(rowHandle, "colTemplate"); |
229 |
string _DrawingNumber = gridViewConverter.GetRowCellDisplayText(rowHandle, "colDrawingNumber"); |
230 |
string _SheetNumber = gridViewConverter.GetRowCellDisplayText(rowHandle, "colSheetNumber"); |
231 |
|
232 |
DataRow[] rows = tDrawing.Select(string.Format("NAME = '{0}'", Path.GetFileNameWithoutExtension(_FilePath) + ".png")); |
233 |
if (rows.Length != 1) |
234 |
continue; |
235 |
|
236 |
Document document = _DicDocuments[_FilePath]; |
237 |
document.UID = rows[0]["UID"].ToString(); |
238 |
document.AvevaDrawingNumber = _DrawingNumber; |
239 |
document.AvevaSheetNumber = _SheetNumber; |
240 |
document.AvevaTemplateID = _ID; |
241 |
document.AvevaTemplateName = _TemplateName; |
242 |
// validation check |
243 |
if (document.SetAvevaInfo(symbolMappingTable, lineMappingTable, opcMappingTable)) |
244 |
Documents.Add(document); |
245 |
else |
246 |
validationFailDocs.Add(document); |
247 |
} |
248 |
|
249 |
symbolMappingTable.Dispose(); |
250 |
lineMappingTable.Dispose(); |
251 |
opcMappingTable.Dispose(); |
252 |
|
253 |
if (validationFailDocs.Count > 0) |
254 |
{ |
255 |
MessageForm messageForm = new MessageForm(validationFailDocs); |
256 |
messageForm.ShowDialog(); |
257 |
} |
258 |
else if (Documents.Count > 0) |
259 |
{ |
260 |
DialogResult = DialogResult.OK; |
261 |
} |
262 |
else |
263 |
{ |
264 |
|
265 |
} |
266 |
} |
267 |
|
268 |
private void btnRefresh_Click(object sender, EventArgs e) |
269 |
{ |
270 |
foreach (DataRow row in _ConverterDT.Rows) |
271 |
{ |
272 |
string fileName = row["colDrawingFilePath"].ToString(); |
273 |
Document document = new Document(fileName, ID2SymbolTypeTable); |
274 |
|
275 |
if (document.Enable) |
276 |
row["colStatus"] = "Ready"; |
277 |
else |
278 |
row["colStatus"] = "Error"; |
279 |
|
280 |
if (!_DicDocuments.ContainsKey(fileName)) |
281 |
_DicDocuments.Add(fileName, null); |
282 |
|
283 |
_DicDocuments[fileName] = document; |
284 |
|
285 |
if (document.Enable) |
286 |
gridViewConverter.SelectRow(gridViewConverter.GetRowHandle(_ConverterDT.Rows.IndexOf(row))); |
287 |
} |
288 |
} |
289 |
|
290 |
private void btnID2DB_Click(object sender, EventArgs e) |
291 |
{ |
292 |
ProjectForm form = new ProjectForm(); |
293 |
if (form.ShowDialog() == DialogResult.OK) |
294 |
{ |
295 |
Project_Info _ProjectInfo = Project_Info.GetInstance(); |
296 |
_ProjectInfo.DefaultPath = Settings.Default.ProjectPath; |
297 |
_ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType; |
298 |
_ProjectInfo.ServerIP = Settings.Default.ProjectServerIP; |
299 |
_ProjectInfo.Port = Settings.Default.ProjectPort; |
300 |
_ProjectInfo.DBUser = Settings.Default.ProjectDBUser; |
301 |
_ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword; |
302 |
|
303 |
if (Project_DB.ConnTestAndCreateTable()) |
304 |
{ |
305 |
_ProjectInfo.Enable = true; |
306 |
MessageBox.Show("Success project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
307 |
} |
308 |
else |
309 |
{ |
310 |
_ProjectInfo.Enable = false; |
311 |
MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
private void btnItemMapping_Click(object sender, EventArgs e) |
317 |
{ |
318 |
MappingForm form = new MappingForm(); |
319 |
if (form.ShowDialog() == DialogResult.OK) |
320 |
{ |
321 |
|
322 |
} |
323 |
} |
324 |
} |
325 |
} |