프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 0243026f

이력 | 보기 | 이력해설 | 다운로드 (39.9 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

    
41
        System.Data.DataTable AvevaSymbolTable = null;
42

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

    
57
        public AutoModeling(Model.Document document, System.Data.DataTable AvevaSymbolTable)
58
        {
59
            this.document = document;
60
            this.AvevaSymbolTable = AvevaSymbolTable;
61
        }
62

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

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

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

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

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

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

    
109
                SetConvertRule();
110

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

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

    
138
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
139
            }
140
        }
141
        private void RunOPCModeling()
142
        {
143
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count);
144
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling");
145
            foreach (var item in document.OPCs)
146
            {
147
                if (item.Aveva.Handle == 0)
148
                    SymbolModeling(item);
149
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
150
            }
151
        }
152
        private void RunSymbolModeling()
153
        {
154
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
155
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
156
            foreach (var item in document.SYMBOLS)
157
            {
158
                if (item.Aveva.Handle == 0)
159
                    SymbolModeling(item);
160
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
161
            }
162
        }
163
        private void RunTextModeling()
164
        {
165
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Texts.Count);
166
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Text Modeling");
167
            foreach (var item in document.Texts)
168
            {
169
                TextModeling(item);
170
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
171
            }
172
        }
173
        private void RunNoteModeling()
174
        {
175
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Notes.Count);
176
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Note Modeling");
177
            foreach (var item in document.Notes)
178
            {
179
                NoteModeling(item);
180
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
181
            }
182
        }
183
        private void RunInputLineAttribute()
184
        {
185

    
186
        }
187
        private void RunInputSymbolAttribute()
188
        {
189

    
190
        }
191
        #endregion
192

    
193
        #region Modeling Method
194
        private void LineModeling(Model.Line line)
195
        {
196
            List<Model.Line> groupLine = new List<Model.Line>();
197
            List<string> points = new List<string>();
198
            GetConnectedGroupLine(line, groupLine, false);
199
            GetGroupLinePoints(groupLine, points);
200

    
201
            LineNumber lineNumber = null;
202
            if (line.Aveva.Type == Model.Type.Pipe)
203
            {
204
                foreach (var item in groupLine)
205
                {
206
                    lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID);
207
                    if (lineNumber != null)
208
                    {
209
                        GUIUtils.SetAutoLabelCheck(true);
210
                        break;
211
                    }
212
                }
213
            }
214

    
215

    
216
            long handle = 0;
217
            if (line.Aveva.Type == Model.Type.Pipe)
218
                handle = DrawPipe(line.Aveva.Name, points, lineNumber);
219
            else if (line.Aveva.Type == Model.Type.Signal)
220
                handle = DrawSignal(line.Aveva.Name, points);
221

    
222
            if (lineNumber != null)
223
                GUIUtils.SetAutoLabelCheck(false);
224

    
225
            foreach (var item in groupLine)
226
                item.Aveva.Handle = handle;
227
        }
228
        private void SymbolModeling(Symbol symbol)
229
        {
230
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
231
            if (allRows.Length == 1)
232
            {
233
                DataRow symbolRow = allRows[0];
234
                bool bAngle = CheckAngle(symbolRow);
235
                bool bBalloon = CheckBalloon(symbol);
236

    
237
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
238
                if (handle != 0)
239
                {
240
                    symbol.Aveva.Handle = handle;
241

    
242
                    if (!bAngle)
243
                    {
244
                        ObjectId objectId = GetObjectIdByHandle(handle);
245
                        if (objectId != ObjectId.Null)
246
                        {
247
                            SetBlockReferenceRotation(objectId, symbol.ANGLE);
248
                        }
249
                    }
250
                }
251
            }
252
        }
253
        private void TextModeling(Text text)
