프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 4328cb06

이력 | 보기 | 이력해설 | 다운로드 (41.8 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Collections;
4
using System.Configuration;
5
using System.Data;
6
using System.Data.Common;
7
using System.Data.SqlClient;
8
using System.IO;
9
using System.Runtime.CompilerServices;
10
using Microsoft.VisualBasic.CompilerServices;
11

    
12
using Autodesk.AutoCAD.ApplicationServices;
13
using Autodesk.AutoCAD.ApplicationServices.Core;
14
using Autodesk.AutoCAD.DatabaseServices;
15
using Autodesk.AutoCAD.EditorInput;
16
using Autodesk.AutoCAD.Geometry;
17
using Autodesk.AutoCAD.Interop;
18
using Autodesk.AutoCAD.Interop.Common;
19
using Autodesk.AutoCAD.Runtime;
20
using Autodesk.AutoCAD.Windows;
21

    
22
using AVEVA.PID.Components;
23
using AVEVA.PID.GUI;
24
using AVEVA.PID.Common;
25
using AVEVA.PID.DWGSelector;
26
using AVEVA.PID.Utilities;
27

    
28
using AVEVA.PID.CustomizationUtility.DB;
29
using AVEVA.PID.CustomizationUtility.Model;
30
using AVEVA.PID.CustomizationUtility.Properties;
31

    
32
using DevExpress.XtraSplashScreen;
33

    
34
namespace AVEVA.PID.CustomizationUtility
35
{
36
    public class AutoModeling
37
    {
38
        public static AutoModeling Running { get; set; }
39
        Model.Document document;
40
        AvevaInfo avevaInfo;
41

    
42
        private void SetConvertRule()
43
        {
44
            #region OPC Setting
45
            document.OPCs.Sort(SortOPC);
46
            int SortOPC(OPC a, OPC b)
47
            {
48
                if (a.FlowType == FlowType.In)
49
                    return 1;
50
                else
51
                    return 0;
52
            }
53
            #endregion
54
        }
55

    
56
        public AutoModeling(Model.Document document)
57
        {
58
            this.document = document;
59
            avevaInfo = AvevaInfo.GetInstance();
60
        }
61

    
62
        public void CreateDrawing()
63
        {
64
            string outPath = Project_DB.GetDirectiveValue("DRGPTH");
65
            string tempPath = Path.GetTempPath();
66
            string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT");
67

    
68
            DWTSelector.strDrawingNumber = document.AvevaDrawingNumber;
69
            DWTSelector.strSheetNumber = document.AvevaSheetNumber;
70
            DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber;
71
            DWTSelector.strCadFilePath = outPath;
72

    
73
            try
74
            {
75
                string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false);
76
                Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
77
                Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text);
78

    
79
                Commands.addLatestPidvesrionToDictionary();
80
                AVVPropCmd.ActivateAcad();
81

    
82
                if (acadDocument != null)
83
                {
84
                    Running = this;
85
                    acadApplication.ActiveDocument = acadDocument;
86
                    acadDocument.SendCommand("QSAVE ");
87
                    acadDocument.SendCommand("APPIDRun ");
88
                    acadDocument.SendCommand("QSAVE ");
89
                    Running = null;
90
                }
91
            }
92
            catch (System.Exception ex)
93
            {
94
                
95
            }
96

    
97
            GC.Collect();
98
        }
99
        public void Run()
100
        {
101
            try
102
            {
103
                SplashScreenManager.ShowForm(typeof(APIDSplashScreen), true, true);
104
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllStepCount, 8);
105
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetDocumentName, document.AvevaDrawingNumber + document.AvevaSheetNumber);
106
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetParent, Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
107

    
108
                SetConvertRule();
109

    
110
                RunLineModeling();
111
                RunOPCModeling();
112
                RunSymbolModeling();
113
                RunTextModeling();
114
                RunNoteModeling();
115
                RunInputLineAttribute();
116
                RunInputLineNumberAttribute();
117
                RunInputSymbolAttribute();
118
            }
