프로젝트

일반

사용자정보

개정판 154d8f43

ID154d8f43aebc83a10debd29d4dde8deae0f977ed
상위 3d7ff99c
하위 b7a29053

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

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

Change-Id: I0150a1f6234dafa298e7bf2a405d290f84ad93a5

차이점 보기:

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)