프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter_AutoModeling / Utill / SPPIDUtill.cs @ 5376996a

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using SPPID.Model;
7
using SPPID.DB;
8
using SPPID.Modeling;
9
using System.IO;
10
using System.Xml.Linq;
11
using Newtonsoft.Json;
12

    
13
namespace SPPID.Utill
14
{
15
    public static class SPPIDUtill
16
    {
17
        private static readonly double _DWG_X = 0.875;
18
        private static readonly double _DWG_Y = 0.617;
19
        public static string defaultPath = "";
20
        public static string mappingFilePath = "";
21
        public static string dbFilePath = "";
22

    
23
        public static void ConvertSPPIDPoint(ref double dX, ref double dY, double dDwgX, double dDwgY)
24
        {
25
            double calcX = 0;
26
            double calcY = 0;
27
            calcX = (dX * _DWG_X) / dDwgX;
28
            calcX = Math.Truncate(calcX * 1000) / 1000;
29
            calcY = _DWG_Y - ((dY * _DWG_Y) / dDwgY);
30
            calcY = Math.Truncate(calcY * 1000) / 1000;
31
            dX = calcX;
32
            dY = calcY;
33
        }
34

    
35
        public static bool ConvertPointBystring(string sPoint, ref double dX, ref double dY)
36
        {
37
            try
38
            {
39
                string[] pointArr = sPoint.Split(new char[] { ',' });
40
                if (pointArr.Length == 2)
41
                {
42
                    dX = Convert.ToDouble(pointArr[0]);
43
                    dY = Convert.ToDouble(pointArr[1]);
44
                }
45
            }
46
            catch (Exception)
47
            {
48
                dX = 0;
49
                dY = 0;
50
                return false;
51
            }
52
                
53
            return true;
54
        }
55

    
56
        public static Line.SlopeType CalcSlop(PointInfo point1, PointInfo point2)
57
        {
58
            Line.SlopeType _type = Line.SlopeType.NONE;
59

    
60
            if (point1.X - point2.X == 0)
61
                _type = Line.SlopeType.VERTICAL;
62
            else
63
            {
64
                double angle = Math.Atan(Math.Abs(point2.Y - point1.Y) / Math.Abs(point2.X - point1.X)) * 180 / Math.PI;
65
                if (angle < 20)
66
                    _type = Line.SlopeType.HORIZONTAL;
67
                else if (angle > 70)
68
                    _type = Line.SlopeType.VERTICAL;
69
                else
70
                    _type = Line.SlopeType.SLOPE;
71
            }
72

    
73
            return _type;
74
        }
75

    
76
        public static object FindObjectByUID(Document document, string UID)
77
        {
78
            foreach (Symbol item in document.SYMBOLS)
79
            {
80
                if (item.UID == UID)
81
                    return item;
82
            }
83

    
84
            foreach (Line item in document.LINES)
85
            {
86
                if (item.UID == UID)
87
                    return item;
88
            }
89

    
90
            foreach (Text item in document.TEXTS)
91
            {
92
                if (item.UID == UID)
93
                    return item;
94
            }
95

    
96
            foreach (LineNumber item in document.LINENUMBERS)
97
            {
98
                if (item.UID == UID)
99
                    return item;
100
            }
101

    
102
            return null;
103
        }
104

    
105
        public static string GetSPPIDSymbolMappingName(string name, Dictionary<string, string> symbolMapping)
106
        {
107
            if (name == "SIZE")
108
            {
109
                if (symbolMapping.ContainsKey(Text.pipingCompSize) && symbolMapping.ContainsKey(Text.instrumentSize))
110
                {
111
                    return "true";
112
                }
113
                else
114
                {
115
                    return "";
116
                }
117
            }
118

    
119
            if (symbolMapping.ContainsKey(name))
120
                return symbolMapping[name];
121
            else
122
                return "";
123
        }
124

    
125
        public static string GetSPPIDAttributeMappingName(string name, Dictionary<string, string> attributeMapping)
126
        {
127
            if (attributeMapping.ContainsKey(name))
128
                return attributeMapping[name];
129
            else
130
                return "";
131
        }
132

    
133
        public static bool IsBranchLine(string UID, Line connectedLine)
134
        {
135
            try
136
            {
137
                if (connectedLine.CONNECTORS[0].CONNECTEDITEM != UID && connectedLine.CONNECTORS[1].CONNECTEDITEM != UID)
138
                    return true; 
139
            }
140
            catch (Exception ex)
141
            {
142
                Log.WriteLine(ex);
143
            }
144

    
145
            return false;
146
        }
147

    
148
        public static double CalcPointToPointdDistance(PointInfo point1, PointInfo point2)
149
        {
150
            return Math.Pow(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2), 0.5);
151
        }
152

    
153
        public static double CalcLineToPointDistance(PointInfo linePoint1, PointInfo linePoint2, PointInfo point)
154
        {
155
            double distance = 0;
156
            if (linePoint1.X == linePoint2.X)
157
                distance = Math.Abs(point.X - linePoint1.X);
158
            else
159
            {
160
                double a;
161
                double b;
162
                double c;
163

    
164
                a = (linePoint2.Y - linePoint1.Y) / (linePoint2.X - linePoint1.X);
165
                b = -1;
166
                c = -a * linePoint1.X + linePoint1.Y;
167

    
168
                distance = Math.Abs(a * point.X + b * point.Y + c) / Math.Pow(a * a + b * b, 0.5);
169
            }
170
            return distance;
171
        }
172

    
173
        public static bool GroupValidationCheck(Group group)
174
        {
175
            bool result = true;
176

    
177
            foreach (SPPID_ITEM item in group.Items)
178
            {
179

    
180
            }
181

    
182

    
183

    
184
            return result;
185
        }
186

    
187

    
188
        public static void SaveMapping(Dictionary<string, string> symbolMapping, Dictionary<string,string> attributeMapping)
189
        {
190
            XElement mappingElement = new XElement("Mapping");
191

    
192
            XElement symbolElement = new XElement("Symbol");
193
            foreach (var item in symbolMapping)
194
            {
195
                string name = item.Key;
196
                string SPPID_Name = item.Value;
197

    
198
                XElement element = new XElement("DATA");
199
                element.SetAttributeValue("SPPID", SPPID_Name);
200
                element.Value = name;
201

    
202
                symbolElement.Add(element);
203
            }
204
            mappingElement.Add(symbolElement);
205

    
206
            XElement attributeElement = new XElement("Attribute");
207
            foreach (var item in attributeMapping)
208
            {
209
                string name = item.Key;
210
                string SPPID_Name = item.Value;
211

    
212
                XElement element = new XElement("DATA");
213
                element.SetAttributeValue("SPPID", SPPID_Name);
214
                element.Value = name;
215

    
216
                attributeElement.Add(element);
217
            }
218
            mappingElement.Add(attributeElement);
219

    
220
            mappingElement.Save(mappingFilePath);
221
        }
222

    
223
        public static void LoadMapping(Dictionary<string, string> symbolMapping, Dictionary<string, string> attributeMapping)
224
        {
225
            try
226
            {
227
                if (System.IO.File.Exists(mappingFilePath))
228
                {
229
                    XElement xml = XElement.Load(mappingFilePath);
230

    
231
                    XElement symbolXml = xml.Element("Symbol");
232
                    foreach (XElement mappingData in symbolXml.Elements("DATA"))
233
                    {
234
                        if (symbolMapping.ContainsKey(mappingData.Value))
235
                            symbolMapping[mappingData.Value] = mappingData.Attribute("SPPID").Value;
236
                        else
237
                            symbolMapping.Add(mappingData.Value, mappingData.Attribute("SPPID").Value);
238
                    }
239

    
240
                    XElement attributeXml = xml.Element("Attribute");
241
                    foreach (XElement mappingData in attributeXml.Elements("DATA"))
242
                    {
243
                        if (attributeMapping.ContainsKey(mappingData.Value))
244
                            attributeMapping[mappingData.Value] = mappingData.Attribute("SPPID").Value;
245
                        else
246
                            attributeMapping.Add(mappingData.Value, mappingData.Attribute("SPPID").Value);
247
                    }
248
                }
249
            }
250
            catch (Exception ex)
251
            {
252
                Log.WriteLine(ex);
253
            }
254
        }
255

    
256
        public static void SaveDB(string sJson)
257
        {
258
            using (StreamWriter sw = new StreamWriter(dbFilePath, false))
259
            {
260
                sw.Write(sJson);
261
            }
262
        }
263

    
264
        public static bool LoadDB()
265
        {
266
            DBInformation dbInfo = DBInformation.GetInstance();
267
            try
268
            {
269
                using (StreamReader sr = new StreamReader(dbFilePath))
270
                {
271
                    DBInformation savedDbInfo = JsonConvert.DeserializeObject<DBInformation>(sr.ReadToEnd());
272
                    dbInfo.DBPassword = savedDbInfo.DBPassword;
273
                    dbInfo.DBType = savedDbInfo.DBType;
274
                    dbInfo.DBUser = savedDbInfo.DBUser;
275
                    dbInfo.Port = savedDbInfo.Port;
276
                    dbInfo.ServerIP = savedDbInfo.ServerIP;
277
                    dbInfo.Service = savedDbInfo.Service;
278
                    dbInfo.Site = savedDbInfo.Site;
279

    
280
                    dbInfo.Plant = savedDbInfo.Plant;
281
                    dbInfo.PlantDic = savedDbInfo.PlantDic;
282
                    dbInfo.PlantPID = savedDbInfo.PlantPID;
283
                    dbInfo.PlantPIDDic = savedDbInfo.PlantPIDDic;
284
                    dbInfo.PlantPath = savedDbInfo.PlantPath;
285

    
286
                    dbInfo.Status = savedDbInfo.Status;
287

    
288
                    return dbInfo.Status;
289
                }
290
            }
291
            catch (Exception ex)
292
            {
293

    
294
            }
295
            return false;
296
        }
297
    }
298
}
클립보드 이미지 추가 (최대 크기: 500 MB)