119
            catch (System.Exception ex)
120
            {
121
                System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace);
122
            }
123
            finally
124
            {
125
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.ClearParent, null);
126
                SplashScreenManager.CloseForm(false);
127
            }
128
        }
129

    
130
        #region Run Method
131
        private void RunLineModeling()
132
        {
133
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
134
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling");
135
            foreach (var item in document.LINES)
136
            {
137
                if (item.Aveva.Handle == 0)
138
                    LineModeling(item);
139

    
140
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
141
            }
142
        }
143
        private void RunOPCModeling()
144
        {
145
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count);
146
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling");
147
            foreach (var item in document.OPCs)
148
            {
149
                if (item.Aveva.Handle == 0)
150
                    SymbolModeling(item);
151
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
152
            }
153
        }
154
        private void RunSymbolModeling()
155
        {
156
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
157
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
158
            foreach (var item in document.SYMBOLS)
159
            {
160
                if (item.Aveva.Handle == 0)
161
                    SymbolModeling(item);
162
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
163
            }
164
        }
165
        private void RunTextModeling()
166
        {
167
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Texts.Count);
168
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Text Modeling");
169
            foreach (var item in document.Texts)
170
            {
171
                TextModeling(item);
172
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
173
            }
174
        }
175
        private void RunNoteModeling()
176
        {
177
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Notes.Count);
178
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Note Modeling");
179
            foreach (var item in document.Notes)
180
            {
181
                NoteModeling(item);
182
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
183
            }
184
        }
185
        private void RunInputLineAttribute()
186
        {
187
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
188
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Input Line Attribute");
189
            foreach (var item in document.LINES)
190
            {
191
                SetLineAttribute(item);
192
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
193
            }
194
        }
195
        private void RunInputSymbolAttribute()
196
        {
197
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
198
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Input Symbol Attribute");
199
            foreach (var item in document.SYMBOLS)
200
            {
201
                SetSymbolAttribute(item);
202
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
203
            }
204
        }
205
        private void RunInputLineNumberAttribute()
206
        {
207
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
208
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Input LineNumber Attribute");
209
            foreach (var item in document.LINENUMBERS)
210
            {
211
                SetLineNumberAttribute(item);
212
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
213
            }
214
        }
215
        #endregion
216

    
217
        #region Modeling Method
218
        private void LineModeling(Model.Line line)
219
        {
220
            List<Model.Line> groupLine = new List<Model.Line>();
221
            List<string> points = new List<string>();
222
            GetConnectedGroupLine(line, groupLine, false);
223
            GetGroupLinePoints(groupLine, points);
224

    
225
            LineNumber lineNumber = null;
226
            if (line.Aveva.Type == Model.Type.Pipe)
227
            {
228
                foreach (var item in groupLine)
229
                {
230
                    lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID);
231
                    if (lineNumber != null)
232
                    {
233
                        GUIUtils.SetAutoLabelCheck(true);
234
                        break;
235
                    }
236
                }
237
            }
238

    
239

    
240
            long handle = 0;
241
            if (line.Aveva.Type == Model.Type.Pipe)
242
                handle = DrawPipe(line.Aveva.Name, points, lineNumber);
243
            else if (line.Aveva.Type == Model.Type.Signal)
244
                handle = DrawSignal(line.Aveva.Name, points);
245

    
246
            if (lineNumber != null)
247
                GUIUtils.SetAutoLabelCheck(false);
248

    
249
            foreach (var item in groupLine)
250
                item.Aveva.Handle = handle;
251
        }
252
        private void SymbolModeling(Symbol symbol)
