프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

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

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

1 74a0c9d6 gaqhf
using System;
2
using System.Collections.Generic;
3 016701e5 gaqhf
using System.Collections;
4 74a0c9d6 gaqhf
using System.Configuration;
5
using System.Data;
6
using System.Data.Common;
7
using System.Data.SqlClient;
8
using System.IO;
9 5c05308b gaqhf
using System.Runtime.CompilerServices;
10
using Microsoft.VisualBasic.CompilerServices;
11 74a0c9d6 gaqhf
12
using Autodesk.AutoCAD.ApplicationServices;
13
using Autodesk.AutoCAD.ApplicationServices.Core;
14
using Autodesk.AutoCAD.DatabaseServices;
15
using Autodesk.AutoCAD.EditorInput;
16
using Autodesk.AutoCAD.Geometry;
17
using Autodesk.AutoCAD.Interop;
18
using Autodesk.AutoCAD.Interop.Common;
19
using Autodesk.AutoCAD.Runtime;
20
using Autodesk.AutoCAD.Windows;
21
22 1c5bd296 gaqhf
using AVEVA.PID.Components;
23
using AVEVA.PID.GUI;
24
using AVEVA.PID.Common;
25
using AVEVA.PID.DWGSelector;
26 495bb8f5 gaqhf
using AVEVA.PID.Utilities;
27 1c5bd296 gaqhf
28 14540282 gaqhf
using AVEVA.PID.CustomizationUtility.DB;
29
using AVEVA.PID.CustomizationUtility.Model;
30
using AVEVA.PID.CustomizationUtility.Properties;
31 74a0c9d6 gaqhf
32 aba318eb gaqhf
using DevExpress.XtraSplashScreen;
33
34 74a0c9d6 gaqhf
namespace AVEVA.PID.CustomizationUtility
35
{
36
    public class AutoModeling
37
    {
38 1c5bd296 gaqhf
        public static AutoModeling Running { get; set; }
39 14540282 gaqhf
        Model.Document document;
40 74a0c9d6 gaqhf
41 495bb8f5 gaqhf
        System.Data.DataTable AvevaSymbolTable = null;
42
43 a19bd6e2 gaqhf
        private void SetConvertRule()
44
        {
45
            #region OPC Setting
46
            document.OPCs.Sort(SortOPC);
47
            int SortOPC(OPC a, OPC b)
48
            {
49
                if (a.FlowType == FlowType.In)
50
                    return 1;
51
                else
52
                    return 0;
53
            }
54
            #endregion
55
        }
56
57 495bb8f5 gaqhf
        public AutoModeling(Model.Document document, System.Data.DataTable AvevaSymbolTable)
58 74a0c9d6 gaqhf
        {
59 14540282 gaqhf
            this.document = document;
60 495bb8f5 gaqhf
            this.AvevaSymbolTable = AvevaSymbolTable;
61 74a0c9d6 gaqhf
        }
62
63 1c5bd296 gaqhf
        public void CreateDrawing()
64 46756c13 gaqhf
        {
65 1c5bd296 gaqhf
            string outPath = Project_DB.GetDirectiveValue("DRGPTH");
66
            string tempPath = Path.GetTempPath();
67
            string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT");
68
69
            DWTSelector.strDrawingNumber = document.AvevaDrawingNumber;
70
            DWTSelector.strSheetNumber = document.AvevaSheetNumber;
71
            DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber;
72
            DWTSelector.strCadFilePath = outPath;
73
74
            try
75 46756c13 gaqhf
            {
76 1c5bd296 gaqhf
                string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false);
77
                Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
78
                Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text);
79
80
                Commands.addLatestPidvesrionToDictionary();
81
                AVVPropCmd.ActivateAcad();
82
83
                if (acadDocument != null)
84
                {
85
                    Running = this;
86
                    acadApplication.ActiveDocument = acadDocument;
87
                    acadDocument.SendCommand("QSAVE ");
88
                    acadDocument.SendCommand("APPIDRun ");
89 9dab7146 gaqhf
                    acadDocument.SendCommand("QSAVE ");
90 1c5bd296 gaqhf
                    Running = null;
91
                }
92 46756c13 gaqhf
            }
93 1c5bd296 gaqhf
            catch (System.Exception ex)
94
            {
95
                
96
            }
97
98
            GC.Collect();
99
        }
100
        public void Run()
101
        {
102 016701e5 gaqhf
            try
103
            {
104 aba318eb gaqhf
                SplashScreenManager.ShowForm(typeof(APIDSplashScreen), true, true);
105 6503fec8 gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllStepCount, 5);
106 aba318eb gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetDocumentName, document.AvevaDrawingNumber + document.AvevaSheetNumber);
107 932933ed gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetParent, Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
108
109 a19bd6e2 gaqhf
                SetConvertRule();
