프로젝트

일반

사용자정보

개정판 6b9e7a56

ID6b9e7a56350cae7bd8f2d3691f891eb1925efc35
상위 1be87cbd
하위 b3b970ef

gaqhf 이(가) 3년 이상 전에 추가함

dev issue #000 : add

Change-Id: Id659aaa3347c5df582b157d90a8a2055d72d43a7

차이점 보기:

DTI_PID/ID2PSN/App.config
1
<?xml version="1.0" encoding="utf-8"?>
1
<?xml version="1.0" encoding="utf-8"?>
2 2
<configuration>
3
    <startup> 
4
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
5
    </startup>
6
</configuration>
3
  <configSections>
4
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
5
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
6
  </configSections>
7
  <startup>
8
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
9
  </startup>
10
  <entityFramework>
11
    <providers>
12
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
13
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
14
    </providers>
15
  </entityFramework>
16
  <system.data>
17
    <DbProviderFactories>
18
      <remove invariant="System.Data.SQLite.EF6" />
19
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
20
    <remove invariant="System.Data.SQLite" /><add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /></DbProviderFactories>
21
  </system.data>
22
</configuration>
DTI_PID/ID2PSN/DB.cs
3 3
using System.Linq;
4 4
using System.Text;
5 5
using System.Threading.Tasks;
6
using System.Data.SQLite;
7
using System.Data;
8
using System.Globalization;
9
using System.Data.SQLite;
10
using System.Data;
6 11

  
7 12
namespace ID2PSN
8 13
{
9
    class DB
14
    public static class DB
10 15
    {
16
        const string PSN_TOPOLOGY_RULE = "T_PSN_TOPOLOGY_RULE";
17
        const string PSN_HEADER_SETTING = "T_PSN_HEADER_SETTING";
18
        const string PSN_PATHITEMS = "T_PSN_PATHITEMS";
19
        const string PSN_SEQUENCEDATA = "T_PSN_SEQUENCEDATA";
20
        const string PSN_PIPESYSTEMNETWORK = "T_PSN_PIPESYSTEMNETWORK";
21
        const string PSN_EQUIPMENT = "T_PSN_EQUIPMENT";
22
        const string PSN_NOZZLE = "T_PSN_NOZZLE";
23
        const string PSN_FLUIDCODE = "T_PSN_FLUIDCODE";
24
        const string PSN_PIPINGMATLCLASS = "T_PSN_PIPINGMATLCLASS";
25
        public static bool ConnTestAndCreateTable()
26
        {
27
            bool result = false;
28
            ID2Info id2Info = ID2Info.GetInstance();
29
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", id2Info.DBFilePath), true))
30
            {
31
                try
32
                {
33
                    connection.Open();
34
                    if (connection.State == ConnectionState.Open)
35
                    {
36
                        using (SQLiteCommand cmd = connection.CreateCommand())
37
                        {
38
                            cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
39
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
40
                            using (DataTable dt = new DataTable())
41
                            {
42
                                dt.Load(dr);
11 43

  
44
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_HEADER_SETTING)).Length == 0)
45
                                {
46
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)", PSN_HEADER_SETTING);
47
                                    cmd.ExecuteNonQuery();
48
                                }
49
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_TOPOLOGY_RULE)).Length == 0)
50
                                {
51
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT)", PSN_TOPOLOGY_RULE);
52
                                    cmd.ExecuteNonQuery();
53
                                }
54
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_PATHITEMS)).Length == 0)
55
                                {
56
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, SequenceData_OID TEXT, TopologySet_OID TEXT, BranchTopologySet_OID TEXT, PipeLine_OID TEXT, ITEMNAME TEXT, ITEMTAG TEXT, TYPE TEXT, PIDNAME TEXT, NPD TEXT, PipeSystemNetwork_OID TEXT, PipeRun_OID TEXT)", PSN_PATHITEMS);
57
                                    cmd.ExecuteNonQuery();
58
                                }
59
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_SEQUENCEDATA)).Length == 0)
60
                                {
61
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, SERIALNUMBER TEXT, PathItem_OID TEXT, TopologySet_OID_Key TEXT)", PSN_SEQUENCEDATA);
62
                                    cmd.ExecuteNonQuery();
63
                                }
64
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_PIPESYSTEMNETWORK)).Length == 0)
65
                                {
66
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, Type TEXT, OrderNumber TEXT, Pipeline_OID TEXT, FROM_DATA TEXT, TO_DATA TEXT, TopologySet_OID_Key TEXT, PSNRevisionNumber TEXT, PathOID TEXT, PBS TEXT, PIDDrawings TEXT)", PSN_PIPESYSTEMNETWORK);
67
                                    cmd.ExecuteNonQuery();
68
                                }
69

  
70
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_EQUIPMENT)).Length == 0)
71
                                {
72
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, ITEMTAG TEXT, XCOORDS TEXT, YCOORDS TEXT)", PSN_EQUIPMENT);
73
                                    cmd.ExecuteNonQuery();
74
                                }
75
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_NOZZLE)).Length == 0)
76
                                {
77
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, ITEMTAG TEXT, XCOORDS TEXT, YCOORDS TEXT, Equipment_OID TEXT, FLUID TEXT, NPD TEXT, ROTATION TEXT, FlowDirection TEXT)", PSN_NOZZLE);
78
                                    cmd.ExecuteNonQuery();
79
                                }
80
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_FLUIDCODE)).Length == 0)
81
                                {
82
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT, Code TEXT, Description TEXT, Condition TEXT, GroundLevel TEXT)", PSN_FLUIDCODE);
83
                                    cmd.ExecuteNonQuery();
84
                                }
85
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_PIPINGMATLCLASS)).Length == 0)
86
                                {
87
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT, Priority TEXT, Code TEXT, Description TEXT, Condition TEXT, GroundLevel TEXT)", PSN_PIPINGMATLCLASS);
88
                                    cmd.ExecuteNonQuery();
89
                                }
90
                            }
91
                        }
92
                        result = true;
93
                    }
94
                    connection.Close();
95
                }
96
                catch (Exception ex)
97
                {
98
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
99
                }
100
                finally
