프로젝트

일반

사용자정보

개정판 154d8f43

ID154d8f43aebc83a10debd29d4dde8deae0f977ed
상위 3d7ff99c
하위 b7a29053

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

dev issue #498 : OPC 연결 기본 로직 완료

Change-Id: I0150a1f6234dafa298e7bf2a405d290f84ad93a5

차이점 보기:

DTI_PID/SPPIDConverter/AutoModeling.cs
105 105
                    RunSpecBreakModeling();
106 106
                    // Join SameConnector
107 107
                    RunJoinRunForSameConnector();
108
                    // Check FlowDirection
109
                    RunFlowDirection();
110 108
                    // Join Run
111 109
                    RunJoinRun();
110
                    // Check FlowDirection
111
                    RunFlowDirection();
112 112
                    //Line Number Modeling
113 113
                    RunLineNumberModeling();
114 114
                    // Note Modeling
......
140 140
            }
141 141
            finally
142 142
            {
143
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
143
                Project_DB.InsertDrawingInfoAndOPCInfo(document.PATH, drawingNumber, drawingName, document);
144 144
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
145 145
                {
146 146
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
......
515 515
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
516 516

  
517 517
                        ReleaseCOMObjects(modelItem);
518
                        modelItem = null;
518 519
                    }
519 520
                }
520 521

  
......
533 534
                SetFlowDirectionByLine(modelId);
534 535

  
535 536
                ReleaseCOMObjects(zeroLengthModelItem);
537
                zeroLengthModelItem = null;
538

  
536 539
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
537 540
            }
538 541
            foreach (var modelId in ZeroLengthModelItemIDReverse)
......
548 551
                SetFlowDirectionByLine(modelId);
549 552

  
550 553
                ReleaseCOMObjects(zeroLengthModelItem);
554
                zeroLengthModelItem = null;
555

  
551 556
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
552 557
            }
553 558
            foreach (var modelId in ZeroLengthSymbolToSymbolModelItemID)
......
581 586
                        }
582 587
                    }
583 588
                    ReleaseCOMObjects(modelItem);
589
                    modelItem = null;
584 590
                }
585 591

  
586 592
                void SetSymbolFlowDirectionByRelationShip(LMRelationship relationship, LMConnector connector, string sFlowDirection)
......
597 603

  
598 604
                        symbolModelItem.Commit();
599 605
                        ReleaseCOMObjects(symbolModelItem);
606
                        symbolModelItem = null;
600 607
                    }
601 608
                    // Item1이 Symbol
602 609
                    else if (!DBNull.Value.Equals(relationship.Item2RepresentationID) && relationship.Item2RepresentationID == connector.Id &&
......
610 617

  
611 618
                        symbolModelItem.Commit();
612 619
                        ReleaseCOMObjects(symbolModelItem);
620
                        symbolModelItem = null;
613 621
                    }
614 622
                }
615 623

  
......
2800 2808
            List<string> result = new List<string>();
2801 2809
            modelItemIDs = modelItemIDs.Distinct().ToList();
2802 2810
            string originalName = GetSPPIDFileName(modelId);
2811
            bool existZeroLength = false;
2803 2812
            foreach (var item in modelItemIDs)
2804 2813
            {
2805 2814
                string fileName = GetSPPIDFileName(item);
......
2808 2817
                else
2809 2818
                {
2810 2819
                    LMConnector connector = GetLMConnectorOnlyOne(item);
2811
                    if (connector != null && Convert.ToBoolean(connector.get_IsZeroLength()))
2820
                    if (connector != null && Convert.ToBoolean(connector.get_IsZeroLength()) && connector.LabelPersists.Count == 0)
2812 2821
                    {
2813 2822
                        result.Add(item);
2814 2823
                        ReleaseCOMObjects(connector);
2824
                        existZeroLength = true;
2815 2825
                    }
2816 2826
                }
2817 2827
            }
2828

  
2829
            if (result.Count == 1 && existZeroLength)
