프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter_AutoModeling / Utill / SPPIDUtill.cs @ 5e67ad61

이력 | 보기 | 이력해설 | 다운로드 (9.62 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.855;//0.8175;//0.875;
18
        private static readonly double _DWG_Y = 0.55;//0.5853;//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 (true)
120
            {
121

    
122
            }
123

    
124
            if (symbolMapping.ContainsKey(name))
125
                return symbolMapping[name];
126
            else
127
                return "";
128
        }
129

    
130
        public static string GetSPPIDAttributeMappingName(string name, Dictionary<string, string> attributeMapping)
131
        {
132
            if (attributeMapping.ContainsKey(name))
133
                return attributeMapping[name];
134
            else
135
                return "";
136
        }
137

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

    
150
            return false;
151
        }
152

    
153
        public static double CalcPointToPointdDistance(PointInfo point1, PointInfo point2)
154
        {
155
            return Math.Pow(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2), 0.5);
156
        }
157

    
158
        public static double CalcLineToPointDistance(PointInfo linePoint1, PointInfo linePoint2, PointInfo point)
159
        {
160
            double distance = 0;
161
            if (linePoint1.X == linePoint2.X)
162
                distance = Math.Abs(point.X - linePoint1.X);
163
            else
164
            {
165
                double a;
166
                double b;
167
                double c;
168

    
169
                a = (linePoint2.Y - linePoint1.Y) / (linePoint2.X - linePoint1.X);
170
                b = -1;
171
                c = -a * linePoint1.X + linePoint1.Y;
172

    
173
                distance = Math.Abs(a * point.X + b * point.Y + c) / Math.Pow(a * a + b * b, 0.5);
174
            }
175
            return distance;
176
        }
177

    
178
        public static bool GroupValidationCheck(Group group)
179
        {
180
            bool result = true;
181

    
182
            foreach (SPPID_ITEM item in group.Items)
183
            {
184

    
185
            }
186

    
187

    
188

    
189
            return result;
190
        }
191

    
192

    
193
        public static void SaveMapping(Dictionary<string, string> symbolMapping, Dictionary<string,string> attributeMapping)
194
        {
195
            XElement mappingElement = new XElement("Mapping");
196

    
197
            XElement symbolElement = new XElement("Symbol");
198
            foreach (var item in symbolMapping)
199
            {
200
                string name = item.Key;
201
                string SPPID_Name = item.Value;
202

    
203
                XElement element = new XElement("DATA");
204
                element.SetAttributeValue("SPPID", SPPID_Name);
205
                element.Value = name;
206

    
207
                symbolElement.Add(element);
208
            }
209
            mappingElement.Add(symbolElement);
210

    
211
            XElement attributeElement = new XElement("Attribute");
212
            foreach (var item in attributeMapping)
213
            {
214
                string name = item.Key;
215
                string SPPID_Name = item.Value;
216

    
217
                XElement element = new XElement("DATA");
218
                element.SetAttributeValue("SPPID", SPPID_Name);
219
                element.Value = name;
220

    
221
                attributeElement.Add(element);
222
            }
223
            mappingElement.Add(attributeElement);
224

    
225
            mappingElement.Save(mappingFilePath);
226
        }
227

    
228
        public static void LoadMapping(Dictionary<string, string> symbolMapping, Dictionary<string, string> attributeMapping)
229
        {
230
            try
231
            {
232
                if (System.IO.File.Exists(mappingFilePath))
233
                {
234
                    XElement xml = XElement.Load(mappingFilePath);
235

    
236
                    XElement symbolXml = xml.Element("Symbol");
237
                    foreach (XElement mappingData in symbolXml.Elements("DATA"))
238
                    {
239
                        if (symbolMapping.ContainsKey(mappingData.Value))
240
                            symbolMapping[mappingData.Value] = mappingData.Attribute("SPPID").Value;
241
                        else
242
                            symbolMapping.Add(mappingData.Value, mappingData.Attribute("SPPID").Value);
243
                    }
244

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

    
261
        public static void SaveDB(string sJson)
262
        {
263
            using (StreamWriter sw = new StreamWriter(dbFilePath, false))
264
            {
265
                sw.Write(sJson);
266
            }
267
        }
268

    
269
        public static bool LoadDB()
270
        {
271
            DBInformation dbInfo = DBInformation.GetInstance();
272
            try
273
            {
274
                using (StreamReader sr = new StreamReader(dbFilePath))
275
                {
276
                    DBInformation savedDbInfo = JsonConvert.DeserializeObject<DBInformation>(sr.ReadToEnd());
277
                    dbInfo.DBPassword = savedDbInfo.DBPassword;
278
                    dbInfo.DBType = savedDbInfo.DBType;
279
                    dbInfo.DBUser = savedDbInfo.DBUser;
280
                    dbInfo.Port = savedDbInfo.Port;
281
                    dbInfo.ServerIP = savedDbInfo.ServerIP;
282
                    dbInfo.Service = savedDbInfo.Service;
283
                    dbInfo.Site = savedDbInfo.Site;
284
                    dbInfo.SelectedPlant = savedDbInfo.SelectedPlant;
285
                    dbInfo.PlantList = savedDbInfo.PlantList;
286

    
287
                    dbInfo.Plant = savedDbInfo.Plant;
288
                    dbInfo.PlantDic = savedDbInfo.PlantDic;
289
                    dbInfo.PlantPID = savedDbInfo.PlantPID;
290
                    dbInfo.PlantPIDDic = savedDbInfo.PlantPIDDic;
291
                    dbInfo.PlantPath = savedDbInfo.PlantPath;
292

    
293
                    dbInfo.Status = savedDbInfo.Status;
294

    
295
                    return dbInfo.Status;
296
                }
297
            }
298
            catch (Exception ex)
299
            {
300

    
301
            }
302
            return false;
303
        }
304
    }
305
}
클립보드 이미지 추가 (최대 크기: 500 MB)