hytos / DTI_PID / SPPIDConverter_AutoModeling / Modeling / AutoModeling.cs @ b8ff72eb
이력 | 보기 | 이력해설 | 다운로드 (32.4 KB)
1 | bf42803b | gaqhf | using System; |
---|---|---|---|
2 | 5f01bbc4 | gaqhf | using System.Windows.Forms; |
3 | bf42803b | gaqhf | using System.Collections.Generic; |
4 | using System.Drawing; |
||
5 | using System.Linq; |
||
6 | using System.Text; |
||
7 | using System.Threading.Tasks; |
||
8 | using System.Xml.Linq; |
||
9 | using Llama; |
||
10 | using Plaice; |
||
11 | using SPPID.Model; |
||
12 | using SPPID.Utill; |
||
13 | using System.Diagnostics; |
||
14 | |||
15 | |||
16 | namespace SPPID.Modeling |
||
17 | { |
||
18 | public class AutoModeling |
||
19 | { |
||
20 | private Document document; |
||
21 | |||
22 | public AutoModeling(Document doc) |
||
23 | { |
||
24 | document = doc; |
||
25 | } |
||
26 | |||
27 | public void Run() |
||
28 | { |
||
29 | Stopwatch sw = new Stopwatch(); |
||
30 | sw.Start(); |
||
31 | CloseOPCForm.Run(); |
||
32 | |||
33 | // Group Symbol 실패시 Group Remove |
||
34 | foreach (Group group in document.GROUPS) |
||
35 | { |
||
36 | if (!GroupSymbolModeling(group)) |
||
37 | { |
||
38 | group.Enable = false; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | // Line Group은 Group 없을 때까지 Loop |
||
43 | while (document.GROUPS.Count > 0) |
||
44 | { |
||
45 | bool loopAction = false; |
||
46 | foreach (Group group in document.GROUPS) |
||
47 | { |
||
48 | if (!group.Enable) |
||
49 | continue; |
||
50 | |||
51 | if (IsPossibleDrawGroupLine(group)) |
||
52 | { |
||
53 | loopAction = true; |
||
54 | GroupLineModeling(group); |
||
55 | document.GROUPS.Remove(group); |
||
56 | break; |
||
57 | } |
||
58 | } |
||
59 | if (!loopAction) |
||
60 | break; |
||
61 | } |
||
62 | 7710b921 | gaqhf | |
63 | bf42803b | gaqhf | // Line Group Modeling 실패한 로그 |
64 | foreach (Group group in document.GROUPS) |
||
65 | { |
||
66 | foreach (SPPID_ITEM item in group.Items) |
||
67 | { |
||
68 | Line line = item as Line; |
||
69 | if (line != null && line.SPPID_ITEM_OBJECT == null) |
||
70 | { |
||
71 | f2151e28 | gaqhf | |
72 | bf42803b | gaqhf | } |
73 | } |
||
74 | } |
||
75 | |||
76 | 7710b921 | gaqhf | // SprecBreak Modeling |
77 | bf42803b | gaqhf | foreach (SpecBreak item in document.SPECBREAK) |
78 | { |
||
79 | item.Modeling(document); |
||
80 | item.SetAttribute(document); |
||
81 | foreach (var result in item.SPPID_ITEM_OBJECT_LIST) |
||
82 | { |
||
83 | LMLabelPersist label = result.Value; |
||
84 | if (label != null) |
||
85 | { |
||
86 | f2151e28 | gaqhf | |
87 | bf42803b | gaqhf | } |
88 | else |
||
89 | { |
||
90 | f2151e28 | gaqhf | |
91 | bf42803b | gaqhf | } |
92 | } |
||
93 | } |
||
94 | |||
95 | // Text Modeling |
||
96 | foreach (Text text in document.TEXTS) |
||
97 | { |
||
98 | text.Modeling(document); |
||
99 | if (text.SPPID_ITEM_OBJECT != null || text.IsAssociation) |
||
100 | f2151e28 | gaqhf | { |
101 | |||
102 | } |
||
103 | bf42803b | gaqhf | else |
104 | { |
||
105 | f2151e28 | gaqhf | |
106 | bf42803b | gaqhf | } |
107 | } |
||
108 | |||
109 | // attribute |
||
110 | foreach (Symbol item in document.SYMBOLS) |
||
111 | { |
||
112 | if (item.SPPID_ITEM_OBJECT != null) |
||
113 | { |
||
114 | item.SetAttribute(document); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | 7710b921 | gaqhf | // Line Number Attribute Modeling |
119 | foreach (LineNumber lineNumber in document.LINENUMBERS) |
||
120 | { |
||
121 | lineNumber.SetAttribute(document); |
||
122 | } |
||
123 | bf42803b | gaqhf | |
124 | CloseOPCForm.Stop(); |
||
125 | 7710b921 | gaqhf | |
126 | bf42803b | gaqhf | // |
127 | sw.Stop(); |
||
128 | TimeSpan ts = sw.Elapsed; |
||
129 | string elapsedTime = String.Format("{0:00}:{1:00}.{2:00}", |
||
130 | ts.Minutes, ts.Seconds, |
||
131 | ts.Milliseconds / 10); |
||
132 | f2151e28 | gaqhf | //Log.WriteLine(new ListBoxItem(string.Format("Time : {0}", ts), Color.Black), logListBox); |
133 | bf42803b | gaqhf | } |
134 | |||
135 | private bool IsPossibleDrawGroupLine(Group group) |
||
136 | { |
||
137 | foreach (SPPID_ITEM item in group.Items) |
||
138 | { |
||
139 | if (item.GetType() == typeof(Symbol)) |
||
140 | { |
||
141 | Symbol symbol = item as Symbol; |
||
142 | foreach (Connector connector in symbol.CONNECTORS) |
||
143 | { |
||
144 | SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM; |
||
145 | if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null) |
||
146 | return false; |
||
147 | } |
||
148 | } |
||
149 | else if (item.GetType() == typeof(Line)) |
||
150 | { |
||
151 | Line line = item as Line; |
||
152 | foreach (Connector connector in line.CONNECTORS) |
||
153 | { |
||
154 | SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM; |
||
155 | if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null) |
||
156 | return false; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return true; |
||
162 | } |
||
163 | |||
164 | #region Modeling |
||
165 | |||
166 | private bool GroupSymbolModeling(Group group) |
||
167 | { |
||
168 | List<object> Items = group.Items; |
||
169 | Symbol prevSymbol = null; |
||
170 | int prevIndex = 0; |
||
171 | bool result = true; |
||
172 | |||
173 | for (int itemIndex = 0; itemIndex < Items.Count(); itemIndex++) |
||
174 | { |
||
175 | Line.SlopeType prevSlope = Line.SlopeType.NONE; |
||
176 | Symbol symbol = Items[itemIndex] as Symbol; |
||
177 | try |
||
178 | { |
||
179 | if (symbol != null) |
||
180 | { |
||
181 | // Group중에 최근에 Modeling된 Symbol |
||
182 | if (prevSymbol != null) |
||
183 | { |
||
184 | LMSymbol prevLMSymbol = prevSymbol.SPPID_ITEM_OBJECT as LMSymbol; |
||
185 | for (int prevLineIndex = prevIndex + 1; prevLineIndex < itemIndex; prevLineIndex++) |
||
186 | { |
||
187 | Line prevLine = Items[prevLineIndex] as Line; |
||
188 | if (prevLine != null) |
||
189 | { |
||
190 | if (prevSlope == Line.SlopeType.NONE) |
||
191 | { |
||
192 | prevSlope = prevLine.SLOPTYPE; |
||
193 | } |
||
194 | else if (prevSlope != prevLine.SLOPTYPE) |
||
195 | { |
||
196 | prevSlope = Line.SlopeType.NONE; |
||
197 | break; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | if (prevSlope == Line.SlopeType.HORIZONTAL) |
||
203 | symbol.LOCATION_Y = prevLMSymbol.get_YCoordinate(); |
||
204 | else if (prevSlope == Line.SlopeType.VERTICAL) |
||
205 | symbol.LOCATION_X = prevLMSymbol.get_XCoordinate(); |
||
206 | } |
||
207 | |||
208 | symbol.Modeling(document); |
||
209 | if (symbol.SPPID_ITEM_OBJECT != null) |
||
210 | { |
||
211 | prevSymbol = symbol; |
||
212 | prevIndex = itemIndex; |
||
213 | } |
||
214 | else |
||
215 | { |
||
216 | result = false; |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | catch (Exception ex) |
||
221 | { |
||
222 | result = false; |
||
223 | return result; |
||
224 | } |
||
225 | |||
226 | |||
227 | } |
||
228 | |||
229 | return result; |
||
230 | } |
||
231 | |||
232 | private bool GroupLineModeling(Group group) |
||
233 | { |
||
234 | try |
||
235 | { |
||
236 | List<object> Items = group.Items; |
||
237 | List<Line> lineList = new List<Line>(); |
||
238 | foreach (SPPID_ITEM item in Items) |
||
239 | { |
||
240 | Line line = item as Line; |
||
241 | if (line != null) |
||
242 | lineList.Add(line); |
||
243 | else if (lineList.Count > 0) |
||
244 | { |
||
245 | GroupLineModelingByList(lineList, group); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | if (lineList.Count > 0) |
||
250 | GroupLineModelingByList(lineList, group); |
||
251 | } |
||
252 | catch (Exception ex) |
||
253 | { |
||
254 | Log.WriteLine(ex); |
||
255 | return false; |
||
256 | } |
||
257 | |||
258 | return true; |
||
259 | } |
||
260 | |||
261 | private void GroupLineModelingByList(List<Line> lines, Group group) |
||
262 | { |
||
263 | List<PointInfo> pointInfos = GetLinePointInfos(lines, group); |
||
264 | LMConnector lMConnector = DrawLines(pointInfos, lines[0].SPPIDMAPPINGNAME, lines); |
||
265 | |||
266 | if (lMConnector != null) |
||
267 | { |
||
268 | string modelId = lMConnector.ModelItemID; |
||
269 | document.PipeRunPoint.Add(lMConnector.ModelItemID, pointInfos); |
||
270 | foreach (Line line in lines) |
||
271 | { |
||
272 | line.SPPID_ITEM_OBJECT = modelId; |
||
273 | IfContainLineNumber(line.UID); |
||
274 | } |
||
275 | } |
||
276 | else |
||
277 | { |
||
278 | foreach (Line line in lines) |
||
279 | { |
||
280 | f2151e28 | gaqhf | //Log.WriteLine(new ListBoxItem(string.Format("Fail Line UID : {0}", line.UID), Color.Red), logListBox); |
281 | //Log.ProgressBarIncrement(progressBar); |
||
282 | bf42803b | gaqhf | } |
283 | } |
||
284 | |||
285 | lines.Clear(); |
||
286 | } |
||
287 | |||
288 | private void IfContainLineNumber(string lineUID) |
||
289 | { |
||
290 | foreach (LineNumber lineNumber in document.LINENUMBERS) |
||
291 | { |
||
292 | if (lineNumber.CONNECTLINE == lineUID) |
||
293 | { |
||
294 | lineNumber.Modeling(document); |
||
295 | |||
296 | if (lineNumber.SPPID_ITEM_OBJECT != null) |
||
297 | f2151e28 | gaqhf | { |
298 | |||
299 | } |
||
300 | bf42803b | gaqhf | else |
301 | f2151e28 | gaqhf | { |
302 | bf42803b | gaqhf | |
303 | f2151e28 | gaqhf | } |
304 | bf42803b | gaqhf | break; |
305 | } |
||
306 | } |
||
307 | } |
||
308 | |||
309 | private LMConnector DrawLines(List<PointInfo> pointInfos, string SPPID_MAPPINGNAME, List<Line> lines) |
||
310 | { |
||
311 | LMConnector lMConnector = null; |
||
312 | try |
||
313 | { |
||
314 | Placement _placement = new Placement(); |
||
315 | _LMAItem _LMAItem = _placement.PIDCreateItem(SPPID_MAPPINGNAME); |
||
316 | PlaceRunInputs placeRunInputs = new PlaceRunInputs(); |
||
317 | |||
318 | PointInfo branchPoint1 = null; |
||
319 | PointInfo branchPoint1_1 = null; |
||
320 | int branchIndex1 = 0; |
||
321 | List<string> branch_SP_ID_List1 = null; |
||
322 | PointInfo branchPoint2 = null; |
||
323 | PointInfo branchPoint2_1 = null; |
||
324 | int branchIndex2 = 0; |
||
325 | List<string> branch_SP_ID_List2 = null; |
||
326 | |||
327 | |||
328 | for (int pointIndex = 0; pointIndex < pointInfos.Count; pointIndex++) |
||
329 | { |
||
330 | PointInfo pointInfo = pointInfos[pointIndex]; |
||
331 | if (pointIndex == 0 || pointIndex == pointInfos.Count - 1) |
||
332 | { |
||
333 | if (pointInfo.ConnectedItemObject == null) |
||
334 | { |
||
335 | placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y); |
||
336 | } |
||
337 | else |
||
338 | { |
||
339 | if (typeof(Symbol) == pointInfo.ConnectedItemObject.GetType()) |
||
340 | { |
||
341 | Symbol symbol = pointInfo.ConnectedItemObject as Symbol; |
||
342 | //check child |
||
343 | List<string> linesUID = new List<string>(); |
||
344 | foreach (Line line in lines) |
||
345 | linesUID.Add(line.UID); |
||
346 | |||
347 | int symbolIndex = 0; |
||
348 | foreach (Connector connector in symbol.CONNECTORS) |
||
349 | { |
||
350 | if (linesUID.Contains(connector.CONNECTEDITEM)) |
||
351 | { |
||
352 | symbolIndex = connector.INDEX; |
||
353 | break; |
||
354 | } |
||
355 | } |
||
356 | LMSymbol tartgetLMSymbol = null; |
||
357 | if (symbolIndex == 0) |
||
358 | tartgetLMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol; |
||
359 | else |
||
360 | { |
||
361 | foreach (SymbolChild child in symbol.CHILD_LIST) |
||
362 | { |
||
363 | if (child.Index == symbolIndex) |
||
364 | { |
||
365 | tartgetLMSymbol = child.SPPID_ITEM_OBJECT as LMSymbol; |
||
366 | break; |
||
367 | } |
||
368 | } |
||
369 | } |
||
370 | |||
371 | placeRunInputs.AddSymbolTarget(tartgetLMSymbol, pointInfo.X, pointInfo.Y); |
||
372 | pointInfo.Type = PointInfo.PointType.Branch; |
||
373 | } |
||
374 | else if (typeof(Line) == pointInfo.ConnectedItemObject.GetType()) |
||
375 | { |
||
376 | Line line = pointInfo.ConnectedItemObject as Line; |
||
377 | Tuple<LMConnector, int> targetLMConnector = GetTargetLMConnector(pointInfo); |
||
378 | if (targetLMConnector != null) |
||
379 | { |
||
380 | if (targetLMConnector.Item1 != null) |
||
381 | { |
||
382 | if (branchPoint1 == null) |
||
383 | { |
||
384 | placeRunInputs.AddConnectorTarget(targetLMConnector.Item1, pointInfo.X, pointInfo.Y); |
||
385 | pointInfo.Type = PointInfo.PointType.Branch; |
||
386 | branchPoint1 = new PointInfo(pointInfo.X, pointInfo.Y); |
||
387 | branchPoint1.Type = PointInfo.PointType.Branch; |
||
388 | branchPoint1.ConnectedItem = pointInfo.ConnectedItem; |
||
389 | branchPoint1.ConnectedItemObject = pointInfo.ConnectedItemObject; |
||
390 | branchIndex1 = targetLMConnector.Item2; |
||
391 | branchPoint1_1 = pointInfo; |
||
392 | |||
393 | branch_SP_ID_List1 = new List<string>(); |
||
394 | LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(line.SPPID_ITEM_OBJECT.ToString()); |
||
395 | foreach (LMRepresentation rep in lMPipeRun.Representations) |
||
396 | if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active") |
||
397 | branch_SP_ID_List1.Add(rep.Id); |
||
398 | } |
||
399 | else |
||
400 | { |
||
401 | placeRunInputs.AddConnectorTarget(targetLMConnector.Item1, pointInfo.X, pointInfo.Y); |
||
402 | pointInfo.Type = PointInfo.PointType.Branch; |
||
403 | branchPoint2 = new PointInfo(pointInfo.X, pointInfo.Y); |
||
404 | branchPoint2.Type = PointInfo.PointType.Branch; |
||
405 | branchPoint2.ConnectedItem = pointInfo.ConnectedItem; |
||
406 | branchPoint2.ConnectedItemObject = pointInfo.ConnectedItemObject; |
||
407 | branchIndex2 = targetLMConnector.Item2; |
||
408 | branchPoint2_1 = pointInfo; |
||
409 | |||
410 | branch_SP_ID_List2 = new List<string>(); |
||
411 | LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(line.SPPID_ITEM_OBJECT.ToString()); |
||
412 | foreach (LMRepresentation rep in lMPipeRun.Representations) |
||
413 | if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active") |
||
414 | branch_SP_ID_List2.Add(rep.Id); |
||
415 | } |
||
416 | } |
||
417 | else |
||
418 | { |
||
419 | placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y); |
||
420 | pointInfo.Type = PointInfo.PointType.Run; |
||
421 | } |
||
422 | } |
||
423 | else |
||
424 | { |
||
425 | placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y); |
||
426 | pointInfo.Type = PointInfo.PointType.Run; |
||
427 | } |
||
428 | } |
||
429 | } |
||
430 | } |
||
431 | else |
||
432 | { |
||
433 | placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y); |
||
434 | pointInfo.Type = PointInfo.PointType.Run; |
||
435 | } |
||
436 | } |
||
437 | |||
438 | lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs); |
||
439 | lMConnector.Commit(); |
||
440 | |||
441 | // Branch Point Setting |
||
442 | if (branchPoint1 != null) |
||
443 | { |
||
444 | Line branchedLine = branchPoint1.ConnectedItemObject as Line; |
||
445 | LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(branchedLine.SPPID_ITEM_OBJECT.ToString()); |
||
446 | foreach (LMRepresentation rep in lMPipeRun.Representations) |
||
447 | if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active") |
||
448 | { |
||
449 | if (!branch_SP_ID_List1.Contains(rep.Id)) |
||
450 | { |
||
451 | branchPoint1.SP_ID = rep.Id; |
||
452 | branchPoint1_1.SP_ID = rep.Id; |
||
453 | document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].Insert(branchIndex1, branchPoint1); |
||
454 | break; |
||
455 | } |
||
456 | } |
||
457 | |||
458 | if (string.IsNullOrEmpty(branchPoint1.SP_ID)) |
||
459 | { |
||
460 | document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].RemoveAt(branchIndex1); |
||
461 | branchPoint1_1.Type = PointInfo.PointType.Run; |
||
462 | } |
||
463 | } |
||
464 | if (branchPoint2 != null) |
||
465 | { |
||
466 | Line branchedLine = branchPoint2.ConnectedItemObject as Line; |
||
467 | LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(branchedLine.SPPID_ITEM_OBJECT.ToString()); |
||
468 | foreach (LMRepresentation rep in lMPipeRun.Representations) |
||
469 | if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active") |
||
470 | { |
||
471 | if (!branch_SP_ID_List2.Contains(rep.Id)) |
||
472 | { |
||
473 | branchPoint2.SP_ID = rep.Id; |
||
474 | branchPoint2_1.SP_ID = rep.Id; |
||
475 | document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].Insert(branchIndex2, branchPoint2); |
||
476 | break; |
||
477 | } |
||
478 | } |
||
479 | |||
480 | if (string.IsNullOrEmpty(branchPoint2.SP_ID)) |
||
481 | { |
||
482 | document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].RemoveAt(branchIndex2); |
||
483 | branchPoint2_1.Type = PointInfo.PointType.Run; |
||
484 | } |
||
485 | } |
||
486 | // End |
||
487 | } |
||
488 | catch (Exception ex) |
||
489 | { |
||
490 | Log.WriteLine(ex); |
||
491 | } |
||
492 | |||
493 | return lMConnector; |
||
494 | } |
||
495 | |||
496 | private List<PointInfo> GetLinePointInfos(List<Line> lineList, Group group) |
||
497 | { |
||
498 | // Line들의 points를 구함 |
||
499 | List<PointInfo> pointInfos = new List<PointInfo>(); |
||
500 | if (lineList.Count == 1) |
||
501 | { |
||
502 | pointInfos.Add(new PointInfo(lineList[0].START_X, lineList[0].START_Y) { ConnectedItem = lineList[0].CONNECTORS[0].CONNECTEDITEM }); |
||
503 | pointInfos.Add(new PointInfo(lineList[0].END_X, lineList[0].END_Y) { ConnectedItem = lineList[0].CONNECTORS[1].CONNECTEDITEM }); |
||
504 | } |
||
505 | else |
||
506 | { |
||
507 | for (int i = 0; i < lineList.Count; i++) |
||
508 | if (lineList.Count > i + 1) |
||
509 | GetLinePoints(lineList[i], lineList[i + 1], pointInfos); |
||
510 | } |
||
511 | |||
512 | // 맨앞 뒤의 ConnectedItem 구함 |
||
513 | for (int i = 0; i < pointInfos.Count; i++) |
||
514 | { |
||
515 | PointInfo info = pointInfos[i]; |
||
516 | if (i == 0 || pointInfos.Count - 1 == i) |
||
517 | { |
||
518 | info.ConnectedItemObject = SPPIDUtill.FindObjectByUID(document, info.ConnectedItem) as SPPID_ITEM; |
||
519 | |||
520 | if (info.ConnectedItemObject == null) |
||
521 | { |
||
522 | info.Type = PointInfo.PointType.Run; |
||
523 | } |
||
524 | else |
||
525 | { |
||
526 | if (info.ConnectedItemObject.GetType() == typeof(Symbol)) |
||
527 | { |
||
528 | Symbol symbol = info.ConnectedItemObject as Symbol; |
||
529 | LMSymbol _lmSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol; |
||
530 | info.SP_ID = _lmSymbol.AsLMRepresentation().Id; |
||
531 | } |
||
532 | info.Type = PointInfo.PointType.Branch; |
||
533 | } |
||
534 | } |
||
535 | else |
||
536 | { |
||
537 | info.ConnectedItem = string.Empty; |
||
538 | info.Type = PointInfo.PointType.Run; |
||
539 | } |
||
540 | |||
541 | } |
||
542 | |||
543 | // 라인 보정 |
||
544 | CalibratePointInfos(pointInfos, lineList); |
||
545 | |||
546 | return pointInfos; |
||
547 | } |
||
548 | |||
549 | private void GetLinePoints(Line line, Line nextLine, List<PointInfo> pointInfos) |
||
550 | { |
||
551 | if (nextLine != null) |
||
552 | { |
||
553 | if (pointInfos.Count > 0) |
||
554 | pointInfos.RemoveRange(pointInfos.Count - 2, 2); |
||
555 | |||
556 | if (line.CONNECTORS[0].CONNECTEDITEM == nextLine.UID) |
||
557 | { |
||
558 | pointInfos.Add(new PointInfo(line.END_X, line.END_Y) { ConnectedItem = line.CONNECTORS[1].CONNECTEDITEM }); |
||
559 | pointInfos.Add(new PointInfo(line.START_X, line.START_Y) { ConnectedItem = line.CONNECTORS[0].CONNECTEDITEM }); |
||
560 | |||
561 | if (nextLine.CONNECTORS[0].CONNECTEDITEM == line.UID) |
||
562 | pointInfos.Add(new PointInfo(nextLine.END_X, nextLine.END_Y) { ConnectedItem = nextLine.CONNECTORS[1].CONNECTEDITEM }); |
||
563 | else if (nextLine.CONNECTORS[1].CONNECTEDITEM == line.UID) |
||
564 | pointInfos.Add(new PointInfo(nextLine.START_X, nextLine.START_Y) { ConnectedItem = nextLine.CONNECTORS[0].CONNECTEDITEM }); |
||
565 | |||
566 | } |
||
567 | else if (line.CONNECTORS[1].CONNECTEDITEM == nextLine.UID) |
||
568 | { |
||
569 | pointInfos.Add(new PointInfo(line.START_X, line.START_Y) { ConnectedItem = line.CONNECTORS[0].CONNECTEDITEM }); |
||
570 | pointInfos.Add(new PointInfo(line.END_X, line.END_Y) { ConnectedItem = line.CONNECTORS[1].CONNECTEDITEM }); |
||
571 | |||
572 | if (nextLine.CONNECTORS[0].CONNECTEDITEM == line.UID) |
||
573 | pointInfos.Add(new PointInfo(nextLine.END_X, nextLine.END_Y) { ConnectedItem = nextLine.CONNECTORS[1].CONNECTEDITEM }); |
||
574 | else if (nextLine.CONNECTORS[1].CONNECTEDITEM == line.UID) |
||
575 | pointInfos.Add(new PointInfo(nextLine.START_X, nextLine.START_Y) { ConnectedItem = nextLine.CONNECTORS[0].CONNECTEDITEM }); |
||
576 | } |
||
577 | } |
||
578 | } |
||
579 | |||
580 | private void CalibratePointInfos(List<PointInfo> pointInfos, List<Line> lines) |
||
581 | { |
||
582 | // 맨 앞이 Symbol일 경우 |
||
583 | if (pointInfos[0].ConnectedItemObject != null && typeof(Symbol) == pointInfos[0].ConnectedItemObject.GetType()) |
||
584 | { |
||
585 | PointInfo startPoint = pointInfos[0]; |
||
586 | PointInfo nextPoint = pointInfos[1]; |
||
587 | |||
588 | Symbol symbol = startPoint.ConnectedItemObject as Symbol; |
||
589 | LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol; |
||
590 | double symbolX = _LMSymbol.get_XCoordinate(); |
||
591 | double symbolY = _LMSymbol.get_YCoordinate(); |
||
592 | |||
593 | if (lines[0].SLOPTYPE == Line.SlopeType.HORIZONTAL) |
||
594 | { |
||
595 | startPoint.Y = symbolY; |
||
596 | nextPoint.Y = symbolY; |
||
597 | } |
||
598 | else if (lines[0].SLOPTYPE == Line.SlopeType.VERTICAL) |
||
599 | { |
||
600 | startPoint.X = symbolX; |
||
601 | nextPoint.X = symbolX; |
||
602 | } |
||
603 | } |
||
604 | // 맨 뒤가 Symbol일 경우 |
||
605 | else if (pointInfos[pointInfos.Count - 1].ConnectedItemObject != null && typeof(Symbol) == pointInfos[pointInfos.Count - 1].ConnectedItemObject.GetType()) |
||
606 | { |
||
607 | PointInfo startPoint = pointInfos[pointInfos.Count - 1]; |
||
608 | PointInfo nextPoint = pointInfos[pointInfos.Count - 2]; |
||
609 | |||
610 | Symbol symbol = startPoint.ConnectedItemObject as Symbol; |
||
611 | LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol; |
||
612 | double symbolX = _LMSymbol.get_XCoordinate(); |
||
613 | double symbolY = _LMSymbol.get_YCoordinate(); |
||
614 | |||
615 | if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.HORIZONTAL) |
||
616 | { |
||
617 | startPoint.Y = symbolY; |
||
618 | nextPoint.Y = symbolY; |
||
619 | } |
||
620 | else if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.VERTICAL) |
||
621 | { |
||
622 | startPoint.X = symbolX; |
||
623 | nextPoint.X = symbolX; |
||
624 | } |
||
625 | } |
||
626 | } |
||
627 | |||
628 | private Tuple<LMConnector, int> GetTargetLMConnector(PointInfo pointInfo) |
||
629 | { |
||
630 | LMConnector lMConnector = null; |
||
631 | int insertIndex = 0; |
||
632 | try |
||
633 | { |
||
634 | Line line = pointInfo.ConnectedItemObject as Line; |
||
635 | if (line.SPPID_ITEM_OBJECT == null) |
||
636 | return null; |
||
637 | |||
638 | string modelId = line.SPPID_ITEM_OBJECT.ToString(); |
||
639 | List<PointInfo> pointInfos = document.PipeRunPoint[modelId]; |
||
640 | double distance = double.MaxValue; |
||
641 | |||
642 | PointInfo point1 = new PointInfo(0, 0); |
||
643 | PointInfo point2 = new PointInfo(0, 0); |
||
644 | |||
645 | for (int i = 0; i < pointInfos.Count - 1; i++) |
||
646 | { |
||
647 | PointInfo tempPoint1 = pointInfos[i]; |
||
648 | PointInfo tempPoint2 = pointInfos[i + 1]; |
||
649 | |||
650 | double tempDistance = double.MaxValue; |
||
651 | |||
652 | Line.SlopeType slopeType = SPPIDUtill.CalcSlop(tempPoint1, tempPoint2); |
||
653 | if (slopeType == Line.SlopeType.HORIZONTAL) |
||
654 | { |
||
655 | double min = Math.Min(tempPoint1.X, tempPoint2.X); |
||
656 | double max = Math.Max(tempPoint1.X, tempPoint2.X); |
||
657 | |||
658 | if (min <= pointInfo.X && max >= pointInfo.X) |
||
659 | tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo); |
||
660 | } |
||
661 | else if (slopeType == Line.SlopeType.VERTICAL) |
||
662 | { |
||
663 | double min = Math.Min(tempPoint1.Y, tempPoint2.Y); |
||
664 | double max = Math.Max(tempPoint1.Y, tempPoint2.Y); |
||
665 | |||
666 | if (min <= pointInfo.Y && max >= pointInfo.Y) |
||
667 | tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo); |
||
668 | } |
||
669 | else |
||
670 | { |
||
671 | tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo); |
||
672 | } |
||
673 | |||
674 | if (tempDistance < distance) |
||
675 | { |
||
676 | insertIndex = i + 1; |
||
677 | distance = tempDistance; |
||
678 | point1 = tempPoint1; |
||
679 | point2 = tempPoint2; |
||
680 | } |
||
681 | } |
||
682 | |||
683 | if (point1.Type == PointInfo.PointType.Run) |
||
684 | lMConnector = GetLMConnectorByPoint(modelId, point1); |
||
685 | else if (point2.Type == PointInfo.PointType.Run) |
||
686 | lMConnector = GetLMConnectorByPoint(modelId, point2); |
||
687 | else |
||
688 | lMConnector = GetLMConnectorBySP_ID(modelId, point1.SP_ID, point2.SP_ID); |
||
689 | } |
||
690 | catch (Exception ex) |
||
691 | { |
||
692 | Log.WriteLine(ex); |
||
693 | return null; |
||
694 | } |
||
695 | |||
696 | if (lMConnector == null) |
||
697 | return null; |
||
698 | else |
||
699 | return new Tuple<LMConnector, int>(lMConnector, insertIndex); |
||
700 | } |
||
701 | |||
702 | private LMConnector GetLMConnectorByPoint(string modelId, PointInfo pointInfo) |
||
703 | { |
||
704 | LMConnector lMConnector = null; |
||
705 | |||
706 | try |
||
707 | { |
||
708 | Placement _placement = new Placement(); |
||
709 | double distance = double.MaxValue; |
||
710 | LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(modelId); |
||
711 | if (lMPipeRun != null) |
||
712 | { |
||
713 | foreach (LMRepresentation rep in lMPipeRun.Representations) |
||
714 | { |
||
715 | if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active") |
||
716 | { |
||
717 | LMConnector _tempLMConnector = _placement.PIDDataSource.GetConnector(rep.Id); |
||
718 | foreach (LMConnectorVertex vertex in _tempLMConnector.ConnectorVertices) |
||
719 | { |
||
720 | double tempDistance = SPPIDUtill.CalcPointToPointdDistance(pointInfo, new PointInfo(vertex.get_XCoordinate(), vertex.get_YCoordinate())); |
||
721 | |||
722 | if (tempDistance < distance) |
||
723 | { |
||
724 | distance = tempDistance; |
||
725 | lMConnector = _tempLMConnector; |
||
726 | } |
||
727 | } |
||
728 | |||
729 | } |
||
730 | } |
||
731 | } |
||
732 | } |
||
733 | catch (Exception ex) |
||
734 | { |
||
735 | Log.WriteLine(ex); |
||
736 | } |
||
737 | |||
738 | |||
739 | return lMConnector; |
||
740 | } |
||
741 | |||
742 | private LMConnector GetLMConnectorBySP_ID(string modelId, string SP_ID1, string SP_ID2) |
||
743 | { |
||
744 | Placement _placement = new Placement(); |
||
745 | |||
746 | LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(modelId); |
||
747 | foreach (LMRepresentation rep in lMPipeRun.Representations) |
||
748 | { |
||
749 | if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active") |
||
750 | { |
||
751 | LMConnector _tempLMConnector = _placement.PIDDataSource.GetConnector(rep.Id); |
||
752 | |||
753 | bool find1 = false; |
||
754 | bool find2 = false; |
||
755 | |||
756 | if (_tempLMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_tempLMConnector.ConnectItem1SymbolID)) |
||
757 | { |
||
758 | string connId = _tempLMConnector.ConnectItem1SymbolID; |
||
759 | if (connId == SP_ID1 || connId == SP_ID2) |
||
760 | find1 = true; |
||
761 | } |
||
762 | |||
763 | if (_tempLMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_tempLMConnector.ConnectItem2SymbolID)) |
||
764 | { |
||
765 | string connId = _tempLMConnector.ConnectItem2SymbolID; |
||
766 | if (connId == SP_ID1 || connId == SP_ID2) |
||
767 | find2 = true; |
||
768 | } |
||
769 | |||
770 | |||
771 | if (find1 && find2) |
||
772 | { |
||
773 | return _tempLMConnector; |
||
774 | } |
||
775 | } |
||
776 | } |
||
777 | return null; |
||
778 | } |
||
779 | |||
780 | #endregion |
||
781 | |||
782 | } |
||
783 | } |