101
                {
102
                    connection.Dispose();
103
                }
104
            }
105
            return result;
106
        }
107

  
108
        public static DataTable SelectHeaderSetting()
109
        {
110
            DataTable dt = new DataTable();
111
            ID2Info id2Info = ID2Info.GetInstance();
112
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
113
            {
114
                try
115
                {
116
                    connection.Open();
117
                    using (SQLiteCommand cmd = connection.CreateCommand())
118
                    {
119
                        cmd.CommandText = string.Format(@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {0};", PSN_HEADER_SETTING);
120
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
121
                            dt.Load(dr);
122
                    }
123
                    connection.Close();
124
                }
125
                catch (Exception ex)
126
                {
127
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
128
                }
129
                finally
130
                {
131
                    connection.Dispose();
132
                }
133
            }
134
            return dt;
135
        }
136

  
137
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
138
        {
139
            ID2Info id2Info = ID2Info.GetInstance();
140
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
141
            {
142
                try
143
                {
144
                    connection.Open();
145
                    using (SQLiteCommand cmd = connection.CreateCommand())
146
                    {
147
                        cmd.CommandText = string.Format("DELETE FROM {0}", PSN_HEADER_SETTING);
148
                        cmd.ExecuteNonQuery();
149

  
150
                        foreach (HeaderInfo headerInfo in headerInfos)
151
                        {
152
                            foreach (HeaderItem item in headerInfo.HeaderItems)
153
                            {
154
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)", PSN_HEADER_SETTING);
155
                                cmd.Parameters.Clear();
156
                                cmd.Parameters.AddWithValue("@GROUP_ID", headerInfo.UID);
157
                                cmd.Parameters.AddWithValue("@DESCRIPTION", headerInfo.Description);
158
                                cmd.Parameters.AddWithValue("@INDEX", item.Index);
159
                                cmd.Parameters.AddWithValue("@NAME", item.Name);
160
                                cmd.ExecuteNonQuery();
161
                            }
162
                        }
163
                    }
164
                    connection.Close();
165
                }
166
                catch (Exception ex)
167
                {
168
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
169
                    return false;
170
                }
171
                finally
172
                {
173
                    connection.Dispose();
174
                }
175
            }
176
            return true;
177
        }
178

  
179
        public static DataTable SelectOPCRelations()
180
        {
181
            DataTable dt = new DataTable();
182
            ID2Info id2Info = ID2Info.GetInstance();
183
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
184
            {
185
                try
186
                {
187
                    connection.Open();
188
                    using (SQLiteCommand cmd = connection.CreateCommand())
189
                    {
190
                        cmd.CommandText = "SELECT * FROM OPCRelations;";
191
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
192
                            dt.Load(dr);
193
                    }
194
                    connection.Close();
195
                }
196
                catch (Exception ex)
197
                {
198
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
199
                }
200
                finally
201
                {
202
                    connection.Dispose();
203
                }
204
            }
205
            return dt;
206
        }
207

  
208
        public static DataTable SelectDrawings()
209
        {
210
            DataTable dt = new DataTable();
211
            ID2Info id2Info = ID2Info.GetInstance();
212
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
213
            {
214
                try
215
                {
216
                    connection.Open();
217
                    using (SQLiteCommand cmd = connection.CreateCommand())
218
                    {
219
                        cmd.CommandText = "SELECT * FROM Drawings;";
220
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
221
                            dt.Load(dr);
222
                    }
223
                    connection.Close();
224
                }
225
                catch (Exception ex)
226
                {
227
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
228
                }
229
                finally
230
                {
231
                    connection.Dispose();
232
                }
233
            }
234
            return dt;
235
        }
236

  
237
        public static DataTable SelectLineProperties()
238
        {
239
            DataTable dt = new DataTable();
240
            ID2Info id2Info = ID2Info.GetInstance();
241
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
242
            {
243
                try
244
                {
245
                    connection.Open();
246
                    using (SQLiteCommand cmd = connection.CreateCommand())
247
                    {
248
                        cmd.CommandText = "SELECT * FROM LineProperties;";
249
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
250
                            dt.Load(dr);
251
                    }
252
                    connection.Close();
253
                }
254
                catch (Exception ex)
255
                {
256
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
257
                }
258
                finally
259
                {
260
                    connection.Dispose();
261
                }
262
            }
263
            return dt;
264
        }
265
        public static DataTable SelectTopologyRule()
266
        {
267
            DataTable dt = new DataTable();
268
            ID2Info id2Info = ID2Info.GetInstance();
269
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
270
            {
271
                try
272
                {
273
                    connection.Open();
274
                    using (SQLiteCommand cmd = connection.CreateCommand())
275
                    {
276
                        cmd.CommandText = string.Format(@"SELECT * FROM {0};", PSN_TOPOLOGY_RULE);
277
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
278
                            dt.Load(dr);
279
                    }
280
                    connection.Close();
281
                }
282
                catch (Exception ex)
283
                {
284
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
285
                }
286
                finally
287
                {
288
                    connection.Dispose();
289
                }
290
            }
291
            return dt;
292
        }
293
        public static bool SaveTopologyRule(DataTable dt)
294
        {
295
            ID2Info id2Info = ID2Info.GetInstance();
296
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
297
            {
298
                try
299
                {
300
                    connection.Open();
301
                    using (SQLiteCommand cmd = connection.CreateCommand())
302
                    {
303
                        cmd.CommandText = string.Format("DELETE FROM {0}", PSN_TOPOLOGY_RULE);
304
                        cmd.ExecuteNonQuery();
305

  
306
                        foreach (DataRow row in dt.Rows)
307
                        {
308
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID)", PSN_TOPOLOGY_RULE);
309
                            cmd.Parameters.Clear();
310
                            cmd.Parameters.AddWithValue("@UID", row["NAME"].ToString());
311
                            cmd.ExecuteNonQuery();
312
                        }
313
                    }
314
                    connection.Close();
315
                }
316
                catch (Exception ex)
317
                {
318
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
319
                    return false;
320
                }
321
                finally
322
                {
323
                    connection.Dispose();
324
                }
325
            }
326
            return true;
