프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 475071f2

이력 | 보기 | 이력해설 | 다운로드 (32.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, 4);
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
            }
114
            catch (System.Exception ex)
115
            {
116
                System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace);
117
            }
118
            finally
119
            {
120
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.ClearParent, null);
121
                SplashScreenManager.CloseForm(false);
122
            }
123
        }
124

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

    
135
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
136
            }
137
        }
138
        private void RunOPCModeling()
139
        {
140
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count);
141
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling");
142
            foreach (var item in document.OPCs)
143
            {
144
                if (item.Aveva.Handle == 0)
145
                    SymbolModeling(item);
146
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
147
            }
148
        }
149
        private void RunSymbolModeling()
150
        {
151
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
152
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
153
            foreach (var item in document.SYMBOLS)
154
            {
155
                if (item.Aveva.Handle == 0)
156
                    SymbolModeling(item);
157
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
158
            }
159
        }
160
        private void RunTextModeling()
161
        {
162
            //SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.text.Count);
163
            //SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling");
164
            //foreach (var item in document.LINES)
165
            //{
166
            //    if (item.Aveva.Handle == 0)
167
            //        LineModeling(item);
168

    
169
            //    SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
170
            //}
171
        }
172
        #endregion
173

    
174
        #region Modeling Method
175
        private void LineModeling(Model.Line line)
176
        {
177
            List<Model.Line> groupLine = new List<Model.Line>();
178
            List<string> points = new List<string>();
179
            GetConnectedGroupLine(line, groupLine, false);
180
            GetGroupLinePoints(groupLine, points);
181

    
182
            LineNumber lineNumber = null;
183
            if (line.Aveva.Type == Model.Type.Pipe)
184
            {
185
                foreach (var item in groupLine)
186
                {
187
                    lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID);
188
                    if (lineNumber != null)
189
                    {
190
                        GUIUtils.SetAutoLabelCheck(true);
191
                        break;
192
                    }
193
                }
194
            }
195

    
196

    
197
            long handle = 0;
198
            if (line.Aveva.Type == Model.Type.Pipe)
199
                handle = DrawPipe(line.Aveva.Name, points, lineNumber);
200
            else if (line.Aveva.Type == Model.Type.Signal)
201
                handle = DrawSignal(line.Aveva.Name, points);
202

    
203
            if (lineNumber != null)
204
                GUIUtils.SetAutoLabelCheck(false);
205

    
206
            foreach (var item in groupLine)
207
                item.Aveva.Handle = handle;
208
        }
209
        private void SymbolModeling(Symbol symbol)
210
        {
211
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
212
            if (allRows.Length == 1)
213
            {
214
                DataRow symbolRow = allRows[0];
215
                bool bAngle = CheckAngle(symbolRow);
216
                bool bBalloon = CheckBalloon(symbol);
217

    
218
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
219
                if (handle != 0)
220
                {
221
                    symbol.Aveva.Handle = handle;
222

    
223
                    if (!bAngle)
224
                    {
225
                        ObjectId objectId = GetObjectIdByHandle(handle);
226
                        if (objectId != ObjectId.Null)
227
                        {
228
                            SetBlockReferenceRotation(objectId, symbol.ANGLE);
229
                        }
230
                    }
231
                }
232
            }
233
        }
234
        #endregion
235

    
236
        #region Drawing Method
237
        private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