253
        {
254
            DataRow[] allRows = avevaInfo.AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
255
            if (allRows.Length == 1)
256
            {
257
                DataRow symbolRow = allRows[0];
258
                bool bAngle = CheckAngle(symbolRow);
259
                bool bBalloon = CheckBalloon(symbol);
260

    
261
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
262
                if (handle != 0)
263
                {
264
                    symbol.Aveva.Handle = handle;
265

    
266
                    if (!bAngle)
267
                    {
268
                        ObjectId objectId = GetObjectIdByHandle(handle);
269
                        if (objectId != ObjectId.Null)
270
                        {
271
                            SetBlockReferenceRotation(objectId, symbol.ANGLE);
272
                        }
273
                    }
274
                }
275
            }
276
        }
277
        private void TextModeling(Text text)
278
        {
279
            if (text.TextAngle == TextAngle.None)
280
                return;
281

    
282
            if (text.Aveva.LabelType == LabelType.SingleText)
283
                DrawText(text.Aveva.X, text.Aveva.Y, text.Aveva.Height, text.VALUE, text.Aveva.Angle);
284
            else if (text.Aveva.LabelType == LabelType.MultiText)
285
            {
286
                string[] valueArray = text.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
287
                List<string> values = new List<string>();
288
                for (int i = 0; i < valueArray.Length; i++)
289
                    values.Insert(0, valueArray[i]);
290

    
291
                for (int i = 0; i < values.Count; i++)
292
                {
293
                    double x = text.Aveva.X;
294
                    double y = text.Aveva.Y;
295
                    int heightIndex = i;
296

    
297
                    if (text.TextAngle == TextAngle.Degree0 || text.TextAngle == TextAngle.Degree180)
298
                        y = y + text.Aveva.Height * i;
299
                    else if (text.TextAngle == TextAngle.Degree90 || text.TextAngle == TextAngle.Degree270)
300
                        x = x + text.Aveva.Height * i;
301

    
302
                    DrawText(x, y, text.Aveva.Height, values[i], text.Aveva.Angle);
303
                }
304
            }
305
                
306

    
307
        }
308
        private void NoteModeling(Model.Note note)
309
        {
310
            if (note.TextAngle == TextAngle.None)
311
                return;
312

    
313
            if (note.Aveva.LabelType == LabelType.SingleNote)
314
                DrawText(note.Aveva.X, note.Aveva.Y, note.Aveva.Height, note.VALUE, note.Aveva.Angle);
315
            else if (note.Aveva.LabelType == LabelType.MultiNote)
316
            {
317
                string[] valueArray = note.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
318
                List<string> values = new List<string>();
319
                for (int i = 0; i < valueArray.Length; i++)
320
                    values.Insert(0, valueArray[i]);
321

    
322
                for (int i = 0; i < values.Count; i++)
323
                {
324
                    double x = note.Aveva.X;
325
                    double y = note.Aveva.Y;
326
                    int heightIndex = i;
327

    
328
                    if (note.TextAngle == TextAngle.Degree0 || note.TextAngle == TextAngle.Degree180)
329
                        y = y + note.Aveva.Height * i;
330
                    else if (note.TextAngle == TextAngle.Degree90 || note.TextAngle == TextAngle.Degree270)
331
                        x = x + note.Aveva.Height * i;
332

    
333
                    DrawText(x, y, note.Aveva.Height, values[i], note.Aveva.Angle);
334
                }
335
            }
336
        }
337
        private void SetLineAttribute(Model.Line line)
338
        {
339

    
340
        }
341
        private void SetLineNumberAttribute(Model.LineNumber lineNumber)
342
        {
343

    
344
        }
345
        private void SetSymbolAttribute(Model.Symbol symbol)
346
        {
347
            foreach (var attribute in symbol.ATTRIBUTES)
348
            {
349
                AttributeInfo info = avevaInfo.AttributeInfo.Find(x => x.UID == attribute.UID);
350
                if (info != null)
351
                {
352

    
353
                }
354
            }
355
        }
356
        #endregion
357

    
358
        #region Drawing Method
359
        private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