327
        }
328

  
329
        public static DataTable SelectFluidCode()
330
        {
331
            DataTable dt = new DataTable();
332
            ID2Info id2Info = ID2Info.GetInstance();
333
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
334
            {
335
                try
336
                {
337
                    connection.Open();
338
                    using (SQLiteCommand cmd = connection.CreateCommand())
339
                    {
340
                        cmd.CommandText = "SELECT * FROM FluidCode;";
341
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
342
                            dt.Load(dr);
343
                    }
344
                    connection.Close();
345
                }
346
                catch (Exception ex)
347
                {
348
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
349
                }
350
                finally
351
                {
352
                    connection.Dispose();
353
                }
354
            }
355
            return dt;
356
        }
357
        public static DataTable SelectPipingMaterialsClass()
358
        {
359
            DataTable dt = new DataTable();
360
            ID2Info id2Info = ID2Info.GetInstance();
361
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
362
            {
363
                try
364
                {
365
                    connection.Open();
366
                    using (SQLiteCommand cmd = connection.CreateCommand())
367
                    {
368
                        cmd.CommandText = "SELECT * FROM PipingMaterialsClass;";
369
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
370
                            dt.Load(dr);
371
                    }
372
                    connection.Close();
373
                }
374
                catch (Exception ex)
375
                {
376
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
377
                }
378
                finally
379
                {
380
                    connection.Dispose();
381
                }
382
            }
383
            return dt;
384
        }
385

  
386
        public static DataTable SelectPSNFluidCode()
387
        {
388
            DataTable dt = new DataTable();
389
            ID2Info id2Info = ID2Info.GetInstance();
390
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
391
            {
392
                try
393
                {
394
                    connection.Open();
395
                    using (SQLiteCommand cmd = connection.CreateCommand())
396
                    {
397
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_FLUIDCODE);
398
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
399
                            dt.Load(dr);
400
                    }
401
                    connection.Close();
402
                }
403
                catch (Exception ex)
404
                {
405
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
406
                }
407
                finally
408
                {
409
                    connection.Dispose();
410
                }
411
            }
412
            return dt;
413
        }
414
        public static DataTable SelectPSNPIPINGMATLCLASS()
415
        {
416
            DataTable dt = new DataTable();
417
            ID2Info id2Info = ID2Info.GetInstance();
418
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
419
            {
420
                try
421
                {
422
                    connection.Open();
423
                    using (SQLiteCommand cmd = connection.CreateCommand())
424
                    {
425
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PIPINGMATLCLASS);
426
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
427
                            dt.Load(dr);
428
                    }
429
                    connection.Close();
430
                }
431
                catch (Exception ex)
432
                {
433
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
434
                }
435
                finally
436
                {
437
                    connection.Dispose();
438
                }
439
            }
440
            return dt;
441
        }
442
        public static DataTable SelectNominalDiameter()
443
        {
444
            DataTable dt = new DataTable();
445
            ID2Info id2Info = ID2Info.GetInstance();
446
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
447
            {
448
                try
449
                {
450
                    connection.Open();
451
                    using (SQLiteCommand cmd = connection.CreateCommand())
452
                    {
453
                        cmd.CommandText = "SELECT * FROM NominalDiameter;";
454
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
455
                            dt.Load(dr);
456
                    }
457
                    connection.Close();
458
                }
459
                catch (Exception ex)
460
                {
461
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
462
                }
463
                finally
464
                {
465
                    connection.Dispose();
466
                }
467
            }
468
            return dt;
469
        }
470

  
471
        public static bool SavePSNFluidCode(DataTable dt)
472
        {
473
            ID2Info id2Info = ID2Info.GetInstance();
474

  
475
            bool result = true;
476
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
477
            {
478
                try
479
                {
480
                    connection.Open();
481

  
482
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
483
                    {
484
                        try
485
                        {
486
                            using (SQLiteCommand cmd = connection.CreateCommand())
487
                            {
488
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_FLUIDCODE);
489
                                cmd.ExecuteNonQuery();
490

  
491
                                foreach (DataRow row in dt.Rows)
492
                                {
493
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID, @Code, @Description, @Condition, @GroundLevel)", PSN_FLUIDCODE);
494
                                    cmd.Parameters.Clear();
495
                                    cmd.Parameters.AddWithValue("@UID", row["UID"].ToString());
496
                                    cmd.Parameters.AddWithValue("@Code", row["Code"].ToString());
497
                                    cmd.Parameters.AddWithValue("@Description", row["Description"].ToString());
498
                                    cmd.Parameters.AddWithValue("@Condition", row["Condition"].ToString());
499
                                    cmd.Parameters.AddWithValue("@GroundLevel", row["GroundLevel"].ToString());
500
                                    cmd.ExecuteNonQuery();
501
                                }
502
                            }
503
                            transaction.Commit();
504
                            connection.Close();
505
                        }
506
                        catch (Exception ex)
507
                        {
508
                            transaction.Rollback();
509
                            result = false;
510
                        }
511
                        finally
512
                        {
513
                            transaction.Dispose();
514
                        }
515
                    }
516
                }
517
                catch (Exception ex)
518
                {
519
                    System.Windows.Forms.MessageBox.Show(ex.Message);
520
                    result = false;
521
                }
522
                finally
523
                {
524
                    connection.Dispose();
525
                }
526
            }
527

  
528
            return result;
529
        }
530

  
531
        public static bool SavePSNPMC(DataTable dt)
