프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 495bb8f5

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

    
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
namespace AVEVA.PID.CustomizationUtility
31
{
32
    public class AutoModeling
33
    {
34
        public static AutoModeling Running { get; set; }
35
        Model.Document document;
36

    
37
        System.Data.DataTable AvevaSymbolTable = null;
38

    
39
        public AutoModeling(Model.Document document, System.Data.DataTable AvevaSymbolTable)
40
        {
41
            this.document = document;
42
            this.AvevaSymbolTable = AvevaSymbolTable;
43
        }
44

    
45
        public void CreateDrawing()
46
        {
47
            string outPath = Project_DB.GetDirectiveValue("DRGPTH");
48
            string tempPath = Path.GetTempPath();
49
            string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT");
50

    
51
            DWTSelector.strDrawingNumber = document.AvevaDrawingNumber;
52
            DWTSelector.strSheetNumber = document.AvevaSheetNumber;
53
            DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber;
54
            DWTSelector.strCadFilePath = outPath;
55

    
56
            try
57
            {
58
                string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false);
59
                Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
60
                Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text);
61

    
62
                Commands.addLatestPidvesrionToDictionary();
63
                AVVPropCmd.ActivateAcad();
64

    
65
                if (acadDocument != null)
66
                {
67
                    Running = this;
68
                    acadApplication.ActiveDocument = acadDocument;
69
                    acadDocument.SendCommand("QSAVE ");
70
                    acadDocument.SendCommand("APPIDRun ");
71
                    acadDocument.SendCommand("QSAVE ");
72
                    Running = null;
73
                }
74
            }
75
            catch (System.Exception ex)
76
            {
77
                
78
            }
79

    
80
            GC.Collect();
81
        }
82
        public void Run()
83
        {
84
            try
85
            {
86
                RunLineModeling();
87
                RunSymbolModeling();
88
            }
89
            catch (System.Exception ex)
90
            {
91
                System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace);
92
            }
93
        }
94

    
95
        #region Run Method
96
        private void RunLineModeling()
97
        {
98
            foreach (var item in document.LINES)
99
            {
100
                if (item.Aveva.Handle == 0)
101
                    LineModeling(item);
102
            }
103
        }
104
        private void RunSymbolModeling()
105
        {
106
            foreach (var item in document.SYMBOLS)
107
            {
108
                if (item.Aveva.Handle == 0)
109
                    SymbolModeling(item);
110
            }
111
        }
112
        #endregion
113

    
114
        #region Modeling Method
115
        private void LineModeling(Model.Line line)
116
        {
117
            List<Model.Line> groupLine = new List<Model.Line>();
118
            List<string> points = new List<string>();
119
            GetConnectedGroupLine(line, groupLine, false);
120
            GetGroupLinePoints(groupLine, points);
121

    
122
            long handle = 0;
123
            if (line.Aveva.Type == Model.Type.Pipe)
124
                handle = DrawPipe(line.Aveva.Name, points);
125
            else if (line.Aveva.Type == Model.Type.Signal)
126
                handle = DrawSignal(line.Aveva.Name, points);
127

    
128
            foreach (var item in groupLine)
129
                item.Aveva.Handle = handle;
130
        }
131
        private void SymbolModeling(Symbol symbol)
132
        {
133
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
134
            if (allRows.Length == 1)
135
            {
136
                DataRow symbolRow = allRows[0];
137
                bool bAngle = CheckAngle(symbolRow);
138
                bool bBalloon = CheckBalloon(symbol);
139

    
140
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
141
                if (handle != 0)
142
                {
143
                    symbol.Aveva.Handle = handle;
144

    
145
                    if (!bAngle)
146
                    {
147
                        ObjectId objectId = GetObjectIdByHandle(handle);
148
                        if (objectId != ObjectId.Null)
149
                        {
150
                            SetBlockReferenceRotation(objectId, symbol.ANGLE);
151
                        }
152
                    }
153
                }
154
            }
155
        }
156
        #endregion
157

    
158
        #region Drawing Method
159
        private long DrawPipe(string style, List<string> coordinates)
