프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 7a8645c9

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

1
using System;
2
using System.Collections.Generic;
3
using System.Collections;
4
using System.Configuration;
5
using System.Data;
6
using System.Data.Common;
7
using System.Data.SqlClient;
8
using System.IO;
9

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

    
20
using AVEVA.PID.Components;
21
using AVEVA.PID.GUI;
22
using AVEVA.PID.Common;
23
using AVEVA.PID.DWGSelector;
24
using AVEVA.PID.Utilities;
25

    
26
using AVEVA.PID.CustomizationUtility.DB;
27
using AVEVA.PID.CustomizationUtility.Model;
28
using AVEVA.PID.CustomizationUtility.Properties;
29

    
30
using DevExpress.XtraSplashScreen;
31

    
32
namespace AVEVA.PID.CustomizationUtility
33
{
34
    public class AutoModeling
35
    {
36
        public static AutoModeling Running { get; set; }
37
        Model.Document document;
38

    
39
        System.Data.DataTable AvevaSymbolTable = null;
40

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

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

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

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

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

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

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

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

    
107
                SetConvertRule();
108

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

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

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

    
183
        #region Modeling Method
184
        private void LineModeling(Model.Line line)
185
        {
186
            List<Model.Line> groupLine = new List<Model.Line>();
187
            List<string> points = new List<string>();
188
            GetConnectedGroupLine(line, groupLine, false);
189
            GetGroupLinePoints(groupLine, points);
190

    
191
            LineNumber lineNumber = null;
192
            if (line.Aveva.Type == Model.Type.Pipe)
193
            {
194
                foreach (var item in groupLine)
195
                {
196
                    lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID);
197
                    if (lineNumber != null)
198
                    {
199
                        GUIUtils.SetAutoLabelCheck(true);
200
                        break;
201
                    }
202
                }
203
            }
204

    
205

    
206
            long handle = 0;
207
            if (line.Aveva.Type == Model.Type.Pipe)
208
                handle = DrawPipe(line.Aveva.Name, points, lineNumber);
209
            else if (line.Aveva.Type == Model.Type.Signal)
210
                handle = DrawSignal(line.Aveva.Name, points);
211

    
212
            if (lineNumber != null)
213
                GUIUtils.SetAutoLabelCheck(false);
214

    
215
            foreach (var item in groupLine)
216
                item.Aveva.Handle = handle;
217
        }
218
        private void SymbolModeling(Symbol symbol)
219
        {
220
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
221
            if (allRows.Length == 1)
222
            {
223
                DataRow symbolRow = allRows[0];
224
                bool bAngle = CheckAngle(symbolRow);
225
                bool bBalloon = CheckBalloon(symbol);
226

    
227
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
228
                if (handle != 0)
229
                {
230
                    symbol.Aveva.Handle = handle;
231

    
232
                    if (!bAngle)
233
                    {
234
                        ObjectId objectId = GetObjectIdByHandle(handle);
235
                        if (objectId != ObjectId.Null)
236
                        {
237
                            SetBlockReferenceRotation(objectId, symbol.ANGLE);
238
                        }
239
                    }
240
                }
241
            }
242
        }
243
        private void TextModeling(Text text)
244
        {
245
            if (text.TextAngle == TextAngle.None)
246
                return;
247

    
248
            if (text.Aveva.LabelType == LabelType.SingleText)
249
                DrawText(text.Aveva.X, text.Aveva.Y, text.Aveva.Height, text.VALUE);
250
            else if (text.Aveva.LabelType == LabelType.MultiText)
251
            {
252
                string[] valueArray = text.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
253
                List<string> values = new List<string>();
254
                for (int i = 0; i < valueArray.Length; i++)
255
                    values.Insert(0, valueArray[i]);
256

    
257
                for (int i = 0; i < values.Count; i++)
258
                {
259
                    double x = text.Aveva.X;
260
                    double y = text.Aveva.Y;
261
                    int heightIndex = i;
262

    
263
                    if (text.TextAngle == TextAngle.Degree0 || text.TextAngle == TextAngle.Degree180)
264
                        y = y + text.Aveva.Height * i;
265
                    else if (text.TextAngle == TextAngle.Degree90 || text.TextAngle == TextAngle.Degree270)
266
                        x = x + text.Aveva.Height * i;
267

    
268
                    DrawText(x, y, text.Aveva.Height, values[i]);
269
                }
270
            }
271
                
272

    
273
        }
274
        private void NoteModeling(Model.Note note)
275
        {
276
            if (note.TextAngle == TextAngle.None)
277
                return;
278

    
279
            if (note.Aveva.LabelType == LabelType.SingleNote)
280
                DrawText(note.Aveva.X, note.Aveva.Y, note.Aveva.Height, note.VALUE);
281
            else if (note.Aveva.LabelType == LabelType.MultiNote)
282
            {
283
                string[] valueArray = note.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
284
                List<string> values = new List<string>();
285
                for (int i = 0; i < valueArray.Length; i++)
286
                    values.Insert(0, valueArray[i]);
287

    
288
                for (int i = 0; i < values.Count; i++)
289
                {
290
                    double x = note.Aveva.X;
291
                    double y = note.Aveva.Y;
292
                    int heightIndex = i;
293

    
294
                    if (note.TextAngle == TextAngle.Degree0 || note.TextAngle == TextAngle.Degree180)
295
                        y = y + note.Aveva.Height * i;
296
                    else if (note.TextAngle == TextAngle.Degree90 || note.TextAngle == TextAngle.Degree270)
297
                        x = x + note.Aveva.Height * i;
298

    
299
                    DrawText(x, y, note.Aveva.Height, values[i]);
300
                }
301
            }
302
        }
303
        #endregion
304

    
305
        #region Drawing Method
306
        private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
307
        {
308
            List<long> prevHandles = GetAllGroupHandles();
309

    
310
            GUIUtils.SetPipeStyle(style);
311
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
312
            Editor editor = acDoc.Editor;
313
            List<object> commandParam = new List<object>();
314
            //command name
315
            commandParam.Add("DRPIPE");
316
            //coordinate
317
            foreach (var item in coordinates)
318
                commandParam.Add(item);
319

    
320
            //enter Parent(null)
321
            commandParam.Add(null);
322

    
323
            if (lineNumber != null)
324
            {
325
                Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
326
                commandParam.Add(point);
327
                commandParam.Add(lineNumber.Aveva.Angle);
328

    
329
                commandParam.Add(null);
330
            }
331
            else
332
                commandParam.Add(null);
333

    
334
            editor.Command(commandParam.ToArray());
335

    
336
            List<long> newHandles = GetAllGroupHandles();
337
            List<long> otherHandles = new List<long>();
338
            foreach (var item in newHandles)
339
                if (!prevHandles.Contains(item))
340
                    otherHandles.Add(item);
341

    
342
            if (otherHandles.Count == 1)
343
                return otherHandles[0];
344
            else
345
                return 0;
346
        }
347
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
348
        {
349
            List<long> prevHandles = GetAllGroupHandles();
350
            GUIUtils.SetPipeStyle(style);
351

    
352
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
353
            Editor editor = acDoc.Editor;
354
            List<object> commandParam = new List<object>();
355
            //command name
356
            commandParam.Add("DRPIPE");
357
            //coordinate
358
            foreach (var item in coordinates)
359
                commandParam.Add(item);
360
            //enter
361
            commandParam.Add(null);
362

    
363
            //Input Object ID
364
            commandParam.Add(objectId);
365

    
366
            //enter
367
            commandParam.Add(null);
368

    
369
            editor.Command(commandParam.ToArray());
370

    
371
            List<long> newHandles = GetAllGroupHandles();
372
            List<long> otherHandles = new List<long>();
373
            foreach (var item in newHandles)
374
                if (!prevHandles.Contains(item))
375
                    otherHandles.Add(item);
376

    
377
            if (otherHandles.Count == 1)
378
                return otherHandles[0];
379
            else
380
                return 0;
381
        }
382
        private long DrawSignal(string style, List<string> coordinates)
383
        {
384
            List<long> prevHandles = GetAllGroupHandles();
385

    
386
            GUIUtils.SetPipeStyle(style);
387
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
388
            Editor editor = acDoc.Editor;
389
            List<object> commandParam = new List<object>();
390
            //command name
391
            commandParam.Add("VPESIGNAL");
392
            //coordinate
393
            foreach (var item in coordinates)
394
                commandParam.Add(item);
395
            //enter
396
            commandParam.Add(null);
397

    
398
            editor.Command(commandParam.ToArray());
399

    
400
            List<long> newHandles = GetAllGroupHandles();
401
            List<long> otherHandles = new List<long>();
402
            foreach (var item in newHandles)
403
                if (!prevHandles.Contains(item))
404
                    otherHandles.Add(item);
405

    
406
            if (otherHandles.Count == 1)
407
                return otherHandles[0];
408
            else
409
                return 0;
410
        }
411
        private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
412
        {
413
            long handle = 0;
414
            try
415
            {
416
                string insertSymbolName = symbol.Aveva.Name;
417
                double x = symbol.Aveva.X;
418
                double y = symbol.Aveva.Y;
419

    
420
                List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
421

    
422
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
423
                drawingData.InsertSymbolName = insertSymbolName;
424
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
425
                Editor editor = acDoc.Editor;
426
                List<object> commandParam = new List<object>();
427
                commandParam.Add("INSSYM");
428

    
429
                // Insert Point
430
                Point3d point = new Point3d(x, y, 0);
431
                commandParam.Add(point);
432

    
433
                // Check Insert Point
434
                if (needAngle)
435
                    commandParam.Add(symbol.ANGLE);
436

    
437
                // Check Balloon
438
                if (needBalloon)
439
                {
440
                    point = new Point3d(x + 20, y + 20, 0);
441
                    commandParam.Add(point);
442
                    
443
                    // Check LeaderLine
444
                    if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
445
                        commandParam.Add(x + "," + y);
446
                }
447

    
448
                for (int i = 0; i < 8; i++)
449
                    commandParam.Add(null);
450

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

    
453
                List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
454
                List<long> otherHandles = new List<long>();
455
                foreach (var item in newHandles)
456
                    if (!prevHandles.Contains(item))
457
                        otherHandles.Add(item);
458

    
459
                if (otherHandles.Count == 1)
460
                    return otherHandles[0];
461
                else
462
                    return 0;
463
            }
464
            catch (System.Exception ex)
465
            {
466

    
467
            }
468

    
469
            return handle;
470
        }
471
        private void DrawText(double x, double y, double height, string text)
472
        {
473
            // Get the current document and database
474
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
475
            Database acCurDb = acDoc.Database;
476

    
477
            // Start a transaction
478
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
479
            {
480
                // Open the Block table for read
481
                BlockTable acBlkTbl;
482
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
483
                                                OpenMode.ForRead) as BlockTable;
484

    
485
                // Open the Block table record Model space for write
486
                BlockTableRecord acBlkTblRec;
487
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
488
                                                OpenMode.ForWrite) as BlockTableRecord;
489

    
490
                // Create a single-line text object
491
                using (DBText acText = new DBText())
492
                {
493
                    acText.Position = new Point3d(x, y, 0);
494
                    acText.Height = height;
495
                    acText.TextString = text;
496

    
497
                    acBlkTblRec.AppendEntity(acText);
498
                    acTrans.AddNewlyCreatedDBObject(acText, true);
499
                }
500

    
501
                // Save the changes and dispose of the transaction
502
                acTrans.Commit();
503
            }
