프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 2e409996

이력 | 보기 | 이력해설 | 다운로드 (36.5 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
        #endregion
506

    
507
        #region Autocad Utils
508
        private List<long> GetAllBlockHandlesByName(string name)
509
        {
510
            List<long> handles = new List<long>();
511

    
512
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
513
            Database acCurDb = acDoc.Database;
514
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
515
            {
516
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
517
                foreach (var entry in gd)
518
                {
519
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
520
                    if (blockTableRecord != null)
521
                    {
522
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
523
                        foreach (var item in records)
524
                        {
525
                            if (item.Name == name)
526
                                handles.Add(item.Handle.Value);
527
                        }
528
                    }
529
                }
530
                    
531
                acTrans.Commit();
532
            }
533
            return handles;
534
        }
535
        private List<long> GetAllGroupHandles()
536
        {
537
            List<long> handles = new List<long>();
538

    
539
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
540
            Database acCurDb = acDoc.Database;
541
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
542
            {
543
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
544
                foreach (var entry in gd)
545
                    handles.Add(entry.Value.Handle.Value);
546
                acTrans.Commit();
547
            }
548
            return handles;
549
        }
550
        private ObjectId GetFirstPolyLine(long groupHandle)
551
        {
552
            ObjectId result = ObjectId.Null;
553

    
554
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
555
            Database acCurDb = acDoc.Database;
556
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
557
            {
558
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
559
                foreach (var entry in gd)
560
                {
561
                    if (entry.Value.Handle.Value == groupHandle)
562
                    {
563
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
564
                        foreach (var item in group.GetAllEntityIds())
565
                        {
566
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
567
                            {
568
                                result = item;
569
                                break;
570
                            }
571
                        }
572
                        break;
573
                    }
574
                }
575
                acTrans.Commit();
576
            }
577

    
578
            return result;
579
        }
580
        private ObjectId GetObjectIdByHandle(long lHandle)
581
        {
582
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
583
            Database acCurDb = acDoc.Database;
584
            Handle handle = new Handle(lHandle);
585

    
586
            return acCurDb.GetObjectId(false, handle, 0);
587
        }
588
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
589
        {
590
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
591
            Database acCurDb = acDoc.Database;
592
            Editor acDocEd = acDoc.Editor;
593

    
594
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
595
            {
596
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
597
                if (objDB != null)
598
                {
599
                    if (objDB.GetType() == typeof(BlockReference))
600
                    {
601
                        BlockReference block = objDB as BlockReference;
602
                        block.Rotation = rotation;
603
                    }
604
                }
605
                acTrans.Commit();
606
            }
607
        }
608
        #endregion
609

    
610
        #region Modeling Utils
611
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
612
        {
613
            if (!group.Contains(line))
614
            {
615
                if (isInsert)
616
                    group.Insert(0, line);
617
                else
618
                    group.Add(line);
619

    
620
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
621
                {
622
                    object connObj = line.CONNECTORS[0].ConnectedObject;
623
                    if (connObj.GetType() == typeof(Model.Line) &&
624
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
625
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
626
                    else if (connObj.GetType() == typeof(Model.Symbol))
627
                    {
628
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
629
                        if (connLine != null)
630
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
631
                    }
632
                }
633
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
634
                {
635
                    object connObj = line.CONNECTORS[1].ConnectedObject;
636
                    if (connObj.GetType() == typeof(Model.Line) &&
637
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
638
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
639
                    else if (connObj.GetType() == typeof(Model.Symbol))
640
                    {
641
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
642
                        if (connLine != null)
643
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
644
                    }
645
                }
646
            }
647
        }
648
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
649
        {
650
            for (int i = 0; i < groupLine.Count; i++)
651
            {
652
                Model.Line line = groupLine[i];
653
                if (i == 0)
654
                {
655
                    object connObj = line.CONNECTORS[0].ConnectedObject;
656
                    if (connObj != null)
657
                    {
658
                        string point = GetPointByConnectedItem(connObj, line, true);
659
                        if (string.IsNullOrEmpty(point))
660
                            points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
661
                        else
662
                            points.Add(point);
663
                    }
664
                    else
665
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
666
                }
667
                else
668
                {
669
                    Model.Line prevLine = groupLine[i - 1];
670
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
671
                        prevLine.SlopeType == SlopeType.VERTICAL)
672
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
673
                    else if (line.SlopeType == SlopeType.VERTICAL &&
674
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
675
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
676
                    else
677
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
678
                }
679

    
680
                if (i == groupLine.Count - 1)
681
                {
682
                    object connObj = line.CONNECTORS[1].ConnectedObject;
683
                    if (connObj != null && connObj.GetType() == typeof(Symbol))
684
                    {
685
                        string point = GetPointByConnectedItem(connObj, line, false);
686
                        if (string.IsNullOrEmpty(point))
687
                            points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
688
                        else
689
                            points.Add(point);
690
                    }
691
                    else
692
                        points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
693
                }
694
            }
695
        }
696
        private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
697
        {
698
            string result = string.Empty;
699
            double x;
700
            double y;
701
            if (isStart)
702
            {
703
                x = line.Aveva.Start_X;
704
                y = line.Aveva.Start_Y;
705
            }
706
            else
707
            {
708
                x = line.Aveva.End_X;
709
                y = line.Aveva.End_Y;
710
            }
711

    
712
            if (connObj.GetType() == typeof(Symbol))
713
            {
714
                Symbol connSymbol = connObj as Symbol;
715
                Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
716

    
717
                SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
718
                if (slopeType == SlopeType.HORIZONTAL)
719
                    result = connSymbol.Aveva.X + "," + y;
720
                else if (slopeType == SlopeType.VERTICAL)
721
                    result = x + "," + connSymbol.Aveva.Y;
722
                else
723
                    result = x + "," + y;
724
            }
725
            else if (connObj.GetType() == typeof(OPC))
726
            {
727
                OPC connOPC = connObj as OPC;
728
                Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
729

    
730
                double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
731
                APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
732
                APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
733
                locX = locX + sizeWidth / 2;
734
                locY = locY + sizeHeight / 2;
735
                aX = locX;
736
                aY = locY;
737
                document.ConvertAvevaPoint(ref aX, ref aY);
738

    
739
                SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
740
                if (slopeType == SlopeType.HORIZONTAL)
741
                    result = aX + "," + y;
742
                else if (slopeType == SlopeType.VERTICAL)
743
                    result = x + "," + aY;
744
                else
745
                    result = x + "," + y;
746
            }
747
            
748
            return result;
749
        }
750
        private bool IsExistEndBreak(object item1, object item2)
751
        {
752
            bool result = false;
753
            if (item1 != null && item2 != null)
754
            {
755
                EndBreak endBreak = document.EndBreaks.Find(x =>
756
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
757
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
758
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
759
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
760

    
761
                if (endBreak != null)
762
                    result = true;
763
            }
764

    
765
            return result;
766
        }
767
        private LineRun FindLineRun(Model.Line line)
768
        {
769
            List<LineRun> allLineRun = new List<LineRun>();
770
            foreach (var item in document.LINENUMBERS)
771
                allLineRun.AddRange(item.RUNS);
772
            foreach (var item in document.TRIMLINES)
773
                allLineRun.AddRange(item.RUNS);
774

    
775
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
776
        }
777
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
778
        {
779
            Model.Line result = null;
780
            LineRun run = FindLineRun(line);
781
            Connector connector = symbol.CONNECTORS.Find(x =>
782
            x.ConnectedObject != null &&
783
            x.ConnectedObject != line &&
784
            run.RUNITEMS.Contains(x.ConnectedObject) &&
785
            x.ConnectedObject.GetType() == typeof(Model.Line));
786

    
787
            if (connector != null)
788
                result = connector.ConnectedObject as Model.Line;
789
            
790
            return result;
791
        }
792
        private bool CheckAngle(DataRow symbolRow)
793
        {
794
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
795
        }
796
        private bool CheckBalloon(Symbol symbol)
797
        {
798
            bool result = false;
799

    
800
            string text = string.Empty;
801
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
802
            if (symbolDefinitions.Count != 0)
803
            {
804
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
805

    
806
                if (text.Equals("VLP"))
807
                    text = "VPO";
808
                else if (text.Equals("IVP"))
809
                    text = "IPO";
810

    
811
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
812
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
813
            }
814

    
815
            return result;
816
        }
817
        #endregion
818

    
819
        #region Test Source
820
        public void test()
821
        {
822
            //InitGUI();
823
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
824

    
825
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
826
            return;
827
            Symbol symbol = new Symbol();
828
            symbol.Aveva = new AvevaSymbolInfo();
829
            symbol.Aveva.Name = "INVB";
830
            symbol.Aveva.X = 50;
831
            symbol.Aveva.Y = 100;
832

    
833
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
834
            if (allRows.Length == 1)
835
            {
836
                DataRow symbolRow = allRows[0];
837
                bool bAngle = CheckAngle(symbolRow);
838
                bool bBalloon = CheckBalloon(symbol);
839
                bBalloon = false;
840
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
841
            }
842

    
843
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
844
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
845
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
846
            {
847
                bool bIsPromptRotate = true;
848
            }
849
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
850

    
851
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
852
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
853
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
854

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

    
857
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
858
        }
859
        public static void TESTStatic()
860
        {
861
            System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable();
862
            AutoModeling auto = new AutoModeling(null, avevaSymbolTable);
863
            auto.test();
864
        }
865
        #endregion
866
    }
867

    
868

    
869
    public static class AcDbLinqExtensionMethods
870
    {
871
        /// <summary>
872
        /// Get all references to the given BlockTableRecord, including 
873
        /// references to anonymous dynamic BlockTableRecords.
874
        /// </summary>
875

    
876
        public static IEnumerable<BlockReference> GetBlockReferences(
877
           this BlockTableRecord btr,
878
           OpenMode mode = OpenMode.ForRead,
879
           bool directOnly = true)
880
        {
881
            if (btr == null)
882
                throw new ArgumentNullException("btr");
883
            var tr = btr.Database.TransactionManager.TopTransaction;
884
            if (tr == null)
885
                throw new InvalidOperationException("No transaction");
886
            var ids = btr.GetBlockReferenceIds(directOnly, true);
887
            int cnt = ids.Count;
888
            for (int i = 0; i < cnt; i++)
889
            {
890
                yield return (BlockReference)
891
                   tr.GetObject(ids[i], mode, false, false);
892
            }
893
            if (btr.IsDynamicBlock)
894
            {
895
                BlockTableRecord btr2 = null;
896
                var blockIds = btr.GetAnonymousBlockIds();
897
                cnt = blockIds.Count;
898
                for (int i = 0; i < cnt; i++)
899
                {
900
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
901
                       OpenMode.ForRead, false, false);
902
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
903
                    int cnt2 = ids.Count;
904
                    for (int j = 0; j < cnt2; j++)
905
                    {
906
                        yield return (BlockReference)
907
                           tr.GetObject(ids[j], mode, false, false);
908
                    }
909
                }
910
            }
911
        }
912
    }
913
}
클립보드 이미지 추가 (최대 크기: 500 MB)