2830
            {
2831
                LMConnector connector = GetLMConnectorOnlyOne(result[0]);
2832
                LMConnector newConnector = ReModelingLMConnector(connector, originalName);
2833
                result.Clear();
2834
                if (newConnector != null)
2835
                    result.Add(newConnector.ModelItemID);
2836

  
2837
                ReleaseCOMObjects(connector);
2838
                connector = null;
2839
                ReleaseCOMObjects(newConnector);
2840
                newConnector = null;
2841
            }
2818 2842
            
2819 2843
            return result;
2820 2844
        }
DTI_PID/SPPIDConverter/BaseModel/Document.cs
16 16
        private string _SIZE;
17 17
        private double _SIZE_WIDTH;
18 18
        private double _SIZE_HEIGHT;
19
        private string _UID;
19 20
        private List<Symbol> _SYMBOLS = new List<Symbol>();
20 21
        private List<Text> _TEXTINFOS = new List<Text>();
21 22
        private List<Note> _NOTES = new List<Note>();
......
66 67
        public bool Enable { get => _Enable; set => _Enable = value; }
67 68
        public bool Validation { get => _Validation; set => _Validation = value; }
68 69
        public bool MappingValidation { get => _MappingValidation; set => _MappingValidation = value; }
70
        public string UID { get => _UID; set => _UID = value; }
69 71

  
70 72
        StringBuilder validationStringBuilder = new StringBuilder();
71 73

  
DTI_PID/SPPIDConverter/ConverterDocking.cs
23 23
using Newtonsoft.Json;
24 24
using System.Runtime.InteropServices;
25 25
using System.Reflection;
26
using Converter.SPPID.OPC;
26 27

  
27 28
namespace Converter.SPPID.Wrapper
28 29
{
......
43 44
            {
44 45
                textEditDrawingX.EditValue = Settings.Default.DrawingX;
45 46
                textEditDrawingY.EditValue = Settings.Default.DrawingY;
47

  
48
                Project_Info _ProjectInfo = Project_Info.GetInstance();
49
                _ProjectInfo.DefaultPath = Settings.Default.LatestProjectPath;
46 50
            }
47 51
            catch (Exception ex)
48 52
            {
......
105 109

  
106 110
        private void btnLinkOPC_Click(object sender, EventArgs e)
107 111
        {
108
            if (application.ActiveDocument == null)
109
                application.Documents.Add();
112
            DataTable tOPCInfo = Project_DB.SelectOPCInfo();
113
            DataTable tOPCRelations = Project_DB.SelectOPCRelations();
114
            DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
115
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
116

  
117
            foreach (DataRow row in tOPCInfo.Rows)
118
            {
119
                if (!Convert.ToBoolean(row["PAIRED"]))
120
                {
121
                    string drawingUID = row["ID2_DRAWING_UID"].ToString();
122
                    string OPCUID = row["ID2_OPC_UID"].ToString();
123
                    DataRow[] rows = tOPCRelations.Select(string.Format("(From_Drawings_UID = '{0}' AND From_OPC_UID = '{1}') OR (To_Drawings_UID = '{0}' AND To_OPC_UID = '{1}')", drawingUID, OPCUID));
124

  
125
                    if (rows.Length == 2)
126
                    {
127
                        string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
128
                        string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
129
                        string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
130
                        string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
131
                        string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
132
                        string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
133
                        string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
134
                        string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
135

  
136
                        DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
137
                        DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
138

  
139
                        if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1)
140
                        {
141
                            DataRow fromOPCInfoRow = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1))[0];
142
                            DataRow toOPCInfoRow = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1))[0];
143
                            string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
144
                            string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
145
                            string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
146
                            AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName);
147
                            if (opc.Run())
148
                            {
149
                                fromOPCInfoRow["PAIRED"] = true;
150
                                toOPCInfoRow["PAIRED"] = true;
110 151

  
152
                                Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
153
                                Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
154
                            }
155
                            break;
156
                        }
157
                    }
158
                }
159
            }
160

  
161
            tOPCInfo.Dispose();
162
            tOPCRelations.Dispose();
163
            tDrawingInfo.Dispose();
164
            ReleaseCOMObjects(dApplication);
165
            dApplication = null;
111 166
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
112 167
            //WrapperApplication wApp = new WrapperApplication(application.Application);
113 168
            //Ingr.RAD2D.Application radApp = wApp.RADApplication;