360
        {
361
            List<long> prevHandles = GetAllGroupHandles();
362

    
363
            GUIUtils.SetPipeStyle(style);
364
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
365
            Editor editor = acDoc.Editor;
366
            List<object> commandParam = new List<object>();
367
            //command name
368
            commandParam.Add("DRPIPE");
369
            //coordinate
370
            foreach (var item in coordinates)
371
                commandParam.Add(item);
372

    
373
            //enter Parent(null)
374
            commandParam.Add(null);
375

    
376
            if (lineNumber != null)
377
            {
378
                Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
379
                commandParam.Add(point);
380
                commandParam.Add(lineNumber.Aveva.Angle);
381

    
382
                commandParam.Add(null);
383
            }
384
            else
385
                commandParam.Add(null);
386

    
387
            editor.Command(commandParam.ToArray());
388

    
389
            List<long> newHandles = GetAllGroupHandles();
390
            List<long> otherHandles = new List<long>();
391
            foreach (var item in newHandles)
392
                if (!prevHandles.Contains(item))
393
                    otherHandles.Add(item);
394

    
395
            if (otherHandles.Count == 1)
396
                return otherHandles[0];
397
            else
398
                return 0;
399
        }
400
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
401
        {
402
            List<long> prevHandles = GetAllGroupHandles();
403
            GUIUtils.SetPipeStyle(style);
404

    
405
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
406
            Editor editor = acDoc.Editor;
407
            List<object> commandParam = new List<object>();
408
            //command name
409
            commandParam.Add("DRPIPE");
410
            //coordinate
411
            foreach (var item in coordinates)
412
                commandParam.Add(item);
413
            //enter
414
            commandParam.Add(null);
415

    
416
            //Input Object ID
417
            commandParam.Add(objectId);
418

    
419
            //enter
420
            commandParam.Add(null);
421

    
422
            editor.Command(commandParam.ToArray());
423

    
424
            List<long> newHandles = GetAllGroupHandles();
425
            List<long> otherHandles = new List<long>();
426
            foreach (var item in newHandles)
427
                if (!prevHandles.Contains(item))
428
                    otherHandles.Add(item);
429

    
430
            if (otherHandles.Count == 1)
431
                return otherHandles[0];
432
            else
433
                return 0;
434
        }
435
        private long DrawSignal(string style, List<string> coordinates)
436
        {
437
            List<long> prevHandles = GetAllGroupHandles();
438

    
439
            GUIUtils.SetPipeStyle(style);
440
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
441
            Editor editor = acDoc.Editor;
442
            List<object> commandParam = new List<object>();
443
            //command name
444
            commandParam.Add("VPESIGNAL");
445
            //coordinate
446
            foreach (var item in coordinates)
447
                commandParam.Add(item);
448
            //enter
449
            commandParam.Add(null);
450

    
451
            editor.Command(commandParam.ToArray());
452

    
453
            List<long> newHandles = GetAllGroupHandles();
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
        private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
465
        {
466
            long handle = 0;
467
            try
468
            {
469
                string insertSymbolName = symbol.Aveva.Name;
470
                double x = symbol.Aveva.X;
471
                double y = symbol.Aveva.Y;
472

    
473
                List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
474

    
475
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
476
                drawingData.InsertSymbolName = insertSymbolName;
477
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
478
                Editor editor = acDoc.Editor;
479
                List<object> commandParam = new List<object>();
480
                commandParam.Add("INSSYM");
481

    
482
                // Insert Point
483
                Point3d point = new Point3d(x, y, 0);
484
                commandParam.Add(point);
485

    
486
                // Check Insert Point
487
                if (needAngle)
488
                    commandParam.Add(symbol.ANGLE);
489

    
490
                // Check Balloon
491
                if (needBalloon)
492
                {
493
                    point = new Point3d(x + 20, y + 20, 0);
494
                    commandParam.Add(point);
495
                    
496
                    // Check LeaderLine
497
                    if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
498
                        commandParam.Add(x + "," + y);
499
                }
500

    
501
                for (int i = 0; i < 8; i++)
502
                    commandParam.Add(null);
503

    
504
                editor.Command(commandParam.ToArray());
505

    
506
                List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
507
                List<long> otherHandles = new List<long>();
508
                foreach (var item in newHandles)
509
                    if (!prevHandles.Contains(item))
510
                        otherHandles.Add(item);
511

    
512
                if (otherHandles.Count == 1)
513
                    return otherHandles[0];
514
                else
515
                    return 0;
516
            }
517
            catch (System.Exception ex)
518
            {
519

    
520
            }
521

    
522
            return handle;
523
        }
524
        private void DrawText(double x, double y, double height, string text, double rotation)
525
        {
526
            // Get the current document and database
527
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
528
            Database acCurDb = acDoc.Database;
529

    
530
            // Start a transaction
531
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
532
            {
533
                // Open the Block table for read
534
                BlockTable acBlkTbl;
535
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
536
                                                OpenMode.ForRead) as BlockTable;
537

    
538
                // Open the Block table record Model space for write
539
                BlockTableRecord acBlkTblRec;
540
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
541
                                                OpenMode.ForWrite) as BlockTableRecord;
542

    
543
                // Create a single-line text object
544
                using (DBText acText = new DBText())
545
                {
546
                    acText.Position = new Point3d(x, y, 0);
547
                    acText.Height = height;
548
                    acText.TextString = text;
549
                    acText.Rotation = rotation;
550

    
551
                    acBlkTblRec.AppendEntity(acText);
552
                    acTrans.AddNewlyCreatedDBObject(acText, true);
553
                }
554

    
555
                // Save the changes and dispose of the transaction
556
                acTrans.Commit();
557
            }