238
        {
239
            List<long> prevHandles = GetAllGroupHandles();
240

    
241
            GUIUtils.SetPipeStyle(style);
242
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
243
            Editor editor = acDoc.Editor;
244
            List<object> commandParam = new List<object>();
245
            //command name
246
            commandParam.Add("DRPIPE");
247
            //coordinate
248
            foreach (var item in coordinates)
249
                commandParam.Add(item);
250

    
251
            //enter Parent(null)
252
            commandParam.Add(null);
253

    
254
            if (lineNumber != null)
255
            {
256
                Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
257
                commandParam.Add(point);
258
                commandParam.Add(lineNumber.Aveva.Angle);
259

    
260
                commandParam.Add(null);
261
            }
262
            else
263
                commandParam.Add(null);
264

    
265
            editor.Command(commandParam.ToArray());
266

    
267
            List<long> newHandles = GetAllGroupHandles();
268
            List<long> otherHandles = new List<long>();
269
            foreach (var item in newHandles)
270
                if (!prevHandles.Contains(item))
271
                    otherHandles.Add(item);
272

    
273
            if (otherHandles.Count == 1)
274
                return otherHandles[0];
275
            else
276
                return 0;
277
        }
278
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
279
        {
280
            List<long> prevHandles = GetAllGroupHandles();
281
            GUIUtils.SetPipeStyle(style);
282

    
283
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
284
            Editor editor = acDoc.Editor;
285
            List<object> commandParam = new List<object>();
286
            //command name
287
            commandParam.Add("DRPIPE");
288
            //coordinate
289
            foreach (var item in coordinates)
290
                commandParam.Add(item);
291
            //enter
292
            commandParam.Add(null);
293

    
294
            //Input Object ID
295
            commandParam.Add(objectId);
296

    
297
            //enter
298
            commandParam.Add(null);
299

    
300
            editor.Command(commandParam.ToArray());
301

    
302
            List<long> newHandles = GetAllGroupHandles();
303
            List<long> otherHandles = new List<long>();
304
            foreach (var item in newHandles)
305
                if (!prevHandles.Contains(item))
306
                    otherHandles.Add(item);
307

    
308
            if (otherHandles.Count == 1)
309
                return otherHandles[0];
310
            else
311
                return 0;
312
        }
313
        private long DrawSignal(string style, List<string> coordinates)
314
        {
315
            List<long> prevHandles = GetAllGroupHandles();
316

    
317
            GUIUtils.SetPipeStyle(style);
318
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
319
            Editor editor = acDoc.Editor;
320
            List<object> commandParam = new List<object>();
321
            //command name
322
            commandParam.Add("VPESIGNAL");
323
            //coordinate
324
            foreach (var item in coordinates)
325
                commandParam.Add(item);
326
            //enter
327
            commandParam.Add(null);
328

    
329
            editor.Command(commandParam.ToArray());
330

    
331
            List<long> newHandles = GetAllGroupHandles();
332
            List<long> otherHandles = new List<long>();
333
            foreach (var item in newHandles)
334
                if (!prevHandles.Contains(item))
335
                    otherHandles.Add(item);
336

    
337
            if (otherHandles.Count == 1)
338
                return otherHandles[0];
339
            else
340
                return 0;
341
        }
342
        private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
343
        {
344
            long handle = 0;
345
            try
346
            {
347
                string insertSymbolName = symbol.Aveva.Name;
348
                double x = symbol.Aveva.X;
349
                double y = symbol.Aveva.Y;
350

    
351
                List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
352

    
353
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
354
                drawingData.InsertSymbolName = insertSymbolName;
355
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
356
                Editor editor = acDoc.Editor;
357
                List<object> commandParam = new List<object>();
358
                commandParam.Add("INSSYM");
359

    
360
                // Insert Point
361
                Point3d point = new Point3d(x, y, 0);
362
                commandParam.Add(point);
363

    
364
                // Check Insert Point
365
                if (needAngle)
366
                    commandParam.Add(symbol.ANGLE);
367

    
368
                // Check Balloon
369
                if (needBalloon)
370
                {
371
                    point = new Point3d(x + 20, y + 20, 0);
372
                    commandParam.Add(point);
373
                    
374
                    // Check LeaderLine
375
                    if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
376
                        commandParam.Add(x + "," + y);
377
                }
378

    
379
                for (int i = 0; i < 8; i++)
380
                    commandParam.Add(null);
381

    
382
                editor.Command(commandParam.ToArray());
383

    
384
                List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
385
                List<long> otherHandles = new List<long>();
386
                foreach (var item in newHandles)
387
                    if (!prevHandles.Contains(item))
388
                        otherHandles.Add(item);
389

    
390
                if (otherHandles.Count == 1)
391
                    return otherHandles[0];
392
                else
393
                    return 0;
394
            }
395
            catch (System.Exception ex)
396
            {
397

    
398
            }
399

    
400
            return handle;
401
        }