504
        }
505
        private void DrawMultiLineText(double x, double y, double width, string text)
506
        {
507
            // Get the current document and database
508
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
509
            Database acCurDb = acDoc.Database;
510

    
511
            // Start a transaction
512
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
513
            {
514
                // Open the Block table for read
515
                BlockTable acBlkTbl;
516
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
517
                                                OpenMode.ForRead) as BlockTable;
518

    
519
                // Open the Block table record Model space for write
520
                BlockTableRecord acBlkTblRec;
521
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
522
                                                OpenMode.ForWrite) as BlockTableRecord;
523

    
524
                // Create a multiline text object
525
                using (MText acMText = new MText())
526
                {
527
                    acMText.Location = new Point3d(x, y, 0);
528
                    acMText.Width = width;
529
                    acMText.Contents = text;
530

    
531
                    acBlkTblRec.AppendEntity(acMText);
532
                    acTrans.AddNewlyCreatedDBObject(acMText, true);
533
                }
534

    
535
                // Save the changes and dispose of the transaction
536
                acTrans.Commit();
537
            }
538
        }
539
        #endregion
540

    
541
        #region Autocad Utils
542
        private List<long> GetAllBlockHandlesByName(string name)
543
        {
544
            List<long> handles = new List<long>();
545

    
546
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
547
            Database acCurDb = acDoc.Database;
548
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
549
            {
550
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
551
                foreach (var entry in gd)
552
                {
553
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
554
                    if (blockTableRecord != null)
555
                    {
556
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
557
                        foreach (var item in records)
558
                        {
559
                            if (item.Name == name)
560
                                handles.Add(item.Handle.Value);
561
                        }
562
                    }
563
                }
564
                    
565
                acTrans.Commit();
566
            }
567
            return handles;
568
        }