......
215 270
        #region TEST
216 271
        private void simpleButton1_Click(object sender, EventArgs e)
217 272
        {
273
            return;
218 274
            Thread outThread = new Thread(func2 =>
219 275
            {
220 276
                for (int i = 100; i < 120; i++)
DTI_PID/SPPIDConverter/ConverterForm.cs
579 579
                MessageBox.Show(Msg.SelectDocument, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
580 580
                return;
581 581
            }
582
             
582
            DataTable tDrawing = Project_DB.SelectDrawings();
583 583
            this._Documents.Clear();
584 584
            foreach (int rowHandle in gridViewConverter.GetSelectedRows())
585 585
            {
......
588 588
                string _Template = gridViewConverter.GetRowCellDisplayText(rowHandle, "colTemplate");
589 589
                string _DrawingNumber = gridViewConverter.GetRowCellDisplayText(rowHandle, "colDrawingNumber");
590 590
                string _DrawingName = gridViewConverter.GetRowCellDisplayText(rowHandle, "colDrawingName");
591

  
592
                DataRow[] rows = tDrawing.Select(string.Format("NAME = '{0}'", Path.GetFileNameWithoutExtension(_FilePath) + ".png"));
593
                if (rows.Length != 1)
594
                    continue;
595

  
591 596
                SPPID_Document document = _DicDocuments[_FilePath];
592 597
                document.Unit = _Unit;
593 598
                document.Template = _Template;
594 599
                document.DrawingNumber = _DrawingNumber;
595 600
                document.DrawingName = _DrawingName;
601
                document.UID = rows[0]["UID"].ToString();
596 602

  
597 603
                document.SymbolTable = _ID2SymbolTable;
598 604
                document.SymbolMappings = symbolMappings;
......
607 613
                    this._Documents.Add(document);
608 614
            }
609 615

  
616
            if (_Documents.Count > 50)
617
                checkEditCloseDocument.Checked = true;
618

  
619
            tDrawing.Dispose();
610 620
            DialogResult = DialogResult.OK;
611 621
        }
612 622

  
DTI_PID/SPPIDConverter/DB/Project_DB.cs
18 18
        const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE";
19 19
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
20 20
        const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO";
21
        const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO";
21 22

  
22 23
        const string LineProperties_TABLE = "LineProperties";
23 24
        const string LineTypes_TABLE = "LineTypes";
24 25
        const string SymbolType_TABLE = "SymbolType";
25 26
        const string SymbolAttribute_TABLE = "SymbolAttribute";
26 27
        const string Symbol_TABLE = "Symbol";
28
        const string OPCRelations_TABLE = "OPCRelations";
27 29

  
28 30
        public static bool ConnTestAndCreateTable()
29 31
        {
......
219 221
                    }
220 222
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
221 223
                    {
222
                        cmd.CommandText = string.Format("CREATE TABLE {0} (PATH TEXT PRIMARY KEY, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT, DOCUMENT Text)", SPPID_DRAWING_INFO);
224
                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
225
                        cmd.ExecuteNonQuery();
226
                    }
227
                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
228
                    {
229
                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID TEXT PRIMARY KEY, SPPID_OPC_MODELITEM_ID TEXT, ID2_DRAWING_UID, PAIRED BOOL)", SPPID_OPC_INFO);
223 230
                        cmd.ExecuteNonQuery();
224 231
                    }
225 232
                }
......
254 261
                using (DataTable dt = new DataTable())
255 262
                {
256 263
                    dt.Load(dr);
257
                    if (!dt.Columns.Contains("DOCUMENT"))
264
                    if (dt.Columns.Contains("DOCUMENT"))
258 265
                    {
259
                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN DOCUMENT Text", SPPID_DRAWING_INFO);
266
                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
267
                        cmd.ExecuteNonQuery();
268

  
269
                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
260 270
                        cmd.ExecuteNonQuery();
261 271
                    }
262 272
                }
......
519 529
            return dt;
520 530
        }
521 531

  
532
        public static DataTable SelectOPCRelations()
