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 = APIDUtils.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
|
datas.Add(new Tuple<string, string>(info.APID_ATTRIBUTE, attribute.VALUE));
|
360
|
}
|
361
|
if (datas.Count > 0)
|
362
|
{
|
363
|
ObjectId objectId = GetFirstPolyLineObjectId(line.Aveva.Handle);
|
364
|
if (!objectId.IsNull)
|
365
|
SetAttributeByAvevaForm(objectId.Handle.Value, datas);
|
366
|
}
|
367
|
}
|
368
|
}
|
369
|
private void SetSymbolAttribute(Model.Symbol symbol)
|
370
|
{
|
371
|
if (symbol.Aveva.Handle != 0)
|
372
|
{
|
373
|
List<Tuple<string, string>> datas = new List<Tuple<string, string>>();
|
374
|
foreach (var attribute in symbol.ATTRIBUTES)
|
375
|
{
|
376
|
AttributeInfo info = avevaInfo.AttributeInfo.Find(x => x.UID == attribute.UID);
|
377
|
if (info != null && !string.IsNullOrEmpty(info.APID_ATTRIBUTE))
|
378
|
datas.Add(new Tuple<string, string>(info.APID_ATTRIBUTE, attribute.VALUE));
|
379
|
}
|
380
|
if (datas.Count > 0)
|
381
|
{
|
382
|
ObjectId objectId = APIDUtils.GetObjectIdByHandle(symbol.Aveva.Handle);
|
383
|
if (!objectId.IsNull)
|
384
|
SetAttributeByAvevaForm(objectId.Handle.Value, datas);
|
385
|
}
|
386
|
}
|
387
|
}
|
388
|
#endregion
|
389
|
|
390
|
#region Drawing Method
|
391
|
private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
|
392
|
{
|
393
|
List<long> prevHandles = GetAllGroupHandles();
|
394
|
|
395
|
GUIUtils.SetPipeStyle(style);
|
396
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
397
|
Editor editor = acDoc.Editor;
|
398
|
List<object> commandParam = new List<object>();
|
399
|
//command name
|
400
|
commandParam.Add("DRPIPE");
|
401
|
//coordinate
|
402
|
foreach (var item in coordinates)
|
403
|
commandParam.Add(item);
|
404
|
|
405
|
//enter Parent(null)
|
406
|
commandParam.Add(null);
|
407
|
|
408
|
if (lineNumber != null)
|
409
|
{
|
410
|
Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
|
411
|
commandParam.Add(point);
|
412
|
commandParam.Add(lineNumber.Aveva.Angle);
|
413
|
|
414
|
commandParam.Add(null);
|
415
|
}
|
416
|
else
|
417
|
commandParam.Add(null);
|
418
|
|
419
|
editor.Command(commandParam.ToArray());
|
420
|
|
421
|
List<long> newHandles = GetAllGroupHandles();
|
422
|
List<long> otherHandles = new List<long>();
|
423
|
foreach (var item in newHandles)
|
424
|
if (!prevHandles.Contains(item))
|
425
|
otherHandles.Add(item);
|
426
|
|
427
|
if (otherHandles.Count == 1)
|
428
|
return otherHandles[0];
|
429
|
else
|
430
|
return 0;
|
431
|
}
|
432
|
private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
|
433
|
{
|
434
|
List<long> prevHandles = GetAllGroupHandles();
|
435
|
GUIUtils.SetPipeStyle(style);
|
436
|
|
437
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
438
|
Editor editor = acDoc.Editor;
|
439
|
List<object> commandParam = new List<object>();
|
440
|
//command name
|
441
|
commandParam.Add("DRPIPE");
|
442
|
//coordinate
|
443
|
foreach (var item in coordinates)
|
444
|
commandParam.Add(item);
|
445
|
//enter
|
446
|
commandParam.Add(null);
|
447
|
|
448
|
//Input Object ID
|
449
|
commandParam.Add(objectId);
|
450
|
|
451
|
//enter
|
452
|
commandParam.Add(null);
|
453
|
|
454
|
editor.Command(commandParam.ToArray());
|
455
|
|
456
|
List<long> newHandles = GetAllGroupHandles();
|
457
|
List<long> otherHandles = new List<long>();
|
458
|
foreach (var item in newHandles)
|
459
|
if (!prevHandles.Contains(item))
|
460
|
otherHandles.Add(item);
|
461
|
|
462
|
if (otherHandles.Count == 1)
|
463
|
return otherHandles[0];
|
464
|
else
|
465
|
return 0;
|
466
|
}
|
467
|
private long DrawSignal(string style, List<string> coordinates)
|
468
|
{
|
469
|
List<long> prevHandles = GetAllGroupHandles();
|
470
|
|
471
|
GUIUtils.SetPipeStyle(style);
|
472
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
473
|
Editor editor = acDoc.Editor;
|
474
|
List<object> commandParam = new List<object>();
|
475
|
//command name
|
476
|
commandParam.Add("VPESIGNAL");
|
477
|
//coordinate
|
478
|
foreach (var item in coordinates)
|
479
|
commandParam.Add(item);
|
480
|
//enter
|
481
|
commandParam.Add(null);
|
482
|
|
483
|
editor.Command(commandParam.ToArray());
|
484
|
|
485
|
List<long> newHandles = GetAllGroupHandles();
|
486
|
List<long> otherHandles = new List<long>();
|
487
|
foreach (var item in newHandles)
|
488
|
if (!prevHandles.Contains(item))
|
489
|
otherHandles.Add(item);
|
490
|
|
491
|
if (otherHandles.Count == 1)
|
492
|
return otherHandles[0];
|
493
|
else
|
494
|
return 0;
|
495
|
}
|
496
|
private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
|
497
|
{
|
498
|
long handle = 0;
|
499
|
try
|
500
|
{
|
501
|
string insertSymbolName = symbol.Aveva.Name;
|
502
|
double x = symbol.Aveva.X;
|
503
|
double y = symbol.Aveva.Y;
|
504
|
|
505
|
List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
|
506
|
|
507
|
AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
|
508
|
drawingData.InsertSymbolName = insertSymbolName;
|
509
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
510
|
Editor editor = acDoc.Editor;
|
511
|
List<object> commandParam = new List<object>();
|
512
|
commandParam.Add("INSSYM");
|
513
|
|
514
|
// Insert Point
|
515
|
Point3d point = new Point3d(x, y, 0);
|
516
|
commandParam.Add(point);
|
517
|
|
518
|
// Check Insert Point
|
519
|
if (needAngle)
|
520
|
commandParam.Add(symbol.ANGLE);
|
521
|
|
522
|
// Check Balloon
|
523
|
if (needBalloon)
|
524
|
{
|
525
|
point = new Point3d(x + 20, y + 20, 0);
|
526
|
commandParam.Add(point);
|
527
|
|
528
|
// Check LeaderLine
|
529
|
if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
|
530
|
commandParam.Add(x + "," + y);
|
531
|
}
|
532
|
|
533
|
for (int i = 0; i < 8; i++)
|
534
|
commandParam.Add(null);
|
535
|
|
536
|
editor.Command(commandParam.ToArray());
|
537
|
|
538
|
List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
|
539
|
List<long> otherHandles = new List<long>();
|
540
|
foreach (var item in newHandles)
|
541
|
if (!prevHandles.Contains(item))
|
542
|
otherHandles.Add(item);
|
543
|
|
544
|
if (otherHandles.Count == 1)
|
545
|
return otherHandles[0];
|
546
|
else
|
547
|
return 0;
|
548
|
}
|
549
|
catch (System.Exception ex)
|
550
|
{
|
551
|
|
552
|
}
|
553
|
|
554
|
return handle;
|
555
|
}
|
556
|
private void DrawText(double x, double y, double height, string text, double rotation)
|
557
|
{
|
558
|
// Get the current document and database
|
559
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
560
|
Database acCurDb = acDoc.Database;
|
561
|
|
562
|
// Start a transaction
|
563
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
564
|
{
|
565
|
// Open the Block table for read
|
566
|
BlockTable acBlkTbl;
|
567
|
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
|
568
|
OpenMode.ForRead) as BlockTable;
|
569
|
|
570
|
// Open the Block table record Model space for write
|
571
|
BlockTableRecord acBlkTblRec;
|
572
|
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
|
573
|
OpenMode.ForWrite) as BlockTableRecord;
|
574
|
|
575
|
// Create a single-line text object
|
576
|
using (DBText acText = new DBText())
|
577
|
{
|
578
|
acText.Position = new Point3d(x, y, 0);
|
579
|
acText.Height = height;
|
580
|
acText.TextString = text;
|
581
|
acText.Rotation = rotation;
|
582
|
|
583
|
acBlkTblRec.AppendEntity(acText);
|
584
|
acTrans.AddNewlyCreatedDBObject(acText, true);
|
585
|
}
|
586
|
|
587
|
// Save the changes and dispose of the transaction
|
588
|
acTrans.Commit();
|
589
|
}
|
590
|
}
|
591
|
#endregion
|
592
|
|
593
|
#region Autocad Utils
|
594
|
private List<long> GetAllBlockHandlesByName(string name)
|
595
|
{
|
596
|
List<long> handles = new List<long>();
|
597
|
|
598
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
599
|
Database acCurDb = acDoc.Database;
|
600
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
601
|
{
|
602
|
BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
|
603
|
foreach (var entry in gd)
|
604
|
{
|
605
|
BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
|
606
|
if (blockTableRecord != null)
|
607
|
{
|
608
|
IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
|
609
|
foreach (var item in records)
|
610
|
{
|
611
|
if (item.Name == name)
|
612
|
handles.Add(item.Handle.Value);
|
613
|
}
|
614
|
}
|
615
|
}
|
616
|
|
617
|
acTrans.Commit();
|
618
|
}
|
619
|
return handles;
|
620
|
}
|
621
|
private List<long> GetAllGroupHandles()
|
622
|
{
|
623
|
List<long> handles = new List<long>();
|
624
|
|
625
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
626
|
Database acCurDb = acDoc.Database;
|
627
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
628
|
{
|
629
|
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
|
630
|
foreach (var entry in gd)
|
631
|
handles.Add(entry.Value.Handle.Value);
|
632
|
acTrans.Commit();
|
633
|
}
|
634
|
return handles;
|
635
|
}
|
636
|
private ObjectId GetFirstPolyLineObjectId(long groupHandle)
|
637
|
{
|
638
|
ObjectId result = ObjectId.Null;
|
639
|
|
640
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
641
|
Database acCurDb = acDoc.Database;
|
642
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
643
|
{
|
644
|
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
|
645
|
foreach (var entry in gd)
|
646
|
{
|
647
|
if (entry.Value.Handle.Value == groupHandle)
|
648
|
{
|
649
|
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
|
650
|
foreach (var item in group.GetAllEntityIds())
|
651
|
{
|
652
|
if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
|
653
|
{
|
654
|
result = item;
|
655
|
break;
|
656
|
}
|
657
|
}
|
658
|
break;
|
659
|
}
|
660
|
}
|
661
|
acTrans.Commit();
|
662
|
}
|
663
|
|
664
|
return result;
|
665
|
}
|
666
|
private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
|
667
|
{
|
668
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
669
|
Database acCurDb = acDoc.Database;
|
670
|
Editor acDocEd = acDoc.Editor;
|
671
|
|
672
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
673
|
{
|
674
|
DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
|
675
|
if (objDB != null)
|
676
|
{
|
677
|
if (objDB.GetType() == typeof(BlockReference))
|
678
|
{
|
679
|
BlockReference block = objDB as BlockReference;
|
680
|
block.Rotation = rotation;
|
681
|
}
|
682
|
}
|
683
|
acTrans.Commit();
|
684
|
}
|
685
|
}
|
686
|
#endregion
|
687
|
|
688
|
#region For Aveva
|
689
|
private void SetAttributeByAvevaForm(long handle, List<Tuple<string, string>> datas)
|
690
|
{
|
691
|
Entity entity = APIDUtils.GetEntityByHandle(handle);
|
692
|
|
693
|
ComponentPropertiesHelper componentPropertiesHelper = new ComponentPropertiesHelper();
|
694
|
AvevaThread.Run(ThreadType.AddProperty, datas);
|
695
|
if (!componentPropertiesHelper.DisplayComponentProperties(entity))
|
696
|
AvevaThread.Stop(ThreadType.AddProperty);
|
697
|
}
|
698
|
#endregion
|
699
|
|
700
|
#region Modeling Utils
|
701
|
private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
|
702
|
{
|
703
|
if (!group.Contains(line))
|
704
|
{
|
705
|
if (isInsert)
|
706
|
group.Insert(0, line);
|
707
|
else
|
708
|
group.Add(line);
|
709
|
|
710
|
if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
|
711
|
{
|
712
|
object connObj = line.CONNECTORS[0].ConnectedObject;
|
713
|
if (connObj.GetType() == typeof(Model.Line) &&
|
714
|
APIDUtils.IsConnectedLine(connObj as Model.Line, line))
|
715
|
GetConnectedGroupLine(connObj as Model.Line, group, true);
|
716
|
else if (connObj.GetType() == typeof(Model.Symbol))
|
717
|
{
|
718
|
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
|
719
|
if (connLine != null)
|
720
|
GetConnectedGroupLine(connLine as Model.Line, group, true);
|
721
|
}
|
722
|
}
|
723
|
if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
|
724
|
{
|
725
|
object connObj = line.CONNECTORS[1].ConnectedObject;
|
726
|
if (connObj.GetType() == typeof(Model.Line) &&
|
727
|
APIDUtils.IsConnectedLine(connObj as Model.Line, line))
|
728
|
GetConnectedGroupLine(connObj as Model.Line, group, false);
|
729
|
else if (connObj.GetType() == typeof(Model.Symbol))
|
730
|
{
|
731
|
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
|
732
|
if (connLine != null)
|
733
|
GetConnectedGroupLine(connLine as Model.Line, group, false);
|
734
|
}
|
735
|
}
|
736
|
}
|
737
|
}
|
738
|
private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
|
739
|
{
|
740
|
for (int i = 0; i < groupLine.Count; i++)
|
741
|
{
|
742
|
Model.Line line = groupLine[i];
|
743
|
if (i == 0)
|
744
|
{
|
745
|
object connObj = line.CONNECTORS[0].ConnectedObject;
|
746
|
if (connObj != null)
|
747
|
{
|
748
|
string point = GetPointByConnectedItem(connObj, line, true);
|
749
|
if (string.IsNullOrEmpty(point))
|
750
|
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
|
751
|
else
|
752
|
points.Add(point);
|
753
|
}
|
754
|
else
|
755
|
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
|
756
|
}
|
757
|
else
|
758
|
{
|
759
|
Model.Line prevLine = groupLine[i - 1];
|
760
|
if (line.SlopeType == SlopeType.HORIZONTAL &&
|
761
|
prevLine.SlopeType == SlopeType.VERTICAL)
|
762
|
points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
|
763
|
else if (line.SlopeType == SlopeType.VERTICAL &&
|
764
|
prevLine.SlopeType == SlopeType.HORIZONTAL)
|
765
|
points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
|
766
|
else
|
767
|
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
|
768
|
}
|
769
|
|
770
|
if (i == groupLine.Count - 1)
|
771
|
{
|
772
|
object connObj = line.CONNECTORS[1].ConnectedObject;
|
773
|
if (connObj != null && connObj.GetType() == typeof(Symbol))
|
774
|
{
|
775
|
string point = GetPointByConnectedItem(connObj, line, false);
|
776
|
if (string.IsNullOrEmpty(point))
|
777
|
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
|
778
|
else
|
779
|
points.Add(point);
|
780
|
}
|
781
|
else
|
782
|
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
|
783
|
}
|
784
|
}
|
785
|
}
|
786
|
private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
|
787
|
{
|
788
|
string result = string.Empty;
|
789
|
double x;
|
790
|
double y;
|
791
|
if (isStart)
|
792
|
{
|
793
|
x = line.Aveva.Start_X;
|
794
|
y = line.Aveva.Start_Y;
|
795
|
}
|
796
|
else
|
797
|
{
|
798
|
x = line.Aveva.End_X;
|
799
|
y = line.Aveva.End_Y;
|
800
|
}
|
801
|
|
802
|
if (connObj.GetType() == typeof(Symbol))
|
803
|
{
|
804
|
Symbol connSymbol = connObj as Symbol;
|
805
|
Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
|
806
|
|
807
|
SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
|
808
|
if (slopeType == SlopeType.HORIZONTAL)
|
809
|
result = connSymbol.Aveva.X + "," + y;
|
810
|
else if (slopeType == SlopeType.VERTICAL)
|
811
|
result = x + "," + connSymbol.Aveva.Y;
|
812
|
else
|
813
|
result = x + "," + y;
|
814
|
}
|
815
|
else if (connObj.GetType() == typeof(OPC))
|
816
|
{
|
817
|
OPC connOPC = connObj as OPC;
|
818
|
Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
|
819
|
|
820
|
double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
|
821
|
APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
|
822
|
APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
|
823
|
locX = locX + sizeWidth / 2;
|
824
|
locY = locY + sizeHeight / 2;
|
825
|
aX = locX;
|
826
|
aY = locY;
|
827
|
document.ConvertAvevaPoint(ref aX, ref aY);
|
828
|
|
829
|
SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
|
830
|
if (slopeType == SlopeType.HORIZONTAL)
|
831
|
result = aX + "," + y;
|
832
|
else if (slopeType == SlopeType.VERTICAL)
|
833
|
result = x + "," + aY;
|
834
|
else
|
835
|
result = x + "," + y;
|
836
|
}
|
837
|
|
838
|
return result;
|
839
|
}
|
840
|
private bool IsExistEndBreak(object item1, object item2)
|
841
|
{
|
842
|
bool result = false;
|
843
|
if (item1 != null && item2 != null)
|
844
|
{
|
845
|
EndBreak endBreak = document.EndBreaks.Find(x =>
|
846
|
(APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
|
847
|
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
|
848
|
(APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
|
849
|
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
|
850
|
|
851
|
if (endBreak != null)
|
852
|
result = true;
|
853
|
}
|
854
|
|
855
|
return result;
|
856
|
}
|
857
|
private LineRun FindLineRun(Model.Line line)
|
858
|
{
|
859
|
List<LineRun> allLineRun = new List<LineRun>();
|
860
|
foreach (var item in document.LINENUMBERS)
|
861
|
allLineRun.AddRange(item.RUNS);
|
862
|
foreach (var item in document.TRIMLINES)
|
863
|
allLineRun.AddRange(item.RUNS);
|
864
|
|
865
|
return allLineRun.Find(x => x.RUNITEMS.Contains(line));
|
866
|
}
|
867
|
private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
|
868
|
{
|
869
|
Model.Line result = null;
|
870
|
LineRun run = FindLineRun(line);
|
871
|
Connector connector = symbol.CONNECTORS.Find(x =>
|
872
|
x.ConnectedObject != null &&
|
873
|
x.ConnectedObject != line &&
|
874
|
run.RUNITEMS.Contains(x.ConnectedObject) &&
|
875
|
x.ConnectedObject.GetType() == typeof(Model.Line));
|
876
|
|
877
|
if (connector != null)
|
878
|
result = connector.ConnectedObject as Model.Line;
|
879
|
|
880
|
return result;
|
881
|
}
|
882
|
private bool CheckAngle(DataRow symbolRow)
|
883
|
{
|
884
|
return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
|
885
|
}
|
886
|
private bool CheckBalloon(Symbol symbol)
|
887
|
{
|
888
|
bool result = false;
|
889
|
|
890
|
string text = string.Empty;
|
891
|
Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
|
892
|
if (symbolDefinitions.Count != 0)
|
893
|
{
|
894
|
text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
|
895
|
|
896
|
if (text.Equals("VLP"))
|
897
|
text = "VPO";
|
898
|
else if (text.Equals("IVP"))
|
899
|
text = "IPO";
|
900
|
|
901
|
if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
|
902
|
result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
|
903
|
}
|
904
|
|
905
|
return result;
|
906
|
}
|
907
|
#endregion
|
908
|
|
909
|
#region Test Source
|
910
|
public void test()
|
911
|
{
|
912
|
|
913
|
}
|
914
|
public static void TESTStatic(long handle)
|
915
|
{
|
916
|
|
917
|
}
|
918
|
#endregion
|
919
|
}
|
920
|
|
921
|
|
922
|
public static class AcDbLinqExtensionMethods
|
923
|
{
|
924
|
/// <summary>
|
925
|
/// Get all references to the given BlockTableRecord, including
|
926
|
/// references to anonymous dynamic BlockTableRecords.
|
927
|
/// </summary>
|
928
|
|
929
|
public static IEnumerable<BlockReference> GetBlockReferences(
|
930
|
this BlockTableRecord btr,
|
931
|
OpenMode mode = OpenMode.ForRead,
|
932
|
bool directOnly = true)
|
933
|
{
|
934
|
if (btr == null)
|
935
|
throw new ArgumentNullException("btr");
|
936
|
var tr = btr.Database.TransactionManager.TopTransaction;
|
937
|
if (tr == null)
|
938
|
throw new InvalidOperationException("No transaction");
|
939
|
var ids = btr.GetBlockReferenceIds(directOnly, true);
|
940
|
int cnt = ids.Count;
|
941
|
for (int i = 0; i < cnt; i++)
|
942
|
{
|
943
|
yield return (BlockReference)
|
944
|
tr.GetObject(ids[i], mode, false, false);
|
945
|
}
|
946
|
if (btr.IsDynamicBlock)
|
947
|
{
|
948
|
BlockTableRecord btr2 = null;
|
949
|
var blockIds = btr.GetAnonymousBlockIds();
|
950
|
cnt = blockIds.Count;
|
951
|
for (int i = 0; i < cnt; i++)
|
952
|
{
|
953
|
btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
|
954
|
OpenMode.ForRead, false, false);
|
955
|
ids = btr2.GetBlockReferenceIds(directOnly, true);
|
956
|
int cnt2 = ids.Count;
|
957
|
for (int j = 0; j < cnt2; j++)
|
958
|
{
|
959
|
yield return (BlockReference)
|
960
|
tr.GetObject(ids[j], mode, false, false);
|
961
|
}
|
962
|
}
|
963
|
}
|
964
|
}
|
965
|
}
|
966
|
}
|