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
|
|
10
|
using Autodesk.AutoCAD.ApplicationServices;
|
11
|
using Autodesk.AutoCAD.ApplicationServices.Core;
|
12
|
using Autodesk.AutoCAD.DatabaseServices;
|
13
|
using Autodesk.AutoCAD.EditorInput;
|
14
|
using Autodesk.AutoCAD.Geometry;
|
15
|
using Autodesk.AutoCAD.Interop;
|
16
|
using Autodesk.AutoCAD.Interop.Common;
|
17
|
using Autodesk.AutoCAD.Runtime;
|
18
|
using Autodesk.AutoCAD.Windows;
|
19
|
|
20
|
using AVEVA.PID.Components;
|
21
|
using AVEVA.PID.GUI;
|
22
|
using AVEVA.PID.Common;
|
23
|
using AVEVA.PID.DWGSelector;
|
24
|
using AVEVA.PID.Utilities;
|
25
|
|
26
|
using AVEVA.PID.CustomizationUtility.DB;
|
27
|
using AVEVA.PID.CustomizationUtility.Model;
|
28
|
using AVEVA.PID.CustomizationUtility.Properties;
|
29
|
|
30
|
using DevExpress.XtraSplashScreen;
|
31
|
|
32
|
namespace AVEVA.PID.CustomizationUtility
|
33
|
{
|
34
|
public class AutoModeling
|
35
|
{
|
36
|
public static AutoModeling Running { get; set; }
|
37
|
Model.Document document;
|
38
|
|
39
|
System.Data.DataTable AvevaSymbolTable = null;
|
40
|
|
41
|
private void SetConvertRule()
|
42
|
{
|
43
|
#region OPC Setting
|
44
|
document.OPCs.Sort(SortOPC);
|
45
|
int SortOPC(OPC a, OPC b)
|
46
|
{
|
47
|
if (a.FlowType == FlowType.In)
|
48
|
return 1;
|
49
|
else
|
50
|
return 0;
|
51
|
}
|
52
|
#endregion
|
53
|
}
|
54
|
|
55
|
public AutoModeling(Model.Document document, System.Data.DataTable AvevaSymbolTable)
|
56
|
{
|
57
|
this.document = document;
|
58
|
this.AvevaSymbolTable = AvevaSymbolTable;
|
59
|
}
|
60
|
|
61
|
public void CreateDrawing()
|
62
|
{
|
63
|
string outPath = Project_DB.GetDirectiveValue("DRGPTH");
|
64
|
string tempPath = Path.GetTempPath();
|
65
|
string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT");
|
66
|
|
67
|
DWTSelector.strDrawingNumber = document.AvevaDrawingNumber;
|
68
|
DWTSelector.strSheetNumber = document.AvevaSheetNumber;
|
69
|
DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber;
|
70
|
DWTSelector.strCadFilePath = outPath;
|
71
|
|
72
|
try
|
73
|
{
|
74
|
string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false);
|
75
|
Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
|
76
|
Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text);
|
77
|
|
78
|
Commands.addLatestPidvesrionToDictionary();
|
79
|
AVVPropCmd.ActivateAcad();
|
80
|
|
81
|
if (acadDocument != null)
|
82
|
{
|
83
|
Running = this;
|
84
|
acadApplication.ActiveDocument = acadDocument;
|
85
|
acadDocument.SendCommand("QSAVE ");
|
86
|
acadDocument.SendCommand("APPIDRun ");
|
87
|
acadDocument.SendCommand("QSAVE ");
|
88
|
Running = null;
|
89
|
}
|
90
|
}
|
91
|
catch (System.Exception ex)
|
92
|
{
|
93
|
|
94
|
}
|
95
|
|
96
|
GC.Collect();
|
97
|
}
|
98
|
public void Run()
|
99
|
{
|
100
|
try
|
101
|
{
|
102
|
SplashScreenManager.ShowForm(typeof(APIDSplashScreen), true, true);
|
103
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllStepCount, 4);
|
104
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetDocumentName, document.AvevaDrawingNumber + document.AvevaSheetNumber);
|
105
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetParent, Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
|
106
|
|
107
|
SetConvertRule();
|
108
|
|
109
|
RunLineModeling();
|
110
|
RunOPCModeling();
|
111
|
RunSymbolModeling();
|
112
|
RunTextModeling();
|
113
|
}
|
114
|
catch (System.Exception ex)
|
115
|
{
|
116
|
System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace);
|
117
|
}
|
118
|
finally
|
119
|
{
|
120
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.ClearParent, null);
|
121
|
SplashScreenManager.CloseForm(false);
|
122
|
}
|
123
|
}
|
124
|
|
125
|
#region Run Method
|
126
|
private void RunLineModeling()
|
127
|
{
|
128
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
|
129
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling");
|
130
|
foreach (var item in document.LINES)
|
131
|
{
|
132
|
if (item.Aveva.Handle == 0)
|
133
|
LineModeling(item);
|
134
|
|
135
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
|
136
|
}
|
137
|
}
|
138
|
private void RunOPCModeling()
|
139
|
{
|
140
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count);
|
141
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling");
|
142
|
foreach (var item in document.OPCs)
|
143
|
{
|
144
|
if (item.Aveva.Handle == 0)
|
145
|
SymbolModeling(item);
|
146
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
|
147
|
}
|
148
|
}
|
149
|
private void RunSymbolModeling()
|
150
|
{
|
151
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
|
152
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
|
153
|
foreach (var item in document.SYMBOLS)
|
154
|
{
|
155
|
if (item.Aveva.Handle == 0)
|
156
|
SymbolModeling(item);
|
157
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
|
158
|
}
|
159
|
}
|
160
|
private void RunTextModeling()
|
161
|
{
|
162
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Texts.Count);
|
163
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Text Modeling");
|
164
|
foreach (var item in document.Texts)
|
165
|
{
|
166
|
|
167
|
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
|
168
|
}
|
169
|
}
|
170
|
#endregion
|
171
|
|
172
|
#region Modeling Method
|
173
|
private void LineModeling(Model.Line line)
|
174
|
{
|
175
|
List<Model.Line> groupLine = new List<Model.Line>();
|
176
|
List<string> points = new List<string>();
|
177
|
GetConnectedGroupLine(line, groupLine, false);
|
178
|
GetGroupLinePoints(groupLine, points);
|
179
|
|
180
|
LineNumber lineNumber = null;
|
181
|
if (line.Aveva.Type == Model.Type.Pipe)
|
182
|
{
|
183
|
foreach (var item in groupLine)
|
184
|
{
|
185
|
lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID);
|
186
|
if (lineNumber != null)
|
187
|
{
|
188
|
GUIUtils.SetAutoLabelCheck(true);
|
189
|
break;
|
190
|
}
|
191
|
}
|
192
|
}
|
193
|
|
194
|
|
195
|
long handle = 0;
|
196
|
if (line.Aveva.Type == Model.Type.Pipe)
|
197
|
handle = DrawPipe(line.Aveva.Name, points, lineNumber);
|
198
|
else if (line.Aveva.Type == Model.Type.Signal)
|
199
|
handle = DrawSignal(line.Aveva.Name, points);
|
200
|
|
201
|
if (lineNumber != null)
|
202
|
GUIUtils.SetAutoLabelCheck(false);
|
203
|
|
204
|
foreach (var item in groupLine)
|
205
|
item.Aveva.Handle = handle;
|
206
|
}
|
207
|
private void SymbolModeling(Symbol symbol)
|
208
|
{
|
209
|
DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
|
210
|
if (allRows.Length == 1)
|
211
|
{
|
212
|
DataRow symbolRow = allRows[0];
|
213
|
bool bAngle = CheckAngle(symbolRow);
|
214
|
bool bBalloon = CheckBalloon(symbol);
|
215
|
|
216
|
long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
|
217
|
if (handle != 0)
|
218
|
{
|
219
|
symbol.Aveva.Handle = handle;
|
220
|
|
221
|
if (!bAngle)
|
222
|
{
|
223
|
ObjectId objectId = GetObjectIdByHandle(handle);
|
224
|
if (objectId != ObjectId.Null)
|
225
|
{
|
226
|
SetBlockReferenceRotation(objectId, symbol.ANGLE);
|
227
|
}
|
228
|
}
|
229
|
}
|
230
|
}
|
231
|
}
|
232
|
#endregion
|
233
|
|
234
|
#region Drawing Method
|
235
|
private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
|
236
|
{
|
237
|
List<long> prevHandles = GetAllGroupHandles();
|
238
|
|
239
|
GUIUtils.SetPipeStyle(style);
|
240
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
241
|
Editor editor = acDoc.Editor;
|
242
|
List<object> commandParam = new List<object>();
|
243
|
//command name
|
244
|
commandParam.Add("DRPIPE");
|
245
|
//coordinate
|
246
|
foreach (var item in coordinates)
|
247
|
commandParam.Add(item);
|
248
|
|
249
|
//enter Parent(null)
|
250
|
commandParam.Add(null);
|
251
|
|
252
|
if (lineNumber != null)
|
253
|
{
|
254
|
Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
|
255
|
commandParam.Add(point);
|
256
|
commandParam.Add(lineNumber.Aveva.Angle);
|
257
|
|
258
|
commandParam.Add(null);
|
259
|
}
|
260
|
else
|
261
|
commandParam.Add(null);
|
262
|
|
263
|
editor.Command(commandParam.ToArray());
|
264
|
|
265
|
List<long> newHandles = GetAllGroupHandles();
|
266
|
List<long> otherHandles = new List<long>();
|
267
|
foreach (var item in newHandles)
|
268
|
if (!prevHandles.Contains(item))
|
269
|
otherHandles.Add(item);
|
270
|
|
271
|
if (otherHandles.Count == 1)
|
272
|
return otherHandles[0];
|
273
|
else
|
274
|
return 0;
|
275
|
}
|
276
|
private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
|
277
|
{
|
278
|
List<long> prevHandles = GetAllGroupHandles();
|
279
|
GUIUtils.SetPipeStyle(style);
|
280
|
|
281
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
282
|
Editor editor = acDoc.Editor;
|
283
|
List<object> commandParam = new List<object>();
|
284
|
//command name
|
285
|
commandParam.Add("DRPIPE");
|
286
|
//coordinate
|
287
|
foreach (var item in coordinates)
|
288
|
commandParam.Add(item);
|
289
|
//enter
|
290
|
commandParam.Add(null);
|
291
|
|
292
|
//Input Object ID
|
293
|
commandParam.Add(objectId);
|
294
|
|
295
|
//enter
|
296
|
commandParam.Add(null);
|
297
|
|
298
|
editor.Command(commandParam.ToArray());
|
299
|
|
300
|
List<long> newHandles = GetAllGroupHandles();
|
301
|
List<long> otherHandles = new List<long>();
|
302
|
foreach (var item in newHandles)
|
303
|
if (!prevHandles.Contains(item))
|
304
|
otherHandles.Add(item);
|
305
|
|
306
|
if (otherHandles.Count == 1)
|
307
|
return otherHandles[0];
|
308
|
else
|
309
|
return 0;
|
310
|
}
|
311
|
private long DrawSignal(string style, List<string> coordinates)
|
312
|
{
|
313
|
List<long> prevHandles = GetAllGroupHandles();
|
314
|
|
315
|
GUIUtils.SetPipeStyle(style);
|
316
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
317
|
Editor editor = acDoc.Editor;
|
318
|
List<object> commandParam = new List<object>();
|
319
|
//command name
|
320
|
commandParam.Add("VPESIGNAL");
|
321
|
//coordinate
|
322
|
foreach (var item in coordinates)
|
323
|
commandParam.Add(item);
|
324
|
//enter
|
325
|
commandParam.Add(null);
|
326
|
|
327
|
editor.Command(commandParam.ToArray());
|
328
|
|
329
|
List<long> newHandles = GetAllGroupHandles();
|
330
|
List<long> otherHandles = new List<long>();
|
331
|
foreach (var item in newHandles)
|
332
|
if (!prevHandles.Contains(item))
|
333
|
otherHandles.Add(item);
|
334
|
|
335
|
if (otherHandles.Count == 1)
|
336
|
return otherHandles[0];
|
337
|
else
|
338
|
return 0;
|
339
|
}
|
340
|
private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
|
341
|
{
|
342
|
long handle = 0;
|
343
|
try
|
344
|
{
|
345
|
string insertSymbolName = symbol.Aveva.Name;
|
346
|
double x = symbol.Aveva.X;
|
347
|
double y = symbol.Aveva.Y;
|
348
|
|
349
|
List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
|
350
|
|
351
|
AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
|
352
|
drawingData.InsertSymbolName = insertSymbolName;
|
353
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
354
|
Editor editor = acDoc.Editor;
|
355
|
List<object> commandParam = new List<object>();
|
356
|
commandParam.Add("INSSYM");
|
357
|
|
358
|
// Insert Point
|
359
|
Point3d point = new Point3d(x, y, 0);
|
360
|
commandParam.Add(point);
|
361
|
|
362
|
// Check Insert Point
|
363
|
if (needAngle)
|
364
|
commandParam.Add(symbol.ANGLE);
|
365
|
|
366
|
// Check Balloon
|
367
|
if (needBalloon)
|
368
|
{
|
369
|
point = new Point3d(x + 20, y + 20, 0);
|
370
|
commandParam.Add(point);
|
371
|
|
372
|
// Check LeaderLine
|
373
|
if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
|
374
|
commandParam.Add(x + "," + y);
|
375
|
}
|
376
|
|
377
|
for (int i = 0; i < 8; i++)
|
378
|
commandParam.Add(null);
|
379
|
|
380
|
editor.Command(commandParam.ToArray());
|
381
|
|
382
|
List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
|
383
|
List<long> otherHandles = new List<long>();
|
384
|
foreach (var item in newHandles)
|
385
|
if (!prevHandles.Contains(item))
|
386
|
otherHandles.Add(item);
|
387
|
|
388
|
if (otherHandles.Count == 1)
|
389
|
return otherHandles[0];
|
390
|
else
|
391
|
return 0;
|
392
|
}
|
393
|
catch (System.Exception ex)
|
394
|
{
|
395
|
|
396
|
}
|
397
|
|
398
|
return handle;
|
399
|
}
|
400
|
#endregion
|
401
|
|
402
|
#region Autocad Utils
|
403
|
private List<long> GetAllBlockHandlesByName(string name)
|
404
|
{
|
405
|
List<long> handles = new List<long>();
|
406
|
|
407
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
408
|
Database acCurDb = acDoc.Database;
|
409
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
410
|
{
|
411
|
BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
|
412
|
foreach (var entry in gd)
|
413
|
{
|
414
|
BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
|
415
|
if (blockTableRecord != null)
|
416
|
{
|
417
|
IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
|
418
|
foreach (var item in records)
|
419
|
{
|
420
|
if (item.Name == name)
|
421
|
handles.Add(item.Handle.Value);
|
422
|
}
|
423
|
}
|
424
|
}
|
425
|
|
426
|
acTrans.Commit();
|
427
|
}
|
428
|
return handles;
|
429
|
}
|
430
|
private List<long> GetAllGroupHandles()
|
431
|
{
|
432
|
List<long> handles = new List<long>();
|
433
|
|
434
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
435
|
Database acCurDb = acDoc.Database;
|
436
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
437
|
{
|
438
|
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
|
439
|
foreach (var entry in gd)
|
440
|
handles.Add(entry.Value.Handle.Value);
|
441
|
acTrans.Commit();
|
442
|
}
|
443
|
return handles;
|
444
|
}
|
445
|
private ObjectId GetFirstPolyLine(long groupHandle)
|
446
|
{
|
447
|
ObjectId result = ObjectId.Null;
|
448
|
|
449
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
450
|
Database acCurDb = acDoc.Database;
|
451
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
452
|
{
|
453
|
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
|
454
|
foreach (var entry in gd)
|
455
|
{
|
456
|
if (entry.Value.Handle.Value == groupHandle)
|
457
|
{
|
458
|
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
|
459
|
foreach (var item in group.GetAllEntityIds())
|
460
|
{
|
461
|
if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
|
462
|
{
|
463
|
result = item;
|
464
|
break;
|
465
|
}
|
466
|
}
|
467
|
break;
|
468
|
}
|
469
|
}
|
470
|
acTrans.Commit();
|
471
|
}
|
472
|
|
473
|
return result;
|
474
|
}
|
475
|
private ObjectId GetObjectIdByHandle(long lHandle)
|
476
|
{
|
477
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
478
|
Database acCurDb = acDoc.Database;
|
479
|
Handle handle = new Handle(lHandle);
|
480
|
|
481
|
return acCurDb.GetObjectId(false, handle, 0);
|
482
|
}
|
483
|
private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
|
484
|
{
|
485
|
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
|
486
|
Database acCurDb = acDoc.Database;
|
487
|
Editor acDocEd = acDoc.Editor;
|
488
|
|
489
|
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
|
490
|
{
|
491
|
DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
|
492
|
if (objDB != null)
|
493
|
{
|
494
|
if (objDB.GetType() == typeof(BlockReference))
|
495
|
{
|
496
|
BlockReference block = objDB as BlockReference;
|
497
|
block.Rotation = rotation;
|
498
|
}
|
499
|
}
|
500
|
acTrans.Commit();
|
501
|
}
|
502
|
}
|
503
|
#endregion
|
504
|
|
505
|
#region Modeling Utils
|
506
|
private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
|
507
|
{
|
508
|
if (!group.Contains(line))
|
509
|
{
|
510
|
if (isInsert)
|
511
|
group.Insert(0, line);
|
512
|
else
|
513
|
group.Add(line);
|
514
|
|
515
|
if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
|
516
|
{
|
517
|
object connObj = line.CONNECTORS[0].ConnectedObject;
|
518
|
if (connObj.GetType() == typeof(Model.Line) &&
|
519
|
APIDUtils.IsConnectedLine(connObj as Model.Line, line))
|
520
|
GetConnectedGroupLine(connObj as Model.Line, group, true);
|
521
|
else if (connObj.GetType() == typeof(Model.Symbol))
|
522
|
{
|
523
|
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
|
524
|
if (connLine != null)
|
525
|
GetConnectedGroupLine(connLine as Model.Line, group, true);
|
526
|
}
|
527
|
}
|
528
|
if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
|
529
|
{
|
530
|
object connObj = line.CONNECTORS[1].ConnectedObject;
|
531
|
if (connObj.GetType() == typeof(Model.Line) &&
|
532
|
APIDUtils.IsConnectedLine(connObj as Model.Line, line))
|
533
|
GetConnectedGroupLine(connObj as Model.Line, group, false);
|
534
|
else if (connObj.GetType() == typeof(Model.Symbol))
|
535
|
{
|
536
|
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
|
537
|
if (connLine != null)
|
538
|
GetConnectedGroupLine(connLine as Model.Line, group, false);
|
539
|
}
|
540
|
}
|
541
|
}
|
542
|
}
|
543
|
private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
|
544
|
{
|
545
|
for (int i = 0; i < groupLine.Count; i++)
|
546
|
{
|
547
|
Model.Line line = groupLine[i];
|
548
|
if (i == 0)
|
549
|
{
|
550
|
object connObj = line.CONNECTORS[0].ConnectedObject;
|
551
|
if (connObj != null)
|
552
|
{
|
553
|
string point = GetPointByConnectedItem(connObj, line, true);
|
554
|
if (string.IsNullOrEmpty(point))
|
555
|
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
|
556
|
else
|
557
|
points.Add(point);
|
558
|
}
|
559
|
else
|
560
|
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
|
561
|
}
|
562
|
else
|
563
|
{
|
564
|
Model.Line prevLine = groupLine[i - 1];
|
565
|
if (line.SlopeType == SlopeType.HORIZONTAL &&
|
566
|
prevLine.SlopeType == SlopeType.VERTICAL)
|
567
|
points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
|
568
|
else if (line.SlopeType == SlopeType.VERTICAL &&
|
569
|
prevLine.SlopeType == SlopeType.HORIZONTAL)
|
570
|
points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
|
571
|
else
|
572
|
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
|
573
|
}
|
574
|
|
575
|
if (i == groupLine.Count - 1)
|
576
|
{
|
577
|
object connObj = line.CONNECTORS[1].ConnectedObject;
|
578
|
if (connObj != null && connObj.GetType() == typeof(Symbol))
|
579
|
{
|
580
|
string point = GetPointByConnectedItem(connObj, line, false);
|
581
|
if (string.IsNullOrEmpty(point))
|
582
|
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
|
583
|
else
|
584
|
points.Add(point);
|
585
|
}
|
586
|
else
|
587
|
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
|
588
|
}
|
589
|
}
|
590
|
}
|
591
|
private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
|
592
|
{
|
593
|
string result = string.Empty;
|
594
|
double x;
|
595
|
double y;
|
596
|
if (isStart)
|
597
|
{
|
598
|
x = line.Aveva.Start_X;
|
599
|
y = line.Aveva.Start_Y;
|
600
|
}
|
601
|
else
|
602
|
{
|
603
|
x = line.Aveva.End_X;
|
604
|
y = line.Aveva.End_Y;
|
605
|
}
|
606
|
|
607
|
if (connObj.GetType() == typeof(Symbol))
|
608
|
{
|
609
|
Symbol connSymbol = connObj as Symbol;
|
610
|
Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
|
611
|
|
612
|
SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
|
613
|
if (slopeType == SlopeType.HORIZONTAL)
|
614
|
result = connSymbol.Aveva.X + "," + y;
|
615
|
else if (slopeType == SlopeType.VERTICAL)
|
616
|
result = x + "," + connSymbol.Aveva.Y;
|
617
|
else
|
618
|
result = x + "," + y;
|
619
|
}
|
620
|
else if (connObj.GetType() == typeof(OPC))
|
621
|
{
|
622
|
OPC connOPC = connObj as OPC;
|
623
|
Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
|
624
|
|
625
|
double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
|
626
|
APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
|
627
|
APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
|
628
|
locX = locX + sizeWidth / 2;
|
629
|
locY = locY + sizeHeight / 2;
|
630
|
aX = locX;
|
631
|
aY = locY;
|
632
|
document.ConvertAvevaPoint(ref aX, ref aY);
|
633
|
|
634
|
SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
|
635
|
if (slopeType == SlopeType.HORIZONTAL)
|
636
|
result = aX + "," + y;
|
637
|
else if (slopeType == SlopeType.VERTICAL)
|
638
|
result = x + "," + aY;
|
639
|
else
|
640
|
result = x + "," + y;
|
641
|
}
|
642
|
|
643
|
return result;
|
644
|
}
|
645
|
private bool IsExistEndBreak(object item1, object item2)
|
646
|
{
|
647
|
bool result = false;
|
648
|
if (item1 != null && item2 != null)
|
649
|
{
|
650
|
EndBreak endBreak = document.EndBreaks.Find(x =>
|
651
|
(APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
|
652
|
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
|
653
|
(APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
|
654
|
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
|
655
|
|
656
|
if (endBreak != null)
|
657
|
result = true;
|
658
|
}
|
659
|
|
660
|
return result;
|
661
|
}
|
662
|
private LineRun FindLineRun(Model.Line line)
|
663
|
{
|
664
|
List<LineRun> allLineRun = new List<LineRun>();
|
665
|
foreach (var item in document.LINENUMBERS)
|
666
|
allLineRun.AddRange(item.RUNS);
|
667
|
foreach (var item in document.TRIMLINES)
|
668
|
allLineRun.AddRange(item.RUNS);
|
669
|
|
670
|
return allLineRun.Find(x => x.RUNITEMS.Contains(line));
|
671
|
}
|
672
|
private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
|
673
|
{
|
674
|
Model.Line result = null;
|
675
|
LineRun run = FindLineRun(line);
|
676
|
Connector connector = symbol.CONNECTORS.Find(x =>
|
677
|
x.ConnectedObject != null &&
|
678
|
x.ConnectedObject != line &&
|
679
|
run.RUNITEMS.Contains(x.ConnectedObject) &&
|
680
|
x.ConnectedObject.GetType() == typeof(Model.Line));
|
681
|
|
682
|
if (connector != null)
|
683
|
result = connector.ConnectedObject as Model.Line;
|
684
|
|
685
|
return result;
|
686
|
}
|
687
|
private bool CheckAngle(DataRow symbolRow)
|
688
|
{
|
689
|
return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
|
690
|
}
|
691
|
private bool CheckBalloon(Symbol symbol)
|
692
|
{
|
693
|
bool result = false;
|
694
|
|
695
|
string text = string.Empty;
|
696
|
Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
|
697
|
if (symbolDefinitions.Count != 0)
|
698
|
{
|
699
|
text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
|
700
|
|
701
|
if (text.Equals("VLP"))
|
702
|
text = "VPO";
|
703
|
else if (text.Equals("IVP"))
|
704
|
text = "IPO";
|
705
|
|
706
|
if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
|
707
|
result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
|
708
|
}
|
709
|
|
710
|
return result;
|
711
|
}
|
712
|
#endregion
|
713
|
|
714
|
#region Test Source
|
715
|
public void test()
|
716
|
{
|
717
|
//InitGUI();
|
718
|
//DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
|
719
|
|
720
|
DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
|
721
|
return;
|
722
|
Symbol symbol = new Symbol();
|
723
|
symbol.Aveva = new AvevaSymbolInfo();
|
724
|
symbol.Aveva.Name = "INVB";
|
725
|
symbol.Aveva.X = 50;
|
726
|
symbol.Aveva.Y = 100;
|
727
|
|
728
|
DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
|
729
|
if (allRows.Length == 1)
|
730
|
{
|
731
|
DataRow symbolRow = allRows[0];
|
732
|
bool bAngle = CheckAngle(symbolRow);
|
733
|
bool bBalloon = CheckBalloon(symbol);
|
734
|
bBalloon = false;
|
735
|
long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
|
736
|
}
|
737
|
|
738
|
Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
|
739
|
string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
|
740
|
if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
|
741
|
{
|
742
|
bool bIsPromptRotate = true;
|
743
|
}
|
744
|
int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
|
745
|
|
746
|
AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
|
747
|
bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
|
748
|
bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
|
749
|
|
750
|
//DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" });
|
751
|
|
752
|
//DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
|
753
|
}
|
754
|
public static void TESTStatic()
|
755
|
{
|
756
|
System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable();
|
757
|
AutoModeling auto = new AutoModeling(null, avevaSymbolTable);
|
758
|
auto.test();
|
759
|
}
|
760
|
#endregion
|
761
|
}
|
762
|
|
763
|
|
764
|
public static class AcDbLinqExtensionMethods
|
765
|
{
|
766
|
/// <summary>
|
767
|
/// Get all references to the given BlockTableRecord, including
|
768
|
/// references to anonymous dynamic BlockTableRecords.
|
769
|
/// </summary>
|
770
|
|
771
|
public static IEnumerable<BlockReference> GetBlockReferences(
|
772
|
this BlockTableRecord btr,
|
773
|
OpenMode mode = OpenMode.ForRead,
|
774
|
bool directOnly = true)
|
775
|
{
|
776
|
if (btr == null)
|
777
|
throw new ArgumentNullException("btr");
|
778
|
var tr = btr.Database.TransactionManager.TopTransaction;
|
779
|
if (tr == null)
|
780
|
throw new InvalidOperationException("No transaction");
|
781
|
var ids = btr.GetBlockReferenceIds(directOnly, true);
|
782
|
int cnt = ids.Count;
|
783
|
for (int i = 0; i < cnt; i++)
|
784
|
{
|
785
|
yield return (BlockReference)
|
786
|
tr.GetObject(ids[i], mode, false, false);
|
787
|
}
|
788
|
if (btr.IsDynamicBlock)
|
789
|
{
|
790
|
BlockTableRecord btr2 = null;
|
791
|
var blockIds = btr.GetAnonymousBlockIds();
|
792
|
cnt = blockIds.Count;
|
793
|
for (int i = 0; i < cnt; i++)
|
794
|
{
|
795
|
btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
|
796
|
OpenMode.ForRead, false, false);
|
797
|
ids = btr2.GetBlockReferenceIds(directOnly, true);
|
798
|
int cnt2 = ids.Count;
|
799
|
for (int j = 0; j < cnt2; j++)
|
800
|
{
|
801
|
yield return (BlockReference)
|
802
|
tr.GetObject(ids[j], mode, false, false);
|
803
|
}
|
804
|
}
|
805
|
}
|
806
|
}
|
807
|
}
|
808
|
}
|