533
        {
534
            DataTable dt = new DataTable();
535
            Project_Info projectInfo = Project_Info.GetInstance();
536
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
537
            {
538
                try
539
                {
540
                    connection.Open();
541
                    using (SQLiteCommand cmd = connection.CreateCommand())
542
                    {
543
                        cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
544
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
545
                            dt.Load(dr);
546
                    }
547
                    connection.Close();
548
                }
549
                catch (Exception ex)
550
                {
551

  
552
                }
553
                finally
554
                {
555
                    connection.Dispose();
556
                }
557
            }
558

  
559
            return dt;
560
        }
561

  
562
        public static DataTable SelectDrawings()
563
        {
564
            DataTable dt = new DataTable();
565
            Project_Info projectInfo = Project_Info.GetInstance();
566
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
567
            {
568
                try
569
                {
570
                    connection.Open();
571
                    using (SQLiteCommand cmd = connection.CreateCommand())
572
                    {
573
                        cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
574
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
575
                            dt.Load(dr);
576
                    }
577
                    connection.Close();
578
                }
579
                catch (Exception ex)
580
                {
581

  
582
                }
583
                finally
584
                {
585
                    connection.Dispose();
586
                }
587
            }
588

  
589
            return dt;
590
        }
591

  
592
        public static DataTable SelectOPCInfo()
593
        {
594
            DataTable dt = new DataTable();
595
            Project_Info projectInfo = Project_Info.GetInstance();
596
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
597
            {
598
                try
599
                {
600
                    connection.Open();
601
                    using (SQLiteCommand cmd = connection.CreateCommand())
602
                    {
603
                        cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
604
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
605
                            dt.Load(dr);
606
                    }
607
                    connection.Close();
608
                }
609
                catch (Exception ex)
610
                {
611

  
612
                }
613
                finally
614
                {
615
                    connection.Dispose();
616
                }
617
            }
618

  
619
            return dt;
620
        }
522 621
        public static DataTable SelectDrawingInfo()
523 622
        {
524
            DataTable dt = new DataTable() { Locale = CultureInfo.CurrentCulture };
623
            DataTable dt = new DataTable();
525 624
            Project_Info projectInfo = Project_Info.GetInstance();
526 625
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
527 626
            {
......
530 629
                    connection.Open();
531 630
                    using (SQLiteCommand cmd = connection.CreateCommand())
532 631
                    {
533
                        cmd.CommandText = string.Format("SELECT * FROM {0}",SPPID_DRAWING_INFO);
534
                        //using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
535
                        //    da.Fill(dt);
632
                        cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
536 633
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
537
                        {
538
                            dt.Columns.Add("PATH", typeof(string));
539
                            dt.Columns.Add("DRAWINGNUMBER", typeof(string));
540
                            dt.Columns.Add("DRAWINGNAME", typeof(string));
541
                            dt.Columns.Add("DOCUMENT", typeof(SPPID.Model.SPPID_Document));
542
                            while (dr.Read())
543
                            {
544
                                DataRow row = dt.NewRow();
545
                                row["PATH"] = dr["PATH"];
546
                                row["DRAWINGNUMBER"] = dr["DRAWINGNUMBER"];
547
                                row["DRAWINGNAME"] = dr["DRAWINGNAME"];
548
                                string jsonString = (string)dr["DOCUMENT"];
549
                                JsonSerializerSettings settings = new JsonSerializerSettings
550
                                {
551
                                    NullValueHandling = NullValueHandling.Ignore,
552
                                    ObjectCreationHandling = ObjectCreationHandling.Replace
553
                                };
554
                                SPPID.Model.SPPID_Document doc = JsonConvert.DeserializeObject<SPPID.Model.SPPID_Document>(jsonString, settings);
555
                                row["DOCUMENT"] = doc;
556
                                dt.Rows.Add(row);
557
                            }
558
                            //dt.Load(dr, LoadOption.Upsert);
559
                        }
560
                            
634
                            dt.Load(dr);
561 635
                    }
562 636
                    connection.Close();
563 637
                }
......
722 796
            return true;
723 797
        }
724 798

  
725
        public static bool InsertDrawingInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