532
        {
533
            ID2Info id2Info = ID2Info.GetInstance();
534

  
535
            bool result = true;
536
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
537
            {
538
                try
539
                {
540
                    connection.Open();
541

  
542
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
543
                    {
544
                        try
545
                        {
546
                            using (SQLiteCommand cmd = connection.CreateCommand())
547
                            {
548
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_PIPINGMATLCLASS);
549
                                cmd.ExecuteNonQuery();
550

  
551
                                foreach (DataRow row in dt.Rows)
552
                                {
553
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID, @Priority, @Code, @Description, @Condition, @GroundLevel)", PSN_PIPINGMATLCLASS);
554
                                    cmd.Parameters.Clear();
555
                                    cmd.Parameters.AddWithValue("@UID", row["UID"].ToString());
556
                                    cmd.Parameters.AddWithValue("@Priority", row["Priority"].ToString());
557
                                    cmd.Parameters.AddWithValue("@Code", row["Code"].ToString());
558
                                    cmd.Parameters.AddWithValue("@Description", row["Description"].ToString());
559
                                    cmd.Parameters.AddWithValue("@Condition", row["Condition"].ToString());
560
                                    cmd.Parameters.AddWithValue("@GroundLevel", row["GroundLevel"].ToString());
561
                                    cmd.ExecuteNonQuery();
562
                                }
563
                            }
564
                            transaction.Commit();
565
                            connection.Close();
566
                        }
567
                        catch (Exception ex)
568
                        {
569
                            transaction.Rollback();
570
                            result = false;
571
                        }
572
                        finally
573
                        {
574
                            transaction.Dispose();
575
                        }
576
                    }
577
                }
578
                catch (Exception ex)
579
                {
580
                    System.Windows.Forms.MessageBox.Show(ex.Message);
581
                    result = false;
582
                }
583
                finally
584
                {
585
                    connection.Dispose();
586
                }
587
            }
588

  
589
            return result;
590
        }
591

  
592
        public static bool SavePathItems(DataTable dt)
593
        {
594
            ID2Info id2Info = ID2Info.GetInstance();
595

  
596
            bool result = true;
597
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
598
            {
599
                try
600
                {
601
                    connection.Open();
602

  
603
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
604
                    {
605
                        try
606
                        {
607
                            using (SQLiteCommand cmd = connection.CreateCommand())
608
                            {
609
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_PATHITEMS);
610
                                cmd.ExecuteNonQuery();
611

  
612
                                foreach (DataRow row in dt.Rows)
613
                                {
614
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @TYPE, @PIDNAME, @NPD, @PipeSystemNetwork_OID, @PipeRun_OID)", PSN_PATHITEMS);
615
                                    cmd.Parameters.Clear();
616
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
617
                                    cmd.Parameters.AddWithValue("@SequenceData_OID", row["SequenceData_OID"].ToString());
618
                                    cmd.Parameters.AddWithValue("@TopologySet_OID", row["TopologySet_OID"].ToString());
619
                                    cmd.Parameters.AddWithValue("@BranchTopologySet_OID", row["BranchTopologySet_OID"].ToString());
620
                                    cmd.Parameters.AddWithValue("@PipeLine_OID", row["PipeLine_OID"].ToString());
621
                                    cmd.Parameters.AddWithValue("@ITEMNAME", row["ITEMNAME"].ToString());
622
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
623
                                    cmd.Parameters.AddWithValue("@TYPE", row["TYPE"].ToString());
624
                                    cmd.Parameters.AddWithValue("@PIDNAME", row["PIDNAME"].ToString());
625
                                    cmd.Parameters.AddWithValue("@NPD", row["NPD"].ToString());
626
                                    cmd.Parameters.AddWithValue("@PipeSystemNetwork_OID", row["PipeSystemNetwork_OID"].ToString());
627
                                    cmd.Parameters.AddWithValue("@PipeRun_OID", row["PipeRun_OID"].ToString());
628
                                    cmd.ExecuteNonQuery();
629
                                }
630
                            }
631
                            transaction.Commit();
632
                            connection.Close();
633
                        }
634
                        catch (Exception ex)
635
                        {
636
                            transaction.Rollback();
637
                            result = false;
638
                        }
639
                        finally
640
                        {
641
                            transaction.Dispose();
642
                        }
643
                    }
644
                }
645
                catch (Exception ex)
646
                {
647
                    System.Windows.Forms.MessageBox.Show(ex.Message);
648
                    result = false;
649
                }
650
                finally
651
                {
652
                    connection.Dispose();
653
                }
654
            }
655

  
656
            return result;
657
        }
658

  
659
        public static bool SaveSequenceData(DataTable dt)
660
        {
661
            ID2Info id2Info = ID2Info.GetInstance();
662

  
663
            bool result = true;
664
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
665
            {
666
                try
667
                {
668
                    connection.Open();
669

  
670
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
671
                    {
672
                        try
673
                        {
674
                            using (SQLiteCommand cmd = connection.CreateCommand())
675
                            {
676
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_SEQUENCEDATA);
677
                                cmd.ExecuteNonQuery();
678

  
679
                                foreach (DataRow row in dt.Rows)
680
                                {
681
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)", PSN_SEQUENCEDATA);
682
                                    cmd.Parameters.Clear();
683
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
684
                                    cmd.Parameters.AddWithValue("@SERIALNUMBER", row["SERIALNUMBER"].ToString());
685
                                    cmd.Parameters.AddWithValue("@PathItem_OID", row["PathItem_OID"].ToString());
686
                                    cmd.Parameters.AddWithValue("@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
687
                                    cmd.ExecuteNonQuery();
688
                                }
689
                            }
690
                            transaction.Commit();
691
                            connection.Close();
692
                        }
693
                        catch (Exception ex)
694
                        {
695
                            transaction.Rollback();
696
                            result = false;
697
                        }
698
                        finally
699
                        {
700
                            transaction.Dispose();
701
                        }
702
                    }
703
                }
704
                catch (Exception ex)
705
                {
706
                    System.Windows.Forms.MessageBox.Show(ex.Message);
707
                    result = false;
708
                }
709
                finally
710
                {
711
                    connection.Dispose();
712
                }
713
            }
714

  
715
            return result;
716
        }
717

  
718
        public static bool SavePipeSystemNetwork(DataTable dt)