254
        {
255
            if (text.TextAngle == TextAngle.None)
256
                return;
257

    
258
            if (text.Aveva.LabelType == LabelType.SingleText)
259
                DrawText(text.Aveva.X, text.Aveva.Y, text.Aveva.Height, text.VALUE, text.Aveva.Angle);
260
            else if (text.Aveva.LabelType == LabelType.MultiText)
261
            {
262
                string[] valueArray = text.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
263
                List<string> values = new List<string>();
264
                for (int i = 0; i < valueArray.Length; i++)
265
                    values.Insert(0, valueArray[i]);
266

    
267
                for (int i = 0; i < values.Count; i++)
268
                {
269
                    double x = text.Aveva.X;
270
                    double y = text.Aveva.Y;
271
                    int heightIndex = i;
272

    
273
                    if (text.TextAngle == TextAngle.Degree0 || text.TextAngle == TextAngle.Degree180)
274
                        y = y + text.Aveva.Height * i;
275
                    else if (text.TextAngle == TextAngle.Degree90 || text.TextAngle == TextAngle.Degree270)
276
                        x = x + text.Aveva.Height * i;
277

    
278
                    DrawText(x, y, text.Aveva.Height, values[i], text.Aveva.Angle);
279
                }
280
            }
281
                
282

    
283
        }
284
        private void NoteModeling(Model.Note note)
285
        {
286
            if (note.TextAngle == TextAngle.None)
287
                return;
288

    
289
            if (note.Aveva.LabelType == LabelType.SingleNote)
290
                DrawText(note.Aveva.X, note.Aveva.Y, note.Aveva.Height, note.VALUE, note.Aveva.Angle);
291
            else if (note.Aveva.LabelType == LabelType.MultiNote)
292
            {
293
                string[] valueArray = note.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
294
                List<string> values = new List<string>();
295
                for (int i = 0; i < valueArray.Length; i++)
296
                    values.Insert(0, valueArray[i]);
297

    
298
                for (int i = 0; i < values.Count; i++)
299
                {
300
                    double x = note.Aveva.X;
301
                    double y = note.Aveva.Y;
302
                    int heightIndex = i;
303

    
304
                    if (note.TextAngle == TextAngle.Degree0 || note.TextAngle == TextAngle.Degree180)
305
                        y = y + note.Aveva.Height * i;
306
                    else if (note.TextAngle == TextAngle.Degree90 || note.TextAngle == TextAngle.Degree270)
307
                        x = x + note.Aveva.Height * i;
308

    
309
                    DrawText(x, y, note.Aveva.Height, values[i], note.Aveva.Angle);
310
                }
311
            }
312
        }
313
        private void SetLineAttribute(Model.Line line)
314
        {
315

    
316
        }
317
        private void SetSymbolAttribute(Model.Symbol symbol)
318
        {
319

    
320
        }
321
        #endregion
322

    
323
        #region Drawing Method
324
        private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
325
        {
326
            List<long> prevHandles = GetAllGroupHandles();
327

    
328
            GUIUtils.SetPipeStyle(style);
329
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
330
            Editor editor = acDoc.Editor;
331
            List<object> commandParam = new List<object>();
332
            //command name
333
            commandParam.Add("DRPIPE");
334
            //coordinate
335
            foreach (var item in coordinates)
336
                commandParam.Add(item);
337

    
338
            //enter Parent(null)
339
            commandParam.Add(null);
340

    
341
            if (lineNumber != null)
342
            {
343
                Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
344
                commandParam.Add(point);
345
                commandParam.Add(lineNumber.Aveva.Angle);
346

    
347
                commandParam.Add(null);
348
            }
349
            else
350
                commandParam.Add(null);
351

    
352
            editor.Command(commandParam.ToArray());
353

    
354
            List<long> newHandles = GetAllGroupHandles();
355
            List<long> otherHandles = new List<long>();
356
            foreach (var item in newHandles)
357
                if (!prevHandles.Contains(item))
358
                    otherHandles.Add(item);
359

    
360
            if (otherHandles.Count == 1)
361
                return otherHandles[0];
362
            else
363
                return 0;
364
        }