110 aba318eb gaqhf
111 016701e5 gaqhf
                RunLineModeling();
112 a19bd6e2 gaqhf
                RunOPCModeling();
113 016701e5 gaqhf
                RunSymbolModeling();
114 475071f2 gaqhf
                RunTextModeling();
115 6503fec8 gaqhf
                RunNoteModeling();
116 016701e5 gaqhf
            }
117
            catch (System.Exception ex)
118
            {
119
                System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace);
120
            }
121 aba318eb gaqhf
            finally
122
            {
123 932933ed gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.ClearParent, null);
124 aba318eb gaqhf
                SplashScreenManager.CloseForm(false);
125
            }
126 46756c13 gaqhf
        }
127
128 9dab7146 gaqhf
        #region Run Method
129
        private void RunLineModeling()
130 74a0c9d6 gaqhf
        {
131 aba318eb gaqhf
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
132
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling");
133 9dab7146 gaqhf
            foreach (var item in document.LINES)
134 74a0c9d6 gaqhf
            {
135 53344e2c gaqhf
                if (item.Aveva.Handle == 0)
136
                    LineModeling(item);
137 aba318eb gaqhf
138
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
139 74a0c9d6 gaqhf
            }
140 9dab7146 gaqhf
        }
141 a19bd6e2 gaqhf
        private void RunOPCModeling()
142
        {
143
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count);
144
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling");
145
            foreach (var item in document.OPCs)
146
            {
147
                if (item.Aveva.Handle == 0)
148
                    SymbolModeling(item);
149
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
150
            }
151
        }
152 016701e5 gaqhf
        private void RunSymbolModeling()
153
        {
154 aba318eb gaqhf
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
155
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
156 b90890eb gaqhf
            foreach (var item in document.SYMBOLS)
157
            {
158
                if (item.Aveva.Handle == 0)
159
                    SymbolModeling(item);
160 aba318eb gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
161 b90890eb gaqhf
            }
162 016701e5 gaqhf
        }
163 475071f2 gaqhf
        private void RunTextModeling()
164
        {
165 0a8db7fe gaqhf
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Texts.Count);
166
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Text Modeling");
167
            foreach (var item in document.Texts)
168
            {
169 6503fec8 gaqhf
                TextModeling(item);
170
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
171
            }
172
        }
173
        private void RunNoteModeling()
174
        {
175
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Notes.Count);
176
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Note Modeling");
177 7a8645c9 gaqhf
            foreach (var item in document.Notes)
178 6503fec8 gaqhf
            {
179 7a8645c9 gaqhf
                NoteModeling(item);
180 0a8db7fe gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
181
            }
182 475071f2 gaqhf
        }
183 5c05308b gaqhf
        private void RunInputLineAttribute()
184
        {
185
186
        }
187
        private void RunInputSymbolAttribute()
188
        {
189
190
        }
191 9dab7146 gaqhf
        #endregion
192 74a0c9d6 gaqhf
193 9dab7146 gaqhf
        #region Modeling Method
194
        private void LineModeling(Model.Line line)
195
        {
196 53344e2c gaqhf
            List<Model.Line> groupLine = new List<Model.Line>();
197 9dab7146 gaqhf
            List<string> points = new List<string>();
198 016701e5 gaqhf
            GetConnectedGroupLine(line, groupLine, false);
199 53344e2c gaqhf
            GetGroupLinePoints(groupLine, points);
200 9dab7146 gaqhf
201 932933ed gaqhf
            LineNumber lineNumber = null;
202
            if (line.Aveva.Type == Model.Type.Pipe)
203
            {
204
                foreach (var item in groupLine)
205
                {
206
                    lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == item.UID);
207
                    if (lineNumber != null)
208
                    {
209
                        GUIUtils.SetAutoLabelCheck(true);
210
                        break;
211
                    }
212
                }
213
            }
214
215
216 53344e2c gaqhf
            long handle = 0;
217 9dab7146 gaqhf
            if (line.Aveva.Type == Model.Type.Pipe)
218 932933ed gaqhf
                handle = DrawPipe(line.Aveva.Name, points, lineNumber);
219 9dab7146 gaqhf
            else if (line.Aveva.Type == Model.Type.Signal)
220 53344e2c gaqhf
                handle = DrawSignal(line.Aveva.Name, points);
221
222 932933ed gaqhf
            if (lineNumber != null)
