hytos / DTI_PID / SPPIDConverter / Util / SPPIDUtil.cs @ 2fdb56bf
이력 | 보기 | 이력해설 | 다운로드 (11.4 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using Newtonsoft.Json; |
7 |
using System.IO; |
8 |
using Converter.SPPID.DB; |
9 |
using Converter.BaseModel; |
10 |
using System.Windows.Forms; |
11 |
using Converter.SPPID.Model; |
12 |
using System.Drawing; |
13 |
|
14 |
namespace Converter.SPPID.Util |
15 |
{ |
16 |
public enum SlopeType |
17 |
{ |
18 |
None, |
19 |
Slope, |
20 |
HORIZONTAL, |
21 |
VERTICAL |
22 |
} |
23 |
public class SPPIDUtil |
24 |
{ |
25 |
public static bool ConvertToSPPIDInfo(string jsonString) |
26 |
{ |
27 |
SPPID_DBInfo _SPPIDInfo = SPPID_DBInfo.GetInstance(); |
28 |
try |
29 |
{ |
30 |
SPPID_DBInfo jsonSPPIDInfo = JsonConvert.DeserializeObject<SPPID_DBInfo>(jsonString); |
31 |
|
32 |
_SPPIDInfo.DBType = jsonSPPIDInfo.DBType; |
33 |
_SPPIDInfo.Service = jsonSPPIDInfo.Service; |
34 |
_SPPIDInfo.Site = jsonSPPIDInfo.Site; |
35 |
_SPPIDInfo.ServerIP = jsonSPPIDInfo.ServerIP; |
36 |
_SPPIDInfo.Port = jsonSPPIDInfo.Port; |
37 |
_SPPIDInfo.DBUser = jsonSPPIDInfo.DBUser; |
38 |
_SPPIDInfo.DBPassword = jsonSPPIDInfo.DBPassword; |
39 |
_SPPIDInfo.PlantPath = jsonSPPIDInfo.PlantPath; |
40 |
_SPPIDInfo.PlantDic = jsonSPPIDInfo.PlantDic; |
41 |
_SPPIDInfo.PlantPID = jsonSPPIDInfo.PlantPID; |
42 |
_SPPIDInfo.PlantPIDDic = jsonSPPIDInfo.PlantPIDDic; |
43 |
_SPPIDInfo.Plant = jsonSPPIDInfo.Plant; |
44 |
_SPPIDInfo.Enable = jsonSPPIDInfo.Enable; |
45 |
_SPPIDInfo.SelectedPlant = jsonSPPIDInfo.SelectedPlant; |
46 |
_SPPIDInfo.PlantList = jsonSPPIDInfo.PlantList; |
47 |
|
48 |
} |
49 |
catch (Exception ex) |
50 |
{ |
51 |
_SPPIDInfo.Enable = false; |
52 |
return false; |
53 |
} |
54 |
return true; |
55 |
} |
56 |
|
57 |
public static bool ConvertToETCSetting(string jsonString) |
58 |
{ |
59 |
ETCSetting _ETCSetting = ETCSetting.GetInstance(); |
60 |
try |
61 |
{ |
62 |
ETCSetting jsonETCSetting = JsonConvert.DeserializeObject<ETCSetting>(jsonString); |
63 |
|
64 |
_ETCSetting.NoteSymbolPath = jsonETCSetting.NoteSymbolPath; |
65 |
_ETCSetting.TextSymbolPath = jsonETCSetting.TextSymbolPath; |
66 |
_ETCSetting.DrainValveSize = jsonETCSetting.DrainValveSize; |
67 |
_ETCSetting.TextLocation = jsonETCSetting.TextLocation; |
68 |
_ETCSetting.NoteLocation = jsonETCSetting.NoteLocation; |
69 |
_ETCSetting.LineNumberLocation = jsonETCSetting.LineNumberLocation; |
70 |
|
71 |
} |
72 |
catch (Exception ex) |
73 |
{ |
74 |
return false; |
75 |
} |
76 |
return true; |
77 |
} |
78 |
|
79 |
public static bool ConvertToGridSetting(string jsonString) |
80 |
{ |
81 |
GridSetting _GridSetting = GridSetting.GetInstance(); |
82 |
try |
83 |
{ |
84 |
GridSetting jsonGridSetting = JsonConvert.DeserializeObject<GridSetting>(jsonString); |
85 |
|
86 |
_GridSetting.UseSnapGrid = jsonGridSetting.UseSnapGrid; |
87 |
_GridSetting.Density = jsonGridSetting.Density; |
88 |
_GridSetting.Unit = jsonGridSetting.Unit; |
89 |
} |
90 |
catch (Exception ex) |
91 |
{ |
92 |
return false; |
93 |
} |
94 |
return true; |
95 |
} |
96 |
|
97 |
public static bool ConvertPointBystring(string sPoint, ref double dX, ref double dY) |
98 |
{ |
99 |
try |
100 |
{ |
101 |
string[] pointArr = sPoint.Split(new char[] { ',' }); |
102 |
if (pointArr.Length == 2) |
103 |
{ |
104 |
dX = Convert.ToDouble(pointArr[0]); |
105 |
dY = Convert.ToDouble(pointArr[1]); |
106 |
} |
107 |
} |
108 |
catch (Exception) |
109 |
{ |
110 |
dX = 0; |
111 |
dY = 0; |
112 |
return false; |
113 |
} |
114 |
|
115 |
return true; |
116 |
} |
117 |
|
118 |
public static void ConvertSPPIDPoint(ref double dX, ref double dY, double dDwgX, double dDwgY, double SPPID_Width, double SPPID_Height) |
119 |
{ |
120 |
decimal calcX = 0; |
121 |
decimal calcY = 0; |
122 |
decimal tempX = Convert.ToDecimal(dX); |
123 |
decimal tempY = Convert.ToDecimal(dY); |
124 |
decimal tempWidth = Convert.ToDecimal(SPPID_Width); |
125 |
decimal tempHeight = Convert.ToDecimal(SPPID_Height); |
126 |
decimal tempDwgX = Convert.ToDecimal(dDwgX); |
127 |
decimal tempDwgY = Convert.ToDecimal(dDwgY); |
128 |
|
129 |
//calcX = (tempX * tempWidth) / tempDwgX; |
130 |
//calcX = Math.Truncate(calcX * 1000) / 1000; |
131 |
//calcY = tempHeight - ((tempY * tempHeight) / tempDwgY); |
132 |
//calcY = Math.Truncate(calcY * 1000) / 1000; |
133 |
//dX = Math.Round(Convert.ToDouble(calcX), 10); |
134 |
//dY = Math.Round(Convert.ToDouble(calcY), 10); |
135 |
|
136 |
calcX = (tempX * tempWidth) / tempDwgX; |
137 |
calcY = tempHeight - ((tempY * tempHeight) / tempDwgY); |
138 |
dX = Convert.ToDouble(calcX); |
139 |
dY = Convert.ToDouble(calcY); |
140 |
} |
141 |
|
142 |
public static void ConvertGridPoint(ref double x, ref double y) |
143 |
{ |
144 |
GridSetting _GridSetting = GridSetting.GetInstance(); |
145 |
if (_GridSetting.UseSnapGrid) |
146 |
{ |
147 |
if (_GridSetting.Unit == GridUnit.Inch) |
148 |
{ |
149 |
double length = _GridSetting.Density * 0.0254; |
150 |
double tempX = x; |
151 |
double tempY = y; |
152 |
x = Math.Round(tempX / length) * length; |
153 |
y = Math.Round(tempY / length) * length; |
154 |
} |
155 |
} |
156 |
} |
157 |
|
158 |
public static SlopeType CalcSlope(double x1, double y1, double x2, double y2) |
159 |
{ |
160 |
if (x1 - x2 == 0) |
161 |
{ |
162 |
return SlopeType.VERTICAL; |
163 |
} |
164 |
else |
165 |
{ |
166 |
double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI; |
167 |
if (angle <= 2) |
168 |
return SlopeType.HORIZONTAL; |
169 |
else if (angle >= 88) |
170 |
return SlopeType.VERTICAL; |
171 |
else |
172 |
return SlopeType.Slope; |
173 |
} |
174 |
} |
175 |
|
176 |
public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y) |
177 |
{ |
178 |
|
179 |
double distance = 0; |
180 |
if (lineX1 == lineX2) |
181 |
distance = Math.Abs(x - lineX1); |
182 |
else |
183 |
{ |
184 |
double a; |
185 |
double b; |
186 |
double c; |
187 |
|
188 |
a = (lineY2 - lineY1) / (lineX2 - lineX1); |
189 |
b = -1; |
190 |
c = -a * lineX1 + lineY1; |
191 |
|
192 |
distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5); |
193 |
} |
194 |
return distance; |
195 |
} |
196 |
|
197 |
public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) |
198 |
{ |
199 |
x1 = Math.Round(x1, 10); |
200 |
x2 = Math.Round(x2, 10); |
201 |
x3 = Math.Round(x3, 10); |
202 |
x4 = Math.Round(x4, 10); |
203 |
|
204 |
y1 = Math.Round(y1, 10); |
205 |
y2 = Math.Round(y2, 10); |
206 |
y3 = Math.Round(y3, 10); |
207 |
y4 = Math.Round(y4, 10); |
208 |
|
209 |
double a1; |
210 |
double a2; |
211 |
double b1; |
212 |
double b2; |
213 |
|
214 |
if (x1 == x2) |
215 |
a1 = 0; |
216 |
else |
217 |
a1 = (y2 - y1) / (x2 - x1); |
218 |
if (x3 == x4) |
219 |
a2 = 0; |
220 |
else |
221 |
a2 = (y4 - y3) / (x4 - x3); |
222 |
|
223 |
b1 = -a1 * x1 + y1; |
224 |
b2 = -a2 * x3 + y3; |
225 |
|
226 |
if ((x1 == x2 && x3 == x4) || |
227 |
(y1 == y2 && y3 == y4)) |
228 |
{ |
229 |
return null; |
230 |
} |
231 |
else if (x1 == x2) |
232 |
{ |
233 |
return new double[] { x1, a2*x1 + b2 }; |
234 |
} |
235 |
else if (x3 == x4) |
236 |
{ |
237 |
return new double[] { x3, a1 * x3 + b1 }; |
238 |
} |
239 |
else |
240 |
{ |
241 |
return new double[] { -(b1 - b2) / (a1 - a2), a1 * (-(b1 - b2) / (a1 - a2) + b1) }; |
242 |
} |
243 |
} |
244 |
|
245 |
public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2) |
246 |
{ |
247 |
return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5); |
248 |
} |
249 |
|
250 |
public static object FindObjectByUID(Document document, string UID) |
251 |
{ |
252 |
foreach (Symbol item in document.SYMBOLS) |
253 |
{ |
254 |
if (item.UID == UID) |
255 |
return item; |
256 |
} |
257 |
|
258 |
foreach (Line item in document.LINES) |
259 |
{ |
260 |
if (item.UID == UID) |
261 |
return item; |
262 |
} |
263 |
|
264 |
foreach (Text item in document.TEXTINFOS) |
265 |
{ |
266 |
if (item.UID == UID) |
267 |
return item; |
268 |
} |
269 |
|
270 |
foreach (Note item in document.NOTES) |
271 |
{ |
272 |
if (item.UID == UID) |
273 |
return item; |
274 |
} |
275 |
|
276 |
foreach (LineNumber item in document.LINENUMBERS) |
277 |
{ |
278 |
if (item.UID == UID) |
279 |
return item; |
280 |
} |
281 |
|
282 |
foreach (Equipment item in document.Equipments) |
283 |
{ |
284 |
if (item.UID == UID) |
285 |
return item; |
286 |
} |
287 |
|
288 |
return null; |
289 |
} |
290 |
|
291 |
public static List<Line> FindLinesByModelId(Document document, string ModelItemId) |
292 |
{ |
293 |
List<Line> lines = new List<Line>(); |
294 |
foreach (Line item in document.LINES) |
295 |
{ |
296 |
if (item.SPPID.ModelItemId == ModelItemId) |
297 |
lines.Add(item); |
298 |
} |
299 |
|
300 |
return lines; |
301 |
} |
302 |
|
303 |
public static List<List<Symbol>> GetThreeConnectedSymbolGroup(Document document) |
304 |
{ |
305 |
List<List<Symbol>> tempGroups = new List<List<Symbol>>(); |
306 |
foreach (Symbol symbol in document.SYMBOLS) |
307 |
{ |
308 |
if (tempGroups.Find(x => x.Find(y => y.UID == symbol.UID) != null) != null) |
309 |
continue; |
310 |
|
311 |
List<Symbol> symbolGroup = new List<Symbol>() { symbol }; |
312 |
GetConnectedSymbol(document, symbol, symbolGroup); |
313 |
tempGroups.Add(symbolGroup); |
314 |
} |
315 |
List<List<Symbol>> symbolGroups = new List<List<Symbol>>(); |
316 |
foreach (var group in tempGroups) |
317 |
if (group.Count == 3) |
318 |
symbolGroups.Add(group); |
319 |
|
320 |
return symbolGroups; |
321 |
} |
322 |
|
323 |
private static void GetConnectedSymbol(Document document, Symbol symbol, List<Symbol> symbolGroup) |
324 |
{ |
325 |
foreach (Connector connector in symbol.CONNECTORS) |
326 |
{ |
327 |
object item = FindObjectByUID(document, connector.CONNECTEDITEM); |
328 |
if (item != null && item.GetType() == typeof(Symbol)) |
329 |
{ |
330 |
Symbol connSymbol = item as Symbol; |
331 |
if (connSymbol != null && !symbolGroup.Contains(connSymbol)) |
332 |
{ |
333 |
symbolGroup.Add(connSymbol); |
334 |
GetConnectedSymbol(document, connSymbol, symbolGroup); |
335 |
} |
336 |
} |
337 |
} |
338 |
} |
339 |
|
340 |
public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol) |
341 |
{ |
342 |
foreach (var connector in targetSymbol.CONNECTORS) |
343 |
{ |
344 |
if (connector.CONNECTEDITEM == uid) |
345 |
{ |
346 |
return connector; |
347 |
} |
348 |
} |
349 |
|
350 |
return null; |
351 |
} |
352 |
} |
353 |
} |