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