160
        {
161
            List<long> prevHandles = GetAllGroupHandles();
162

    
163
            GUIUtils.SetPipeStyle(style);
164
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
165
            Editor editor = acDoc.Editor;
166
            List<object> commandParam = new List<object>();
167
            //command name
168
            commandParam.Add("DRPIPE");
169
            //coordinate
170
            foreach (var item in coordinates)
171
                commandParam.Add(item);
172
            //enter
173
            commandParam.Add(null);
174
            //enter
175
            commandParam.Add(null);
176

    
177
            editor.Command(commandParam.ToArray());
178

    
179
            List<long> newHandles = GetAllGroupHandles();
180
            List<long> otherHandles = new List<long>();
181
            foreach (var item in newHandles)
182
                if (!prevHandles.Contains(item))
183
                    otherHandles.Add(item);
184

    
185
            if (otherHandles.Count == 1)
186
                return otherHandles[0];
187
            else
188
                return 0;
189
        }
190
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
191
        {
192
            List<long> prevHandles = GetAllGroupHandles();
193
            GUIUtils.SetPipeStyle(style);
194

    
195
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
196
            Editor editor = acDoc.Editor;
197
            List<object> commandParam = new List<object>();
198
            //command name
199
            commandParam.Add("DRPIPE");
200
            //coordinate
201
            foreach (var item in coordinates)
202
                commandParam.Add(item);
203
            //enter
204
            commandParam.Add(null);
205

    
206
            //Input Object ID
207
            commandParam.Add(objectId);
208

    
209
            //enter
210
            commandParam.Add(null);
211

    
212
            editor.Command(commandParam.ToArray());
213

    
214
            List<long> newHandles = GetAllGroupHandles();
215
            List<long> otherHandles = new List<long>();
216
            foreach (var item in newHandles)
217
                if (!prevHandles.Contains(item))
218
                    otherHandles.Add(item);
219

    
220
            if (otherHandles.Count == 1)
221
                return otherHandles[0];
222
            else
223
                return 0;
224
        }
225
        private long DrawSignal(string style, List<string> coordinates)
226
        {
227
            List<long> prevHandles = GetAllGroupHandles();
228

    
229
            GUIUtils.SetPipeStyle(style);
230
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
231
            Editor editor = acDoc.Editor;
232
            List<object> commandParam = new List<object>();
233
            //command name
234
            commandParam.Add("VPESIGNAL");
235
            //coordinate
236
            foreach (var item in coordinates)
237
                commandParam.Add(item);
238
            //enter
239
            commandParam.Add(null);
240

    
241
            editor.Command(commandParam.ToArray());
242

    
243
            List<long> newHandles = GetAllGroupHandles();
244
            List<long> otherHandles = new List<long>();
245
            foreach (var item in newHandles)
246
                if (!prevHandles.Contains(item))
247
                    otherHandles.Add(item);
248

    
249
            if (otherHandles.Count == 1)
250
                return otherHandles[0];
251
            else
252
                return 0;
253
        }
254
        private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
255
        {
256
            long handle = 0;
257
            try
258
            {
259
                string insertSymbolName = symbol.Aveva.Name;
260
                double x = symbol.Aveva.X;
261
                double y = symbol.Aveva.Y;
262

    
263
                List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
264

    
265
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
266
                drawingData.InsertSymbolName = insertSymbolName;
267
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
268
                Editor editor = acDoc.Editor;
269
                List<object> commandParam = new List<object>();
270
                commandParam.Add("INSSYM");
271

    
272
                // Insert Point
273
                Point3d point = new Point3d(x, y, 0);
274
                commandParam.Add(point);
275

    
276
                // Check Insert Point
277
                if (needAngle)
278
                    commandParam.Add(symbol.ANGLE);
279

    
280
                // Check Balloon
281
                if (needBalloon)
282
                {
283
                    point = new Point3d(x + 20, y + 20, 0);
284
                    commandParam.Add(point);
285
                    
286
                    // Check LeaderLine
287
                    if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
288
                        commandParam.Add(x + "," + y);
289
                }
290

    
291
                for (int i = 0; i < 8; i++)
292
                    commandParam.Add(null);
293

    
294
                editor.Command(commandParam.ToArray());
295

    
296
                List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
297
                List<long> otherHandles = new List<long>();
298
                foreach (var item in newHandles)
299
                    if (!prevHandles.Contains(item))
300
                        otherHandles.Add(item);
301

    
302
                if (otherHandles.Count == 1)
303
                    return otherHandles[0];
304
                else
305
                    return 0;
306
            }
307
            catch (System.Exception ex)
308
            {
309

    
310
            }
311

    
312
            return handle;
313
        }
314
        #endregion
315

    
316
        #region Autocad Utils