569
        private List<long> GetAllGroupHandles()
570
        {
571
            List<long> handles = new List<long>();
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
                    handles.Add(entry.Value.Handle.Value);
580
                acTrans.Commit();
581
            }
582
            return handles;
583
        }
584
        private ObjectId GetFirstPolyLine(long groupHandle)
585
        {
586
            ObjectId result = ObjectId.Null;
587

    
588
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
589
            Database acCurDb = acDoc.Database;
590
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
591
            {
592
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
593
                foreach (var entry in gd)
594
                {
595
                    if (entry.Value.Handle.Value == groupHandle)
596
                    {
597
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
598
                        foreach (var item in group.GetAllEntityIds())
599
                        {
600
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
601
                            {
602
                                result = item;
603
                                break;
604
                            }
605
                        }
606
                        break;
607
                    }
608
                }
609
                acTrans.Commit();
610
            }
611

    
612
            return result;
613
        }
614
        private ObjectId GetObjectIdByHandle(long lHandle)
615
        {
616
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
617
            Database acCurDb = acDoc.Database;
618
            Handle handle = new Handle(lHandle);
619

    
620
            return acCurDb.GetObjectId(false, handle, 0);
621
        }
622
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
623
        {
624
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
625
            Database acCurDb = acDoc.Database;
626
            Editor acDocEd = acDoc.Editor;
627

    
628
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
629
            {
630
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
631
                if (objDB != null)
632
                {
633
                    if (objDB.GetType() == typeof(BlockReference))
634
                    {
635
                        BlockReference block = objDB as BlockReference;
636
                        block.Rotation = rotation;
637
                    }
638
                }
639
                acTrans.Commit();
640
            }
641
        }
