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
|
_ETCSetting.FlowMarkSymbolPath = jsonETCSetting.FlowMarkSymbolPath;
|
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
|
public static void ConvertGridPointOnlyOnePoint(ref double value)
|
158
|
{
|
159
|
GridSetting _GridSetting = GridSetting.GetInstance();
|
160
|
if (_GridSetting.UseSnapGrid)
|
161
|
{
|
162
|
if (_GridSetting.Unit == GridUnit.Inch)
|
163
|
{
|
164
|
double length = _GridSetting.Density * 0.0254;
|
165
|
double temp = value;
|
166
|
value = Math.Round(value / length) * length;
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
public static SlopeType CalcSlope(double x1, double y1, double x2, double y2)
|
172
|
{
|
173
|
if (x1 - x2 == 0)
|
174
|
{
|
175
|
return SlopeType.VERTICAL;
|
176
|
}
|
177
|
else
|
178
|
{
|
179
|
double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
|
180
|
if (angle <= 10)
|
181
|
return SlopeType.HORIZONTAL;
|
182
|
else if (angle >= 80)
|
183
|
return SlopeType.VERTICAL;
|
184
|
else
|
185
|
return SlopeType.Slope;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
|
190
|
{
|
191
|
|
192
|
double distance = 0;
|
193
|
if (lineX1 == lineX2)
|
194
|
distance = Math.Abs(x - lineX1);
|
195
|
else
|
196
|
{
|
197
|
double a;
|
198
|
double b;
|
199
|
double c;
|
200
|
|
201
|
a = (lineY2 - lineY1) / (lineX2 - lineX1);
|
202
|
b = -1;
|
203
|
c = -a * lineX1 + lineY1;
|
204
|
|
205
|
distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
|
206
|
}
|
207
|
return distance;
|
208
|
}
|
209
|
|
210
|
public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
|
211
|
{
|
212
|
x1 = Math.Round(x1, 10);
|
213
|
x2 = Math.Round(x2, 10);
|
214
|
x3 = Math.Round(x3, 10);
|
215
|
x4 = Math.Round(x4, 10);
|
216
|
|
217
|
y1 = Math.Round(y1, 10);
|
218
|
y2 = Math.Round(y2, 10);
|
219
|
y3 = Math.Round(y3, 10);
|
220
|
y4 = Math.Round(y4, 10);
|
221
|
|
222
|
double a1;
|
223
|
double a2;
|
224
|
double b1;
|
225
|
double b2;
|
226
|
|
227
|
if (x1 == x2)
|
228
|
a1 = 0;
|
229
|
else
|
230
|
a1 = (y2 - y1) / (x2 - x1);
|
231
|
if (x3 == x4)
|
232
|
a2 = 0;
|
233
|
else
|
234
|
a2 = (y4 - y3) / (x4 - x3);
|
235
|
|
236
|
b1 = -a1 * x1 + y1;
|
237
|
b2 = -a2 * x3 + y3;
|
238
|
|
239
|
if ((x1 == x2 && x3 == x4) ||
|
240
|
(y1 == y2 && y3 == y4))
|
241
|
{
|
242
|
return null;
|
243
|
}
|
244
|
else if (x1 == x2)
|
245
|
{
|
246
|
return new double[] { x1, a2*x1 + b2 };
|
247
|
}
|
248
|
else if (x3 == x4)
|
249
|
{
|
250
|
return new double[] { x3, a1 * x3 + b1 };
|
251
|
}
|
252
|
else
|
253
|
{
|
254
|
return new double[] { -(b1 - b2) / (a1 - a2), a1 * (-(b1 - b2) / (a1 - a2) + b1) };
|
255
|
}
|
256
|
}
|
257
|
|
258
|
public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
|
259
|
{
|
260
|
return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
|
261
|
}
|
262
|
|
263
|
public static object FindObjectByUID(Document document, string UID)
|
264
|
{
|
265
|
foreach (Symbol item in document.SYMBOLS)
|
266
|
{
|
267
|
if (item.UID == UID)
|
268
|
return item;
|
269
|
}
|
270
|
|
271
|
foreach (Line item in document.LINES)
|
272
|
{
|
273
|
if (item.UID == UID)
|
274
|
return item;
|
275
|
}
|
276
|
|
277
|
foreach (Text item in document.TEXTINFOS)
|
278
|
{
|
279
|
if (item.UID == UID)
|
280
|
return item;
|
281
|
}
|
282
|
|
283
|
foreach (Note item in document.NOTES)
|
284
|
{
|
285
|
if (item.UID == UID)
|
286
|
return item;
|
287
|
}
|
288
|
|
289
|
foreach (LineNumber item in document.LINENUMBERS)
|
290
|
{
|
291
|
if (item.UID == UID)
|
292
|
return item;
|
293
|
}
|
294
|
|
295
|
foreach (Equipment item in document.Equipments)
|
296
|
{
|
297
|
if (item.UID == UID)
|
298
|
return item;
|
299
|
}
|
300
|
|
301
|
return null;
|
302
|
}
|
303
|
|
304
|
public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
|
305
|
{
|
306
|
List<Line> lines = new List<Line>();
|
307
|
foreach (Line item in document.LINES)
|
308
|
{
|
309
|
if (item.SPPID.ModelItemId == ModelItemId)
|
310
|
lines.Add(item);
|
311
|
}
|
312
|
|
313
|
return lines;
|
314
|
}
|
315
|
|
316
|
public static List<List<Symbol>> GetThreeConnectedSymbolGroup(Document document)
|
317
|
{
|
318
|
List<List<Symbol>> tempGroups = new List<List<Symbol>>();
|
319
|
foreach (Symbol symbol in document.SYMBOLS)
|
320
|
{
|
321
|
if (tempGroups.Find(x => x.Find(y => y.UID == symbol.UID) != null) != null)
|
322
|
continue;
|
323
|
|
324
|
List<Symbol> symbolGroup = new List<Symbol>() { symbol };
|
325
|
GetConnectedSymbol(document, symbol, symbolGroup);
|
326
|
tempGroups.Add(symbolGroup);
|
327
|
}
|
328
|
List<List<Symbol>> symbolGroups = new List<List<Symbol>>();
|
329
|
foreach (var group in tempGroups)
|
330
|
if (group.Count == 3)
|
331
|
symbolGroups.Add(group);
|
332
|
|
333
|
return symbolGroups;
|
334
|
}
|
335
|
|
336
|
private static void GetConnectedSymbol(Document document, Symbol symbol, List<Symbol> symbolGroup)
|
337
|
{
|
338
|
foreach (Connector connector in symbol.CONNECTORS)
|
339
|
{
|
340
|
object item = FindObjectByUID(document, connector.CONNECTEDITEM);
|
341
|
if (item != null && item.GetType() == typeof(Symbol))
|
342
|
{
|
343
|
Symbol connSymbol = item as Symbol;
|
344
|
if (connSymbol != null && !symbolGroup.Contains(connSymbol))
|
345
|
{
|
346
|
symbolGroup.Add(connSymbol);
|
347
|
GetConnectedSymbol(document, connSymbol, symbolGroup);
|
348
|
}
|
349
|
}
|
350
|
}
|
351
|
}
|
352
|
|
353
|
public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol)
|
354
|
{
|
355
|
foreach (var connector in targetSymbol.CONNECTORS)
|
356
|
{
|
357
|
if (connector.CONNECTEDITEM == uid)
|
358
|
{
|
359
|
return connector;
|
360
|
}
|
361
|
}
|
362
|
|
363
|
return null;
|
364
|
}
|
365
|
}
|
366
|
}
|