223
                GUIUtils.SetAutoLabelCheck(false);
224
225 53344e2c gaqhf
            foreach (var item in groupLine)
226
                item.Aveva.Handle = handle;
227 74a0c9d6 gaqhf
        }
228 b90890eb gaqhf
        private void SymbolModeling(Symbol symbol)
229
        {
230 495bb8f5 gaqhf
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
231
            if (allRows.Length == 1)
232 2d09df82 gaqhf
            {
233 495bb8f5 gaqhf
                DataRow symbolRow = allRows[0];
234
                bool bAngle = CheckAngle(symbolRow);
235
                bool bBalloon = CheckBalloon(symbol);
236
237
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
238
                if (handle != 0)
239 2d09df82 gaqhf
                {
240 495bb8f5 gaqhf
                    symbol.Aveva.Handle = handle;
241
242
                    if (!bAngle)
243
                    {
244
                        ObjectId objectId = GetObjectIdByHandle(handle);
245
                        if (objectId != ObjectId.Null)
246
                        {
247
                            SetBlockReferenceRotation(objectId, symbol.ANGLE);
248
                        }
249
                    }
250 2d09df82 gaqhf
                }
251
            }
252 b90890eb gaqhf
        }
253 6503fec8 gaqhf
        private void TextModeling(Text text)
254
        {
255
            if (text.TextAngle == TextAngle.None)
256
                return;
257
258
            if (text.Aveva.LabelType == LabelType.SingleText)
259 a03f7b94 gaqhf
                DrawText(text.Aveva.X, text.Aveva.Y, text.Aveva.Height, text.VALUE, text.Aveva.Angle);
260 6503fec8 gaqhf
            else if (text.Aveva.LabelType == LabelType.MultiText)
261 7a8645c9 gaqhf
            {
262
                string[] valueArray = text.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
263
                List<string> values = new List<string>();
264
                for (int i = 0; i < valueArray.Length; i++)
265
                    values.Insert(0, valueArray[i]);
266
267
                for (int i = 0; i < values.Count; i++)
268
                {
269
                    double x = text.Aveva.X;
270
                    double y = text.Aveva.Y;
271
                    int heightIndex = i;
272
273
                    if (text.TextAngle == TextAngle.Degree0 || text.TextAngle == TextAngle.Degree180)
274
                        y = y + text.Aveva.Height * i;
275
                    else if (text.TextAngle == TextAngle.Degree90 || text.TextAngle == TextAngle.Degree270)
276
                        x = x + text.Aveva.Height * i;
277
278 a03f7b94 gaqhf
                    DrawText(x, y, text.Aveva.Height, values[i], text.Aveva.Angle);
279 7a8645c9 gaqhf
                }
280
            }
281
                
282 6503fec8 gaqhf
283
        }
284
        private void NoteModeling(Model.Note note)
285
        {
286
            if (note.TextAngle == TextAngle.None)
287
                return;
288
289
            if (note.Aveva.LabelType == LabelType.SingleNote)
290 a03f7b94 gaqhf
                DrawText(note.Aveva.X, note.Aveva.Y, note.Aveva.Height, note.VALUE, note.Aveva.Angle);
291 6503fec8 gaqhf
            else if (note.Aveva.LabelType == LabelType.MultiNote)
292 7a8645c9 gaqhf
            {
293
                string[] valueArray = note.VALUE.Split(new string[] { "\n" }, StringSplitOptions.None);
294
                List<string> values = new List<string>();
295
                for (int i = 0; i < valueArray.Length; i++)
296
                    values.Insert(0, valueArray[i]);
297
298
                for (int i = 0; i < values.Count; i++)
299
                {
300
                    double x = note.Aveva.X;
301
                    double y = note.Aveva.Y;
302
                    int heightIndex = i;
303
304
                    if (note.TextAngle == TextAngle.Degree0 || note.TextAngle == TextAngle.Degree180)
305
                        y = y + note.Aveva.Height * i;
306
                    else if (note.TextAngle == TextAngle.Degree90 || note.TextAngle == TextAngle.Degree270)
307
                        x = x + note.Aveva.Height * i;
308 6503fec8 gaqhf
309 a03f7b94 gaqhf
                    DrawText(x, y, note.Aveva.Height, values[i], note.Aveva.Angle);
310 7a8645c9 gaqhf
                }
311
            }
312 6503fec8 gaqhf
        }
313 5c05308b gaqhf
        private void SetLineAttribute(Model.Line line)
314
        {
315
316
        }
317
        private void SetSymbolAttribute(Model.Symbol symbol)
318
        {
319
320
        }