402
        #endregion
403

    
404
        #region Autocad Utils
405
        private List<long> GetAllBlockHandlesByName(string name)
406
        {
407
            List<long> handles = new List<long>();
408

    
409
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
410
            Database acCurDb = acDoc.Database;
411
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
412
            {
413
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
414
                foreach (var entry in gd)
415
                {
416
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
417
                    if (blockTableRecord != null)
418
                    {
419
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
420
                        foreach (var item in records)
421
                        {
422
                            if (item.Name == name)
423
                                handles.Add(item.Handle.Value);
424
                        }
425
                    }
426
                }
427
                    
428
                acTrans.Commit();
429
            }
430
            return handles;
431
        }
432
        private List<long> GetAllGroupHandles()
433
        {
434
            List<long> handles = new List<long>();
435

    
436
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
437
            Database acCurDb = acDoc.Database;
438
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
439
            {
440
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
441
                foreach (var entry in gd)
442
                    handles.Add(entry.Value.Handle.Value);
443
                acTrans.Commit();
444
            }
445
            return handles;
446
        }
447
        private ObjectId GetFirstPolyLine(long groupHandle)
448
        {
449
            ObjectId result = ObjectId.Null;
450

    
451
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
452
            Database acCurDb = acDoc.Database;
453
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
454
            {
455
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
456
                foreach (var entry in gd)
457
                {
458
                    if (entry.Value.Handle.Value == groupHandle)
459
                    {
460
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
461
                        foreach (var item in group.GetAllEntityIds())
462
                        {
463
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
464
                            {
465
                                result = item;
466
                                break;
467
                            }
468
                        }
469
                        break;
470
                    }
471
                }
472
                acTrans.Commit();
473
            }
474

    
475
            return result;
476
        }
477
        private ObjectId GetObjectIdByHandle(long lHandle)
478
        {
479
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
480
            Database acCurDb = acDoc.Database;
481
            Handle handle = new Handle(lHandle);
482

    
483
            return acCurDb.GetObjectId(false, handle, 0);
484
        }
485
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
486
        {
487
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
488
            Database acCurDb = acDoc.Database;
489
            Editor acDocEd = acDoc.Editor;
490

    
491
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
492
            {
493
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
494
                if (objDB != null)
495
                {
496
                    if (objDB.GetType() == typeof(BlockReference))
497
                    {
498
                        BlockReference block = objDB as BlockReference;
499
                        block.Rotation = rotation;
500
                    }
501
                }
502
                acTrans.Commit();
503
            }
504
        }
505
        #endregion
506

    
507
        #region Modeling Utils