365
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
366
        {
367
            List<long> prevHandles = GetAllGroupHandles();
368
            GUIUtils.SetPipeStyle(style);
369

    
370
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
371
            Editor editor = acDoc.Editor;
372
            List<object> commandParam = new List<object>();
373
            //command name
374
            commandParam.Add("DRPIPE");
375
            //coordinate
376
            foreach (var item in coordinates)
377
                commandParam.Add(item);
378
            //enter
379
            commandParam.Add(null);
380

    
381
            //Input Object ID
382
            commandParam.Add(objectId);
383

    
384
            //enter
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 DrawSignal(string style, List<string> coordinates)
401
        {
402
            List<long> prevHandles = GetAllGroupHandles();
403

    
404
            GUIUtils.SetPipeStyle(style);
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("VPESIGNAL");
410
            //coordinate
411
            foreach (var item in coordinates)
412
                commandParam.Add(item);
413
            //enter
414
            commandParam.Add(null);
415

    
416
            editor.Command(commandParam.ToArray());
417

    
418
            List<long> newHandles = GetAllGroupHandles();
419
            List<long> otherHandles = new List<long>();
420
            foreach (var item in newHandles)
421
                if (!prevHandles.Contains(item))
422
                    otherHandles.Add(item);
423

    
424
            if (otherHandles.Count == 1)
425
                return otherHandles[0];
426
            else
427
                return 0;
428
        }
429
        private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
430
        {
431
            long handle = 0;
432
            try
433
            {
434
                string insertSymbolName = symbol.Aveva.Name;
435
                double x = symbol.Aveva.X;
436
                double y = symbol.Aveva.Y;
437

    
438
                List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
439

    
440
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
441
                drawingData.InsertSymbolName = insertSymbolName;
442
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
443
                Editor editor = acDoc.Editor;
444
                List<object> commandParam = new List<object>();
445
                commandParam.Add("INSSYM");
446

    
447
                // Insert Point
448
                Point3d point = new Point3d(x, y, 0);
449
                commandParam.Add(point);
450

    
451
                // Check Insert Point
452
                if (needAngle)
453
                    commandParam.Add(symbol.ANGLE);
454

    
455
                // Check Balloon
456
                if (needBalloon)
457
                {
458
                    point = new Point3d(x + 20, y + 20, 0);
459
                    commandParam.Add(point);
460
                    
461
                    // Check LeaderLine
462
                    if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
463
                        commandParam.Add(x + "," + y);
464
                }
465

    
466
                for (int i = 0; i < 8; i++)
467
                    commandParam.Add(null);
468

    
469
                editor.Command(commandParam.ToArray());
470

    
471
                List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
472
                List<long> otherHandles = new List<long>();
473
                foreach (var item in newHandles)
474
                    if (!prevHandles.Contains(item))
475
                        otherHandles.Add(item);
476

    
477
                if (otherHandles.Count == 1)
478
                    return otherHandles[0];
479
                else
480
                    return 0;
481
            }
482
            catch (System.Exception ex)
483
            {
484

    
485
            }
486

    
487
            return handle;
488
        }
489
        private void DrawText(double x, double y, double height, string text, double rotation)
490
        {
491
            // Get the current document and database
492
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
493
            Database acCurDb = acDoc.Database;
494

    
495
            // Start a transaction
496
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
497
            {
498
                // Open the Block table for read
499
                BlockTable acBlkTbl;
500
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
501
                                                OpenMode.ForRead) as BlockTable;
502

    
503
                // Open the Block table record Model space for write
504
                BlockTableRecord acBlkTblRec;
505
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
506
                                                OpenMode.ForWrite) as BlockTableRecord;
507

    
508
                // Create a single-line text object
509
                using (DBText acText = new DBText())
510
                {
511
                    acText.Position = new Point3d(x, y, 0);
512
                    acText.Height = height;
513
                    acText.TextString = text;
514
                    acText.Rotation = rotation;
515

    
516
                    acBlkTblRec.AppendEntity(acText);
517
                    acTrans.AddNewlyCreatedDBObject(acText, true);
518
                }
519

    
520
                // Save the changes and dispose of the transaction
521
                acTrans.Commit();
522
            }
523
        }
524
        #endregion
525

    
526
        #region Autocad Utils
527
        private List<long> GetAllBlockHandlesByName(string name)
528
        {
529
            List<long> handles = new List<long>();
530

    
531
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
532
            Database acCurDb = acDoc.Database;
533
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
534
            {
535
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
536
                foreach (var entry in gd)
537
                {
538
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
539
                    if (blockTableRecord != null)
540
                    {
541
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
542
                        foreach (var item in records)
543
                        {
544
                            if (item.Name == name)
545
                                handles.Add(item.Handle.Value);
546
                        }
547
                    }
548
                }
549
                    
550
                acTrans.Commit();
551
            }
552
            return handles;
553
        }
554
        private List<long> GetAllGroupHandles()
555
        {
556
            List<long> handles = new List<long>();
557

    
558
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
559
            Database acCurDb = acDoc.Database;
560
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
561
            {
562
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
563
                foreach (var entry in gd)
564
                    handles.Add(entry.Value.Handle.Value);
565
                acTrans.Commit();
566
            }
567
            return handles;
568
        }
