hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 87f22597
이력 | 보기 | 이력해설 | 다운로드 (41.1 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Collections; |
4 |
using System.Configuration; |
5 |
using System.Data; |
6 |
using System.Data.Common; |
7 |
using System.Data.SqlClient; |
8 |
using System.IO; |
9 |
using System.Runtime.CompilerServices; |
10 |
using Microsoft.VisualBasic.CompilerServices; |
11 |
using System.Runtime.InteropServices; |
12 |
using System.Threading; |
13 |
|
14 |
using Autodesk.AutoCAD.ApplicationServices; |
15 |
using Autodesk.AutoCAD.ApplicationServices.Core; |
16 |
using Autodesk.AutoCAD.DatabaseServices; |
17 |
using Autodesk.AutoCAD.EditorInput; |
18 |
using Autodesk.AutoCAD.Geometry; |
19 |
using Autodesk.AutoCAD.Interop; |
20 |
using Autodesk.AutoCAD.Interop.Common; |
21 |
using Autodesk.AutoCAD.Runtime; |
22 |
using Autodesk.AutoCAD.Windows; |
23 |
|
24 |
using AVEVA.PID.Components; |
25 |
using AVEVA.PID.GUI; |
26 |
using AVEVA.PID.Common; |
27 |
using AVEVA.PID.DWGSelector; |
28 |
using AVEVA.PID.Utilities; |
29 |
|
30 |
using AVEVA.PID.CustomizationUtility.DB; |
31 |
using AVEVA.PID.CustomizationUtility.Model; |
32 |
using AVEVA.PID.CustomizationUtility.Properties; |
33 |
|
34 |
using DevExpress.XtraSplashScreen; |
35 |
|
36 |
namespace AVEVA.PID.CustomizationUtility |
37 |
{ |
38 |
public class AutoModeling |
39 |
{ |
40 |
[DllImport("user32")] |
41 |
public static extern int FindWindow(string lpClassName, string lpWindowName); |
42 |
|
43 |
public static AutoModeling Running { get; set; } |
44 |
Model.Document document; |
45 |
AvevaInfo avevaInfo; |
46 |
|
47 |
private void SetConvertRule() |
48 |
{ |
49 |
#region OPC Setting |
50 |
document.OPCs.Sort(SortOPC); |
51 |
int SortOPC(OPC a, OPC b) |
52 |
{ |
53 |
if (a.FlowType == FlowType.In) |
54 |
return 1; |
55 |
else |
56 |
return 0; |
57 |
} |
58 |
#endregion |
59 |
} |
60 |
|
61 |
public AutoModeling(Model.Document document) |
62 |
{ |
63 |
this.document = document; |
64 |
avevaInfo = AvevaInfo.GetInstance(); |
65 |
} |
66 |
|
67 |
public void CreateDrawing() |
68 |
{ |
69 |
string outPath = Project_DB.GetDirectiveValue("DRGPTH"); |
70 |
string tempPath = Path.GetTempPath(); |
71 |
string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT"); |
72 |
|
73 |
DWTSelector.strDrawingNumber = document.AvevaDrawingNumber; |
74 |
DWTSelector.strSheetNumber = document.AvevaSheetNumber; |
75 |
DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber; |
76 |
DWTSelector.strCadFilePath = outPath; |
77 |
|
78 |
try |
79 |
{ |
80 |
string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false); |
81 |
Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication; |
82 |
Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text); |
83 |
|
84 |
Commands.addLatestPidvesrionToDictionary(); |
85 |
AVVPropCmd.ActivateAcad(); |
86 |
|
87 |
if (acadDocument != null) |
88 |
{ |
89 |
Running = this; |
90 |
acadApplication.ActiveDocument = acadDocument; |
91 |
acadDocument.SendCommand("QSAVE "); |
92 |
acadDocument.SendCommand("APPIDRun "); |
93 |
acadDocument.SendCommand("QSAVE "); |
94 |
Running = null; |
95 |
} |
96 |
} |
97 |
catch (System.Exception ex) |
98 |
{ |
99 |
|
100 |
} |
101 |
|
102 |
GC.Collect(); |
103 |
} |
104 |
public void Run() |
105 |
{ |
106 |
try |
107 |
{ |
108 |
SplashScreenManager.ShowForm(typeof(APIDSplashScreen), true, true); |
109 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllStepCount, 8); |
110 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetDocumentName, document.AvevaDrawingNumber + document.AvevaSheetNumber); |
111 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetParent, Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle); |
112 |
|
113 |
SetConvertRule(); |
114 |
|
115 |
RunLineModeling(); |
116 |
RunOPCModeling(); |
117 |
RunSymbolModeling(); |
118 |
RunTextModeling(); |
119 |
RunNoteModeling(); |
120 |
RunInputLineAttribute(); |
121 |
RunInputLineNumberAttribute(); |
122 |
RunInputSymbolAttribute(); |
123 |
} |
124 |
catch (System.Exception ex) |
125 |
{ |
126 |
System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace); |
127 |
} |
128 |
finally |
129 |
{ |
130 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.ClearParent, null); |
131 |
SplashScreenManager.CloseForm(false); |
132 |
} |
133 |
} |
134 |
|
135 |
#region Run Method |
136 |
private void RunLineModeling() |
137 |
{ |
138 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count); |
139 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling"); |
140 |
foreach (var item in document.LINES) |
141 |
{ |
142 |
if (item.Aveva.Handle == 0) |
143 |
LineModeling(item); |
144 |
|
145 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
146 |
} |
147 |
} |
148 |
private void RunOPCModeling() |
149 |
{ |
150 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count); |
151 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling"); |
152 |
foreach (var item in document.OPCs) |
153 |
{ |
154 |
if (item.Aveva.Handle == 0) |
155 |
SymbolModeling(item); |
156 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
157 |
} |
158 |
} |
159 |
private void RunSymbolModeling() |
160 |
{ |
161 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count); |
162 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling"); |
163 |
foreach (var item in document.SYMBOLS) |
164 |
{ |
165 |
if (item.Aveva.Handle == 0) |
166 |
SymbolModeling(item); |
167 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
168 |
} |
169 |
} |
170 |
private void RunTextModeling() |
171 |
{ |
172 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Texts.Count); |
173 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Text Modeling"); |
174 |
foreach (var item in document.Texts) |
175 |
{ |
176 |
TextModeling(item); |
177 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
178 |
} |
179 |
} |
180 |
private void RunNoteModeling() |
181 |
{ |
182 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Notes.Count); |
183 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Note Modeling"); |
184 |
foreach (var item in document.Notes) |
185 |
{ |
186 |
NoteModeling(item); |
187 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
188 |
} |
189 |
} |
190 |
private void RunInputLineAttribute() |
191 |
{ |
192 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count); |
193 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Input Line Attribute"); |
194 |
foreach (var item in document.LINES) |
195 |
{ |
196 |
SetLineAttribute(item); |
197 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
198 |
} |
199 |
} |
200 |
private void RunInputSymbolAttribute() |
201 |
{ |
202 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count); |
203 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Input Symbol Attribute"); |
204 |
foreach (var item in document.SYMBOLS) |
205 |
{ |
206 |
SetSymbolAttribute(item); |
207 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
208 |
} |
209 |
} |
210 |
private void RunInputLineNumberAttribute() |
211 |
{ |
212 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count); |
213 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Input LineNumber Attribute"); |
214 |
foreach (var item in document.LINENUMBERS) |
215 |
{ |
216 |
SetLineNumberAttribute(item); |
217 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
218 |
} |
219 |
} |
220 |
#endregion |
221 |
|
222 |
#region Modeling Method |
223 |
private void LineModeling(Model.Line line) |
224 |
{ |
225 |
List<Model.Line> groupLine = new List<Model.Line>(); |
226 |
List<string> points = new List<string>(); |
227 |
GetConnectedGroupLine(line, groupLine, false); |
228 |
GetGroupLinePoints(groupLine, points); |
229 |
|
230 |
LineNumber lineNumber = null; |
231 |
if (line.Aveva.Type == Model.Type.Pipe) |
232 |
{ |
233 |
foreach (var item in groupLine) |
234 |
{ |
235 |
lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID); |
236 |
if (lineNumber != null) |
237 |
{ |
238 |
GUIUtils.SetAutoLabelCheck(true); |
239 |
AvevaThread.Run(ThreadType.LineNumberModeling, null); |
240 |
break; |
241 |
} |
242 |
} |
243 |
} |
244 |
|
245 |
long handle = 0; |
246 |
if (line.Aveva.Type == Model.Type.Pipe) |
247 |
handle = DrawPipe(line.Aveva.Name, points, lineNumber); |
248 |
else if (line.Aveva.Type == Model.Type.Signal) |
249 |
handle = DrawSignal(line.Aveva.Name, points); |
250 |
|
251 |
if (lineNumber != null) |
252 |
{ |
253 |
GUIUtils.SetAutoLabelCheck(false); |
254 |
AvevaThread.Stop(ThreadType.LineNumberModeling); |
255 |
} |
256 |
|
257 |
foreach (var item in groupLine) |
258 |
item.Aveva.Handle = handle; |
259 |
} |
260 |
private void SymbolModeling(Symbol symbol) |
261 |
{ |
262 |
DataRow[] allRows = avevaInfo.AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name)); |
263 |
if (allRows.Length == 1) |
264 |
{ |
265 |
DataRow symbolRow = allRows[0]; |
266 |
bool bAngle = CheckAngle(symbolRow); |
267 |
bool bBalloon = CheckBalloon(symbol); |
268 |
|
269 |
long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon); |
270 |
if (handle != 0) |
271 |
{ |
272 |
symbol.Aveva.Handle = handle; |
273 |
|
274 |
if (!bAngle) |
275 |
{ |
276 |
ObjectId objectId = GetObjectIdByHandle(handle); |
277 |
if (objectId != ObjectId.Null) |
278 |
{ |
279 |
SetBlockReferenceRotation(objectId, symbol.ANGLE); |
280 |
} |
281 |
} |
282 |
} |
283 |
} |
284 |
} |
285 |
private void TextModeling(Text text) |
286 |
{ |
287 |
if (text.TextAngle == TextAngle.None) |
288 |
return; |
289 |
|
290 |
if (text.Aveva.LabelType == LabelType.SingleText) |
291 |
DrawText(text.Aveva.X, text.Aveva.Y, text.Aveva.Height, text.VALUE, text.Aveva.Angle); |
292 |
else if (text.Aveva.LabelType == LabelType.MultiText) |
293 |
{ |
294 |
string[] valueArray = text.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None); |
295 |
List<string> values = new List<string>(); |
296 |
for (int i = 0; i < valueArray.Length; i++) |
297 |
values.Insert(0, valueArray[i]); |
298 |
|
299 |
for (int i = 0; i < values.Count; i++) |
300 |
{ |
301 |
double x = text.Aveva.X; |
302 |
double y = text.Aveva.Y; |
303 |
int heightIndex = i; |
304 |
|
305 |
if (text.TextAngle == TextAngle.Degree0 || text.TextAngle == TextAngle.Degree180) |
306 |
y = y + text.Aveva.Height * i; |
307 |
else if (text.TextAngle == TextAngle.Degree90 || text.TextAngle == TextAngle.Degree270) |
308 |
x = x + text.Aveva.Height * i; |
309 |
|
310 |
DrawText(x, y, text.Aveva.Height, values[i], text.Aveva.Angle); |
311 |
} |
312 |
} |
313 |
|
314 |
|
315 |
} |
316 |
private void NoteModeling(Model.Note note) |
317 |
{ |
318 |
if (note.TextAngle == TextAngle.None) |
319 |
return; |
320 |
|
321 |
if (note.Aveva.LabelType == LabelType.SingleNote) |
322 |
DrawText(note.Aveva.X, note.Aveva.Y, note.Aveva.Height, note.VALUE, note.Aveva.Angle); |
323 |
else if (note.Aveva.LabelType == LabelType.MultiNote) |
324 |
{ |
325 |
string[] valueArray = note.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None); |
326 |
List<string> values = new List<string>(); |
327 |
for (int i = 0; i < valueArray.Length; i++) |
328 |
values.Insert(0, valueArray[i]); |
329 |
|
330 |
for (int i = 0; i < values.Count; i++) |
331 |
{ |
332 |
double x = note.Aveva.X; |
333 |
double y = note.Aveva.Y; |
334 |
int heightIndex = i; |
335 |
|
336 |
if (note.TextAngle == TextAngle.Degree0 || note.TextAngle == TextAngle.Degree180) |
337 |
y = y + note.Aveva.Height * i; |
338 |
else if (note.TextAngle == TextAngle.Degree90 || note.TextAngle == TextAngle.Degree270) |
339 |
x = x + note.Aveva.Height * i; |
340 |
|
341 |
DrawText(x, y, note.Aveva.Height, values[i], note.Aveva.Angle); |
342 |
} |
343 |
} |
344 |
} |
345 |
private void SetLineAttribute(Model.Line line) |
346 |
{ |
347 |
|
348 |
} |
349 |
private void SetLineNumberAttribute(Model.LineNumber lineNumber) |
350 |
{ |
351 |
Model.Line line = APIDUtils.FindObjectByUID(document, lineNumber.CONNLINE) as Model.Line; |
352 |
if (line != null && line.Aveva.Handle != 0) |
353 |
{ |
354 |
List<Tuple<string, string>> datas = new List<Tuple<string, string>>(); |
355 |
foreach (Model.Attribute attribute in lineNumber.ATTRIBUTES) |
356 |
{ |
357 |
AttributeInfo info = avevaInfo.AttributeInfo.Find(x => x.UID == attribute.UID); |
358 |
if (info != null && !string.IsNullOrEmpty(info.APID_ATTRIBUTE)) |
359 |
{ |
360 |
if (info.APID_ATTRIBUTE_TYPE == AvevaAttributeType.UDA) |
361 |
datas.Add(new Tuple<string, string>("UDA" + info.APID_ATTRIBUTE, attribute.VALUE)); |
362 |
else |
363 |
datas.Add(new Tuple<string, string>(info.APID_ATTRIBUTE, attribute.VALUE)); |
364 |
} |
365 |
} |
366 |
if (datas.Count > 0) |
367 |
{ |
368 |
datas.Add(new Tuple<string, string>("AddLabel", "No")); |
369 |
ObjectId objectId = GetFirstPolyLineObjectId(line.Aveva.Handle); |
370 |
if (!objectId.IsNull) |
371 |
SetAttributeByAvevaForm(objectId.Handle.Value, datas); |
372 |
} |
373 |
} |
374 |
} |
375 |
private void SetSymbolAttribute(Model.Symbol symbol) |
376 |
{ |
377 |
if (symbol.Aveva.Handle != 0) |
378 |
{ |
379 |
List<Tuple<string, string>> datas = new List<Tuple<string, string>>(); |
380 |
foreach (var attribute in symbol.ATTRIBUTES) |
381 |
{ |
382 |
AttributeInfo info = avevaInfo.AttributeInfo.Find(x => x.UID == attribute.UID); |
383 |
if (info != null && !string.IsNullOrEmpty(info.APID_ATTRIBUTE)) |
384 |
{ |
385 |
if (info.APID_ATTRIBUTE_TYPE == AvevaAttributeType.UDA) |
386 |
datas.Add(new Tuple<string, string>("UDA" + info.APID_ATTRIBUTE, attribute.VALUE)); |
387 |
else |
388 |
datas.Add(new Tuple<string, string>(info.APID_ATTRIBUTE, attribute.VALUE)); |
389 |
} |
390 |
} |
391 |
if (datas.Count > 0) |
392 |
{ |
393 |
ObjectId objectId = GetObjectIdByHandle(symbol.Aveva.Handle); |
394 |
if (!objectId.IsNull) |
395 |
SetAttributeByAvevaForm(objectId.Handle.Value, datas); |
396 |
} |
397 |
} |
398 |
} |
399 |
#endregion |
400 |
|
401 |
#region Drawing Method |
402 |
private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null) |
403 |
{ |
404 |
List<long> prevHandles = GetAllGroupHandles(); |
405 |
|
406 |
GUIUtils.SetPipeStyle(style); |
407 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
408 |
Editor editor = acDoc.Editor; |
409 |
List<object> commandParam = new List<object>(); |
410 |
//command name |
411 |
commandParam.Add("DRPIPE"); |
412 |
//coordinate |
413 |
foreach (var item in coordinates) |
414 |
commandParam.Add(item); |
415 |
|
416 |
//enter Parent(null) |
417 |
commandParam.Add(null); |
418 |
|
419 |
if (lineNumber != null) |
420 |
{ |
421 |
Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0); |
422 |
commandParam.Add(point); |
423 |
commandParam.Add(lineNumber.Aveva.Angle); |
424 |
|
425 |
commandParam.Add(null); |
426 |
} |
427 |
else |
428 |
commandParam.Add(null); |
429 |
|
430 |
editor.Command(commandParam.ToArray()); |
431 |
|
432 |
List<long> newHandles = GetAllGroupHandles(); |
433 |
List<long> otherHandles = new List<long>(); |
434 |
foreach (var item in newHandles) |
435 |
if (!prevHandles.Contains(item)) |
436 |
otherHandles.Add(item); |
437 |
|
438 |
if (otherHandles.Count == 1) |
439 |
return otherHandles[0]; |
440 |
else |
441 |
return 0; |
442 |
} |
443 |
private long DrawPipe(string style, List<string> coordinates, ObjectId objectId) |
444 |
{ |
445 |
List<long> prevHandles = GetAllGroupHandles(); |
446 |
GUIUtils.SetPipeStyle(style); |
447 |
|
448 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
449 |
Editor editor = acDoc.Editor; |
450 |
List<object> commandParam = new List<object>(); |
451 |
//command name |
452 |
commandParam.Add("DRPIPE"); |
453 |
//coordinate |
454 |
foreach (var item in coordinates) |
455 |
commandParam.Add(item); |
456 |
//enter |
457 |
commandParam.Add(null); |
458 |
|
459 |
//Input Object ID |
460 |
commandParam.Add(objectId); |
461 |
|
462 |
//enter |
463 |
commandParam.Add(null); |
464 |
|
465 |
editor.Command(commandParam.ToArray()); |
466 |
|
467 |
List<long> newHandles = GetAllGroupHandles(); |
468 |
List<long> otherHandles = new List<long>(); |
469 |
foreach (var item in newHandles) |
470 |
if (!prevHandles.Contains(item)) |
471 |
otherHandles.Add(item); |
472 |
|
473 |
if (otherHandles.Count == 1) |
474 |
return otherHandles[0]; |
475 |
else |
476 |
return 0; |
477 |
} |
478 |
private long DrawSignal(string style, List<string> coordinates) |
479 |
{ |
480 |
List<long> prevHandles = GetAllGroupHandles(); |
481 |
|
482 |
GUIUtils.SetPipeStyle(style); |
483 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
484 |
Editor editor = acDoc.Editor; |
485 |
List<object> commandParam = new List<object>(); |
486 |
//command name |
487 |
commandParam.Add("VPESIGNAL"); |
488 |
//coordinate |
489 |
foreach (var item in coordinates) |
490 |
commandParam.Add(item); |
491 |
//enter |
492 |
commandParam.Add(null); |
493 |
|
494 |
editor.Command(commandParam.ToArray()); |
495 |
|
496 |
List<long> newHandles = GetAllGroupHandles(); |
497 |
List<long> otherHandles = new List<long>(); |
498 |
foreach (var item in newHandles) |
499 |
if (!prevHandles.Contains(item)) |
500 |
otherHandles.Add(item); |
501 |
|
502 |
if (otherHandles.Count == 1) |
503 |
return otherHandles[0]; |
504 |
else |
505 |
return 0; |
506 |
} |
507 |
private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon) |
508 |
{ |
509 |
long handle = 0; |
510 |
try |
511 |
{ |
512 |
string insertSymbolName = symbol.Aveva.Name; |
513 |
double x = symbol.Aveva.X; |
514 |
double y = symbol.Aveva.Y; |
515 |
|
516 |
List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName); |
517 |
|
518 |
AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData(); |
519 |
drawingData.InsertSymbolName = insertSymbolName; |
520 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
521 |
Editor editor = acDoc.Editor; |
522 |
List<object> commandParam = new List<object>(); |
523 |
commandParam.Add("INSSYM"); |
524 |
|
525 |
// Insert Point |
526 |
Point3d point = new Point3d(x, y, 0); |
527 |
commandParam.Add(point); |
528 |
|
529 |
// Check Insert Point |
530 |
if (needAngle) |
531 |
commandParam.Add(symbol.ANGLE); |
532 |
|
533 |
// Check Balloon |
534 |
if (needBalloon) |
535 |
{ |
536 |
point = new Point3d(x + 20, y + 20, 0); |
537 |
commandParam.Add(point); |
538 |
|
539 |
// Check LeaderLine |
540 |
if (GraphicalUtility.IsLeaderReqOnSymbolBalloons()) |
541 |
commandParam.Add(x + "," + y); |
542 |
} |
543 |
|
544 |
for (int i = 0; i < 8; i++) |
545 |
commandParam.Add(null); |
546 |
|
547 |
editor.Command(commandParam.ToArray()); |
548 |
|
549 |
List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName); |
550 |
List<long> otherHandles = new List<long>(); |
551 |
foreach (var item in newHandles) |
552 |
if (!prevHandles.Contains(item)) |
553 |
otherHandles.Add(item); |
554 |
|
555 |
if (otherHandles.Count == 1) |
556 |
return otherHandles[0]; |
557 |
else |
558 |
return 0; |
559 |
} |
560 |
catch (System.Exception ex) |
561 |
{ |
562 |
|
563 |
} |
564 |
|
565 |
return handle; |
566 |
} |
567 |
private void DrawText(double x, double y, double height, string text, double rotation) |
568 |
{ |
569 |
// Get the current document and database |
570 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
571 |
Database acCurDb = acDoc.Database; |
572 |
|
573 |
// Start a transaction |
574 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
575 |
{ |
576 |
// Open the Block table for read |
577 |
BlockTable acBlkTbl; |
578 |
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, |
579 |
OpenMode.ForRead) as BlockTable; |
580 |
|
581 |
// Open the Block table record Model space for write |
582 |
BlockTableRecord acBlkTblRec; |
583 |
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], |
584 |
OpenMode.ForWrite) as BlockTableRecord; |
585 |
|
586 |
// Create a single-line text object |
587 |
using (DBText acText = new DBText()) |
588 |
{ |
589 |
acText.Position = new Point3d(x, y, 0); |
590 |
acText.Height = height; |
591 |
acText.TextString = text; |
592 |
acText.Rotation = rotation; |
593 |
|
594 |
acBlkTblRec.AppendEntity(acText); |
595 |
acTrans.AddNewlyCreatedDBObject(acText, true); |
596 |
} |
597 |
|
598 |
// Save the changes and dispose of the transaction |
599 |
acTrans.Commit(); |
600 |
} |
601 |
} |
602 |
#endregion |
603 |
|
604 |
#region Autocad Utils |
605 |
private List<long> GetAllBlockHandlesByName(string name) |
606 |
{ |
607 |
List<long> handles = new List<long>(); |
608 |
|
609 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
610 |
Database acCurDb = acDoc.Database; |
611 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
612 |
{ |
613 |
BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead); |
614 |
foreach (var entry in gd) |
615 |
{ |
616 |
BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord; |
617 |
if (blockTableRecord != null) |
618 |
{ |
619 |
IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord); |
620 |
foreach (var item in records) |
621 |
{ |
622 |
if (item.Name == name) |
623 |
handles.Add(item.Handle.Value); |
624 |
} |
625 |
} |
626 |
} |
627 |
|
628 |
acTrans.Commit(); |
629 |
} |
630 |
return handles; |
631 |
} |
632 |
private List<long> GetAllGroupHandles() |
633 |
{ |
634 |
List<long> handles = new List<long>(); |
635 |
|
636 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
637 |
Database acCurDb = acDoc.Database; |
638 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
639 |
{ |
640 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
641 |
foreach (var entry in gd) |
642 |
handles.Add(entry.Value.Handle.Value); |
643 |
acTrans.Commit(); |
644 |
} |
645 |
return handles; |
646 |
} |
647 |
private ObjectId GetFirstPolyLineObjectId(long groupHandle) |
648 |
{ |
649 |
ObjectId result = ObjectId.Null; |
650 |
|
651 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
652 |
Database acCurDb = acDoc.Database; |
653 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
654 |
{ |
655 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
656 |
foreach (var entry in gd) |
657 |
{ |
658 |
if (entry.Value.Handle.Value == groupHandle) |
659 |
{ |
660 |
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group; |
661 |
foreach (var item in group.GetAllEntityIds()) |
662 |
{ |
663 |
if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)) |
664 |
{ |
665 |
result = item; |
666 |
break; |
667 |
} |
668 |
} |
669 |
break; |
670 |
} |
671 |
} |
672 |
acTrans.Commit(); |
673 |
} |
674 |
|
675 |
return result; |
676 |
} |
677 |
private ObjectId GetObjectIdByHandle(long lHandle) |
678 |
{ |
679 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
680 |
Database acCurDb = acDoc.Database; |
681 |
Handle handle = new Handle(lHandle); |
682 |
ObjectId result = ObjectId.Null; |
683 |
try |
684 |
{ |
685 |
result = acCurDb.GetObjectId(false, handle, 0); |
686 |
} |
687 |
catch (System.Exception ex) |
688 |
{ |
689 |
|
690 |
} |
691 |
return result; |
692 |
} |
693 |
private Entity GetEntityByHandle(long handle) |
694 |
{ |
695 |
Entity entity = null; |
696 |
ObjectId objectId = GetObjectIdByHandle(handle); |
697 |
if (objectId != ObjectId.Null) |
698 |
{ |
699 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
700 |
Database acCurDb = acDoc.Database; |
701 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
702 |
{ |
703 |
entity = (Entity)acTrans.GetObject(objectId, OpenMode.ForRead); |
704 |
acTrans.Commit(); |
705 |
} |
706 |
} |
707 |
|
708 |
return entity; |
709 |
} |
710 |
private void SetBlockReferenceRotation(ObjectId objectId, double rotation) |
711 |
{ |
712 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
713 |
Database acCurDb = acDoc.Database; |
714 |
Editor acDocEd = acDoc.Editor; |
715 |
|
716 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
717 |
{ |
718 |
DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite); |
719 |
if (objDB != null) |
720 |
{ |
721 |
if (objDB.GetType() == typeof(BlockReference)) |
722 |
{ |
723 |
BlockReference block = objDB as BlockReference; |
724 |
block.Rotation = rotation; |
725 |
} |
726 |
} |
727 |
acTrans.Commit(); |
728 |
} |
729 |
} |
730 |
#endregion |
731 |
|
732 |
#region For Aveva |
733 |
private void SetAttributeByAvevaForm(long handle, List<Tuple<string, string>> datas) |
734 |
{ |
735 |
Entity entity = GetEntityByHandle(handle); |
736 |
|
737 |
ComponentPropertiesHelper componentPropertiesHelper = new ComponentPropertiesHelper(); |
738 |
AvevaThread.Run(ThreadType.AddProperty, datas); |
739 |
if (!componentPropertiesHelper.DisplayComponentProperties(entity)) |
740 |
AvevaThread.Stop(ThreadType.AddProperty); |
741 |
} |
742 |
#endregion |
743 |
|
744 |
#region Modeling Utils |
745 |
private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert) |
746 |
{ |
747 |
if (!group.Contains(line)) |
748 |
{ |
749 |
if (isInsert) |
750 |
group.Insert(0, line); |
751 |
else |
752 |
group.Add(line); |
753 |
|
754 |
if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject)) |
755 |
{ |
756 |
object connObj = line.CONNECTORS[0].ConnectedObject; |
757 |
if (connObj.GetType() == typeof(Model.Line) && |
758 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
759 |
GetConnectedGroupLine(connObj as Model.Line, group, true); |
760 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
761 |
{ |
762 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
763 |
if (connLine != null) |
764 |
GetConnectedGroupLine(connLine as Model.Line, group, true); |
765 |
} |
766 |
} |
767 |
if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject)) |
768 |
{ |
769 |
object connObj = line.CONNECTORS[1].ConnectedObject; |
770 |
if (connObj.GetType() == typeof(Model.Line) && |
771 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
772 |
GetConnectedGroupLine(connObj as Model.Line, group, false); |
773 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
774 |
{ |
775 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
776 |
if (connLine != null) |
777 |
GetConnectedGroupLine(connLine as Model.Line, group, false); |
778 |
} |
779 |
} |
780 |
} |
781 |
} |
782 |
private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points) |
783 |
{ |
784 |
for (int i = 0; i < groupLine.Count; i++) |
785 |
{ |
786 |
Model.Line line = groupLine[i]; |
787 |
if (i == 0) |
788 |
{ |
789 |
object connObj = line.CONNECTORS[0].ConnectedObject; |
790 |
if (connObj != null) |
791 |
{ |
792 |
string point = GetPointByConnectedItem(connObj, line, true); |
793 |
if (string.IsNullOrEmpty(point)) |
794 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
795 |
else |
796 |
points.Add(point); |
797 |
} |
798 |
else |
799 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
800 |
} |
801 |
else |
802 |
{ |
803 |
Model.Line prevLine = groupLine[i - 1]; |
804 |
if (line.SlopeType == SlopeType.HORIZONTAL && |
805 |
prevLine.SlopeType == SlopeType.VERTICAL) |
806 |
points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y); |
807 |
else if (line.SlopeType == SlopeType.VERTICAL && |
808 |
prevLine.SlopeType == SlopeType.HORIZONTAL) |
809 |
points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y); |
810 |
else |
811 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
812 |
} |
813 |
|
814 |
if (i == groupLine.Count - 1) |
815 |
{ |
816 |
object connObj = line.CONNECTORS[1].ConnectedObject; |
817 |
if (connObj != null && connObj.GetType() == typeof(Symbol)) |
818 |
{ |
819 |
string point = GetPointByConnectedItem(connObj, line, false); |
820 |
if (string.IsNullOrEmpty(point)) |
821 |
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
822 |
else |
823 |
points.Add(point); |
824 |
} |
825 |
else |
826 |
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
827 |
} |
828 |
} |
829 |
} |
830 |
private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart) |
831 |
{ |
832 |
string result = string.Empty; |
833 |
double x; |
834 |
double y; |
835 |
if (isStart) |
836 |
{ |
837 |
x = line.Aveva.Start_X; |
838 |
y = line.Aveva.Start_Y; |
839 |
} |
840 |
else |
841 |
{ |
842 |
x = line.Aveva.End_X; |
843 |
y = line.Aveva.End_Y; |
844 |
} |
845 |
|
846 |
if (connObj.GetType() == typeof(Symbol)) |
847 |
{ |
848 |
Symbol connSymbol = connObj as Symbol; |
849 |
Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line); |
850 |
|
851 |
SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y); |
852 |
if (slopeType == SlopeType.HORIZONTAL) |
853 |
result = connSymbol.Aveva.X + "," + y; |
854 |
else if (slopeType == SlopeType.VERTICAL) |
855 |
result = x + "," + connSymbol.Aveva.Y; |
856 |
else |
857 |
result = x + "," + y; |
858 |
} |
859 |
else if (connObj.GetType() == typeof(OPC)) |
860 |
{ |
861 |
OPC connOPC = connObj as OPC; |
862 |
Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line); |
863 |
|
864 |
double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0; |
865 |
APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY); |
866 |
APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight); |
867 |
locX = locX + sizeWidth / 2; |
868 |
locY = locY + sizeHeight / 2; |
869 |
aX = locX; |
870 |
aY = locY; |
871 |
document.ConvertAvevaPoint(ref aX, ref aY); |
872 |
|
873 |
SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y); |
874 |
if (slopeType == SlopeType.HORIZONTAL) |
875 |
result = aX + "," + y; |
876 |
else if (slopeType == SlopeType.VERTICAL) |
877 |
result = x + "," + aY; |
878 |
else |
879 |
result = x + "," + y; |
880 |
} |
881 |
|
882 |
return result; |
883 |
} |
884 |
private bool IsExistEndBreak(object item1, object item2) |
885 |
{ |
886 |
bool result = false; |
887 |
if (item1 != null && item2 != null) |
888 |
{ |
889 |
EndBreak endBreak = document.EndBreaks.Find(x => |
890 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item1 && |
891 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) || |
892 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item2 && |
893 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1)); |
894 |
|
895 |
if (endBreak != null) |
896 |
result = true; |
897 |
} |
898 |
|
899 |
return result; |
900 |
} |
901 |
private LineRun FindLineRun(Model.Line line) |
902 |
{ |
903 |
List<LineRun> allLineRun = new List<LineRun>(); |
904 |
foreach (var item in document.LINENUMBERS) |
905 |
allLineRun.AddRange(item.RUNS); |
906 |
foreach (var item in document.TRIMLINES) |
907 |
allLineRun.AddRange(item.RUNS); |
908 |
|
909 |
return allLineRun.Find(x => x.RUNITEMS.Contains(line)); |
910 |
} |
911 |
private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol) |
912 |
{ |
913 |
Model.Line result = null; |
914 |
LineRun run = FindLineRun(line); |
915 |
Connector connector = symbol.CONNECTORS.Find(x => |
916 |
x.ConnectedObject != null && |
917 |
x.ConnectedObject != line && |
918 |
run.RUNITEMS.Contains(x.ConnectedObject) && |
919 |
x.ConnectedObject.GetType() == typeof(Model.Line)); |
920 |
|
921 |
if (connector != null) |
922 |
result = connector.ConnectedObject as Model.Line; |
923 |
|
924 |
return result; |
925 |
} |
926 |
private bool CheckAngle(DataRow symbolRow) |
927 |
{ |
928 |
return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2); |
929 |
} |
930 |
private bool CheckBalloon(Symbol symbol) |
931 |
{ |
932 |
bool result = false; |
933 |
|
934 |
string text = string.Empty; |
935 |
Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name); |
936 |
if (symbolDefinitions.Count != 0) |
937 |
{ |
938 |
text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper(); |
939 |
|
940 |
if (text.Equals("VLP")) |
941 |
text = "VPO"; |
942 |
else if (text.Equals("IVP")) |
943 |
text = "IPO"; |
944 |
|
945 |
if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text)) |
946 |
result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId); |
947 |
} |
948 |
|
949 |
return result; |
950 |
} |
951 |
#endregion |
952 |
|
953 |
#region Test Source |
954 |
public void test() |
955 |
{ |
956 |
|
957 |
} |
958 |
public static void TESTStatic(long handle) |
959 |
{ |
960 |
AutoModeling auto = new AutoModeling(null); |
961 |
auto.SetAttributeByAvevaForm(handle, new List<Tuple<string, string>>() { new Tuple<string, string>("Size", "100") }); |
962 |
} |
963 |
#endregion |
964 |
} |
965 |
|
966 |
|
967 |
public static class AcDbLinqExtensionMethods |
968 |
{ |
969 |
/// <summary> |
970 |
/// Get all references to the given BlockTableRecord, including |
971 |
/// references to anonymous dynamic BlockTableRecords. |
972 |
/// </summary> |
973 |
|
974 |
public static IEnumerable<BlockReference> GetBlockReferences( |
975 |
this BlockTableRecord btr, |
976 |
OpenMode mode = OpenMode.ForRead, |
977 |
bool directOnly = true) |
978 |
{ |
979 |
if (btr == null) |
980 |
throw new ArgumentNullException("btr"); |
981 |
var tr = btr.Database.TransactionManager.TopTransaction; |
982 |
if (tr == null) |
983 |
throw new InvalidOperationException("No transaction"); |
984 |
var ids = btr.GetBlockReferenceIds(directOnly, true); |
985 |
int cnt = ids.Count; |
986 |
for (int i = 0; i < cnt; i++) |
987 |
{ |
988 |
yield return (BlockReference) |
989 |
tr.GetObject(ids[i], mode, false, false); |
990 |
} |
991 |
if (btr.IsDynamicBlock) |
992 |
{ |
993 |
BlockTableRecord btr2 = null; |
994 |
var blockIds = btr.GetAnonymousBlockIds(); |
995 |
cnt = blockIds.Count; |
996 |
for (int i = 0; i < cnt; i++) |
997 |
{ |
998 |
btr2 = (BlockTableRecord)tr.GetObject(blockIds[i], |
999 |
OpenMode.ForRead, false, false); |
1000 |
ids = btr2.GetBlockReferenceIds(directOnly, true); |
1001 |
int cnt2 = ids.Count; |
1002 |
for (int j = 0; j < cnt2; j++) |
1003 |
{ |
1004 |
yield return (BlockReference) |
1005 |
tr.GetObject(ids[j], mode, false, false); |
1006 |
} |
1007 |
} |
1008 |
} |
1009 |
} |
1010 |
} |
1011 |
} |