719
        {
720
            ID2Info id2Info = ID2Info.GetInstance();
721

  
722
            bool result = true;
723
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
724
            {
725
                try
726
                {
727
                    connection.Open();
728

  
729
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
730
                    {
731
                        try
732
                        {
733
                            using (SQLiteCommand cmd = connection.CreateCommand())
734
                            {
735
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_PIPESYSTEMNETWORK);
736
                                cmd.ExecuteNonQuery();
737

  
738
                                foreach (DataRow row in dt.Rows)
739
                                {
740
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber, @PathOID, @PBS, @PIDDrawings)", PSN_PIPESYSTEMNETWORK);
741
                                    cmd.Parameters.Clear();
742
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
743
                                    cmd.Parameters.AddWithValue("@Type", row["Type"].ToString());
744
                                    cmd.Parameters.AddWithValue("@OrderNumber", row["OrderNumber"].ToString());
745
                                    cmd.Parameters.AddWithValue("@Pipeline_OID", row["Pipeline_OID"].ToString());
746
                                    cmd.Parameters.AddWithValue("@FROM_DATA", row["FROM_DATA"].ToString());
747
                                    cmd.Parameters.AddWithValue("@TO_DATA", row["TO_DATA"].ToString());
748
                                    cmd.Parameters.AddWithValue("@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
749
                                    cmd.Parameters.AddWithValue("@PSNRevisionNumber", row["PSNRevisionNumber"].ToString());
750
                                    cmd.Parameters.AddWithValue("@PathOID", row["PathOID"].ToString());
751
                                    cmd.Parameters.AddWithValue("@PBS", row["PBS"].ToString());
752
                                    cmd.Parameters.AddWithValue("@PIDDrawings", row["PIDDrawings"].ToString());
753
                                    cmd.ExecuteNonQuery();
754
                                }
755
                            }
756
                            transaction.Commit();
757
                            connection.Close();
758
                        }
759
                        catch (Exception ex)
760
                        {
761
                            transaction.Rollback();
762
                            result = false;
763
                        }
764
                        finally
765
                        {
766
                            transaction.Dispose();
767
                        }
768
                    }
769
                }
770
                catch (Exception ex)
771
                {
772
                    System.Windows.Forms.MessageBox.Show(ex.Message);
773
                    result = false;
774
                }
775
                finally
776
                {
777
                    connection.Dispose();
778
                }
779
            }
780

  
781
            return result;
782
        }
783

  
784
        public static bool SaveEquipment(DataTable dt)
785
        {
786
            ID2Info id2Info = ID2Info.GetInstance();
787

  
788
            bool result = true;
789
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
790
            {
791
                try
792
                {
793
                    connection.Open();
794

  
795
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
796
                    {
797
                        try
798
                        {
799
                            using (SQLiteCommand cmd = connection.CreateCommand())
800
                            {
801
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_EQUIPMENT);
802
                                cmd.ExecuteNonQuery();
803

  
804
                                foreach (DataRow row in dt.Rows)
805
                                {
806
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)", PSN_EQUIPMENT);
807
                                    cmd.Parameters.Clear();
808
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
809
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
810
                                    cmd.Parameters.AddWithValue("@XCOORDS", row["XCOORDS"].ToString());
811
                                    cmd.Parameters.AddWithValue("@YCOORDS", row["YCOORDS"].ToString());
812
                                    cmd.ExecuteNonQuery();
813
                                }
814
                            }
815
                            transaction.Commit();
816
                            connection.Close();
817
                        }
818
                        catch (Exception ex)
819
                        {
820
                            transaction.Rollback();
821
                            result = false;
822
                        }
823
                        finally
824
                        {
825
                            transaction.Dispose();
826
                        }
827
                    }
828
                }
829
                catch (Exception ex)
830
                {
831
                    System.Windows.Forms.MessageBox.Show(ex.Message);
832
                    result = false;
833
                }
834
                finally
835
                {
836
                    connection.Dispose();
837
                }
838
            }
839

  
840
            return result;
841
        }
842

  
843
        public static bool SaveNozzle(DataTable dt)
844
        {
845
            ID2Info id2Info = ID2Info.GetInstance();
846

  
847
            bool result = true;
848
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
849
            {
850
                try
851
                {
852
                    connection.Open();
853

  
854
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
855
                    {
856
                        try
857
                        {
858
                            using (SQLiteCommand cmd = connection.CreateCommand())
859
                            {
860
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_NOZZLE);
861
                                cmd.ExecuteNonQuery();
862

  
863
                                foreach (DataRow row in dt.Rows)
864
                                {
865
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @ROTATION, @FlowDirection)", PSN_NOZZLE);
866
                                    cmd.Parameters.Clear();
867
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
868
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
869
                                    cmd.Parameters.AddWithValue("@XCOORDS", row["XCOORDS"].ToString());
870
                                    cmd.Parameters.AddWithValue("@YCOORDS", row["YCOORDS"].ToString());
871
                                    cmd.Parameters.AddWithValue("@Equipment_OID", row["Equipment_OID"].ToString());
872
                                    cmd.Parameters.AddWithValue("@FLUID", row["FLUID"].ToString());
873
                                    cmd.Parameters.AddWithValue("@NPD", row["NPD"].ToString());
874
                                    cmd.Parameters.AddWithValue("@ROTATION", row["ROTATION"].ToString());
875
                                    cmd.Parameters.AddWithValue("@FlowDirection", row["FlowDirection"].ToString());
876
                                    cmd.ExecuteNonQuery();
877
                                }
878
                            }
879
                            transaction.Commit();
880
                            connection.Close();
881
                        }
882
                        catch (Exception ex)
883
                        {
884
                            transaction.Rollback();
885
                            result = false;
886
                        }
887
                        finally
888
                        {
889
                            transaction.Dispose();
890
                        }
891
                    }
892
                }
893
                catch (Exception ex)
894
                {
895
                    System.Windows.Forms.MessageBox.Show(ex.Message);
896
                    result = false;
897
                }
898
                finally
899
                {
900
                    connection.Dispose();
901
                }
902
            }
903

  
904
            return result;
905
        }
12 906
    }