321 9dab7146 gaqhf
        #endregion
322 74a0c9d6 gaqhf
323
        #region Drawing Method
324 932933ed gaqhf
        private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
325 74a0c9d6 gaqhf
        {
326 9dab7146 gaqhf
            List<long> prevHandles = GetAllGroupHandles();
327 74a0c9d6 gaqhf
328 9dab7146 gaqhf
            GUIUtils.SetPipeStyle(style);
329 14540282 gaqhf
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
330 74a0c9d6 gaqhf
            Editor editor = acDoc.Editor;
331
            List<object> commandParam = new List<object>();
332
            //command name
333
            commandParam.Add("DRPIPE");
334
            //coordinate
335
            foreach (var item in coordinates)
336
                commandParam.Add(item);
337 932933ed gaqhf
338
            //enter Parent(null)
339 74a0c9d6 gaqhf
            commandParam.Add(null);
340
341 932933ed gaqhf
            if (lineNumber != null)
342
            {
343
                Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
344
                commandParam.Add(point);
345 475071f2 gaqhf
                commandParam.Add(lineNumber.Aveva.Angle);
346 932933ed gaqhf
347
                commandParam.Add(null);
348
            }
349
            else
350
                commandParam.Add(null);
351
352 74a0c9d6 gaqhf
            editor.Command(commandParam.ToArray());
353
354 9dab7146 gaqhf
            List<long> newHandles = GetAllGroupHandles();
355
            List<long> otherHandles = new List<long>();
356
            foreach (var item in newHandles)
357
                if (!prevHandles.Contains(item))
358
                    otherHandles.Add(item);
359 74a0c9d6 gaqhf
360 9dab7146 gaqhf
            if (otherHandles.Count == 1)
361
                return otherHandles[0];
362
            else
363
                return 0;
364 74a0c9d6 gaqhf
        }
365 9dab7146 gaqhf
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
366 74a0c9d6 gaqhf
        {
367 9dab7146 gaqhf
            List<long> prevHandles = GetAllGroupHandles();
368 14540282 gaqhf
            GUIUtils.SetPipeStyle(style);
369 74a0c9d6 gaqhf
370 14540282 gaqhf
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
371 74a0c9d6 gaqhf
            Editor editor = acDoc.Editor;
372
            List<object> commandParam = new List<object>();
373
            //command name
374
            commandParam.Add("DRPIPE");
375
            //coordinate
376
            foreach (var item in coordinates)
377
                commandParam.Add(item);
378
            //enter
379
            commandParam.Add(null);
380
381
            //Input Object ID
382
            commandParam.Add(objectId);
383
384
            //enter
385
            commandParam.Add(null);
386
387
            editor.Command(commandParam.ToArray());
388 9dab7146 gaqhf
389
            List<long> newHandles = GetAllGroupHandles();
390
            List<long> otherHandles = new List<long>();
391
            foreach (var item in newHandles)
392
                if (!prevHandles.Contains(item))
393
                    otherHandles.Add(item);
394
395
            if (otherHandles.Count == 1)
396
                return otherHandles[0];
397
            else
398
                return 0;
399
        }
400
        private long DrawSignal(string style, List<string> coordinates)
401
        {
402
            List<long> prevHandles = GetAllGroupHandles();
403
404
            GUIUtils.SetPipeStyle(style);
405
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
406
            Editor editor = acDoc.Editor;
407
            List<object> commandParam = new List<object>();
408
            //command name
409
            commandParam.Add("VPESIGNAL");
410
            //coordinate
411
            foreach (var item in coordinates)
412
                commandParam.Add(item);
413
            //enter
414
            commandParam.Add(null);
415
416
            editor.Command(commandParam.ToArray());
417
418
            List<long> newHandles = GetAllGroupHandles();
419
            List<long> otherHandles = new List<long>();
420
            foreach (var item in newHandles)
421
                if (!prevHandles.Contains(item))
422
                    otherHandles.Add(item);
423
424
            if (otherHandles.Count == 1)
425
                return otherHandles[0];
426
            else
427
                return 0;
428 74a0c9d6 gaqhf
        }
429 495bb8f5 gaqhf
        private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