642
        #endregion
643

    
644
        #region Modeling Utils
645
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
646
        {
647
            if (!group.Contains(line))
648
            {
649
                if (isInsert)
650
                    group.Insert(0, line);
651
                else
652
                    group.Add(line);
653

    
654
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
655
                {
656
                    object connObj = line.CONNECTORS[0].ConnectedObject;
657
                    if (connObj.GetType() == typeof(Model.Line) &&
658
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
659
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
660
                    else if (connObj.GetType() == typeof(Model.Symbol))
661
                    {
662
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
663
                        if (connLine != null)
664
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
665
                    }
666
                }
667
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
668
                {
669
                    object connObj = line.CONNECTORS[1].ConnectedObject;
670
                    if (connObj.GetType() == typeof(Model.Line) &&
671
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
672
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
673
                    else if (connObj.GetType() == typeof(Model.Symbol))
674
                    {
675
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
676
                        if (connLine != null)
677
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
678
                    }
679
                }
680
            }
681
        }
682
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
683
        {
684
            for (int i = 0; i < groupLine.Count; i++)
685
            {
686
                Model.Line line = groupLine[i];
687
                if (i == 0)
688
                {
689
                    object connObj = line.CONNECTORS[0].ConnectedObject;
690
                    if (connObj != null)
691
                    {
692
                        string point = GetPointByConnectedItem(connObj, line, true);
693
                        if (string.IsNullOrEmpty(point))
694
                            points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
695
                        else
696
                            points.Add(point);
697
                    }
698
                    else
699
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
700
                }
701
                else
702
                {
703
                    Model.Line prevLine = groupLine[i - 1];
704
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
705
                        prevLine.SlopeType == SlopeType.VERTICAL)
706
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
707
                    else if (line.SlopeType == SlopeType.VERTICAL &&
708
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
709
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
710
                    else
711
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
712
                }
713

    
714
                if (i == groupLine.Count - 1)
715
                {
716
                    object connObj = line.CONNECTORS[1].ConnectedObject;
717
                    if (connObj != null && connObj.GetType() == typeof(Symbol))
718
                    {
719
                        string point = GetPointByConnectedItem(connObj, line, false);
720
                        if (string.IsNullOrEmpty(point))
721
                            points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
722
                        else
723
                            points.Add(point);
724
                    }
725
                    else
726
                        points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
727
                }
728
            }
729
        }
730
        private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
731
        {
732
            string result = string.Empty;
733
            double x;
734
            double y;
735
            if (isStart)
736
            {
737
                x = line.Aveva.Start_X;
738
                y = line.Aveva.Start_Y;
739
            }
740
            else
741
            {
742
                x = line.Aveva.End_X;
743
                y = line.Aveva.End_Y;
744
            }
745

    
746
            if (connObj.GetType() == typeof(Symbol))
747
            {
748
                Symbol connSymbol = connObj as Symbol;
749
                Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
750

    
751
                SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
752
                if (slopeType == SlopeType.HORIZONTAL)
753
                    result = connSymbol.Aveva.X + "," + y;
754
                else if (slopeType == SlopeType.VERTICAL)
755
                    result = x + "," + connSymbol.Aveva.Y;
756
                else
757
                    result = x + "," + y;
758
            }
759
            else if (connObj.GetType() == typeof(OPC))
760
            {
761
                OPC connOPC = connObj as OPC;
762
                Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
763

    
764
                double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
765
                APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
766
                APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
767
                locX = locX + sizeWidth / 2;
768
                locY = locY + sizeHeight / 2;
769
                aX = locX;
770
                aY = locY;
771
                document.ConvertAvevaPoint(ref aX, ref aY);
772

    
773
                SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
774
                if (slopeType == SlopeType.HORIZONTAL)
775
                    result = aX + "," + y;
776
                else if (slopeType == SlopeType.VERTICAL)
777
                    result = x + "," + aY;
778
                else
779
                    result = x + "," + y;
780
            }
781
            
782
            return result;
783
        }
784
        private bool IsExistEndBreak(object item1, object item2)
785
        {
786
            bool result = false;
787
            if (item1 != null && item2 != null)
788
            {
789
                EndBreak endBreak = document.EndBreaks.Find(x =>
790
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
791
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
792
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
793
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
794

    
795
                if (endBreak != null)
796
                    result = true;
797
            }
798

    
799
            return result;
800
        }
801
        private LineRun FindLineRun(Model.Line line)
802
        {
803
            List<LineRun> allLineRun = new List<LineRun>();
804
            foreach (var item in document.LINENUMBERS)
805
                allLineRun.AddRange(item.RUNS);
806
            foreach (var item in document.TRIMLINES)
807
                allLineRun.AddRange(item.RUNS);
808

    
809
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
810
        }
811
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
812
        {
813
            Model.Line result = null;
814
            LineRun run = FindLineRun(line);
815
            Connector connector = symbol.CONNECTORS.Find(x =>
816
            x.ConnectedObject != null &&
817
            x.ConnectedObject != line &&
818
            run.RUNITEMS.Contains(x.ConnectedObject) &&
819
            x.ConnectedObject.GetType() == typeof(Model.Line));
820

    
821
            if (connector != null)
822
                result = connector.ConnectedObject as Model.Line;
823
            
824
            return result;
825
        }
826
        private bool CheckAngle(DataRow symbolRow)
827
        {
828
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
829
        }
830
        private bool CheckBalloon(Symbol symbol)
831
        {
832
            bool result = false;
833

    
834
            string text = string.Empty;
835
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
836
            if (symbolDefinitions.Count != 0)
837
            {
838
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
839

    
840
                if (text.Equals("VLP"))
841
                    text = "VPO";
842
                else if (text.Equals("IVP"))
843
                    text = "IPO";
844

    
845
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
846
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
847
            }
848

    
849
            return result;
850
        }
851
        #endregion
852

    
853
        #region Test Source
854
        public void test()
855
        {
856
            //InitGUI();
857
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
858

    
859
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
860
            return;
861
            Symbol symbol = new Symbol();
862
            symbol.Aveva = new AvevaSymbolInfo();
863
            symbol.Aveva.Name = "INVB";
864
            symbol.Aveva.X = 50;
865
            symbol.Aveva.Y = 100;
866

    
867
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
868
            if (allRows.Length == 1)
869
            {
870
                DataRow symbolRow = allRows[0];
871
                bool bAngle = CheckAngle(symbolRow);
872
                bool bBalloon = CheckBalloon(symbol);
873
                bBalloon = false;
874
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
875
            }
876

    
877
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
878
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
879
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
880
            {
881
                bool bIsPromptRotate = true;
882
            }
883
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
884

    
885
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
886
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
887
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
888

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

    
891
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
892
        }
893
        public static void TESTStatic()
894
        {
895
            System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable();
896
            AutoModeling auto = new AutoModeling(null, avevaSymbolTable);
897
            auto.test();
898
        }
899
        #endregion
900
    }
901

    
902

    
903
    public static class AcDbLinqExtensionMethods
904
    {
905
        /// <summary>
906
        /// Get all references to the given BlockTableRecord, including 
907
        /// references to anonymous dynamic BlockTableRecords.
908
        /// </summary>
909

    
910
        public static IEnumerable<BlockReference> GetBlockReferences(
911
           this BlockTableRecord btr,
912
           OpenMode mode = OpenMode.ForRead,
913
           bool directOnly = true)
914
        {
915
            if (btr == null)
916
                throw new ArgumentNullException("btr");
917
            var tr = btr.Database.TransactionManager.TopTransaction;
918
            if (tr == null)
919
                throw new InvalidOperationException("No transaction");
920
            var ids = btr.GetBlockReferenceIds(directOnly, true);
921
            int cnt = ids.Count;
922
            for (int i = 0; i < cnt; i++)
923
            {
924
                yield return (BlockReference)
925
                   tr.GetObject(ids[i], mode, false, false);
926
            }
927
            if (btr.IsDynamicBlock)
928
            {
929
                BlockTableRecord btr2 = null;
930
                var blockIds = btr.GetAnonymousBlockIds();
931
                cnt = blockIds.Count;
932
                for (int i = 0; i < cnt; i++)
933
                {
934
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
935
                       OpenMode.ForRead, false, false);
936
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
937
                    int cnt2 = ids.Count;
938
                    for (int j = 0; j < cnt2; j++)
939
                    {
940
                        yield return (BlockReference)
941
                           tr.GetObject(ids[j], mode, false, false);
942
                    }
943
                }
944
            }
945
        }
946
    }
947
}
클립보드 이미지 추가 (최대 크기: 500 MB)