13 907
}
DTI_PID/ID2PSN/Document.cs
1
using DevExpress.Utils.StructuredStorage.Internal.Reader;
2
using System;
3
using System.Collections.Generic;
4
using System.Data;
5
using System.IO;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Xml.Linq;
10

  
11
namespace ID2PSN
12
{
13
    public class Document
14
    {
15
        public string DrawingName { get; set; }
16
        public string FileName { get; set; }
17
        public string FilePath { get; set; }
18
        public List<Item> Items { get; set; }
19
        public List<LineNumber> LineNumbers { get; set; }
20
        public List<Group> Groups { get; set; }
21
        public List<RunInfo> RunInfos { get; set; }
22
        public List<Item> SegmentBreaks { get; set; }
23
        public List<Equipment> Equipments { get; set; }
24

  
25
        public Document(string filePath)
26
        {
27
            FilePath = filePath;
28
            ReadFile();
29
            SetRelationItem();
30
            SetGroup();
31
        }
32
        private void ReadFile()
33
        {
34
            XElement xml = XElement.Load(FilePath);
35
            FileName = Path.GetFileNameWithoutExtension(FilePath);
36
            DrawingName = xml.Element("DWGNAME").Value;
37
            Items = new List<Item>();
38
            LineNumbers = new List<LineNumber>();
39
            RunInfos = new List<RunInfo>();
40
            Groups = new List<Group>();
41
            SegmentBreaks = new List<Item>();
42
            Equipments = new List<Equipment>();
43

  
44
            foreach (XElement element in xml.Element("SYMBOLS").Elements("SYMBOL"))
45
                Items.Add(GetSymbolItem(element));
46
            foreach (XElement element in xml.Element("LINEINFOS").Elements("LINE"))
47
                Items.Add(GetLineItem(element));
48
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
49
                LineNumbers.Add(GetLineNumber(element));
50
            foreach (XElement element in xml.Element("VENDORS").Elements("VENDOR"))
51
                Equipments.Add(GetEquipment(element));
52

  
53

  
54
            List<Item> segmentBreaks = Items.FindAll(x => x.Type.ToUpper() == "Segment Breaks".ToUpper() || x.Type.ToUpper() == "End Break".ToUpper());
55
            SegmentBreaks.AddRange(segmentBreaks);
56
            foreach (var item in segmentBreaks)
57
                Items.Remove(item);
58

  
59
            SetRunInfo(xml);
60
        }
61
        private Item GetSymbolItem(XElement element)
62
        {
63
            Item item = new Item();
64
            item.UID = element.Element("UID").Value;
65
            item.Name = element.Element("NAME").Value;
66
            item.Type = element.Element("TYPE").Value;
67
            item.Owner = element.Element("OWNER").Value;
68
            item.Parent = element.Element("PARENT").Value;
69
            string[] sPoint = element.Element("ORIGINALPOINT").Value.Split(new char[] { ',' });
70
            item.POINT = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) };
71
            item.ANGLE = element.Element("ANGLE").Value;
72
            item.Relations = GetRelations(element.Element("CONNECTORS"));
73
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
74
            item.ItemType = ItemType.Symbol;
75

  
76
            if (item.Type.ToUpper().Equals("PIPING OPC'S"))
77
                item.SubItemType = SubItemType.OPC;
78
            else if (item.Type.ToUpper().Equals("NOZZLES"))
79
                item.SubItemType = SubItemType.Nozzle;
80

  
81
            return item;
82
        }
83
        private Item GetLineItem(XElement element)
84
        {
85
            Item item = new Item();
86
            item.UID = element.Element("UID").Value;
87
            item.Name = element.Element("TYPE").Value;
88
            item.Type = element.Element("TYPE").Value;
89
            item.Owner = element.Attribute("OWNER").Value;
90

  
91
            item.Relations = GetRelations(element.Element("CONNECTORS"));
92
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
93
            item.ItemType = ItemType.Line;
94

  
95
            return item;
96
        }
97
        private LineNumber GetLineNumber(XElement element)
98
        {
99
            LineNumber lineNumber = new LineNumber();
100
            lineNumber.UID = element.Element("UID").Value;
101
            lineNumber.Name = element.Element("TEXT").Value;
102
            lineNumber.Attributes = GetAttributes(element);
103
            return lineNumber;
104
        }
105
        private Equipment GetEquipment(XElement element)
106
        {
107
            Equipment item = new Equipment();
108
            item.UID = element.Element("UID").Value;
109
            string[] sPoint = element.Element("POINT").Value.Split(new char[] { '/' });
110
            foreach (var sp in sPoint)
111
            {
112
                string[] xy = sp.Split(new char[] { ',' });
113
                item.POINT.Add(new double[] { Convert.ToDouble(xy[0]), Convert.ToDouble(xy[1]) });
114
            }
115

  
116
            return item;
117
        }
118
        private List<Relation> GetRelations(XElement element)
119
        {
120
            List<Relation> result = new List<Relation>();
121
            foreach (XElement item in element.Elements("CONNECTOR"))
122
            {
123
                string[] sPoint = item.Element("SCENECONNECTPOINT").Value.Split(new char[] { ',' });
124
                result.Add(new Relation()
125
                {
126
                    UID = item.Element("CONNECTEDITEM").Value,
127
                    Point = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) }
128
                });
129
            }
130

  
131
            return result;
132
        }
133
        private List<Attribute> GetAttributes(XElement element)
134
        {
135
            List<Attribute> result = new List<Attribute>();
136
            foreach (XElement item in element.Elements("ATTRIBUTE"))
137
            {
138
                result.Add(new Attribute()
139
                {
140
                    UID = item.Attribute("UID").Value,
141
                    Name = item.Attribute("Attribute").Value,
142
                    DisplayName = item.Attribute("DisplayAttribute").Value,
143
                    Value = item.Value
144
                });
145
            }
146
            return result;
147
        }
148
        private void SetRelationItem()
149
        {
150
            foreach (Item item in Items)
151
                foreach (Relation relation in item.Relations)
152
                    relation.Item = Items.Find(x => x.UID.Equals(relation.UID));
153
        }
154
        private void SetRunInfo(XElement xml)