430 6b3cb476 gaqhf
        {
431 73152510 gaqhf
            long handle = 0;
432 6b3cb476 gaqhf
            try
433
            {
434 495bb8f5 gaqhf
                string insertSymbolName = symbol.Aveva.Name;
435
                double x = symbol.Aveva.X;
436
                double y = symbol.Aveva.Y;
437
438 73152510 gaqhf
                List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
439
440 6b3cb476 gaqhf
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
441
                drawingData.InsertSymbolName = insertSymbolName;
442
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
443
                Editor editor = acDoc.Editor;
444
                List<object> commandParam = new List<object>();
445
                commandParam.Add("INSSYM");
446 495bb8f5 gaqhf
447
                // Insert Point
448 6b3cb476 gaqhf
                Point3d point = new Point3d(x, y, 0);
449
                commandParam.Add(point);
450 495bb8f5 gaqhf
451
                // Check Insert Point
452
                if (needAngle)
453
                    commandParam.Add(symbol.ANGLE);
454
455
                // Check Balloon
456
                if (needBalloon)
457
                {
458
                    point = new Point3d(x + 20, y + 20, 0);
459
                    commandParam.Add(point);
460
                    
461
                    // Check LeaderLine
462
                    if (GraphicalUtility.IsLeaderReqOnSymbolBalloons())
463
                        commandParam.Add(x + "," + y);
464
                }
465
466
                for (int i = 0; i < 8; i++)
467
                    commandParam.Add(null);
468 6b3cb476 gaqhf
469
                editor.Command(commandParam.ToArray());
470 73152510 gaqhf
471
                List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName);
472
                List<long> otherHandles = new List<long>();
473
                foreach (var item in newHandles)
474
                    if (!prevHandles.Contains(item))
475
                        otherHandles.Add(item);
476
477
                if (otherHandles.Count == 1)
478
                    return otherHandles[0];
479
                else
480
                    return 0;
481 6b3cb476 gaqhf
            }
482
            catch (System.Exception ex)
483
            {
484 74a0c9d6 gaqhf
485 6b3cb476 gaqhf
            }
486 73152510 gaqhf
487
            return handle;
488 6b3cb476 gaqhf
        }
489 a03f7b94 gaqhf
        private void DrawText(double x, double y, double height, string text, double rotation)
490 6503fec8 gaqhf
        {
491
            // Get the current document and database
492
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
493
            Database acCurDb = acDoc.Database;
494
495
            // Start a transaction
496
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
497
            {
498
                // Open the Block table for read
499
                BlockTable acBlkTbl;
500
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
501
                                                OpenMode.ForRead) as BlockTable;
502
503
                // Open the Block table record Model space for write
504
                BlockTableRecord acBlkTblRec;
505
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
506
                                                OpenMode.ForWrite) as BlockTableRecord;
507
508
                // Create a single-line text object
509
                using (DBText acText = new DBText())
510
                {
511
                    acText.Position = new Point3d(x, y, 0);
512
                    acText.Height = height;
513
                    acText.TextString = text;
514 a03f7b94 gaqhf
                    acText.Rotation = rotation;
515 6503fec8 gaqhf
516
                    acBlkTblRec.AppendEntity(acText);
517
                    acTrans.AddNewlyCreatedDBObject(acText, true);
518
                }
519
520
                // Save the changes and dispose of the transaction
521
                acTrans.Commit();
522
            }
523
        }
524 74a0c9d6 gaqhf
        #endregion
525
526 73152510 gaqhf
        #region Autocad Utils
527
        private List<long> GetAllBlockHandlesByName(string name)
528
        {
529
            List<long> handles = new List<long>();
530
531
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
532
            Database acCurDb = acDoc.Database;
533
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
534
            {
535
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
536
                foreach (var entry in gd)
537
                {
538
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
539
                    if (blockTableRecord != null)
540
                    {
541
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
542
                        foreach (var item in records)
543
                        {
544
                            if (item.Name == name)
545
                                handles.Add(item.Handle.Value);
546
                        }
547
                    }
548
                }
549
                    
550
                acTrans.Commit();
551
            }
552
            return handles;
553
        }
554 9dab7146 gaqhf
        private List<long> GetAllGroupHandles()
555
        {
556
            List<long> handles = new List<long>();
557
558
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
559
            Database acCurDb = acDoc.Database;
560
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
561
            {
562
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
563
                foreach (var entry in gd)
564
                    handles.Add(entry.Value.Handle.Value);
565
                acTrans.Commit();
566
            }
567
            return handles;
568
        }
569
        private ObjectId GetFirstPolyLine(long groupHandle)
570
        {
571
            ObjectId result = ObjectId.Null;
572
573
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
574
            Database acCurDb = acDoc.Database;
575
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
576
            {
577
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
578
                foreach (var entry in gd)
579
                {
580
                    if (entry.Value.Handle.Value == groupHandle)
581
                    {
582
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
583
                        foreach (var item in group.GetAllEntityIds())
584
                        {
585
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
586
                            {
587
                                result = item;
588
                                break;
589
                            }
590
                        }
591
                        break;
592
                    }
593
                }
594
                acTrans.Commit();
595
            }
596
597
            return result;
598
        }