317
        private List<long> GetAllBlockHandlesByName(string name)
318
        {
319
            List<long> handles = new List<long>();
320

    
321
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
322
            Database acCurDb = acDoc.Database;
323
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
324
            {
325
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
326
                foreach (var entry in gd)
327
                {
328
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
329
                    if (blockTableRecord != null)
330
                    {
331
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
332
                        foreach (var item in records)
333
                        {
334
                            if (item.Name == name)
335
                                handles.Add(item.Handle.Value);
336
                        }
337
                    }
338
                }
339
                    
340
                acTrans.Commit();
341
            }
342
            return handles;
343
        }
344
        private List<long> GetAllGroupHandles()
345
        {
346
            List<long> handles = new List<long>();
347

    
348
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
349
            Database acCurDb = acDoc.Database;
350
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
351
            {
352
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
353
                foreach (var entry in gd)
354
                    handles.Add(entry.Value.Handle.Value);
355
                acTrans.Commit();
356
            }
357
            return handles;
358
        }
359
        private ObjectId GetFirstPolyLine(long groupHandle)
360
        {
361
            ObjectId result = ObjectId.Null;
362

    
363
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
364
            Database acCurDb = acDoc.Database;
365
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
366
            {
367
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
368
                foreach (var entry in gd)
369
                {
370
                    if (entry.Value.Handle.Value == groupHandle)
371
                    {
372
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
373
                        foreach (var item in group.GetAllEntityIds())
374
                        {
375
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
376
                            {
377
                                result = item;
378
                                break;
379
                            }
380
                        }
381
                        break;
382
                    }
383
                }
384
                acTrans.Commit();
385
            }
386

    
387
            return result;
388
        }
389
        private ObjectId GetObjectIdByHandle(long lHandle)
390
        {
391
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
392
            Database acCurDb = acDoc.Database;
393
            Handle handle = new Handle(lHandle);
394

    
395
            return acCurDb.GetObjectId(false, handle, 0);
396
        }
397
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
398
        {
399
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
400
            Database acCurDb = acDoc.Database;
401
            Editor acDocEd = acDoc.Editor;
402

    
403
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
404
            {
405
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
406
                if (objDB != null)
407
                {
408
                    if (objDB.GetType() == typeof(BlockReference))
409
                    {
410
                        BlockReference block = objDB as BlockReference;
411
                        block.Rotation = rotation;
412
                    }
413
                }
414
                acTrans.Commit();
415
            }
416
        }
417
        #endregion
418

    
419
        #region Modeling Utils