155
        {
156
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
157
            {
158
                string UID = element.Element("UID").Value;
159
                foreach (XElement run in element.Elements("RUN"))
160
                {
161
                    RunInfo runInfo = new RunInfo() { UID = UID };
162
                    foreach (XElement line in run.Elements("LINE"))
163
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
164
                    foreach (XElement symbol in run.Elements("SYMBOL"))
165
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
166
                    RunInfos.Add(runInfo);
167
                }
168
            }
169

  
170
            foreach (XElement element in xml.Element("TRIMLINENOS").Elements("TRIM_LINE_NO"))
171
            {
172
                string UID = element.Element("UID").Value;
173
                foreach (XElement run in element.Elements("RUN"))
174
                {
175
                    RunInfo runInfo = new RunInfo() { UID = UID };
176
                    foreach (XElement line in run.Elements("LINE"))
177
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
178
                    foreach (XElement symbol in run.Elements("SYMBOL"))
179
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
180
                    RunInfos.Add(runInfo);
181
                }
182
            }
183
        }
184
        private void SetGroup()
185
        {
186
            List<Item> orderItems = Items.OrderByDescending(x => x.ItemType == ItemType.Line).ToList();
187

  
188
            Dictionary<Item, string> itemDic = new Dictionary<Item, string>();
189
            foreach (Item item in orderItems)
190
                itemDic.Add(item, Guid.NewGuid().ToString());
191

  
192
            foreach (Item item in orderItems)
193
            {
194
                string groupKey = itemDic[item];
195
                if (item.ItemType == ItemType.Line)
196
                {
197
                    GroupingForwardLine();
198
                    GroupingBackwardLine();
199
                }
200

  
201
                void GroupingForwardLine()
202
                {
203
                    Item connItem = item.Relations[1].Item;
204
                    if (connItem != null && IsConnected(connItem, item))
205
                    {
206
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[0].Item))
207
                            ChangeGroupID(itemDic[connItem], groupKey);
208
                        else if (connItem.ItemType == ItemType.Symbol)
209
                        {
210
                            List<Item> allConnItems = GetConnectedLines(connItem);
211
                            allConnItems.Remove(item);
212
                            List<Item> connItems = GetConnectedForwardLines(connItem);
213
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
214
                            {
215
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
216
                                foreach (Item loopItem in connSymbols)
217
                                    ChangeGroupID(itemDic[loopItem], groupKey);
218
                            }
219
                            else
220
                            {
221
                                List<Item> endItems = new List<Item>();
222
                                Stack<Item> stacks = new Stack<Item>();
223
                                stacks.Push(connItem);
224
                                while (stacks.Count > 0)
225
                                {
226
                                    Item stack = stacks.Pop();
227
                                    if (endItems.Contains(stack))
228
                                        continue;
229
                                    endItems.Add(stack);
230

  
231
                                    if (GetConnectedItemCount(stack) < 3)
232
                                    {
233
                                        ChangeGroupID(itemDic[stack], groupKey);
234
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
235
                                        foreach (Relation relation in relations)
236
                                            stacks.Push(relation.Item);
237
                                    }
238
                                    else if (IsSameRun(item, stack))
239
                                    {
240
                                        ChangeGroupID(itemDic[stack], groupKey);
241
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
242
                                        foreach (Relation relation in relations)
243
                                            stacks.Push(relation.Item);
244
                                    }
245
                                }
246
                            }
247
                        }
248
                    }
249
                }
250
                void GroupingBackwardLine()
251
                {
252
                    Item connItem = item.Relations[0].Item;
253
                    if (connItem != null && IsConnected(connItem, item))
254
                    {
255
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[1].Item))
256
                            ChangeGroupID(itemDic[connItem], groupKey);
257
                        else if (connItem.ItemType == ItemType.Symbol)
258
                        {
259
                            List<Item> allConnItems = GetConnectedLines(connItem);
260
                            allConnItems.Remove(item);
261
                            List<Item> connItems = GetConnectedBackwardLines(connItem);
262
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
263
                            {
264
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
265
                                foreach (Item loopItem in connSymbols)
266
                                    ChangeGroupID(itemDic[loopItem], groupKey);
267
                            }
268
                            else
269
                            {
270
                                List<Item> endItems = new List<Item>();
271
                                Stack<Item> stacks = new Stack<Item>();
272
                                stacks.Push(connItem);
273
                                while (stacks.Count > 0)
274
                                {
275
                                    Item stack = stacks.Pop();
276
                                    if (endItems.Contains(stack))
277
                                        continue;
278
                                    endItems.Add(stack);
279

  
280
                                    if (GetConnectedItemCount(stack) < 3)
281
                                    {
282
                                        ChangeGroupID(itemDic[stack], groupKey);
283
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
284
                                        foreach (Relation relation in relations)
285
                                            stacks.Push(relation.Item);
286
                                    }
287
                                    else if (IsSameRun(item, stack))
288
                                    {
289
                                        ChangeGroupID(itemDic[stack], groupKey);
290
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
291
                                        foreach (Relation relation in relations)
292
                                            stacks.Push(relation.Item);
293
                                    }
294
                                }
295
                            }
296
                        }
297
                    }
298
                }
299
            }
300

  
301
            List<string> endGroupIds = new List<string>();
302
            foreach (var item in itemDic)
303
            {
304
                if (endGroupIds.Contains(item.Value))
305
                    continue;
306
                endGroupIds.Add(item.Value);
307

  
308
                Group group = new Group(this);
309
                group.Items = itemDic.Where(x => x.Value.Equals(item.Value)).Select(x => x.Key).ToList();
310
                group.UID = item.Value;
311
                group.SortItems();
312

  
313
                Groups.Add(group);
314
            }
315

  
316
            #region HeaderSetting
317
            List<HeaderInfo> HeaderInfos = new List<HeaderInfo>();
318
            DataTable dt = DB.SelectHeaderSetting();
319
            foreach (DataRow row in dt.Rows)
320
            {
321
                string groupID = row["GROUP_ID"].ToString();
322
                string desc = row["DESCRIPTION"].ToString();
323
                int index = Convert.ToInt32(row["INDEX"]);
324
                string name = row["NAME"].ToString();
325

  
326
                HeaderInfo headerInfo = HeaderInfos.Find(x => x.UID.Equals(groupID));
327
                if (headerInfo == null)
328
                {
329
                    headerInfo = new HeaderInfo(groupID);
330
                    headerInfo.Description = desc;
331
                    HeaderInfos.Add(headerInfo);
332
                }
333

  
334
                headerInfo.HeaderItems.Add(new HeaderItem()
335
                {
336
                    Index = index,
337
                    Name = name
338
                });
339
            }
