프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / OPC / AutoModeling_OPC.cs @ b7a29053

이력 | 보기 | 이력해설 | 다운로드 (8.61 KB)

1 69b7387a gaqhf
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Data;
7
using Llama;
8
using Plaice;
9
using Ingr.RAD2D.Interop.RAD2D;
10
using Ingr.RAD2D.Internal;
11
using Ingr.RAD2D.Helper;
12
using Converter.BaseModel;
13
using Converter.SPPID.Model;
14
using Converter.SPPID.Properties;
15
using Converter.SPPID.Util;
16
using Converter.SPPID.DB;
17
using Ingr.RAD2D.MacroControls.CmdCtrl;
18
using Ingr.RAD2D;
19
using System.Windows;
20
using System.Threading;
21
using System.Drawing;
22
using Microsoft.VisualBasic;
23
using Newtonsoft.Json;
24
25
using DevExpress.XtraSplashScreen;
26 3d7ff99c gaqhf
namespace Converter.SPPID.OPC
27 69b7387a gaqhf
{
28
    public class AutoModeling_OPC
29
    {
30
        dynamic application;
31 154d8f43 gaqhf
        dynamic drawing;
32 69b7387a gaqhf
        Ingr.RAD2D.Application radApp;
33 154d8f43 gaqhf
        string fromOPCModelId;
34
        string toOPCModelId;
35
        string toDrawingName;
36 b7a29053 gaqhf
        List<string[]> toOPCAttributes;
37 69b7387a gaqhf
38 b7a29053 gaqhf
        public AutoModeling_OPC(dynamic application, Ingr.RAD2D.Application radApp, string fromOPCModelId, string toOPCModelId, string toDrawingName, List<string[]> toOPCAttributes)
39 69b7387a gaqhf
        {
40
            this.application = application;
41
            this.radApp = radApp;
42 154d8f43 gaqhf
            this.fromOPCModelId = fromOPCModelId;
43
            this.toOPCModelId = toOPCModelId;
44
            this.toDrawingName = toDrawingName;
45 b7a29053 gaqhf
            this.toOPCAttributes = toOPCAttributes;
46 69b7387a gaqhf
        }
47
48 154d8f43 gaqhf
        public bool Run()
49 69b7387a gaqhf
        {
50 154d8f43 gaqhf
            bool result = false;
51
            if (OpenDrawing(toDrawingName))
52
                result = PairedOPCModeling();
53
            CloseDrawing();
54 69b7387a gaqhf
55 154d8f43 gaqhf
            return result;
56 1ed39474 gaqhf
        }
57 69b7387a gaqhf
58 154d8f43 gaqhf
        public bool PairedOPCModeling()
59 1ed39474 gaqhf
        {
60 154d8f43 gaqhf
            Placement _placement = new Placement();
61
            LMADataSource dataSource = _placement.PIDDataSource;
62 224535bb gaqhf
63 154d8f43 gaqhf
            bool result = false;
64
            bool isPaired = false;
65
            LMOPC _FROM_OPC = dataSource.GetOPC(fromOPCModelId);
66
            LMOPC _TO_OPC = dataSource.GetOPC(toOPCModelId);
67
            if (_FROM_OPC != null && _FROM_OPC.get_ItemStatus() == "Active" && _TO_OPC != null && _TO_OPC.get_ItemStatus() == "Active")
68 1ed39474 gaqhf
            {
69 154d8f43 gaqhf
                string symbolPath = GetSPPIDFileName(_FROM_OPC);
70
                LMOPC pairOPC = _FROM_OPC.pairedWithOPCObject;
71 224535bb gaqhf
72 154d8f43 gaqhf
                foreach (LMRepresentation rep in pairOPC.Representations)
73
                    if (rep.DrawingID != "0")
74
                        isPaired = true;
75
                try
76 71ba1ca3 gaqhf
                {
77 154d8f43 gaqhf
                    if (!isPaired)
78 88bac50c gaqhf
                    {
79 154d8f43 gaqhf
                        foreach (LMRepresentation rep in _TO_OPC.Representations)
80 88bac50c gaqhf
                        {
81 154d8f43 gaqhf
                            LMSymbol removeSymbol = dataSource.GetSymbol(rep.Id);
82
                            if (removeSymbol != null)
83 88bac50c gaqhf
                            {
84 154d8f43 gaqhf
                                ZoomObjectByGraphicOID(removeSymbol.get_GraphicOID().ToString());
85 88bac50c gaqhf
86 154d8f43 gaqhf
                                int mirror = removeSymbol.get_IsMirroredIndex();
87
                                double angle = Convert.ToDouble(removeSymbol.get_RotationAngle());
88
                                double x = removeSymbol.get_XCoordinate();
89
                                double y = removeSymbol.get_YCoordinate();
90
                                LMSymbol newOPC = null;
91
                                foreach (LMConnector LMConnector in removeSymbol.Avoid1Connectors)
92 88bac50c gaqhf
                                {
93 154d8f43 gaqhf
                                    _placement.PIDRemovePlacement(removeSymbol.AsLMRepresentation());
94
                                    ReleaseCOMObjects(removeSymbol);
95
                                    newOPC = _placement.PIDPlaceSymbol(symbolPath, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
96 88bac50c gaqhf
                                    break;
97
                                }
98 154d8f43 gaqhf
99
                                if (newOPC == null)
100
                                {
101
                                    foreach (LMConnector LMConnector in removeSymbol.Avoid2Connectors)
102
                                    {
103
                                        _placement.PIDRemovePlacement(removeSymbol.AsLMRepresentation());
104
                                        ReleaseCOMObjects(removeSymbol);
105
                                        newOPC = _placement.PIDPlaceSymbol(symbolPath, x, y, Mirror: mirror, Rotation: angle, ExistingItem: pairOPC.AsLMAItem(), TargetItem: LMConnector);
106
                                        break;
107
                                    }
108
                                }
109
110
                                if (newOPC != null)
111 b7a29053 gaqhf
                                {
112 154d8f43 gaqhf
                                    result = true;
113 b7a29053 gaqhf
                                    foreach (var attribute in toOPCAttributes)
114
                                    {
115
                                        LMAAttribute _attribute = newOPC.Attributes[attribute[0]];
116
                                        if (_attribute != null)
117
                                            _attribute.set_Value(attribute[1]);
118
                                    }
119
                                    newOPC.Commit();
120
                                }
121 154d8f43 gaqhf
122
                                ReleaseCOMObjects(newOPC);
123
                                newOPC = null;
124
                                ReleaseCOMObjects(removeSymbol);
125
                                removeSymbol = null;
126
                                break;
127 88bac50c gaqhf
                            }
128 154d8f43 gaqhf
                            ReleaseCOMObjects(removeSymbol);
129
                            removeSymbol = null;
130 88bac50c gaqhf
                        }
131
                    }
132 71ba1ca3 gaqhf
                }
133 154d8f43 gaqhf
                catch (Exception ex)
134
                {
135
                    Log.Write("error OPC");
136
                }
137
                finally
138
                {
139
                    ReleaseCOMObjects(_placement);
140
                    _placement = null;
141
                    ReleaseCOMObjects(dataSource);
142
                    dataSource = null;
143
144
                    ReleaseCOMObjects(_FROM_OPC);
145
                    _FROM_OPC = null;
146
                    ReleaseCOMObjects(_TO_OPC);
147
                    _TO_OPC = null;
148
                    ReleaseCOMObjects(pairOPC);
149
                    pairOPC = null;
150
                }
151 71ba1ca3 gaqhf
            }
152 224535bb gaqhf
153 154d8f43 gaqhf
            return result;
154
        }
155
156
        /// <summary>
157
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
158
        /// </summary>
159
        /// <param name="graphicOID"></param>
160
        /// <param name="milliseconds"></param>
161
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
162
        {
163
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
164
            {
165
                double minX = 0;
166
                double minY = 0;
167
                double maxX = 0;
168
                double maxY = 0;
169
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
170
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
171
172
                Thread.Sleep(milliseconds);
173
            }
174
        }
175
176
        private string GetSPPIDFileName(LMOPC opc)
177
        {
178
            string symbolPath = null;
179
            foreach (LMRepresentation rep in opc.Representations)
180
            {
181
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
182
                {
183
                    symbolPath = rep.get_FileName();
184
                    break;
185
                }
186
            }
187
            return symbolPath;
188 224535bb gaqhf
        }
189
190
        public void ReleaseCOMObjects(params object[] objVars)
191
        {
192 154d8f43 gaqhf
            if (objVars != null)
193 224535bb gaqhf
            {
194 154d8f43 gaqhf
                int intNewRefCount = 0;
195
                foreach (object obj in objVars)
196
                {
197
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
198
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
199
                }
200 224535bb gaqhf
            }
201 69b7387a gaqhf
        }
202 1ed39474 gaqhf
203 71ba1ca3 gaqhf
        private bool OpenDrawing(string drawingName)
204 1ed39474 gaqhf
        {
205 71ba1ca3 gaqhf
            bool result = false;
206
            try
207
            {
208 154d8f43 gaqhf
                drawing = application.Drawings.OpenDrawing(drawingName);
209
                drawing.Activate();
210 71ba1ca3 gaqhf
211
                result = true;
212
            }
213
            catch (Exception ex)
214
            {
215
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
216
            }
217 1ed39474 gaqhf
218 71ba1ca3 gaqhf
            return result;
219 1ed39474 gaqhf
        }
220 154d8f43 gaqhf
221
        private void CloseDrawing()
222
        {
223
            if (drawing != null)
224
            {
225
                drawing.CloseDrawing(true);
226
                ReleaseCOMObjects(drawing);
227
                drawing = null;
228
            }
229
        }
230 69b7387a gaqhf
    }
231
}
클립보드 이미지 추가 (최대 크기: 500 MB)