558
        }
559
        #endregion
560

    
561
        #region Autocad Utils
562
        private List<long> GetAllBlockHandlesByName(string name)
563
        {
564
            List<long> handles = new List<long>();
565

    
566
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
567
            Database acCurDb = acDoc.Database;
568
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
569
            {
570
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
571
                foreach (var entry in gd)
572
                {
573
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
574
                    if (blockTableRecord != null)
575
                    {
576
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
577
                        foreach (var item in records)
578
                        {
579
                            if (item.Name == name)
580
                                handles.Add(item.Handle.Value);
581
                        }
582
                    }
583
                }
584
                    
585
                acTrans.Commit();
586
            }
587
            return handles;
588
        }
589
        private List<long> GetAllGroupHandles()
590
        {
591
            List<long> handles = new List<long>();
592

    
593
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
594
            Database acCurDb = acDoc.Database;
595
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
596
            {
597
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
598
                foreach (var entry in gd)
599
                    handles.Add(entry.Value.Handle.Value);
600
                acTrans.Commit();
601
            }
602
            return handles;
603
        }
604
        private ObjectId GetFirstPolyLine(long groupHandle)
605
        {
606
            ObjectId result = ObjectId.Null;
607

    
608
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
609
            Database acCurDb = acDoc.Database;
610
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
611
            {
612
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
613
                foreach (var entry in gd)
614
                {
615
                    if (entry.Value.Handle.Value == groupHandle)
616
                    {
617
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
618
                        foreach (var item in group.GetAllEntityIds())
619
                        {
620
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
621
                            {
622
                                result = item;
623
                                break;
624
                            }
625
                        }
626
                        break;
627
                    }
628
                }
629
                acTrans.Commit();
630
            }
631

    
632
            return result;
633
        }
634
        private ObjectId GetObjectIdByHandle(long lHandle)
635
        {
636
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
637
            Database acCurDb = acDoc.Database;
638
            Handle handle = new Handle(lHandle);
639

    
640
            return acCurDb.GetObjectId(false, handle, 0);
641
        }
642
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
643
        {
644
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
645
            Database acCurDb = acDoc.Database;
646
            Editor acDocEd = acDoc.Editor;
647

    
648
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
649
            {
650
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
651
                if (objDB != null)
652
                {
653
                    if (objDB.GetType() == typeof(BlockReference))
654
                    {
655
                        BlockReference block = objDB as BlockReference;
656
                        block.Rotation = rotation;
657
                    }
658
                }
659
                acTrans.Commit();
660
            }
661
        }