799
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
726 800
        {
727 801
            Project_Info projectInfo = Project_Info.GetInstance();
728 802
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
......
736 810
                        {
737 811
                            using (SQLiteCommand cmd = connection.CreateCommand())
738 812
                            {
739
                                cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (PATH, DRAWINGNUMBER, DRAWINGNAME, DOCUMENT) VALUES (@PATH, @DRAWINGNUMBER, @DRAWINGNAME, @DOCUMENT)", SPPID_DRAWING_INFO);
813
                                cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO);
814
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
740 815
                                cmd.Parameters.AddWithValue("@PATH", path);
741 816
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
742 817
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
743
                                cmd.Parameters.AddWithValue("@DOCUMENT", JsonConvert.SerializeObject(document));
744 818
                                cmd.ExecuteNonQuery();
745 819
                            }
820

  
821
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
822
                            foreach (var item in OPCs)
823
                            {
824
                                using (SQLiteCommand cmd = connection.CreateCommand())
825
                                {
826
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @PAIRED)", SPPID_OPC_INFO);
827
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
828
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
829
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
830
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
831
                                    cmd.ExecuteNonQuery();
832
                                }
833
                            }
834

  
835
                            transaction.Commit();
836
                            connection.Close();
837
                        }
838
                        catch (Exception ex)
839
                        {
840
                            transaction.Rollback();
841
                        }
842
                        finally
843
                        {
844
                            transaction.Dispose();
845
                        }
846
                    }
847
                }
848
                catch (Exception ex)
849
                {
850
                    return false;
851
                }
852
                finally
853
                {
854
                    connection.Dispose();
855
                }
856
            }
857
            return true;
858
        }