569
        private ObjectId GetFirstPolyLine(long groupHandle)
570
        {
571
            ObjectId result = ObjectId.Null;
572

    
573
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
574
            Database acCurDb = acDoc.Database;
575
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
576
            {
577
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
578
                foreach (var entry in gd)
579
                {
580
                    if (entry.Value.Handle.Value == groupHandle)
581
                    {
582
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
583
                        foreach (var item in group.GetAllEntityIds())
584
                        {
585
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
586
                            {
587
                                result = item;
588
                                break;
589
                            }
590
                        }
591
                        break;
592
                    }
593
                }
594
                acTrans.Commit();
595
            }
596

    
597
            return result;
598
        }
599
        private ObjectId GetObjectIdByHandle(long lHandle)
600
        {
601
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
602
            Database acCurDb = acDoc.Database;
603
            Handle handle = new Handle(lHandle);
604

    
605
            return acCurDb.GetObjectId(false, handle, 0);
606
        }
607
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
608
        {
609
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
610
            Database acCurDb = acDoc.Database;
611
            Editor acDocEd = acDoc.Editor;
612

    
613
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
614
            {
615
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
616
                if (objDB != null)
617
                {
618
                    if (objDB.GetType() == typeof(BlockReference))
619
                    {
620
                        BlockReference block = objDB as BlockReference;
621
                        block.Rotation = rotation;
622
                    }
623
                }
624
                acTrans.Commit();
625
            }
626
        }
627
        private void SetSymbolUDAAttribute(ObjectId objectId, List<Tuple<string, string>> datas)
628
        {
629
            AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
630
            AcadBlockReference acadBlockReference = acadApplication.ActiveDocument.ObjectIdToObject((long)objectId.OldIdPtr);
631
            if (acadBlockReference.HasAttributes)
632
            {
633
                object objectValue = RuntimeHelpers.GetObjectValue(acadBlockReference.GetAttributes());
634
                int arg_5B_0 = Microsoft.VisualBasic.Information.LBound((Array)objectValue, 1);
635
                int num = Microsoft.VisualBasic.Information.UBound((Array)objectValue, 1);
636
                for (int i = arg_5B_0; i <= num; i++)
637
                {
638
                    string text = string.Empty;
639
                    text = Conversions.ToString(NewLateBinding.LateGet(NewLateBinding.LateIndexGet(objectValue, new object[]
640
                    {
641
                                i
642
                    }, null), null, "tagString", new object[0], null, null, null));
643

    
644
                    Tuple<string, string> attribute = datas.Find(x => x.Item1 == text);
645
                    if (attribute != null)
646
                    {
647
                        NewLateBinding.LateSetComplex(NewLateBinding.LateIndexGet(objectValue, new object[]
648
                                {
649
                                    i
650
                                }, null), null, "textString", new object[]
651
                                {
652
                                    attribute.Item2.Split("|".ToCharArray())[0]
653
                                }, null, null, false, true);
654
                    }
655
                }
656
            }
657
        }
658
        #endregion
659

    
660
        #region Modeling Utils