599 2d09df82 gaqhf
        private ObjectId GetObjectIdByHandle(long lHandle)
600
        {
601
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
602
            Database acCurDb = acDoc.Database;
603
            Handle handle = new Handle(lHandle);
604
605
            return acCurDb.GetObjectId(false, handle, 0);
606
        }
607
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
608
        {
609
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
610
            Database acCurDb = acDoc.Database;
611
            Editor acDocEd = acDoc.Editor;
612
613
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
614
            {
615
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
616
                if (objDB != null)
617
                {
618
                    if (objDB.GetType() == typeof(BlockReference))
619
                    {
620
                        BlockReference block = objDB as BlockReference;
621
                        block.Rotation = rotation;
622
                    }
623
                }
624
                acTrans.Commit();
625
            }
626
        }
627 5c05308b gaqhf
        private void SetSymbolUDAAttribute(ObjectId objectId, List<Tuple<string, string>> datas)
628
        {
629
            AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
630
            AcadBlockReference acadBlockReference = acadApplication.ActiveDocument.ObjectIdToObject((long)objectId.OldIdPtr);
631
            if (acadBlockReference.HasAttributes)
632
            {
633
                object objectValue = RuntimeHelpers.GetObjectValue(acadBlockReference.GetAttributes());
634
                int arg_5B_0 = Microsoft.VisualBasic.Information.LBound((Array)objectValue, 1);
635
                int num = Microsoft.VisualBasic.Information.UBound((Array)objectValue, 1);
636
                for (int i = arg_5B_0; i <= num; i++)
637
                {
638
                    string text = string.Empty;
639
                    text = Conversions.ToString(NewLateBinding.LateGet(NewLateBinding.LateIndexGet(objectValue, new object[]
640
                    {
641
                                i
642
                    }, null), null, "tagString", new object[0], null, null, null));
643
644
                    Tuple<string, string> attribute = datas.Find(x => x.Item1 == text);
645
                    if (attribute != null)
646
                    {
647
                        NewLateBinding.LateSetComplex(NewLateBinding.LateIndexGet(objectValue, new object[]
648
                                {
649
                                    i
650
                                }, null), null, "textString", new object[]
651
                                {
652
                                    attribute.Item2.Split("|".ToCharArray())[0]
653
                                }, null, null, false, true);
654
                    }
655
                }
656
            }
657
        }
658 016701e5 gaqhf
        #endregion
659
660 73152510 gaqhf
        #region Modeling Utils