859

  
860
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
861
        {
862
            Project_Info projectInfo = Project_Info.GetInstance();
863
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
864
            {
865
                try
866
                {
867
                    connection.Open();
868
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
869
                    {
870
                        try
871
                        {
872
                            using (SQLiteCommand cmd = connection.CreateCommand())
873
                            {
874
                                cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @PAIRED)", SPPID_OPC_INFO);
875
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
876
                                cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
877
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
878
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
879
                                cmd.ExecuteNonQuery();
880
                            }
881

  
746 882
                            transaction.Commit();
747 883
                            connection.Close();
748 884
                        }
DTI_PID/SPPIDConverter/OPC/AutoModeling_OPC.cs
27 27
{
28 28
    public class AutoModeling_OPC
29 29
    {
30
        Placement _placement;
31
        LMADataSource dataSource;
32 30
        dynamic application;
31
        dynamic drawing;
33 32
        Ingr.RAD2D.Application radApp;
34
        List<SPPID_Document> allDocuments;
33
        string fromOPCModelId;
34
        string toOPCModelId;
35
        string toDrawingName;
35 36

  
36
        public AutoModeling_OPC(List<SPPID_Document> allDocuments, dynamic application, Ingr.RAD2D.Application radApp)
37
        public AutoModeling_OPC(dynamic application, Ingr.RAD2D.Application radApp, string fromOPCModelId, string toOPCModelId, string toDrawingName)
37 38
        {
38 39
            this.application = application;
39 40
            this.radApp = radApp;
40
            this.allDocuments = allDocuments;
41
            this.fromOPCModelId = fromOPCModelId;
42
            this.toOPCModelId = toOPCModelId;
43
            this.toDrawingName = toDrawingName;
41 44
        }
42 45

  
43
        public void Run()
46
        public bool Run()
44 47
        {
45
            _placement = new Placement();
46
            dataSource = _placement.PIDDataSource;
47

  
48
            foreach (var document in allDocuments)
49
            {
50
                foreach (var opc in document.SYMBOLS)
51
                {
52
                    if ((opc.TYPE == "Piping OPC's" ||
53
                        opc.TYPE == "Instrument OPC's") &&
54
                        !string.IsNullOrEmpty(opc.SPPID.ModelItemID))
55
                    {
56
                        PairedOPCModeling(opc);
57
                    }
58
                }
59
            }
48
            bool result = false;
49
            if (OpenDrawing(toDrawingName))
50
                result = PairedOPCModeling();
51
            CloseDrawing();
60 52

  
61
            ReleaseCOMObjects(_placement);
62
            ReleaseCOMObjects(dataSource);
53
            return result;
63 54
        }
64 55

  
65
        public void PairedOPCModeling(Symbol opc)
56
        public bool PairedOPCModeling()
66 57
        {
67
            bool result = false;
68
            LMOPC _LMOPC = dataSource.GetOPC(opc.SPPID.ModelItemID);
69
            LMOPC pairOPC = _LMOPC.pairedWithOPCObject;
70

  
71
            foreach (LMRepresentation rep in pairOPC.Representations)
72
                if (rep.DrawingID != "0")
73
                    result = true;
58
            Placement _placement = new Placement();
59
            LMADataSource dataSource = _placement.PIDDataSource;
74 60

  
75
            if (!result)
61
            bool result = false;
62
            bool isPaired = false;
63
            LMOPC _FROM_OPC = dataSource.GetOPC(fromOPCModelId);
64
            LMOPC _TO_OPC = dataSource.GetOPC(toOPCModelId);
65
            if (_FROM_OPC != null && _FROM_OPC.get_ItemStatus() == "Active" && _TO_OPC != null && _TO_OPC.get_ItemStatus() == "Active")
76 66
            {
77
                // OPC Pair 찾아서 도면 오픈후 Pair 모델링
78
                BaseModel.Attribute pathAttribute = opc.ATTRIBUTES.Find(x => x.ATTRIBUTE == "PATH");
79
                BaseModel.Attribute linkOPCAttribute = opc.ATTRIBUTES.Find(x => x.ATTRIBUTE == "LinkOPC");
67
                string symbolPath = GetSPPIDFileName(_FROM_OPC);
68
                LMOPC pairOPC = _FROM_OPC.pairedWithOPCObject;
80 69

  
81
                SPPID_Document targetDocument = allDocuments.Find(x => x.PATH == pathAttribute.VALUE);
82
                if (targetDocument != null)
70
                foreach (LMRepresentation rep in pairOPC.Representations)
71
                    if (rep.DrawingID != "0")
72
                        isPaired = true;
73
                try
83 74
                {
84
                    Symbol targetOPC = SPPIDUtil.FindObjectByUID(targetDocument, linkOPCAttribute.VALUE) as Symbol;
85
                    if (targetOPC != null && OpenDrawing(targetDocument.SPPID_DrawingName))
75
                    if (!isPaired)
86 76
                    {
87
                        LMSymbol targetLMOPC = dataSource.GetSymbol(targetOPC.SPPID.RepresentationId);
88
                        if (targetLMOPC != null)
77
                        foreach (LMRepresentation rep in _TO_OPC.Representations)
89 78
                        {
90
                            int mirror = targetLMOPC.get_IsMirroredIndex();
91
                            double angle = Convert.ToDouble(targetLMOPC.get_RotationAngle());
92
                            double x = targetLMOPC.get_XCoordinate();
93
                            double y = targetLMOPC.get_YCoordinate();
94
                            LMSymbol newOPC = null;
95
                            foreach (LMConnector LMConnector in targetLMOPC.Avoid1Connectors)
79
                            LMSymbol removeSymbol = dataSource.GetSymbol(rep.Id);
80
                            if (removeSymbol != null)
96 81
                            {
97
                                _placement.PIDRemovePlacement(targetLMOPC.AsLMRepresentation());
98
                                ReleaseCOMObjects(targetLMOPC);
99
                                newOPC = _placement.PIDPlaceSymbol(targetOPC.SPPID.MAPPINGNAME, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
100
                                break;
101
                            }
82
                                ZoomObjectByGraphicOID(removeSymbol.get_GraphicOID().ToString());
102 83

  
103
                            if (newOPC == null)
104
                            {
105
                                foreach (LMConnector LMConnector in targetLMOPC.Avoid2Connectors)
84
                                int mirror = removeSymbol.get_IsMirroredIndex();
85
                                double angle = Convert.ToDouble(removeSymbol.get_RotationAngle());
86
                                double x = removeSymbol.get_XCoordinate();
87
                                double y = removeSymbol.get_YCoordinate();
88
                                LMSymbol newOPC = null;
89
                                foreach (LMConnector LMConnector in removeSymbol.Avoid1Connectors)
106 90
                                {
107
                                    _placement.PIDRemovePlacement(targetLMOPC.AsLMRepresentation());
108
                                    ReleaseCOMObjects(targetLMOPC);
109
                                    newOPC = _placement.PIDPlaceSymbol(targetOPC.SPPID.MAPPINGNAME, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
91
                                    _placement.PIDRemovePlacement(removeSymbol.AsLMRepresentation());
92
                                    ReleaseCOMObjects(removeSymbol);
93
                                    newOPC = _placement.PIDPlaceSymbol(symbolPath, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
110 94
                                    break;
111 95
                                }
96

  
97
                                if (newOPC == null)
98
                                {
99
                                    foreach (LMConnector LMConnector in removeSymbol.Avoid2Connectors)
100
                                    {
101
                                        _placement.PIDRemovePlacement(removeSymbol.AsLMRepresentation());
102
                                        ReleaseCOMObjects(removeSymbol);
103
                                        newOPC = _placement.PIDPlaceSymbol(symbolPath, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
104
                                        break;
105
                                    }
106
                                }
107

  
108
                                if (newOPC != null)
109
                                    result = true;
110

  
111
                                ReleaseCOMObjects(newOPC);
112
                                newOPC = null;
113
                                ReleaseCOMObjects(removeSymbol);
114
                                removeSymbol = null;
115
                                break;
112 116
                            }
117
                            ReleaseCOMObjects(removeSymbol);
118
                            removeSymbol = null;
113 119
                        }
114 120
                    }
115 121
                }
122
                catch (Exception ex)
123
                {
124
                    Log.Write("error OPC");
125
                }
126
                finally
127
                {
128
                    ReleaseCOMObjects(_placement);
129
                    _placement = null;
130
                    ReleaseCOMObjects(dataSource);
131
                    dataSource = null;
132

  
133
                    ReleaseCOMObjects(_FROM_OPC);
134
                    _FROM_OPC = null;
135
                    ReleaseCOMObjects(_TO_OPC);
136
                    _TO_OPC = null;
137
                    ReleaseCOMObjects(pairOPC);
138
                    pairOPC = null;
139
                }
116 140
            }
117 141

  
118
            ReleaseCOMObjects(_LMOPC);
119
            ReleaseCOMObjects(pairOPC);
142
            return result;
143
        }
144

  
145
        /// <summary>
146
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
147
        /// </summary>
148
        /// <param name="graphicOID"></param>
149
        /// <param name="milliseconds"></param>
150
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
151
        {
152
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
153
            {
154
                double minX = 0;
155
                double minY = 0;
156
                double maxX = 0;
157
                double maxY = 0;
158
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
159
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
160

  
161
                Thread.Sleep(milliseconds);
162
            }
163
        }
164

  
165
        private string GetSPPIDFileName(LMOPC opc)
166
        {
167
            string symbolPath = null;
168
            foreach (LMRepresentation rep in opc.Representations)
169
            {
170
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
171
                {
172
                    symbolPath = rep.get_FileName();
173
                    break;
174
                }
175
            }
176
            return symbolPath;
120 177
        }
121 178

  
122 179
        public void ReleaseCOMObjects(params object[] objVars)
123 180
        {
124
            int intNewRefCount = 0;
125
            foreach (object obj in objVars)
181
            if (objVars != null)
126 182
            {
127
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
128
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
183
                int intNewRefCount = 0;
184
                foreach (object obj in objVars)
185
                {
186
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
187
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
188
                }
129 189
            }
130 190
        }
131 191

  
......
134 194
            bool result = false;
135 195
            try
136 196
            {
137
                dynamic doc = application.Drawings.OpenDrawing(drawingName);
138
                doc.Activate();
197
                drawing = application.Drawings.OpenDrawing(drawingName);
198
                drawing.Activate();
139 199

  
140 200
                result = true;
141 201
            }
......
146 206

  
147 207
            return result;
148 208
        }
209

  
210
        private void CloseDrawing()
211
        {
212
            if (drawing != null)
213
            {
214
                drawing.CloseDrawing(true);
215
                ReleaseCOMObjects(drawing);
216
                drawing = null;
217
            }
218
        }
149 219
    }
150 220
}

내보내기 Unified diff

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