662
        private void SetSymbolUDAAttribute(ObjectId objectId, List<Tuple<string, string>> datas)
663
        {
664
            AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
665
            AcadBlockReference acadBlockReference = acadApplication.ActiveDocument.ObjectIdToObject((long)objectId.OldIdPtr);
666
            if (acadBlockReference.HasAttributes)
667
            {
668
                object objectValue = RuntimeHelpers.GetObjectValue(acadBlockReference.GetAttributes());
669
                int arg_5B_0 = Microsoft.VisualBasic.Information.LBound((Array)objectValue, 1);
670
                int num = Microsoft.VisualBasic.Information.UBound((Array)objectValue, 1);
671
                for (int i = arg_5B_0; i <= num; i++)
672
                {
673
                    string text = string.Empty;
674
                    text = Conversions.ToString(NewLateBinding.LateGet(NewLateBinding.LateIndexGet(objectValue, new object[]
675
                    {
676
                                i
677
                    }, null), null, "tagString", new object[0], null, null, null));
678

    
679
                    Tuple<string, string> attribute = datas.Find(x => x.Item1 == text);
680
                    if (attribute != null)
681
                    {
682
                        NewLateBinding.LateSetComplex(NewLateBinding.LateIndexGet(objectValue, new object[]
683
                                {
684
                                    i
685
                                }, null), null, "textString", new object[]
686
                                {
687
                                    attribute.Item2.Split("|".ToCharArray())[0]
688
                                }, null, null, false, true);
689
                    }
690
                }
691
            }
692
        }
693
        #endregion
694

    
695
        #region Modeling Utils