661
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
662
        {
663
            if (!group.Contains(line))
664
            {
665
                if (isInsert)
666
                    group.Insert(0, line);
667
                else
668
                    group.Add(line);
669

    
670
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
671
                {
672
                    object connObj = line.CONNECTORS[0].ConnectedObject;
673
                    if (connObj.GetType() == typeof(Model.Line) &&
674
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
675
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
676
                    else if (connObj.GetType() == typeof(Model.Symbol))
677
                    {
678
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
679
                        if (connLine != null)
680
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
681
                    }
682
                }
683
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
684
                {
685
                    object connObj = line.CONNECTORS[1].ConnectedObject;
686
                    if (connObj.GetType() == typeof(Model.Line) &&
687
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
688
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
689
                    else if (connObj.GetType() == typeof(Model.Symbol))
690
                    {
691
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
692
                        if (connLine != null)
693
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
694
                    }
695
                }
696
            }
697
        }
698
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
699
        {
700
            for (int i = 0; i < groupLine.Count; i++)
701
            {
702
                Model.Line line = groupLine[i];
703
                if (i == 0)
704
                {
705
                    object connObj = line.CONNECTORS[0].ConnectedObject;
706
                    if (connObj != null)
707
                    {
708
                        string point = GetPointByConnectedItem(connObj, line, true);
709
                        if (string.IsNullOrEmpty(point))
710
                            points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
711
                        else
712
                            points.Add(point);
713
                    }
714
                    else
715
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
716
                }
717
                else
718
                {
719
                    Model.Line prevLine = groupLine[i - 1];
720
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
721
                        prevLine.SlopeType == SlopeType.VERTICAL)
722
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
723
                    else if (line.SlopeType == SlopeType.VERTICAL &&
724
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
725
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
726
                    else
727
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
728
                }
729

    
730
                if (i == groupLine.Count - 1)
731
                {
732
                    object connObj = line.CONNECTORS[1].ConnectedObject;
733
                    if (connObj != null && connObj.GetType() == typeof(Symbol))
734
                    {
735
                        string point = GetPointByConnectedItem(connObj, line, false);
736
                        if (string.IsNullOrEmpty(point))
737
                            points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
738
                        else
739
                            points.Add(point);
740
                    }
741
                    else
742
                        points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
743
                }
744
            }
745
        }
746
        private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
747
        {
748
            string result = string.Empty;
749
            double x;
750
            double y;
751
            if (isStart)
752
            {
753
                x = line.Aveva.Start_X;
754
                y = line.Aveva.Start_Y;
755
            }
756
            else
757
            {
758
                x = line.Aveva.End_X;
759
                y = line.Aveva.End_Y;
760
            }
761

    
762
            if (connObj.GetType() == typeof(Symbol))
763
            {
764
                Symbol connSymbol = connObj as Symbol;
765
                Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
766

    
767
                SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
768
                if (slopeType == SlopeType.HORIZONTAL)
769
                    result = connSymbol.Aveva.X + "," + y;
770
                else if (slopeType == SlopeType.VERTICAL)
771
                    result = x + "," + connSymbol.Aveva.Y;
772
                else
773
                    result = x + "," + y;
774
            }
775
            else if (connObj.GetType() == typeof(OPC))
776
            {
777
                OPC connOPC = connObj as OPC;
778
                Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
779

    
780
                double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
781
                APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
782
                APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
783
                locX = locX + sizeWidth / 2;
784
                locY = locY + sizeHeight / 2;
785
                aX = locX;
786
                aY = locY;
787
                document.ConvertAvevaPoint(ref aX, ref aY);
788

    
789
                SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
790
                if (slopeType == SlopeType.HORIZONTAL)
791
                    result = aX + "," + y;
792
                else if (slopeType == SlopeType.VERTICAL)
793
                    result = x + "," + aY;
794
                else
795
                    result = x + "," + y;
796
            }
797
            
798
            return result;
799
        }
800
        private bool IsExistEndBreak(object item1, object item2)
801
        {
802
            bool result = false;
803
            if (item1 != null && item2 != null)
804
            {
805
                EndBreak endBreak = document.EndBreaks.Find(x =>
806
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
807
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
808
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
809
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
810

    
811
                if (endBreak != null)
812
                    result = true;
813
            }
814

    
815
            return result;
816
        }
817
        private LineRun FindLineRun(Model.Line line)
818
        {
819
            List<LineRun> allLineRun = new List<LineRun>();
820
            foreach (var item in document.LINENUMBERS)
821
                allLineRun.AddRange(item.RUNS);
822
            foreach (var item in document.TRIMLINES)
823
                allLineRun.AddRange(item.RUNS);
824

    
825
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
826
        }
827
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
828
        {
829
            Model.Line result = null;
830
            LineRun run = FindLineRun(line);
831
            Connector connector = symbol.CONNECTORS.Find(x =>
832
            x.ConnectedObject != null &&
833
            x.ConnectedObject != line &&
834
            run.RUNITEMS.Contains(x.ConnectedObject) &&
835
            x.ConnectedObject.GetType() == typeof(Model.Line));
836

    
837
            if (connector != null)
838
                result = connector.ConnectedObject as Model.Line;
839
            
840
            return result;
841
        }
842
        private bool CheckAngle(DataRow symbolRow)
843
        {
844
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
845
        }
846
        private bool CheckBalloon(Symbol symbol)
847
        {
848
            bool result = false;
849

    
850
            string text = string.Empty;
851
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
852
            if (symbolDefinitions.Count != 0)
853
            {
854
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
855

    
856
                if (text.Equals("VLP"))
857
                    text = "VPO";
858
                else if (text.Equals("IVP"))
859
                    text = "IPO";
860

    
861
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
862
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
863
            }
864

    
865
            return result;
866
        }
867
        #endregion
868

    
869
        #region Test Source
870
        public void test()
871
        {
872
            //InitGUI();
873
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
874

    
875
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
876
            return;
877
            Symbol symbol = new Symbol();
878
            symbol.Aveva = new AvevaSymbolInfo();
879
            symbol.Aveva.Name = "INVB";
880
            symbol.Aveva.X = 50;
881
            symbol.Aveva.Y = 100;
882

    
883
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
884
            if (allRows.Length == 1)
885
            {
886
                DataRow symbolRow = allRows[0];
887
                bool bAngle = CheckAngle(symbolRow);
888
                bool bBalloon = CheckBalloon(symbol);
889
                bBalloon = false;
890
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
891
            }
892

    
893
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
894
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
895
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
896
            {
897
                bool bIsPromptRotate = true;
898
            }
899
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
900

    
901
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
902
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
903
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
904

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

    
907
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
908
        }
909
        public static void TESTStatic(ObjectId objectId)
910
        {
911
            // Get the current document and database, and start a transaction
912
            //Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
913
            //Database acCurDb = acDoc.Database;
914
            //Editor acDocEd = acDoc.Editor;
915

    
916
            //using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
917
            //{
918
            //    Entity objDb = (Entity)acTrans.GetObject(objectId, OpenMode.ForWrite);
919
            //    Entity startNodeEntityForPipe = Pipe.GetStartNodeEntityForPipe(objDb);
920

    
921
            //    //ArrayList arrayList = new ArrayList();
922
            //    //ArrayList arrayList2 = new ArrayList();
923
            //    //PIDUtils.VBUtils.GetUserDefineAttributes(startNodeEntityForPipe.ObjectId, ref arrayList, ref arrayList2);
924
            //    //PIDUtils.VBUtils.GetUserDefineAttributes(objDb.ObjectId, ref arrayList, ref arrayList2);
925
            //    //NamedXDataBlock namedXDataBlock = XDataUtilities.GetNamedBlock(objDb, "ReferenceObject");
926

    
927
            //    XData xData = XDataUtilities.GetXData<Entity>(objDb);
928
            //    XData xData2 = XDataUtilities.GetXData<Entity>(startNodeEntityForPipe);
929
            //    acTrans.Commit();
930
            //}
931

    
932
            //PLABFO
933
        }
934
        #endregion
935
    }
936

    
937

    
938
    public static class AcDbLinqExtensionMethods
939
    {
940
        /// <summary>
941
        /// Get all references to the given BlockTableRecord, including 
942
        /// references to anonymous dynamic BlockTableRecords.
943
        /// </summary>
944

    
945
        public static IEnumerable<BlockReference> GetBlockReferences(
946
           this BlockTableRecord btr,
947
           OpenMode mode = OpenMode.ForRead,
948
           bool directOnly = true)
949
        {
950
            if (btr == null)
951
                throw new ArgumentNullException("btr");
952
            var tr = btr.Database.TransactionManager.TopTransaction;
953
            if (tr == null)
954
                throw new InvalidOperationException("No transaction");
955
            var ids = btr.GetBlockReferenceIds(directOnly, true);
956
            int cnt = ids.Count;
957
            for (int i = 0; i < cnt; i++)
958
            {
959
                yield return (BlockReference)
960
                   tr.GetObject(ids[i], mode, false, false);
961
            }
962
            if (btr.IsDynamicBlock)
963
            {
964
                BlockTableRecord btr2 = null;
965
                var blockIds = btr.GetAnonymousBlockIds();
966
                cnt = blockIds.Count;
967
                for (int i = 0; i < cnt; i++)
968
                {
969
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
970
                       OpenMode.ForRead, false, false);
971
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
972
                    int cnt2 = ids.Count;
973
                    for (int j = 0; j < cnt2; j++)
974
                    {
975
                        yield return (BlockReference)
976
                           tr.GetObject(ids[j], mode, false, false);
977
                    }
978
                }
979
            }
980
        }
981
    }
982
}
클립보드 이미지 추가 (최대 크기: 500 MB)