프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / Form / APIDConverter.cs @ 0249157a

이력 | 보기 | 이력해설 | 다운로드 (11.9 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

    
31
        DataTable ID2SymbolTypeTable = null;
32
        private DataTable _ConverterDT = new DataTable();
33
        public List<Document> Documents = new List<Document>();
34
        private Dictionary<string, Document> _DicDocuments = new Dictionary<string, Document>();
35

    
36
        private void InitUsedDataTable()
37
        {
38
            // Converter Table
39
            DataColumn col = _ConverterDT.Columns.Add("colDrawingFileName");
40
            col.Caption = "Drawing File Name";
41
            col = _ConverterDT.Columns.Add("colDrawingFilePath");
42
            col.Caption = "DrawingFilePath";
43
            col = _ConverterDT.Columns.Add("colTemplate");
44
            col.Caption = "Template";
45
            col = _ConverterDT.Columns.Add("colDrawingNumber");
46
            col.Caption = "Drawing Number";
47
            col = _ConverterDT.Columns.Add("colSheetNumber");
48
            col.Caption = "Sheet Number";
49
            col = _ConverterDT.Columns.Add("colStatus");
50
            col.Caption = "Status";
51
            col = _ConverterDT.Columns.Add("colUID");
52
        }
53

    
54
        private void InitGridControl()
55
        {
56
            #region Converter Page
57
            gridViewConverter.OptionsSelection.MultiSelect = true;
58
            gridViewConverter.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
59

    
60
            gridControlConverter.DataSource = _ConverterDT;
61

    
62
            gridViewConverter.Columns["colDrawingFileName"].OptionsColumn.AllowEdit = false;
63
            gridViewConverter.Columns["colStatus"].OptionsColumn.AllowEdit = false;
64
            gridViewConverter.Columns["colDrawingFilePath"].Visible = false;
65
            gridViewConverter.Columns["colUID"].Visible = false;
66

    
67
            gridViewConverter.BestFitColumns();
68
            #endregion
69

    
70
            #region set lookUpEdit
71
            DataTable templateTable = Project_DB.GetDrawingTemplate();
72
            RepositoryItemGridLookUpEdit lookUpEdit = new RepositoryItemGridLookUpEdit();
73
            lookUpEdit.BeginInit();
74
            lookUpEdit.DataSource = templateTable;
75
            lookUpEdit.DisplayMember = "Name";
76
            lookUpEdit.ValueMember = "ID";
77
            lookUpEdit.NullText = "";
78
            lookUpEdit.BestFitMode = BestFitMode.BestFitResizePopup;
79
            lookUpEdit.EndInit();
80

    
81
            lookUpEdit.View.OptionsBehavior.AutoPopulateColumns = false;
82
            DevExpress.XtraGrid.Columns.GridColumn colName = lookUpEdit.View.Columns.AddField("Name");
83
            colName.VisibleIndex = 0;
84
            DevExpress.XtraGrid.Columns.GridColumn colDate = lookUpEdit.View.Columns.AddField("DateCreation");
85
            colDate.VisibleIndex = 1;
86
            DevExpress.XtraGrid.Columns.GridColumn colMachineName = lookUpEdit.View.Columns.AddField("MachineName");
87
            colMachineName.VisibleIndex = 2;
88

    
89
            gridControlConverter.RepositoryItems.Add(lookUpEdit);
90
            gridViewConverter.Columns["colTemplate"].ColumnEdit = lookUpEdit;
91
            #endregion
92
        }
93

    
94
        private void btnLoadFile_Click(object sender, EventArgs e)
95
        {
96
            Project_Info _ProjectInfo = Project_Info.GetInstance();
97

    
98
            OpenFileDialog dia = new OpenFileDialog();
99
            dia.Filter = "Xml Files(*.xml)|*.xml";
100
            dia.InitialDirectory = _ProjectInfo.TempDirPath;
101
            dia.Multiselect = true;
102

    
103
            if (dia.ShowDialog() == DialogResult.OK)
104
            {
105
                DataTable drawingTable = Project_DB.SelectDrawingTable();
106

    
107
                foreach (var fileName in dia.FileNames)
108
                {
109
                    Document document = new Document(fileName, ID2SymbolTypeTable);
110

    
111
                    DataRow[] rows = _ConverterDT.Select(string.Format("colDrawingFilePath = '{0}'", fileName));
112
                    if (rows.Length == 0)
113
                    {
114
                        DataRow row = _ConverterDT.NewRow();
115
                        string drawingName = Path.GetFileNameWithoutExtension(fileName);
116
                        row["colDrawingFileName"] = drawingName;
117
                        row["colDrawingFilePath"] = fileName;
118
                        if (document.Enable)
119
                            row["colStatus"] = "Ready";
120
                        else
121
                            row["colStatus"] = "Error";
122
                        row["colUID"] = "";
123

    
124
                        row["colDrawingNumber"] = drawingName;
125
                        row["colSheetNumber"] = GetSheetNumber(drawingTable, drawingName);
126

    
127
                        _ConverterDT.Rows.Add(row);
128

    
129
                        if (document.Enable)
130
                            gridViewConverter.SelectRow(gridViewConverter.GetRowHandle(_ConverterDT.Rows.IndexOf(row)));
131
                    }
132
                    else
133
                    {
134
                        foreach (DataRow row in rows)
135
                        {
136
                            if (document.Enable)
137
                                row["colStatus"] = "Ready";
138
                            else
139
                                row["colStatus"] = "Error";
140
                           
141
                            if (document.Enable)
142
                                gridViewConverter.SelectRow(gridViewConverter.GetRowHandle(_ConverterDT.Rows.IndexOf(row)));
143
                        }
144
                    }
145

    
146
                    if (!_DicDocuments.ContainsKey(fileName))
147
                        _DicDocuments.Add(fileName, null);
148

    
149
                    _DicDocuments[fileName] = document;
150
                }
151
            }
152
        }
153

    
154
        private string GetSheetNumber(DataTable drawingTable, string drawingName)
155
        {
156
            string result = string.Empty;
157
            List<DataRow> rows = drawingTable.Select(string.Format("XDNLABEL = '{0}'", drawingName)).ToList();
158
            if (rows.Count != 0)
159
            {
160
                int index = 1;
161
                while (true)
162
                {
163
                    DataRow row = rows.Find(x => !DBNull.Value.Equals(x["XDSLABEL"]) && x["XDSLABEL"].ToString() == index.ToString());
164
                    if (row == null)
165
                    {
166
                        result = index.ToString();
167
                        break;
168
                    }
169
                    index++;
170
                }    
171
            }
172
            return result;
173
        }
174

    
175
        private void APIDConverter_Load(object sender, EventArgs e)
176
        {
177
            Project_Info project = Project_Info.GetInstance();
178
            if (!project.Enable)
179
            {
180
                MessageBox.Show("Check Project Setting!", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning);
181
                //DialogResult = DialogResult.Cancel;
182
            }
183
            else
184
            {
185
                ID2SymbolTypeTable = Project_DB.GetSymbolType();
186
            }
187
        }
188

    
189
        private void btnRun_Click(object sender, EventArgs e)
190
        {
191
            if (gridViewConverter.GetSelectedRows().Length == 0)
192
            {
193
                MessageBox.Show("Select Document", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning);
194
                return;
195
            }
196

    
197
            DataTable symbolMappingTable = Project_DB.GetSymbolMappingTable();
198
            DataTable lineMappingTable = Project_DB.GetLineMappingTable();
199
            DataTable opcMappingTable = Project_DB.GetOPCMappingTable();
200
            DataTable tDrawing = Project_DB.GetDrawings();
201
            Documents.Clear();
202
            foreach (int rowHandle in gridViewConverter.GetSelectedRows())
203
            {
204
                string _FilePath = gridViewConverter.GetRowCellDisplayText(rowHandle, "colDrawingFilePath");
205
                string _ID = gridViewConverter.GetRowCellValue(rowHandle, "colTemplate").ToString();
206
                string _TemplateName = gridViewConverter.GetRowCellDisplayText(rowHandle, "colTemplate");
207
                string _DrawingNumber = gridViewConverter.GetRowCellDisplayText(rowHandle, "colDrawingNumber");
208
                string _SheetNumber = gridViewConverter.GetRowCellDisplayText(rowHandle, "colSheetNumber");
209

    
210
                DataRow[] rows = tDrawing.Select(string.Format("NAME = '{0}'", Path.GetFileNameWithoutExtension(_FilePath) + ".png"));
211
                if (rows.Length != 1)
212
                    continue;
213

    
214
                Document document = _DicDocuments[_FilePath];
215
                document.UID = rows[0]["UID"].ToString();
216
                document.AvevaDrawingNumber = _DrawingNumber;
217
                document.AvevaSheetNumber = _SheetNumber;
218
                document.AvevaTemplateID = _ID;
219
                document.AvevaTemplateName = _TemplateName;
220
                // validation check
221
                if (document.SetAvevaInfo(symbolMappingTable, lineMappingTable, opcMappingTable))
222
                    Documents.Add(document);
223
            }
224

    
225
            symbolMappingTable.Dispose();
226
            lineMappingTable.Dispose();
227
            opcMappingTable.Dispose();
228

    
229
            if (Documents.Count > 0)
230
            {
231
                DialogResult = DialogResult.OK;
232
            }
233
            else
234
            {
235

    
236
            }
237
        }
238

    
239
        private void btnRefresh_Click(object sender, EventArgs e)
240
        {
241
            foreach (DataRow row in _ConverterDT.Rows)
242
            {
243
                string fileName = row["colDrawingFilePath"].ToString();
244
                Document document = new Document(fileName, ID2SymbolTypeTable);
245

    
246
                if (document.Enable)
247
                    row["colStatus"] = "Ready";
248
                else
249
                    row["colStatus"] = "Error";
250

    
251
                if (!_DicDocuments.ContainsKey(fileName))
252
                    _DicDocuments.Add(fileName, null);
253

    
254
                _DicDocuments[fileName] = document;
255

    
256
                if (document.Enable)
257
                    gridViewConverter.SelectRow(gridViewConverter.GetRowHandle(_ConverterDT.Rows.IndexOf(row)));
258
            }
259
        }
260

    
261
        private void btnID2DB_Click(object sender, EventArgs e)
262
        {
263
            ProjectForm form = new ProjectForm();
264
            if (form.ShowDialog() == DialogResult.OK)
265
            {
266
                Project_Info _ProjectInfo = Project_Info.GetInstance();
267
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
268
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
269
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
270
                _ProjectInfo.Port = Settings.Default.ProjectPort;
271
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
272
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
273

    
274
                if (Project_DB.ConnTestAndCreateTable())
275
                {
276
                    _ProjectInfo.Enable = true;
277
                    MessageBox.Show("Success project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
278
                }
279
                else
280
                {
281
                    _ProjectInfo.Enable = false;
282
                    MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
283
                }
284
            }
285
        }
286

    
287
        private void btnItemMapping_Click(object sender, EventArgs e)
288
        {
289
            MappingForm form = new MappingForm();
290
            if (form.ShowDialog() == DialogResult.OK)
291
            {
292

    
293
            }
294
        }
295
    }
296
}
클립보드 이미지 추가 (최대 크기: 500 MB)