661 016701e5 gaqhf
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
662 53344e2c gaqhf
        {
663
            if (!group.Contains(line))
664
            {
665 016701e5 gaqhf
                if (isInsert)
666 53344e2c gaqhf
                    group.Insert(0, line);
667
                else
668
                    group.Add(line);
669
670 016701e5 gaqhf
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
671
                {
672
                    object connObj = line.CONNECTORS[0].ConnectedObject;
673
                    if (connObj.GetType() == typeof(Model.Line) &&
674
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
675
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
676
                    else if (connObj.GetType() == typeof(Model.Symbol))
677
                    {
678
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
679
                        if (connLine != null)
680
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
681
                    }
682
                }
683
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
684
                {
685
                    object connObj = line.CONNECTORS[1].ConnectedObject;
686
                    if (connObj.GetType() == typeof(Model.Line) &&
687
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
688
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
689
                    else if (connObj.GetType() == typeof(Model.Symbol))
690
                    {
691
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
692
                        if (connLine != null)
693
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
694
                    }
695
                }
696 53344e2c gaqhf
            }
697
        }
698
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
699
        {
700
            for (int i = 0; i < groupLine.Count; i++)
701
            {
702
                Model.Line line = groupLine[i];
703 016701e5 gaqhf
                if (i == 0)
704 f7e09e14 gaqhf
                {
705
                    object connObj = line.CONNECTORS[0].ConnectedObject;
706 ff4941b2 gaqhf
                    if (connObj != null)
707 f7e09e14 gaqhf
                    {
708 ff4941b2 gaqhf
                        string point = GetPointByConnectedItem(connObj, line, true);
709
                        if (string.IsNullOrEmpty(point))
710 f7e09e14 gaqhf
                            points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
711 ff4941b2 gaqhf
                        else
712
                            points.Add(point);
713 f7e09e14 gaqhf
                    }
714
                    else
715
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
716
                }
717 016701e5 gaqhf
                else
718
                {
719
                    Model.Line prevLine = groupLine[i - 1];
720
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
721
                        prevLine.SlopeType == SlopeType.VERTICAL)
722
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
723
                    else if (line.SlopeType == SlopeType.VERTICAL &&
724
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
725
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
726
                    else
727
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
728
                }
729
730 53344e2c gaqhf
                if (i == groupLine.Count - 1)
731 f7e09e14 gaqhf
                {
732
                    object connObj = line.CONNECTORS[1].ConnectedObject;
733
                    if (connObj != null && connObj.GetType() == typeof(Symbol))
734
                    {
735 ff4941b2 gaqhf
                        string point = GetPointByConnectedItem(connObj, line, false);
736
                        if (string.IsNullOrEmpty(point))
737 f7e09e14 gaqhf
                            points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
738 ff4941b2 gaqhf
                        else
739
                            points.Add(point);
740 f7e09e14 gaqhf
                    }
741
                    else
742
                        points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
743
                }
744 53344e2c gaqhf
            }
745
        }
746 ff4941b2 gaqhf
        private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
747
        {
748
            string result = string.Empty;
749
            double x;
750
            double y;
751
            if (isStart)
752
            {
753
                x = line.Aveva.Start_X;
754
                y = line.Aveva.Start_Y;
755
            }
756
            else
757
            {
758
                x = line.Aveva.End_X;
759
                y = line.Aveva.End_Y;
760
            }
761
762
            if (connObj.GetType() == typeof(Symbol))
763
            {
764
                Symbol connSymbol = connObj as Symbol;
765
                Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
766
767
                SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
768
                if (slopeType == SlopeType.HORIZONTAL)
769
                    result = connSymbol.Aveva.X + "," + y;
770
                else if (slopeType == SlopeType.VERTICAL)
771
                    result = x + "," + connSymbol.Aveva.Y;
772
                else
773
                    result = x + "," + y;
774
            }
775
            else if (connObj.GetType() == typeof(OPC))
776
            {
777
                OPC connOPC = connObj as OPC;
778
                Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
779
780
                double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
781
                APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
782
                APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
783
                locX = locX + sizeWidth / 2;
784
                locY = locY + sizeHeight / 2;
785
                aX = locX;
786
                aY = locY;
787
                document.ConvertAvevaPoint(ref aX, ref aY);
788
789
                SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
790
                if (slopeType == SlopeType.HORIZONTAL)
791
                    result = aX + "," + y;
792
                else if (slopeType == SlopeType.VERTICAL)
793
                    result = x + "," + aY;
794
                else
795
                    result = x + "," + y;
796
            }
797
            
798
            return result;
799
        }
800 016701e5 gaqhf
        private bool IsExistEndBreak(object item1, object item2)
801
        {
802
            bool result = false;
803
            if (item1 != null && item2 != null)
804
            {
805
                EndBreak endBreak = document.EndBreaks.Find(x =>
806
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
807
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
808
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
809
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
810
811
                if (endBreak != null)
812
                    result = true;
813
            }
814
815
            return result;
816
        }
817
        private LineRun FindLineRun(Model.Line line)
818
        {
819
            List<LineRun> allLineRun = new List<LineRun>();
820
            foreach (var item in document.LINENUMBERS)
821
                allLineRun.AddRange(item.RUNS);
822
            foreach (var item in document.TRIMLINES)
823
                allLineRun.AddRange(item.RUNS);
824
825
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
826
        }
827
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
828
        {
829
            Model.Line result = null;
830
            LineRun run = FindLineRun(line);
831
            Connector connector = symbol.CONNECTORS.Find(x =>
832
            x.ConnectedObject != null &&
833
            x.ConnectedObject != line &&
834
            run.RUNITEMS.Contains(x.ConnectedObject) &&
835
            x.ConnectedObject.GetType() == typeof(Model.Line));
836
837
            if (connector != null)
838
                result = connector.ConnectedObject as Model.Line;
839 2d09df82 gaqhf
            
840 016701e5 gaqhf
            return result;
841
        }
842 495bb8f5 gaqhf
        private bool CheckAngle(DataRow symbolRow)
843
        {
844
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
845
        }
846
        private bool CheckBalloon(Symbol symbol)
847
        {
848
            bool result = false;
849
850
            string text = string.Empty;
851
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
852
            if (symbolDefinitions.Count != 0)
853
            {
854
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
855
856
                if (text.Equals("VLP"))
857
                    text = "VPO";
858
                else if (text.Equals("IVP"))
859
                    text = "IPO";
860
861
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
862
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
863
            }
864
865
            return result;
866
        }
867 53344e2c gaqhf
        #endregion
868 9dab7146 gaqhf
869 74a0c9d6 gaqhf
        #region Test Source
870
        public void test()
871
        {
872 14540282 gaqhf
            //InitGUI();
873 9dab7146 gaqhf
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
874 74a0c9d6 gaqhf
875 932933ed gaqhf
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
876
            return;
877 495bb8f5 gaqhf
            Symbol symbol = new Symbol();
878
            symbol.Aveva = new AvevaSymbolInfo();
879
            symbol.Aveva.Name = "INVB";
880
            symbol.Aveva.X = 50;
881
            symbol.Aveva.Y = 100;
882
883
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
884
            if (allRows.Length == 1)
885
            {
886
                DataRow symbolRow = allRows[0];
887
                bool bAngle = CheckAngle(symbolRow);
888
                bool bBalloon = CheckBalloon(symbol);
889
                bBalloon = false;
890
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
891
            }
892
893
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
894
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
895
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
896
            {
897
                bool bIsPromptRotate = true;
898
            }
899
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
900
901
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
902
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
903
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
904
905 9dab7146 gaqhf
            //DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" });
906 74a0c9d6 gaqhf
907 9dab7146 gaqhf
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
908 74a0c9d6 gaqhf
        }
909 5c05308b gaqhf
        public static void TESTStatic(ObjectId objectId)
910 74a0c9d6 gaqhf
        {
911 5c05308b gaqhf
            // Get the current document and database, and start a transaction
912 77a33d5f gaqhf
            //Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
913
            //Database acCurDb = acDoc.Database;
914
            //Editor acDocEd = acDoc.Editor;
915
916
            //using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
917
            //{
918
            //    Entity objDb = (Entity)acTrans.GetObject(objectId, OpenMode.ForWrite);
919
            //    Entity startNodeEntityForPipe = Pipe.GetStartNodeEntityForPipe(objDb);
920
921
            //    //ArrayList arrayList = new ArrayList();
922
            //    //ArrayList arrayList2 = new ArrayList();
923
            //    //PIDUtils.VBUtils.GetUserDefineAttributes(startNodeEntityForPipe.ObjectId, ref arrayList, ref arrayList2);
924
            //    //PIDUtils.VBUtils.GetUserDefineAttributes(objDb.ObjectId, ref arrayList, ref arrayList2);
925
            //    //NamedXDataBlock namedXDataBlock = XDataUtilities.GetNamedBlock(objDb, "ReferenceObject");
926
927
            //    XData xData = XDataUtilities.GetXData<Entity>(objDb);
928
            //    XData xData2 = XDataUtilities.GetXData<Entity>(startNodeEntityForPipe);
929
            //    acTrans.Commit();
930
            //}
931
932
            //PLABFO
933 74a0c9d6 gaqhf
        }
934
        #endregion
935
    }
936 73152510 gaqhf
937
938
    public static class AcDbLinqExtensionMethods
939
    {
940
        /// <summary>
941
        /// Get all references to the given BlockTableRecord, including 
942
        /// references to anonymous dynamic BlockTableRecords.
943
        /// </summary>
944
945
        public static IEnumerable<BlockReference> GetBlockReferences(
946
           this BlockTableRecord btr,
947
           OpenMode mode = OpenMode.ForRead,
948
           bool directOnly = true)
949
        {
950
            if (btr == null)
951
                throw new ArgumentNullException("btr");
952
            var tr = btr.Database.TransactionManager.TopTransaction;
953
            if (tr == null)
954
                throw new InvalidOperationException("No transaction");
955
            var ids = btr.GetBlockReferenceIds(directOnly, true);
956
            int cnt = ids.Count;
957
            for (int i = 0; i < cnt; i++)
958
            {
959
                yield return (BlockReference)
960
                   tr.GetObject(ids[i], mode, false, false);
961
            }
962
            if (btr.IsDynamicBlock)
963
            {
964
                BlockTableRecord btr2 = null;
965
                var blockIds = btr.GetAnonymousBlockIds();
966
                cnt = blockIds.Count;
967
                for (int i = 0; i < cnt; i++)
968
                {
969
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
970
                       OpenMode.ForRead, false, false);
971
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
972
                    int cnt2 = ids.Count;
973
                    for (int j = 0; j < cnt2; j++)
974
                    {
975
                        yield return (BlockReference)
976
                           tr.GetObject(ids[j], mode, false, false);
977
                    }
978
                }
979
            }
980
        }
981
    }
982 74a0c9d6 gaqhf
}
클립보드 이미지 추가 (최대 크기: 500 MB)