508
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
509
        {
510
            if (!group.Contains(line))
511
            {
512
                if (isInsert)
513
                    group.Insert(0, line);
514
                else
515
                    group.Add(line);
516

    
517
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
518
                {
519
                    object connObj = line.CONNECTORS[0].ConnectedObject;
520
                    if (connObj.GetType() == typeof(Model.Line) &&
521
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
522
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
523
                    else if (connObj.GetType() == typeof(Model.Symbol))
524
                    {
525
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
526
                        if (connLine != null)
527
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
528
                    }
529
                }
530
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
531
                {
532
                    object connObj = line.CONNECTORS[1].ConnectedObject;
533
                    if (connObj.GetType() == typeof(Model.Line) &&
534
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
535
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
536
                    else if (connObj.GetType() == typeof(Model.Symbol))
537
                    {
538
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
539
                        if (connLine != null)
540
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
541
                    }
542
                }
543
            }
544
        }
545
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
546
        {
547
            for (int i = 0; i < groupLine.Count; i++)
548
            {
549
                Model.Line line = groupLine[i];
550
                if (i == 0)
551
                {
552
                    object connObj = line.CONNECTORS[0].ConnectedObject;
553
                    if (connObj != null)
554
                    {
555
                        string point = GetPointByConnectedItem(connObj, line, true);
556
                        if (string.IsNullOrEmpty(point))
557
                            points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
558
                        else
559
                            points.Add(point);
560
                    }
561
                    else
562
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
563
                }
564
                else
565
                {
566
                    Model.Line prevLine = groupLine[i - 1];
567
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
568
                        prevLine.SlopeType == SlopeType.VERTICAL)
569
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
570
                    else if (line.SlopeType == SlopeType.VERTICAL &&
571
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
572
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
573
                    else
574
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
575
                }
576

    
577
                if (i == groupLine.Count - 1)
578
                {
579
                    object connObj = line.CONNECTORS[1].ConnectedObject;
580
                    if (connObj != null && connObj.GetType() == typeof(Symbol))
581
                    {
582
                        string point = GetPointByConnectedItem(connObj, line, false);
583
                        if (string.IsNullOrEmpty(point))
584
                            points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
585
                        else
586
                            points.Add(point);
587
                    }
588
                    else
589
                        points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
590
                }
591
            }
592
        }
593
        private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
594
        {
595
            string result = string.Empty;
596
            double x;
597
            double y;
598
            if (isStart)
599
            {
600
                x = line.Aveva.Start_X;
601
                y = line.Aveva.Start_Y;
602
            }
603
            else
604
            {
605
                x = line.Aveva.End_X;
606
                y = line.Aveva.End_Y;
607
            }
608

    
609
            if (connObj.GetType() == typeof(Symbol))
610
            {
611
                Symbol connSymbol = connObj as Symbol;
612
                Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
613

    
614
                SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
615
                if (slopeType == SlopeType.HORIZONTAL)
616
                    result = connSymbol.Aveva.X + "," + y;
617
                else if (slopeType == SlopeType.VERTICAL)
618
                    result = x + "," + connSymbol.Aveva.Y;
619
                else
620
                    result = x + "," + y;
621
            }
622
            else if (connObj.GetType() == typeof(OPC))
623
            {
624
                OPC connOPC = connObj as OPC;
625
                Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
626

    
627
                double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
628
                APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
629
                APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
630
                locX = locX + sizeWidth / 2;
631
                locY = locY + sizeHeight / 2;
632
                aX = locX;
633
                aY = locY;
634
                document.ConvertAvevaPoint(ref aX, ref aY);
635

    
636
                SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
637
                if (slopeType == SlopeType.HORIZONTAL)
638
                    result = aX + "," + y;
639
                else if (slopeType == SlopeType.VERTICAL)
640
                    result = x + "," + aY;
641
                else
642
                    result = x + "," + y;
643
            }
644
            
645
            return result;
646
        }
647
        private bool IsExistEndBreak(object item1, object item2)
648
        {
649
            bool result = false;
650
            if (item1 != null && item2 != null)
651
            {
652
                EndBreak endBreak = document.EndBreaks.Find(x =>
653
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
654
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
655
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
656
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
657

    
658
                if (endBreak != null)
659
                    result = true;
660
            }
661

    
662
            return result;
663
        }
664
        private LineRun FindLineRun(Model.Line line)
665
        {
666
            List<LineRun> allLineRun = new List<LineRun>();
667
            foreach (var item in document.LINENUMBERS)
668
                allLineRun.AddRange(item.RUNS);
669
            foreach (var item in document.TRIMLINES)
670
                allLineRun.AddRange(item.RUNS);
671

    
672
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
673
        }
674
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
675
        {
676
            Model.Line result = null;
677
            LineRun run = FindLineRun(line);
678
            Connector connector = symbol.CONNECTORS.Find(x =>
679
            x.ConnectedObject != null &&
680
            x.ConnectedObject != line &&
681
            run.RUNITEMS.Contains(x.ConnectedObject) &&
682
            x.ConnectedObject.GetType() == typeof(Model.Line));
683

    
684
            if (connector != null)
685
                result = connector.ConnectedObject as Model.Line;
686
            
687
            return result;
688
        }
689
        private bool CheckAngle(DataRow symbolRow)
690
        {
691
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
692
        }
693
        private bool CheckBalloon(Symbol symbol)
694
        {
695
            bool result = false;
696

    
697
            string text = string.Empty;
698
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
699
            if (symbolDefinitions.Count != 0)
700
            {
701
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
702

    
703
                if (text.Equals("VLP"))
704
                    text = "VPO";
705
                else if (text.Equals("IVP"))
706
                    text = "IPO";
707

    
708
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
709
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
710
            }
711

    
712
            return result;
713
        }
714
        #endregion
715

    
716
        #region Test Source
717
        public void test()
718
        {
719
            //InitGUI();
720
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
721

    
722
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
723
            return;
724
            Symbol symbol = new Symbol();
725
            symbol.Aveva = new AvevaSymbolInfo();
726
            symbol.Aveva.Name = "INVB";
727
            symbol.Aveva.X = 50;
728
            symbol.Aveva.Y = 100;
729

    
730
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
731
            if (allRows.Length == 1)
732
            {
733
                DataRow symbolRow = allRows[0];
734
                bool bAngle = CheckAngle(symbolRow);
735
                bool bBalloon = CheckBalloon(symbol);
736
                bBalloon = false;
737
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
738
            }
739

    
740
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
741
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
742
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
743
            {
744
                bool bIsPromptRotate = true;
745
            }
746
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
747

    
748
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
749
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
750
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
751

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

    
754
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
755
        }
756
        public static void TESTStatic()
757
        {
758
            System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable();
759
            AutoModeling auto = new AutoModeling(null, avevaSymbolTable);
760
            auto.test();
761
        }
762
        #endregion
763
    }
764

    
765

    
766
    public static class AcDbLinqExtensionMethods
767
    {
768
        /// <summary>
769
        /// Get all references to the given BlockTableRecord, including 
770
        /// references to anonymous dynamic BlockTableRecords.
771
        /// </summary>
772

    
773
        public static IEnumerable<BlockReference> GetBlockReferences(
774
           this BlockTableRecord btr,
775
           OpenMode mode = OpenMode.ForRead,
776
           bool directOnly = true)
777
        {
778
            if (btr == null)
779
                throw new ArgumentNullException("btr");
780
            var tr = btr.Database.TransactionManager.TopTransaction;
781
            if (tr == null)
782
                throw new InvalidOperationException("No transaction");
783
            var ids = btr.GetBlockReferenceIds(directOnly, true);
784
            int cnt = ids.Count;
785
            for (int i = 0; i < cnt; i++)
786
            {
787
                yield return (BlockReference)
788
                   tr.GetObject(ids[i], mode, false, false);
789
            }
790
            if (btr.IsDynamicBlock)
791
            {
792
                BlockTableRecord btr2 = null;
793
                var blockIds = btr.GetAnonymousBlockIds();
794
                cnt = blockIds.Count;
795
                for (int i = 0; i < cnt; i++)
796
                {
797
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
798
                       OpenMode.ForRead, false, false);
799
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
800
                    int cnt2 = ids.Count;
801
                    for (int j = 0; j < cnt2; j++)
802
                    {
803
                        yield return (BlockReference)
804
                           tr.GetObject(ids[j], mode, false, false);
805
                    }
806
                }
807
            }
808
        }
809
    }
810
}
클립보드 이미지 추가 (최대 크기: 500 MB)