hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 6503fec8
이력 | 보기 | 이력해설 | 다운로드 (36.4 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 |
|
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, 5); |
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 |
RunNoteModeling(); |
114 |
} |
115 |
catch (System.Exception ex) |
116 |
{ |
117 |
System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace); |
118 |
} |
119 |
finally |
120 |
{ |
121 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.ClearParent, null); |
122 |
SplashScreenManager.CloseForm(false); |
123 |
} |
124 |
} |
125 |
|
126 |
#region Run Method |
127 |
private void RunLineModeling() |
128 |
{ |
129 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count); |
130 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling"); |
131 |
foreach (var item in document.LINES) |
132 |
{ |
133 |
if (item.Aveva.Handle == 0) |
134 |
LineModeling(item); |
135 |
|
136 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
137 |
} |
138 |
} |
139 |
private void RunOPCModeling() |
140 |
{ |
141 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count); |
142 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling"); |
143 |
foreach (var item in document.OPCs) |
144 |
{ |
145 |
if (item.Aveva.Handle == 0) |
146 |
SymbolModeling(item); |
147 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
148 |
} |
149 |
} |
150 |
private void RunSymbolModeling() |
151 |
{ |
152 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count); |
153 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling"); |
154 |
foreach (var item in document.SYMBOLS) |
155 |
{ |
156 |
if (item.Aveva.Handle == 0) |
157 |
SymbolModeling(item); |
158 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
159 |
} |
160 |
} |
161 |
private void RunTextModeling() |
162 |
{ |
163 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Texts.Count); |
164 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Text Modeling"); |
165 |
foreach (var item in document.Texts) |
166 |
{ |
167 |
TextModeling(item); |
168 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
169 |
} |
170 |
} |
171 |
private void RunNoteModeling() |
172 |
{ |
173 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Notes.Count); |
174 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Note Modeling"); |
175 |
foreach (var item in document.Texts) |
176 |
{ |
177 |
TextModeling(item); |
178 |
SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
179 |
} |
180 |
} |
181 |
#endregion |
182 |
|
183 |
#region Modeling Method |
184 |
private void LineModeling(Model.Line line) |
185 |
{ |
186 |
List<Model.Line> groupLine = new List<Model.Line>(); |
187 |
List<string> points = new List<string>(); |
188 |
GetConnectedGroupLine(line, groupLine, false); |
189 |
GetGroupLinePoints(groupLine, points); |
190 |
|
191 |
LineNumber lineNumber = null; |
192 |
if (line.Aveva.Type == Model.Type.Pipe) |
193 |
{ |
194 |
foreach (var item in groupLine) |
195 |
{ |
196 |
lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID); |
197 |
if (lineNumber != null) |
198 |
{ |
199 |
GUIUtils.SetAutoLabelCheck(true); |
200 |
break; |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
|
206 |
long handle = 0; |
207 |
if (line.Aveva.Type == Model.Type.Pipe) |
208 |
handle = DrawPipe(line.Aveva.Name, points, lineNumber); |
209 |
else if (line.Aveva.Type == Model.Type.Signal) |
210 |
handle = DrawSignal(line.Aveva.Name, points); |
211 |
|
212 |
if (lineNumber != null) |
213 |
GUIUtils.SetAutoLabelCheck(false); |
214 |
|
215 |
foreach (var item in groupLine) |
216 |
item.Aveva.Handle = handle; |
217 |
} |
218 |
private void SymbolModeling(Symbol symbol) |
219 |
{ |
220 |
DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name)); |
221 |
if (allRows.Length == 1) |
222 |
{ |
223 |
DataRow symbolRow = allRows[0]; |
224 |
bool bAngle = CheckAngle(symbolRow); |
225 |
bool bBalloon = CheckBalloon(symbol); |
226 |
|
227 |
long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon); |
228 |
if (handle != 0) |
229 |
{ |
230 |
symbol.Aveva.Handle = handle; |
231 |
|
232 |
if (!bAngle) |
233 |
{ |
234 |
ObjectId objectId = GetObjectIdByHandle(handle); |
235 |
if (objectId != ObjectId.Null) |
236 |
{ |
237 |
SetBlockReferenceRotation(objectId, symbol.ANGLE); |
238 |
} |
239 |
} |
240 |
} |
241 |
} |
242 |
} |
243 |
private void TextModeling(Text text) |
244 |
{ |
245 |
if (text.TextAngle == TextAngle.None) |
246 |
return; |
247 |
|
248 |
if (text.Aveva.LabelType == LabelType.SingleText) |
249 |
DrawText(text.Aveva.X, text.Aveva.Y, text.Aveva.Height, text.VALUE); |
250 |
else if (text.Aveva.LabelType == LabelType.MultiText) |
251 |
DrawMultiLineText(text.Aveva.X, text.Aveva.Y, text.Aveva.Width, text.VALUE); |
252 |
|
253 |
} |
254 |
private void NoteModeling(Model.Note note) |
255 |
{ |
256 |
if (note.TextAngle == TextAngle.None) |
257 |
return; |
258 |
|
259 |
if (note.Aveva.LabelType == LabelType.SingleNote) |
260 |
DrawText(note.Aveva.X, note.Aveva.Y, note.Aveva.Height, note.VALUE); |
261 |
else if (note.Aveva.LabelType == LabelType.MultiNote) |
262 |
DrawMultiLineText(note.Aveva.X, note.Aveva.Y, note.Aveva.Width, note.VALUE); |
263 |
|
264 |
} |
265 |
#endregion |
266 |
|
267 |
#region Drawing Method |
268 |
private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null) |
269 |
{ |
270 |
List<long> prevHandles = GetAllGroupHandles(); |
271 |
|
272 |
GUIUtils.SetPipeStyle(style); |
273 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
274 |
Editor editor = acDoc.Editor; |
275 |
List<object> commandParam = new List<object>(); |
276 |
//command name |
277 |
commandParam.Add("DRPIPE"); |
278 |
//coordinate |
279 |
foreach (var item in coordinates) |
280 |
commandParam.Add(item); |
281 |
|
282 |
//enter Parent(null) |
283 |
commandParam.Add(null); |
284 |
|
285 |
if (lineNumber != null) |
286 |
{ |
287 |
Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0); |
288 |
commandParam.Add(point); |
289 |
commandParam.Add(lineNumber.Aveva.Angle); |
290 |
|
291 |
commandParam.Add(null); |
292 |
} |
293 |
else |
294 |
commandParam.Add(null); |
295 |
|
296 |
editor.Command(commandParam.ToArray()); |
297 |
|
298 |
List<long> newHandles = GetAllGroupHandles(); |
299 |
List<long> otherHandles = new List<long>(); |
300 |
foreach (var item in newHandles) |
301 |
if (!prevHandles.Contains(item)) |
302 |
otherHandles.Add(item); |
303 |
|
304 |
if (otherHandles.Count == 1) |
305 |
return otherHandles[0]; |
306 |
else |
307 |
return 0; |
308 |
} |
309 |
private long DrawPipe(string style, List<string> coordinates, ObjectId objectId) |
310 |
{ |
311 |
List<long> prevHandles = GetAllGroupHandles(); |
312 |
GUIUtils.SetPipeStyle(style); |
313 |
|
314 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
315 |
Editor editor = acDoc.Editor; |
316 |
List<object> commandParam = new List<object>(); |
317 |
//command name |
318 |
commandParam.Add("DRPIPE"); |
319 |
//coordinate |
320 |
foreach (var item in coordinates) |
321 |
commandParam.Add(item); |
322 |
//enter |
323 |
commandParam.Add(null); |
324 |
|
325 |
//Input Object ID |
326 |
commandParam.Add(objectId); |
327 |
|
328 |
//enter |
329 |
commandParam.Add(null); |
330 |
|
331 |
editor.Command(commandParam.ToArray()); |
332 |
|
333 |
List<long> newHandles = GetAllGroupHandles(); |
334 |
List<long> otherHandles = new List<long>(); |
335 |
foreach (var item in newHandles) |
336 |
if (!prevHandles.Contains(item)) |
337 |
otherHandles.Add(item); |
338 |
|
339 |
if (otherHandles.Count == 1) |
340 |
return otherHandles[0]; |
341 |
else |
342 |
return 0; |
343 |
} |
344 |
private long DrawSignal(string style, List<string> coordinates) |
345 |
{ |
346 |
List<long> prevHandles = GetAllGroupHandles(); |
347 |
|
348 |
GUIUtils.SetPipeStyle(style); |
349 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
350 |
Editor editor = acDoc.Editor; |
351 |
List<object> commandParam = new List<object>(); |
352 |
//command name |
353 |
commandParam.Add("VPESIGNAL"); |
354 |
//coordinate |
355 |
foreach (var item in coordinates) |
356 |
commandParam.Add(item); |
357 |
//enter |
358 |
commandParam.Add(null); |
359 |
|
360 |
editor.Command(commandParam.ToArray()); |
361 |
|
362 |
List<long> newHandles = GetAllGroupHandles(); |
363 |
List<long> otherHandles = new List<long>(); |
364 |
foreach (var item in newHandles) |
365 |
if (!prevHandles.Contains(item)) |
366 |
otherHandles.Add(item); |
367 |
|
368 |
if (otherHandles.Count == 1) |
369 |
return otherHandles[0]; |
370 |
else |
371 |
return 0; |
372 |
} |
373 |
private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon) |
374 |
{ |
375 |
long handle = 0; |
376 |
try |
377 |
{ |
378 |
string insertSymbolName = symbol.Aveva.Name; |
379 |
double x = symbol.Aveva.X; |
380 |
double y = symbol.Aveva.Y; |
381 |
|
382 |
List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName); |
383 |
|
384 |
AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData(); |
385 |
drawingData.InsertSymbolName = insertSymbolName; |
386 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
387 |
Editor editor = acDoc.Editor; |
388 |
List<object> commandParam = new List<object>(); |
389 |
commandParam.Add("INSSYM"); |
390 |
|
391 |
// Insert Point |
392 |
Point3d point = new Point3d(x, y, 0); |
393 |
commandParam.Add(point); |
394 |
|
395 |
// Check Insert Point |
396 |
if (needAngle) |
397 |
commandParam.Add(symbol.ANGLE); |
398 |
|
399 |
// Check Balloon |
400 |
if (needBalloon) |
401 |
{ |
402 |
point = new Point3d(x + 20, y + 20, 0); |
403 |
commandParam.Add(point); |
404 |
|
405 |
// Check LeaderLine |
406 |
if (GraphicalUtility.IsLeaderReqOnSymbolBalloons()) |
407 |
commandParam.Add(x + "," + y); |
408 |
} |
409 |
|
410 |
for (int i = 0; i < 8; i++) |
411 |
commandParam.Add(null); |
412 |
|
413 |
editor.Command(commandParam.ToArray()); |
414 |
|
415 |
List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName); |
416 |
List<long> otherHandles = new List<long>(); |
417 |
foreach (var item in newHandles) |
418 |
if (!prevHandles.Contains(item)) |
419 |
otherHandles.Add(item); |
420 |
|
421 |
if (otherHandles.Count == 1) |
422 |
return otherHandles[0]; |
423 |
else |
424 |
return 0; |
425 |
} |
426 |
catch (System.Exception ex) |
427 |
{ |
428 |
|
429 |
} |
430 |
|
431 |
return handle; |
432 |
} |
433 |
private void DrawText(double x, double y, double height, string text) |
434 |
{ |
435 |
// Get the current document and database |
436 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
437 |
Database acCurDb = acDoc.Database; |
438 |
|
439 |
// Start a transaction |
440 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
441 |
{ |
442 |
// Open the Block table for read |
443 |
BlockTable acBlkTbl; |
444 |
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, |
445 |
OpenMode.ForRead) as BlockTable; |
446 |
|
447 |
// Open the Block table record Model space for write |
448 |
BlockTableRecord acBlkTblRec; |
449 |
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], |
450 |
OpenMode.ForWrite) as BlockTableRecord; |
451 |
|
452 |
// Create a single-line text object |
453 |
using (DBText acText = new DBText()) |
454 |
{ |
455 |
acText.Position = new Point3d(x, y, 0); |
456 |
acText.Height = height; |
457 |
acText.TextString = text; |
458 |
|
459 |
acBlkTblRec.AppendEntity(acText); |
460 |
acTrans.AddNewlyCreatedDBObject(acText, true); |
461 |
} |
462 |
|
463 |
// Save the changes and dispose of the transaction |
464 |
acTrans.Commit(); |
465 |
} |
466 |
} |
467 |
private void DrawMultiLineText(double x, double y, double width, string text) |
468 |
{ |
469 |
// Get the current document and database |
470 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
471 |
Database acCurDb = acDoc.Database; |
472 |
|
473 |
// Start a transaction |
474 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
475 |
{ |
476 |
// Open the Block table for read |
477 |
BlockTable acBlkTbl; |
478 |
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, |
479 |
OpenMode.ForRead) as BlockTable; |
480 |
|
481 |
// Open the Block table record Model space for write |
482 |
BlockTableRecord acBlkTblRec; |
483 |
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], |
484 |
OpenMode.ForWrite) as BlockTableRecord; |
485 |
|
486 |
// Create a multiline text object |
487 |
using (MText acMText = new MText()) |
488 |
{ |
489 |
acMText.Location = new Point3d(x, y, 0); |
490 |
acMText.Width = width; |
491 |
acMText.Contents = text; |
492 |
|
493 |
acBlkTblRec.AppendEntity(acMText); |
494 |
acTrans.AddNewlyCreatedDBObject(acMText, true); |
495 |
} |
496 |
|
497 |
// Save the changes and dispose of the transaction |
498 |
acTrans.Commit(); |
499 |
} |
500 |
} |
501 |
#endregion |
502 |
|
503 |
#region Autocad Utils |
504 |
private List<long> GetAllBlockHandlesByName(string name) |
505 |
{ |
506 |
List<long> handles = new List<long>(); |
507 |
|
508 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
509 |
Database acCurDb = acDoc.Database; |
510 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
511 |
{ |
512 |
BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead); |
513 |
foreach (var entry in gd) |
514 |
{ |
515 |
BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord; |
516 |
if (blockTableRecord != null) |
517 |
{ |
518 |
IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord); |
519 |
foreach (var item in records) |
520 |
{ |
521 |
if (item.Name == name) |
522 |
handles.Add(item.Handle.Value); |
523 |
} |
524 |
} |
525 |
} |
526 |
|
527 |
acTrans.Commit(); |
528 |
} |
529 |
return handles; |
530 |
} |
531 |
private List<long> GetAllGroupHandles() |
532 |
{ |
533 |
List<long> handles = new List<long>(); |
534 |
|
535 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
536 |
Database acCurDb = acDoc.Database; |
537 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
538 |
{ |
539 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
540 |
foreach (var entry in gd) |
541 |
handles.Add(entry.Value.Handle.Value); |
542 |
acTrans.Commit(); |
543 |
} |
544 |
return handles; |
545 |
} |
546 |
private ObjectId GetFirstPolyLine(long groupHandle) |
547 |
{ |
548 |
ObjectId result = ObjectId.Null; |
549 |
|
550 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
551 |
Database acCurDb = acDoc.Database; |
552 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
553 |
{ |
554 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
555 |
foreach (var entry in gd) |
556 |
{ |
557 |
if (entry.Value.Handle.Value == groupHandle) |
558 |
{ |
559 |
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group; |
560 |
foreach (var item in group.GetAllEntityIds()) |
561 |
{ |
562 |
if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)) |
563 |
{ |
564 |
result = item; |
565 |
break; |
566 |
} |
567 |
} |
568 |
break; |
569 |
} |
570 |
} |
571 |
acTrans.Commit(); |
572 |
} |
573 |
|
574 |
return result; |
575 |
} |
576 |
private ObjectId GetObjectIdByHandle(long lHandle) |
577 |
{ |
578 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
579 |
Database acCurDb = acDoc.Database; |
580 |
Handle handle = new Handle(lHandle); |
581 |
|
582 |
return acCurDb.GetObjectId(false, handle, 0); |
583 |
} |
584 |
private void SetBlockReferenceRotation(ObjectId objectId, double rotation) |
585 |
{ |
586 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
587 |
Database acCurDb = acDoc.Database; |
588 |
Editor acDocEd = acDoc.Editor; |
589 |
|
590 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
591 |
{ |
592 |
DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite); |
593 |
if (objDB != null) |
594 |
{ |
595 |
if (objDB.GetType() == typeof(BlockReference)) |
596 |
{ |
597 |
BlockReference block = objDB as BlockReference; |
598 |
block.Rotation = rotation; |
599 |
} |
600 |
} |
601 |
acTrans.Commit(); |
602 |
} |
603 |
} |
604 |
#endregion |
605 |
|
606 |
#region Modeling Utils |
607 |
private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert) |
608 |
{ |
609 |
if (!group.Contains(line)) |
610 |
{ |
611 |
if (isInsert) |
612 |
group.Insert(0, line); |
613 |
else |
614 |
group.Add(line); |
615 |
|
616 |
if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject)) |
617 |
{ |
618 |
object connObj = line.CONNECTORS[0].ConnectedObject; |
619 |
if (connObj.GetType() == typeof(Model.Line) && |
620 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
621 |
GetConnectedGroupLine(connObj as Model.Line, group, true); |
622 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
623 |
{ |
624 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
625 |
if (connLine != null) |
626 |
GetConnectedGroupLine(connLine as Model.Line, group, true); |
627 |
} |
628 |
} |
629 |
if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject)) |
630 |
{ |
631 |
object connObj = line.CONNECTORS[1].ConnectedObject; |
632 |
if (connObj.GetType() == typeof(Model.Line) && |
633 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
634 |
GetConnectedGroupLine(connObj as Model.Line, group, false); |
635 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
636 |
{ |
637 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
638 |
if (connLine != null) |
639 |
GetConnectedGroupLine(connLine as Model.Line, group, false); |
640 |
} |
641 |
} |
642 |
} |
643 |
} |
644 |
private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points) |
645 |
{ |
646 |
for (int i = 0; i < groupLine.Count; i++) |
647 |
{ |
648 |
Model.Line line = groupLine[i]; |
649 |
if (i == 0) |
650 |
{ |
651 |
object connObj = line.CONNECTORS[0].ConnectedObject; |
652 |
if (connObj != null) |
653 |
{ |
654 |
string point = GetPointByConnectedItem(connObj, line, true); |
655 |
if (string.IsNullOrEmpty(point)) |
656 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
657 |
else |
658 |
points.Add(point); |
659 |
} |
660 |
else |
661 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
662 |
} |
663 |
else |
664 |
{ |
665 |
Model.Line prevLine = groupLine[i - 1]; |
666 |
if (line.SlopeType == SlopeType.HORIZONTAL && |
667 |
prevLine.SlopeType == SlopeType.VERTICAL) |
668 |
points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y); |
669 |
else if (line.SlopeType == SlopeType.VERTICAL && |
670 |
prevLine.SlopeType == SlopeType.HORIZONTAL) |
671 |
points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y); |
672 |
else |
673 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
674 |
} |
675 |
|
676 |
if (i == groupLine.Count - 1) |
677 |
{ |
678 |
object connObj = line.CONNECTORS[1].ConnectedObject; |
679 |
if (connObj != null && connObj.GetType() == typeof(Symbol)) |
680 |
{ |
681 |
string point = GetPointByConnectedItem(connObj, line, false); |
682 |
if (string.IsNullOrEmpty(point)) |
683 |
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
684 |
else |
685 |
points.Add(point); |
686 |
} |
687 |
else |
688 |
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
689 |
} |
690 |
} |
691 |
} |
692 |
private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart) |
693 |
{ |
694 |
string result = string.Empty; |
695 |
double x; |
696 |
double y; |
697 |
if (isStart) |
698 |
{ |
699 |
x = line.Aveva.Start_X; |
700 |
y = line.Aveva.Start_Y; |
701 |
} |
702 |
else |
703 |
{ |
704 |
x = line.Aveva.End_X; |
705 |
y = line.Aveva.End_Y; |
706 |
} |
707 |
|
708 |
if (connObj.GetType() == typeof(Symbol)) |
709 |
{ |
710 |
Symbol connSymbol = connObj as Symbol; |
711 |
Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line); |
712 |
|
713 |
SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y); |
714 |
if (slopeType == SlopeType.HORIZONTAL) |
715 |
result = connSymbol.Aveva.X + "," + y; |
716 |
else if (slopeType == SlopeType.VERTICAL) |
717 |
result = x + "," + connSymbol.Aveva.Y; |
718 |
else |
719 |
result = x + "," + y; |
720 |
} |
721 |
else if (connObj.GetType() == typeof(OPC)) |
722 |
{ |
723 |
OPC connOPC = connObj as OPC; |
724 |
Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line); |
725 |
|
726 |
double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0; |
727 |
APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY); |
728 |
APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight); |
729 |
locX = locX + sizeWidth / 2; |
730 |
locY = locY + sizeHeight / 2; |
731 |
aX = locX; |
732 |
aY = locY; |
733 |
document.ConvertAvevaPoint(ref aX, ref aY); |
734 |
|
735 |
SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y); |
736 |
if (slopeType == SlopeType.HORIZONTAL) |
737 |
result = aX + "," + y; |
738 |
else if (slopeType == SlopeType.VERTICAL) |
739 |
result = x + "," + aY; |
740 |
else |
741 |
result = x + "," + y; |
742 |
} |
743 |
|
744 |
return result; |
745 |
} |
746 |
private bool IsExistEndBreak(object item1, object item2) |
747 |
{ |
748 |
bool result = false; |
749 |
if (item1 != null && item2 != null) |
750 |
{ |
751 |
EndBreak endBreak = document.EndBreaks.Find(x => |
752 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item1 && |
753 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) || |
754 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item2 && |
755 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1)); |
756 |
|
757 |
if (endBreak != null) |
758 |
result = true; |
759 |
} |
760 |
|
761 |
return result; |
762 |
} |
763 |
private LineRun FindLineRun(Model.Line line) |
764 |
{ |
765 |
List<LineRun> allLineRun = new List<LineRun>(); |
766 |
foreach (var item in document.LINENUMBERS) |
767 |
allLineRun.AddRange(item.RUNS); |
768 |
foreach (var item in document.TRIMLINES) |
769 |
allLineRun.AddRange(item.RUNS); |
770 |
|
771 |
return allLineRun.Find(x => x.RUNITEMS.Contains(line)); |
772 |
} |
773 |
private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol) |
774 |
{ |
775 |
Model.Line result = null; |
776 |
LineRun run = FindLineRun(line); |
777 |
Connector connector = symbol.CONNECTORS.Find(x => |
778 |
x.ConnectedObject != null && |
779 |
x.ConnectedObject != line && |
780 |
run.RUNITEMS.Contains(x.ConnectedObject) && |
781 |
x.ConnectedObject.GetType() == typeof(Model.Line)); |
782 |
|
783 |
if (connector != null) |
784 |
result = connector.ConnectedObject as Model.Line; |
785 |
|
786 |
return result; |
787 |
} |
788 |
private bool CheckAngle(DataRow symbolRow) |
789 |
{ |
790 |
return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2); |
791 |
} |
792 |
private bool CheckBalloon(Symbol symbol) |
793 |
{ |
794 |
bool result = false; |
795 |
|
796 |
string text = string.Empty; |
797 |
Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name); |
798 |
if (symbolDefinitions.Count != 0) |
799 |
{ |
800 |
text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper(); |
801 |
|
802 |
if (text.Equals("VLP")) |
803 |
text = "VPO"; |
804 |
else if (text.Equals("IVP")) |
805 |
text = "IPO"; |
806 |
|
807 |
if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text)) |
808 |
result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId); |
809 |
} |
810 |
|
811 |
return result; |
812 |
} |
813 |
#endregion |
814 |
|
815 |
#region Test Source |
816 |
public void test() |
817 |
{ |
818 |
//InitGUI(); |
819 |
//DrawSignal("SONIC", new List<string>() { "2,100", "100,100" }); |
820 |
|
821 |
DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null); |
822 |
return; |
823 |
Symbol symbol = new Symbol(); |
824 |
symbol.Aveva = new AvevaSymbolInfo(); |
825 |
symbol.Aveva.Name = "INVB"; |
826 |
symbol.Aveva.X = 50; |
827 |
symbol.Aveva.Y = 100; |
828 |
|
829 |
DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name)); |
830 |
if (allRows.Length == 1) |
831 |
{ |
832 |
DataRow symbolRow = allRows[0]; |
833 |
bool bAngle = CheckAngle(symbolRow); |
834 |
bool bBalloon = CheckBalloon(symbol); |
835 |
bBalloon = false; |
836 |
long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon); |
837 |
} |
838 |
|
839 |
Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name); |
840 |
string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper(); |
841 |
if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2)) |
842 |
{ |
843 |
bool bIsPromptRotate = true; |
844 |
} |
845 |
int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString()); |
846 |
|
847 |
AssociateSymbolType eAssosiateSym = AssociateSymbolType.None; |
848 |
bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym); |
849 |
bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text); |
850 |
|
851 |
//DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" }); |
852 |
|
853 |
//DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" }); |
854 |
} |
855 |
public static void TESTStatic() |
856 |
{ |
857 |
System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable(); |
858 |
AutoModeling auto = new AutoModeling(null, avevaSymbolTable); |
859 |
auto.test(); |
860 |
} |
861 |
#endregion |
862 |
} |
863 |
|
864 |
|
865 |
public static class AcDbLinqExtensionMethods |
866 |
{ |
867 |
/// <summary> |
868 |
/// Get all references to the given BlockTableRecord, including |
869 |
/// references to anonymous dynamic BlockTableRecords. |
870 |
/// </summary> |
871 |
|
872 |
public static IEnumerable<BlockReference> GetBlockReferences( |
873 |
this BlockTableRecord btr, |
874 |
OpenMode mode = OpenMode.ForRead, |
875 |
bool directOnly = true) |
876 |
{ |
877 |
if (btr == null) |
878 |
throw new ArgumentNullException("btr"); |
879 |
var tr = btr.Database.TransactionManager.TopTransaction; |
880 |
if (tr == null) |
881 |
throw new InvalidOperationException("No transaction"); |
882 |
var ids = btr.GetBlockReferenceIds(directOnly, true); |
883 |
int cnt = ids.Count; |
884 |
for (int i = 0; i < cnt; i++) |
885 |
{ |
886 |
yield return (BlockReference) |
887 |
tr.GetObject(ids[i], mode, false, false); |
888 |
} |
889 |
if (btr.IsDynamicBlock) |
890 |
{ |
891 |
BlockTableRecord btr2 = null; |
892 |
var blockIds = btr.GetAnonymousBlockIds(); |
893 |
cnt = blockIds.Count; |
894 |
for (int i = 0; i < cnt; i++) |
895 |
{ |
896 |
btr2 = (BlockTableRecord)tr.GetObject(blockIds[i], |
897 |
OpenMode.ForRead, false, false); |
898 |
ids = btr2.GetBlockReferenceIds(directOnly, true); |
899 |
int cnt2 = ids.Count; |
900 |
for (int j = 0; j < cnt2; j++) |
901 |
{ |
902 |
yield return (BlockReference) |
903 |
tr.GetObject(ids[j], mode, false, false); |
904 |
} |
905 |
} |
906 |
} |
907 |
} |
908 |
} |
909 |
} |