340
            foreach (HeaderInfo headerInfo in HeaderInfos)
341
                headerInfo.HeaderItems = headerInfo.HeaderItems.OrderBy(x => x.Index).ToList();
342

  
343
            foreach (Group group in Groups)
344
            {
345
                List<HeaderInfo> endInfos = new List<HeaderInfo>();
346
                bool bFind = false;
347
                for (int i = 0; i < group.Items.Count; i++)
348
                {
349
                    Item item = group.Items[i];
350
                    foreach (HeaderInfo header in HeaderInfos)
351
                    {
352
                        if (endInfos.Contains(header))
353
                            continue;
354

  
355
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
356
                        {
357
                            endInfos.Add(header);
358
                            continue;
359
                        }
360

  
361
                        if (header.HeaderItems.Count.Equals(i + 1))
362
                        {
363
                            for (int j = 0; j < i + 1; j++)
364
                                group.Items[j].SubItemType = SubItemType.Header;
365
                            bFind = true;
366
                            break;
367
                        }
368
                    }
369

  
370
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
371
                        break;
372
                }
373

  
374
                endInfos = new List<HeaderInfo>();
375
                bFind = false;
376
                for (int i = 0; i < group.Items.Count; i++)
377
                {
378
                    Item item = group.Items[group.Items.Count - i - 1];
379
                    foreach (HeaderInfo header in HeaderInfos)
380
                    {
381
                        if (endInfos.Contains(header))
382
                            continue;
383

  
384
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
385
                        {
386
                            endInfos.Add(header);
387
                            continue;
388
                        }
389

  
390
                        if (header.HeaderItems.Count.Equals(i + 1))
391
                        {
392
                            for (int j = 0; j < i + 1; j++)
393
                                group.Items[group.Items.Count - j - 1].SubItemType = SubItemType.Header;
394
                            bFind = true;
395
                            break;
396
                        }
397
                    }
398

  
399
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
400
                        break;
401
                }
402
            }
403
            #endregion
404

  
405
            int GetConnectedItemCount(Item item)
406
            {
407
                return item.Relations.FindAll(x => x.Item != null).Count;
408
            }
409

  
410
            List<Item> GetConnectedLines(Item item)
411
            {
412
                List<Item> result = new List<Item>();
413
                List<Item> end = new List<Item>();
414
                Stack<Item> stacks = new Stack<Item>();
415
                stacks.Push(item);
416
                while (stacks.Count > 0)
417
                {
418
                    Item stack = stacks.Pop();
419
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
420
                        continue;
421
                    end.Add(stack);
422

  
423
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
424
                    foreach (Relation relation in relations)
425
                        stacks.Push(relation.Item);
426

  
427
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
428
                    foreach (Relation relation in relations)
429
                    {
430
                        result.Add(relation.Item);
431
                        end.Add(relation.Item);
432
                    }
433
                }
434

  
435
                return result;
436
            }
437

  
438
            List<Item> GetConnectedForwardLines(Item item)
439
            {
440
                List<Item> result = new List<Item>();
441
                List<Item> end = new List<Item>();
442
                Stack<Item> stacks = new Stack<Item>();
443
                stacks.Push(item);
444
                while (stacks.Count > 0)
445
                {
446
                    Item stack = stacks.Pop();
447
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
448
                        continue;
449
                    end.Add(stack);
450

  
451
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
452
                    foreach (Relation relation in relations)
453
                        stacks.Push(relation.Item);
454

  
455
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
456
                    foreach (Relation relation in relations)
457
                    {
458
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(0))
459
                            result.Add(relation.Item);
460

  
461
                        end.Add(relation.Item);
462
                    }
463
                }
464

  
465
                return result;
466
            }
467

  
468
            List<Item> GetConnectedBackwardLines(Item item)
469
            {
470
                List<Item> result = new List<Item>();
471
                List<Item> end = new List<Item>();
472
                Stack<Item> stacks = new Stack<Item>();
473
                stacks.Push(item);
474
                while (stacks.Count > 0)
475
                {
476
                    Item stack = stacks.Pop();
477
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
478
                        continue;
479
                    end.Add(stack);
480

  
481
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
482
                    foreach (Relation relation in relations)
483
                        stacks.Push(relation.Item);
484

  
485
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
486
                    foreach (Relation relation in relations)
487
                    {
488
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(1))
489
                            result.Add(relation.Item);
490

  
491
                        end.Add(relation.Item);
492
                    }
493
                }
494

  
495
                return result;
496
            }
497

  
498
            List<Item> GetConnectedSymbols(Item item)
499
            {
500
                List<Item> result = new List<Item>();
501
                List<Item> end = new List<Item>();
502
                Stack<Item> stacks = new Stack<Item>();
503
                stacks.Push(item);
504
                while (stacks.Count > 0)
505
                {
506
                    Item stack = stacks.Pop();
507
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
508
                        continue;
509
                    end.Add(stack);
510
                    result.Add(stack);
511
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
512
                    foreach (Relation relation in relations)
513
                        stacks.Push(relation.Item);
514
                }
515

  
516
                return result;
517
            }
518

  
519
            void ChangeGroupID(string from, string to)
520
            {
521
                if (from.Equals(to))
522
                    return;
523

  
524
                List<Item> changeItems = new List<Item>();
525
                foreach (var _item in itemDic)
526
                    if (_item.Value.Equals(from))
527
                        changeItems.Add(_item.Key);
528
                foreach (var _item in changeItems)
529
                    itemDic[_item] = to;
530
            }
531

  
532
            bool IsConnected(Item item1, Item item2)
533
            {
534
                if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
535
                    item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
536
                    return true;
537
                else
538
                    return false;
539
            }
540

  
541
            bool IsSameRun(Item item1, Item item2)
542
            {
543
                return RunInfos.Find(x => x.Items.Contains(item1) && x.Items.Contains(item2)) != null ? true : false;
... 이 차이점은 표시할 수 있는 최대 줄수를 초과해서 이 차이점은 잘렸습니다.

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)