프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / OPC / AutoModeling_OPC.cs @ 3d7ff99c

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

1
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
namespace Converter.SPPID.OPC
27
{
28
    public class AutoModeling_OPC
29
    {
30
        Placement _placement;
31
        LMADataSource dataSource;
32
        dynamic application;
33
        Ingr.RAD2D.Application radApp;
34
        List<SPPID_Document> allDocuments;
35

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

    
43
        public void Run()
44
        {
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
            }
60

    
61
            ReleaseCOMObjects(_placement);
62
            ReleaseCOMObjects(dataSource);
63
        }
64

    
65
        public void PairedOPCModeling(Symbol opc)
66
        {
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;
74

    
75
            if (!result)
76
            {
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");
80

    
81
                SPPID_Document targetDocument = allDocuments.Find(x => x.PATH == pathAttribute.VALUE);
82
                if (targetDocument != null)
83
                {
84
                    Symbol targetOPC = SPPIDUtil.FindObjectByUID(targetDocument, linkOPCAttribute.VALUE) as Symbol;
85
                    if (targetOPC != null && OpenDrawing(targetDocument.SPPID_DrawingName))
86
                    {
87
                        LMSymbol targetLMOPC = dataSource.GetSymbol(targetOPC.SPPID.RepresentationId);
88
                        if (targetLMOPC != null)
89
                        {
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)
96
                            {
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
                            }
102

    
103
                            if (newOPC == null)
104
                            {
105
                                foreach (LMConnector LMConnector in targetLMOPC.Avoid2Connectors)
106
                                {
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);
110
                                    break;
111
                                }
112
                            }
113
                        }
114
                    }
115
                }
116
            }
117

    
118
            ReleaseCOMObjects(_LMOPC);
119
            ReleaseCOMObjects(pairOPC);
120
        }
121

    
122
        public void ReleaseCOMObjects(params object[] objVars)
123
        {
124
            int intNewRefCount = 0;
125
            foreach (object obj in objVars)
126
            {
127
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
128
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
129
            }
130
        }
131

    
132
        private bool OpenDrawing(string drawingName)
133
        {
134
            bool result = false;
135
            try
136
            {
137
                dynamic doc = application.Drawings.OpenDrawing(drawingName);
138
                doc.Activate();
139

    
140
                result = true;
141
            }
142
            catch (Exception ex)
143
            {
144
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
145
            }
146

    
147
            return result;
148
        }
149
    }
150
}
클립보드 이미지 추가 (최대 크기: 500 MB)