420
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
421
        {
422
            if (!group.Contains(line))
423
            {
424
                if (isInsert)
425
                    group.Insert(0, line);
426
                else
427
                    group.Add(line);
428

    
429
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
430
                {
431
                    object connObj = line.CONNECTORS[0].ConnectedObject;
432
                    if (connObj.GetType() == typeof(Model.Line) &&
433
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
434
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
435
                    else if (connObj.GetType() == typeof(Model.Symbol))
436
                    {
437
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
438
                        if (connLine != null)
439
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
440
                    }
441
                }
442
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
443
                {
444
                    object connObj = line.CONNECTORS[1].ConnectedObject;
445
                    if (connObj.GetType() == typeof(Model.Line) &&
446
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
447
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
448
                    else if (connObj.GetType() == typeof(Model.Symbol))
449
                    {
450
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
451
                        if (connLine != null)
452
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
453
                    }
454
                }
455
            }
456
        }
457
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
458
        {
459
            for (int i = 0; i < groupLine.Count; i++)
460
            {
461
                Model.Line line = groupLine[i];
462
                if (i == 0)
463
                    points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
464
                else
465
                {
466
                    Model.Line prevLine = groupLine[i - 1];
467
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
468
                        prevLine.SlopeType == SlopeType.VERTICAL)
469
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
470
                    else if (line.SlopeType == SlopeType.VERTICAL &&
471
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
472
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
473
                    else
474
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
475
                }
476

    
477
                if (i == groupLine.Count - 1)
478
                    points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
479
            }
480
        }
481
        private bool IsExistEndBreak(object item1, object item2)
482
        {
483
            bool result = false;
484
            if (item1 != null && item2 != null)
485
            {
486
                EndBreak endBreak = document.EndBreaks.Find(x =>
487
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
488
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
489
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
490
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
491

    
492
                if (endBreak != null)
493
                    result = true;
494
            }
495

    
496
            return result;
497
        }
498
        private LineRun FindLineRun(Model.Line line)
499
        {
500
            List<LineRun> allLineRun = new List<LineRun>();
501
            foreach (var item in document.LINENUMBERS)
502
                allLineRun.AddRange(item.RUNS);
503
            foreach (var item in document.TRIMLINES)
504
                allLineRun.AddRange(item.RUNS);
505

    
506
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
507
        }
508
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
509
        {
510
            Model.Line result = null;
511
            LineRun run = FindLineRun(line);
512
            Connector connector = symbol.CONNECTORS.Find(x =>
513
            x.ConnectedObject != null &&
514
            x.ConnectedObject != line &&
515
            run.RUNITEMS.Contains(x.ConnectedObject) &&
516
            x.ConnectedObject.GetType() == typeof(Model.Line));
517

    
518
            if (connector != null)
519
                result = connector.ConnectedObject as Model.Line;
520
            
521
            return result;
522
        }
523
        private bool CheckAngle(DataRow symbolRow)
524
        {
525
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
526
        }
527
        private bool CheckBalloon(Symbol symbol)
528
        {
529
            bool result = false;
530

    
531
            string text = string.Empty;
532
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
533
            if (symbolDefinitions.Count != 0)
534
            {
535
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
536

    
537
                if (text.Equals("VLP"))
538
                    text = "VPO";
539
                else if (text.Equals("IVP"))
540
                    text = "IPO";
541

    
542
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
543
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
544
            }
545

    
546
            return result;
547
        }
548
        #endregion
549

    
550
        #region Test Source
551
        public void test()
552
        {
553
            //InitGUI();
554
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
555

    
556
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" });
557
            Symbol symbol = new Symbol();
558
            symbol.Aveva = new AvevaSymbolInfo();
559
            symbol.Aveva.Name = "INVB";
560
            symbol.Aveva.X = 50;
561
            symbol.Aveva.Y = 100;
562

    
563
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
564
            if (allRows.Length == 1)
565
            {
566
                DataRow symbolRow = allRows[0];
567
                bool bAngle = CheckAngle(symbolRow);
568
                bool bBalloon = CheckBalloon(symbol);
569
                bBalloon = false;
570
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
571
            }
572

    
573
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
574
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
575
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
576
            {
577
                bool bIsPromptRotate = true;
578
            }
579
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
580

    
581
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
582
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
583
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
584

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

    
587
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
588
        }
589
        public static void TESTStatic()
590
        {
591
            System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable();
592
            AutoModeling auto = new AutoModeling(null, avevaSymbolTable);
593
            auto.test();
594
        }
595
        #endregion
596
    }
597

    
598

    
599
    public static class AcDbLinqExtensionMethods
600
    {
601
        /// <summary>
602
        /// Get all references to the given BlockTableRecord, including 
603
        /// references to anonymous dynamic BlockTableRecords.
604
        /// </summary>
605

    
606
        public static IEnumerable<BlockReference> GetBlockReferences(
607
           this BlockTableRecord btr,
608
           OpenMode mode = OpenMode.ForRead,
609
           bool directOnly = true)
610
        {
611
            if (btr == null)
612
                throw new ArgumentNullException("btr");
613
            var tr = btr.Database.TransactionManager.TopTransaction;
614
            if (tr == null)
615
                throw new InvalidOperationException("No transaction");
616
            var ids = btr.GetBlockReferenceIds(directOnly, true);
617
            int cnt = ids.Count;
618
            for (int i = 0; i < cnt; i++)
619
            {
620
                yield return (BlockReference)
621
                   tr.GetObject(ids[i], mode, false, false);
622
            }
623
            if (btr.IsDynamicBlock)
624
            {
625
                BlockTableRecord btr2 = null;
626
                var blockIds = btr.GetAnonymousBlockIds();
627
                cnt = blockIds.Count;
628
                for (int i = 0; i < cnt; i++)
629
                {
630
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
631
                       OpenMode.ForRead, false, false);
632
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
633
                    int cnt2 = ids.Count;
634
                    for (int j = 0; j < cnt2; j++)
635
                    {
636
                        yield return (BlockReference)
637
                           tr.GetObject(ids[j], mode, false, false);
638
                    }
639
                }
640
            }
641
        }
642
    }
643
}
클립보드 이미지 추가 (최대 크기: 500 MB)