696
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
697
        {
698
            if (!group.Contains(line))
699
            {
700
                if (isInsert)
701
                    group.Insert(0, line);
702
                else
703
                    group.Add(line);
704

    
705
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
706
                {
707
                    object connObj = line.CONNECTORS[0].ConnectedObject;
708
                    if (connObj.GetType() == typeof(Model.Line) &&
709
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
710
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
711
                    else if (connObj.GetType() == typeof(Model.Symbol))
712
                    {
713
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
714
                        if (connLine != null)
715
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
716
                    }
717
                }
718
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
719
                {
720
                    object connObj = line.CONNECTORS[1].ConnectedObject;
721
                    if (connObj.GetType() == typeof(Model.Line) &&
722
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
723
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
724
                    else if (connObj.GetType() == typeof(Model.Symbol))
725
                    {
726
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
727
                        if (connLine != null)
728
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
729
                    }
730
                }
731
            }
732
        }
733
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
734
        {
735
            for (int i = 0; i < groupLine.Count; i++)
736
            {
737
                Model.Line line = groupLine[i];
738
                if (i == 0)
739
                {
740
                    object connObj = line.CONNECTORS[0].ConnectedObject;
741
                    if (connObj != null)
742
                    {
743
                        string point = GetPointByConnectedItem(connObj, line, true);
744
                        if (string.IsNullOrEmpty(point))
745
                            points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
746
                        else
747
                            points.Add(point);
748
                    }
749
                    else
750
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
751
                }
752
                else
753
                {
754
                    Model.Line prevLine = groupLine[i - 1];
755
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
756
                        prevLine.SlopeType == SlopeType.VERTICAL)
757
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
758
                    else if (line.SlopeType == SlopeType.VERTICAL &&
759
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
760
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
761
                    else
762
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
763
                }
764

    
765
                if (i == groupLine.Count - 1)
766
                {
767
                    object connObj = line.CONNECTORS[1].ConnectedObject;
768
                    if (connObj != null && connObj.GetType() == typeof(Symbol))
769
                    {
770
                        string point = GetPointByConnectedItem(connObj, line, false);
771
                        if (string.IsNullOrEmpty(point))
772
                            points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
773
                        else
774
                            points.Add(point);
775
                    }
776
                    else
777
                        points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
778
                }
779
            }
780
        }
781
        private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
782
        {
783
            string result = string.Empty;
784
            double x;
785
            double y;
786
            if (isStart)
787
            {
788
                x = line.Aveva.Start_X;
789
                y = line.Aveva.Start_Y;
790
            }
791
            else
792
            {
793
                x = line.Aveva.End_X;
794
                y = line.Aveva.End_Y;
795
            }
796

    
797
            if (connObj.GetType() == typeof(Symbol))
798
            {
799
                Symbol connSymbol = connObj as Symbol;
800
                Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
801

    
802
                SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
803
                if (slopeType == SlopeType.HORIZONTAL)
804
                    result = connSymbol.Aveva.X + "," + y;
805
                else if (slopeType == SlopeType.VERTICAL)
806
                    result = x + "," + connSymbol.Aveva.Y;
807
                else
808
                    result = x + "," + y;
809
            }
810
            else if (connObj.GetType() == typeof(OPC))
811
            {
812
                OPC connOPC = connObj as OPC;
813
                Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
814

    
815
                double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
816
                APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
817
                APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
818
                locX = locX + sizeWidth / 2;
819
                locY = locY + sizeHeight / 2;
820
                aX = locX;
821
                aY = locY;
822
                document.ConvertAvevaPoint(ref aX, ref aY);
823

    
824
                SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
825
                if (slopeType == SlopeType.HORIZONTAL)
826
                    result = aX + "," + y;
827
                else if (slopeType == SlopeType.VERTICAL)
828
                    result = x + "," + aY;
829
                else
830
                    result = x + "," + y;
831
            }
832
            
833
            return result;
834
        }
835
        private bool IsExistEndBreak(object item1, object item2)
836
        {
837
            bool result = false;
838
            if (item1 != null && item2 != null)
839
            {
840
                EndBreak endBreak = document.EndBreaks.Find(x =>
841
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
842
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
843
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
844
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
845

    
846
                if (endBreak != null)
847
                    result = true;
848
            }
849

    
850
            return result;
851
        }
852
        private LineRun FindLineRun(Model.Line line)
853
        {
854
            List<LineRun> allLineRun = new List<LineRun>();
855
            foreach (var item in document.LINENUMBERS)
856
                allLineRun.AddRange(item.RUNS);
857
            foreach (var item in document.TRIMLINES)
858
                allLineRun.AddRange(item.RUNS);
859

    
860
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
861
        }
862
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
863
        {
864
            Model.Line result = null;
865
            LineRun run = FindLineRun(line);
866
            Connector connector = symbol.CONNECTORS.Find(x =>
867
            x.ConnectedObject != null &&
868
            x.ConnectedObject != line &&
869
            run.RUNITEMS.Contains(x.ConnectedObject) &&
870
            x.ConnectedObject.GetType() == typeof(Model.Line));
871

    
872
            if (connector != null)
873
                result = connector.ConnectedObject as Model.Line;
874
            
875
            return result;
876
        }
877
        private bool CheckAngle(DataRow symbolRow)
878
        {
879
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
880
        }
881
        private bool CheckBalloon(Symbol symbol)
882
        {
883
            bool result = false;
884

    
885
            string text = string.Empty;
886
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
887
            if (symbolDefinitions.Count != 0)
888
            {
889
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
890

    
891
                if (text.Equals("VLP"))
892
                    text = "VPO";
893
                else if (text.Equals("IVP"))
894
                    text = "IPO";
895

    
896
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
897
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
898
            }
899

    
900
            return result;
901
        }
902
        #endregion
903

    
904
        #region Test Source
905
        public void test()
906
        {
907
            //InitGUI();
908
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
909

    
910
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
911
            return;
912
            Symbol symbol = new Symbol();
913
            symbol.Aveva = new AvevaSymbolInfo();
914
            symbol.Aveva.Name = "INVB";
915
            symbol.Aveva.X = 50;
916
            symbol.Aveva.Y = 100;
917

    
918
            DataRow[] allRows = avevaInfo.AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
919
            if (allRows.Length == 1)
920
            {
921
                DataRow symbolRow = allRows[0];
922
                bool bAngle = CheckAngle(symbolRow);
923
                bool bBalloon = CheckBalloon(symbol);
924
                bBalloon = false;
925
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
926
            }
927

    
928
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
929
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
930
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
931
            {
932
                bool bIsPromptRotate = true;
933
            }
934
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
935

    
936
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
937
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
938
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
939

    
940
            //DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" });
941

    
942
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
943
        }
944
        public static void TESTStatic(ObjectId objectId)
945
        {
946
            // Get the current document and database, and start a transaction
947
            //Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
948
            //Database acCurDb = acDoc.Database;
949
            //Editor acDocEd = acDoc.Editor;
950

    
951
            //using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
952
            //{
953
            //    Entity objDb = (Entity)acTrans.GetObject(objectId, OpenMode.ForWrite);
954
            //    Entity startNodeEntityForPipe = Pipe.GetStartNodeEntityForPipe(objDb);
955

    
956
            //    //ArrayList arrayList = new ArrayList();
957
            //    //ArrayList arrayList2 = new ArrayList();
958
            //    //PIDUtils.VBUtils.GetUserDefineAttributes(startNodeEntityForPipe.ObjectId, ref arrayList, ref arrayList2);
959
            //    //PIDUtils.VBUtils.GetUserDefineAttributes(objDb.ObjectId, ref arrayList, ref arrayList2);
960
            //    //NamedXDataBlock namedXDataBlock = XDataUtilities.GetNamedBlock(objDb, "ReferenceObject");
961

    
962
            //    XData xData = XDataUtilities.GetXData<Entity>(objDb);
963
            //    XData xData2 = XDataUtilities.GetXData<Entity>(startNodeEntityForPipe);
964
            //    acTrans.Commit();
965
            //}
966

    
967
            //PLABFO
968
        }
969
        #endregion
970
    }
971

    
972

    
973
    public static class AcDbLinqExtensionMethods
974
    {
975
        /// <summary>
976
        /// Get all references to the given BlockTableRecord, including 
977
        /// references to anonymous dynamic BlockTableRecords.
978
        /// </summary>
979

    
980
        public static IEnumerable<BlockReference> GetBlockReferences(
981
           this BlockTableRecord btr,
982
           OpenMode mode = OpenMode.ForRead,
983
           bool directOnly = true)
984
        {
985
            if (btr == null)
986
                throw new ArgumentNullException("btr");
987
            var tr = btr.Database.TransactionManager.TopTransaction;
988
            if (tr == null)
989
                throw new InvalidOperationException("No transaction");
990
            var ids = btr.GetBlockReferenceIds(directOnly, true);
991
            int cnt = ids.Count;
992
            for (int i = 0; i < cnt; i++)
993
            {
994
                yield return (BlockReference)
995
                   tr.GetObject(ids[i], mode, false, false);
996
            }
997
            if (btr.IsDynamicBlock)
998
            {
999
                BlockTableRecord btr2 = null;
1000
                var blockIds = btr.GetAnonymousBlockIds();
1001
                cnt = blockIds.Count;
1002
                for (int i = 0; i < cnt; i++)
1003
                {
1004
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
1005
                       OpenMode.ForRead, false, false);
1006
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
1007
                    int cnt2 = ids.Count;
1008
                    for (int j = 0; j < cnt2; j++)
1009
                    {
1010
                        yield return (BlockReference)
1011
                           tr.GetObject(ids[j], mode, false, false);
1012
                    }
1013
                }
1014
            }
1015
        }
1016
    }
1017
}
클립보드 이미지 추가 (최대 크기: 500 MB)