프로젝트

일반

사용자정보

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

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 28f44436

이력 | 보기 | 이력해설 | 다운로드 (36.7 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
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 1c5bd296 gaqhf
using AVEVA.PID.Components;
21
using AVEVA.PID.GUI;
22
using AVEVA.PID.Common;
23
using AVEVA.PID.DWGSelector;
24 495bb8f5 gaqhf
using AVEVA.PID.Utilities;
25 1c5bd296 gaqhf
26 14540282 gaqhf
using AVEVA.PID.CustomizationUtility.DB;
27
using AVEVA.PID.CustomizationUtility.Model;
28
using AVEVA.PID.CustomizationUtility.Properties;
29 74a0c9d6 gaqhf
30 aba318eb gaqhf
using DevExpress.XtraSplashScreen;
31
32 74a0c9d6 gaqhf
namespace AVEVA.PID.CustomizationUtility
33
{
34
    public class AutoModeling
35
    {
36 1c5bd296 gaqhf
        public static AutoModeling Running { get; set; }
37 14540282 gaqhf
        Model.Document document;
38 74a0c9d6 gaqhf
39 495bb8f5 gaqhf
        System.Data.DataTable AvevaSymbolTable = null;
40
41 a19bd6e2 gaqhf
        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 495bb8f5 gaqhf
        public AutoModeling(Model.Document document, System.Data.DataTable AvevaSymbolTable)
56 74a0c9d6 gaqhf
        {
57 14540282 gaqhf
            this.document = document;
58 495bb8f5 gaqhf
            this.AvevaSymbolTable = AvevaSymbolTable;
59 74a0c9d6 gaqhf
        }
60
61 1c5bd296 gaqhf
        public void CreateDrawing()
62 46756c13 gaqhf
        {
63 1c5bd296 gaqhf
            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 46756c13 gaqhf
            {
74 1c5bd296 gaqhf
                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 9dab7146 gaqhf
                    acadDocument.SendCommand("QSAVE ");
88 1c5bd296 gaqhf
                    Running = null;
89
                }
90 46756c13 gaqhf
            }
91 1c5bd296 gaqhf
            catch (System.Exception ex)
92
            {
93
                
94
            }
95
96
            GC.Collect();
97
        }
98
        public void Run()
99
        {
100 016701e5 gaqhf
            try
101
            {
102 aba318eb gaqhf
                SplashScreenManager.ShowForm(typeof(APIDSplashScreen), true, true);
103 6503fec8 gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllStepCount, 5);
104 aba318eb gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetDocumentName, document.AvevaDrawingNumber + document.AvevaSheetNumber);
105 932933ed gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetParent, Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
106
107 a19bd6e2 gaqhf
                SetConvertRule();
108 aba318eb gaqhf
109 016701e5 gaqhf
                RunLineModeling();
110 a19bd6e2 gaqhf
                RunOPCModeling();
111 016701e5 gaqhf
                RunSymbolModeling();
112 475071f2 gaqhf
                RunTextModeling();
113 6503fec8 gaqhf
                RunNoteModeling();
114 016701e5 gaqhf
            }
115
            catch (System.Exception ex)
116
            {
117
                System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace);
118
            }
119 aba318eb gaqhf
            finally
120
            {
121 932933ed gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.ClearParent, null);
122 aba318eb gaqhf
                SplashScreenManager.CloseForm(false);
123
            }
124 46756c13 gaqhf
        }
125
126 9dab7146 gaqhf
        #region Run Method
127
        private void RunLineModeling()
128 74a0c9d6 gaqhf
        {
129 aba318eb gaqhf
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
130
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling");
131 9dab7146 gaqhf
            foreach (var item in document.LINES)
132 74a0c9d6 gaqhf
            {
133 53344e2c gaqhf
                if (item.Aveva.Handle == 0)
134
                    LineModeling(item);
135 aba318eb gaqhf
136
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
137 74a0c9d6 gaqhf
            }
138 9dab7146 gaqhf
        }
139 a19bd6e2 gaqhf
        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 016701e5 gaqhf
        private void RunSymbolModeling()
151
        {
152 aba318eb gaqhf
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
153
            SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
154 b90890eb gaqhf
            foreach (var item in document.SYMBOLS)
155
            {
156
                if (item.Aveva.Handle == 0)
157
                    SymbolModeling(item);
158 aba318eb gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
159 b90890eb gaqhf
            }
160 016701e5 gaqhf
        }
161 475071f2 gaqhf
        private void RunTextModeling()
162
        {
163 0a8db7fe gaqhf
            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 6503fec8 gaqhf
                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 7a8645c9 gaqhf
            foreach (var item in document.Notes)
176 6503fec8 gaqhf
            {
177 7a8645c9 gaqhf
                NoteModeling(item);
178 0a8db7fe gaqhf
                SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null);
179
            }
180 475071f2 gaqhf
        }
181 9dab7146 gaqhf
        #endregion
182 74a0c9d6 gaqhf
183 9dab7146 gaqhf
        #region Modeling Method
184
        private void LineModeling(Model.Line line)
185
        {
186 53344e2c gaqhf
            List<Model.Line> groupLine = new List<Model.Line>();
187 9dab7146 gaqhf
            List<string> points = new List<string>();
188 016701e5 gaqhf
            GetConnectedGroupLine(line, groupLine, false);
189 53344e2c gaqhf
            GetGroupLinePoints(groupLine, points);
190 9dab7146 gaqhf
191 932933ed gaqhf
            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 53344e2c gaqhf
            long handle = 0;
207 9dab7146 gaqhf
            if (line.Aveva.Type == Model.Type.Pipe)
208 932933ed gaqhf
                handle = DrawPipe(line.Aveva.Name, points, lineNumber);
209 9dab7146 gaqhf
            else if (line.Aveva.Type == Model.Type.Signal)
210 53344e2c gaqhf
                handle = DrawSignal(line.Aveva.Name, points);
211
212 932933ed gaqhf
            if (lineNumber != null)
213
                GUIUtils.SetAutoLabelCheck(false);
214
215 53344e2c gaqhf
            foreach (var item in groupLine)
216
                item.Aveva.Handle = handle;
217 74a0c9d6 gaqhf
        }
218 b90890eb gaqhf
        private void SymbolModeling(Symbol symbol)
219
        {
220 495bb8f5 gaqhf
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
221
            if (allRows.Length == 1)
222 2d09df82 gaqhf
            {
223 495bb8f5 gaqhf
                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 2d09df82 gaqhf
                {
230 495bb8f5 gaqhf
                    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 2d09df82 gaqhf
                }
241
            }
242 b90890eb gaqhf
        }
243 6503fec8 gaqhf
        private void TextModeling(Text text)
244
        {
245
            if (text.TextAngle == TextAngle.None)
246
                return;
247
248
            if (text.Aveva.LabelType == LabelType.SingleText)
249 a03f7b94 gaqhf
                DrawText(text.Aveva.X, text.Aveva.Y, text.Aveva.Height, text.VALUE, text.Aveva.Angle);
250 6503fec8 gaqhf
            else if (text.Aveva.LabelType == LabelType.MultiText)
251 7a8645c9 gaqhf
            {
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 a03f7b94 gaqhf
                    DrawText(x, y, text.Aveva.Height, values[i], text.Aveva.Angle);
269 7a8645c9 gaqhf
                }
270
            }
271
                
272 6503fec8 gaqhf
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 a03f7b94 gaqhf
                DrawText(note.Aveva.X, note.Aveva.Y, note.Aveva.Height, note.VALUE, note.Aveva.Angle);
281 6503fec8 gaqhf
            else if (note.Aveva.LabelType == LabelType.MultiNote)
282 7a8645c9 gaqhf
            {
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 6503fec8 gaqhf
299 a03f7b94 gaqhf
                    DrawText(x, y, note.Aveva.Height, values[i], note.Aveva.Angle);
300 7a8645c9 gaqhf
                }
301
            }
302 6503fec8 gaqhf
        }
303 9dab7146 gaqhf
        #endregion
304 74a0c9d6 gaqhf
305
        #region Drawing Method
306 932933ed gaqhf
        private long DrawPipe(string style, List<string> coordinates, LineNumber lineNumber = null)
307 74a0c9d6 gaqhf
        {
308 9dab7146 gaqhf
            List<long> prevHandles = GetAllGroupHandles();
309 74a0c9d6 gaqhf
310 9dab7146 gaqhf
            GUIUtils.SetPipeStyle(style);
311 14540282 gaqhf
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
312 74a0c9d6 gaqhf
            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 932933ed gaqhf
320
            //enter Parent(null)
321 74a0c9d6 gaqhf
            commandParam.Add(null);
322
323 932933ed gaqhf
            if (lineNumber != null)
324
            {
325
                Point3d point = new Point3d(lineNumber.Aveva.X, lineNumber.Aveva.Y, 0);
326
                commandParam.Add(point);
327 475071f2 gaqhf
                commandParam.Add(lineNumber.Aveva.Angle);
328 932933ed gaqhf
329
                commandParam.Add(null);
330
            }
331
            else
332
                commandParam.Add(null);
333
334 74a0c9d6 gaqhf
            editor.Command(commandParam.ToArray());
335
336 9dab7146 gaqhf
            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 74a0c9d6 gaqhf
342 9dab7146 gaqhf
            if (otherHandles.Count == 1)
343
                return otherHandles[0];
344
            else
345
                return 0;
346 74a0c9d6 gaqhf
        }
347 9dab7146 gaqhf
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
348 74a0c9d6 gaqhf
        {
349 9dab7146 gaqhf
            List<long> prevHandles = GetAllGroupHandles();
350 14540282 gaqhf
            GUIUtils.SetPipeStyle(style);
351 74a0c9d6 gaqhf
352 14540282 gaqhf
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
353 74a0c9d6 gaqhf
            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 9dab7146 gaqhf
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 74a0c9d6 gaqhf
        }
411 495bb8f5 gaqhf
        private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon)
412 6b3cb476 gaqhf
        {
413 73152510 gaqhf
            long handle = 0;
414 6b3cb476 gaqhf
            try
415
            {
416 495bb8f5 gaqhf
                string insertSymbolName = symbol.Aveva.Name;
417
                double x = symbol.Aveva.X;
418
                double y = symbol.Aveva.Y;
419
420 73152510 gaqhf
                List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName);
421
422 6b3cb476 gaqhf
                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 495bb8f5 gaqhf
429
                // Insert Point
430 6b3cb476 gaqhf
                Point3d point = new Point3d(x, y, 0);
431
                commandParam.Add(point);
432 495bb8f5 gaqhf
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 6b3cb476 gaqhf
451
                editor.Command(commandParam.ToArray());
452 73152510 gaqhf
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 6b3cb476 gaqhf
            }
464
            catch (System.Exception ex)
465
            {
466 74a0c9d6 gaqhf
467 6b3cb476 gaqhf
            }
468 73152510 gaqhf
469
            return handle;
470 6b3cb476 gaqhf
        }
471 a03f7b94 gaqhf
        private void DrawText(double x, double y, double height, string text, double rotation)
472 6503fec8 gaqhf
        {
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 a03f7b94 gaqhf
                    acText.Rotation = rotation;
497 6503fec8 gaqhf
498
                    acBlkTblRec.AppendEntity(acText);
499
                    acTrans.AddNewlyCreatedDBObject(acText, true);
500
                }
501
502
                // Save the changes and dispose of the transaction
503
                acTrans.Commit();
504
            }
505
        }
506 74a0c9d6 gaqhf
        #endregion
507
508 73152510 gaqhf
        #region Autocad Utils
509
        private List<long> GetAllBlockHandlesByName(string name)
510
        {
511
            List<long> handles = new List<long>();
512
513
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
514
            Database acCurDb = acDoc.Database;
515
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
516
            {
517
                BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
518
                foreach (var entry in gd)
519
                {
520
                    BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord;
521
                    if (blockTableRecord != null)
522
                    {
523
                        IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord);
524
                        foreach (var item in records)
525
                        {
526
                            if (item.Name == name)
527
                                handles.Add(item.Handle.Value);
528
                        }
529
                    }
530
                }
531
                    
532
                acTrans.Commit();
533
            }
534
            return handles;
535
        }
536 9dab7146 gaqhf
        private List<long> GetAllGroupHandles()
537
        {
538
            List<long> handles = new List<long>();
539
540
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
541
            Database acCurDb = acDoc.Database;
542
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
543
            {
544
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
545
                foreach (var entry in gd)
546
                    handles.Add(entry.Value.Handle.Value);
547
                acTrans.Commit();
548
            }
549
            return handles;
550
        }
551
        private ObjectId GetFirstPolyLine(long groupHandle)
552
        {
553
            ObjectId result = ObjectId.Null;
554
555
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
556
            Database acCurDb = acDoc.Database;
557
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
558
            {
559
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
560
                foreach (var entry in gd)
561
                {
562
                    if (entry.Value.Handle.Value == groupHandle)
563
                    {
564
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
565
                        foreach (var item in group.GetAllEntityIds())
566
                        {
567
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
568
                            {
569
                                result = item;
570
                                break;
571
                            }
572
                        }
573
                        break;
574
                    }
575
                }
576
                acTrans.Commit();
577
            }
578
579
            return result;
580
        }
581 2d09df82 gaqhf
        private ObjectId GetObjectIdByHandle(long lHandle)
582
        {
583
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
584
            Database acCurDb = acDoc.Database;
585
            Handle handle = new Handle(lHandle);
586
587
            return acCurDb.GetObjectId(false, handle, 0);
588
        }
589
        private void SetBlockReferenceRotation(ObjectId objectId, double rotation)
590
        {
591
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
592
            Database acCurDb = acDoc.Database;
593
            Editor acDocEd = acDoc.Editor;
594
595
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
596
            {
597
                DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite);
598
                if (objDB != null)
599
                {
600
                    if (objDB.GetType() == typeof(BlockReference))
601
                    {
602
                        BlockReference block = objDB as BlockReference;
603
                        block.Rotation = rotation;
604
                    }
605
                }
606
                acTrans.Commit();
607
            }
608
        }
609 016701e5 gaqhf
        #endregion
610
611 73152510 gaqhf
        #region Modeling Utils
612 016701e5 gaqhf
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
613 53344e2c gaqhf
        {
614
            if (!group.Contains(line))
615
            {
616 016701e5 gaqhf
                if (isInsert)
617 53344e2c gaqhf
                    group.Insert(0, line);
618
                else
619
                    group.Add(line);
620
621 016701e5 gaqhf
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
622
                {
623
                    object connObj = line.CONNECTORS[0].ConnectedObject;
624
                    if (connObj.GetType() == typeof(Model.Line) &&
625
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
626
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
627
                    else if (connObj.GetType() == typeof(Model.Symbol))
628
                    {
629
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
630
                        if (connLine != null)
631
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
632
                    }
633
                }
634
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
635
                {
636
                    object connObj = line.CONNECTORS[1].ConnectedObject;
637
                    if (connObj.GetType() == typeof(Model.Line) &&
638
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
639
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
640
                    else if (connObj.GetType() == typeof(Model.Symbol))
641
                    {
642
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
643
                        if (connLine != null)
644
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
645
                    }
646
                }
647 53344e2c gaqhf
            }
648
        }
649
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
650
        {
651
            for (int i = 0; i < groupLine.Count; i++)
652
            {
653
                Model.Line line = groupLine[i];
654 016701e5 gaqhf
                if (i == 0)
655 f7e09e14 gaqhf
                {
656
                    object connObj = line.CONNECTORS[0].ConnectedObject;
657 ff4941b2 gaqhf
                    if (connObj != null)
658 f7e09e14 gaqhf
                    {
659 ff4941b2 gaqhf
                        string point = GetPointByConnectedItem(connObj, line, true);
660
                        if (string.IsNullOrEmpty(point))
661 f7e09e14 gaqhf
                            points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
662 ff4941b2 gaqhf
                        else
663
                            points.Add(point);
664 f7e09e14 gaqhf
                    }
665
                    else
666
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
667
                }
668 016701e5 gaqhf
                else
669
                {
670
                    Model.Line prevLine = groupLine[i - 1];
671
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
672
                        prevLine.SlopeType == SlopeType.VERTICAL)
673
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
674
                    else if (line.SlopeType == SlopeType.VERTICAL &&
675
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
676
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
677
                    else
678
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
679
                }
680
681 53344e2c gaqhf
                if (i == groupLine.Count - 1)
682 f7e09e14 gaqhf
                {
683
                    object connObj = line.CONNECTORS[1].ConnectedObject;
684
                    if (connObj != null && connObj.GetType() == typeof(Symbol))
685
                    {
686 ff4941b2 gaqhf
                        string point = GetPointByConnectedItem(connObj, line, false);
687
                        if (string.IsNullOrEmpty(point))
688 f7e09e14 gaqhf
                            points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
689 ff4941b2 gaqhf
                        else
690
                            points.Add(point);
691 f7e09e14 gaqhf
                    }
692
                    else
693
                        points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
694
                }
695 53344e2c gaqhf
            }
696
        }
697 ff4941b2 gaqhf
        private string GetPointByConnectedItem(object connObj, Model.Line line, bool isStart)
698
        {
699
            string result = string.Empty;
700
            double x;
701
            double y;
702
            if (isStart)
703
            {
704
                x = line.Aveva.Start_X;
705
                y = line.Aveva.Start_Y;
706
            }
707
            else
708
            {
709
                x = line.Aveva.End_X;
710
                y = line.Aveva.End_Y;
711
            }
712
713
            if (connObj.GetType() == typeof(Symbol))
714
            {
715
                Symbol connSymbol = connObj as Symbol;
716
                Connector connector = connSymbol.CONNECTORS.Find(loop => loop.ConnectedObject == line);
717
718
                SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y);
719
                if (slopeType == SlopeType.HORIZONTAL)
720
                    result = connSymbol.Aveva.X + "," + y;
721
                else if (slopeType == SlopeType.VERTICAL)
722
                    result = x + "," + connSymbol.Aveva.Y;
723
                else
724
                    result = x + "," + y;
725
            }
726
            else if (connObj.GetType() == typeof(OPC))
727
            {
728
                OPC connOPC = connObj as OPC;
729
                Connector connector = connOPC.CONNECTORS.Find(loop => loop.ConnectedObject == line);
730
731
                double locX = 0, locY = 0, sizeWidth = 0, sizeHeight = 0, aX = 0, aY = 0;
732
                APIDUtils.ConvertPointBystring(connOPC.LOCATION, ref locX, ref locY);
733
                APIDUtils.ConvertPointBystring(connOPC.SIZE, ref sizeWidth, ref sizeHeight);
734
                locX = locX + sizeWidth / 2;
735
                locY = locY + sizeHeight / 2;
736
                aX = locX;
737
                aY = locY;
738
                document.ConvertAvevaPoint(ref aX, ref aY);
739
740
                SlopeType slopeType = APIDUtils.CalcSlope(locX, locY, connector.X, connector.Y);
741
                if (slopeType == SlopeType.HORIZONTAL)
742
                    result = aX + "," + y;
743
                else if (slopeType == SlopeType.VERTICAL)
744
                    result = x + "," + aY;
745
                else
746
                    result = x + "," + y;
747
            }
748
            
749
            return result;
750
        }
751 016701e5 gaqhf
        private bool IsExistEndBreak(object item1, object item2)
752
        {
753
            bool result = false;
754
            if (item1 != null && item2 != null)
755
            {
756
                EndBreak endBreak = document.EndBreaks.Find(x =>
757
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
758
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
759
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
760
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
761
762
                if (endBreak != null)
763
                    result = true;
764
            }
765
766
            return result;
767
        }
768
        private LineRun FindLineRun(Model.Line line)
769
        {
770
            List<LineRun> allLineRun = new List<LineRun>();
771
            foreach (var item in document.LINENUMBERS)
772
                allLineRun.AddRange(item.RUNS);
773
            foreach (var item in document.TRIMLINES)
774
                allLineRun.AddRange(item.RUNS);
775
776
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
777
        }
778
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
779
        {
780
            Model.Line result = null;
781
            LineRun run = FindLineRun(line);
782
            Connector connector = symbol.CONNECTORS.Find(x =>
783
            x.ConnectedObject != null &&
784
            x.ConnectedObject != line &&
785
            run.RUNITEMS.Contains(x.ConnectedObject) &&
786
            x.ConnectedObject.GetType() == typeof(Model.Line));
787
788
            if (connector != null)
789
                result = connector.ConnectedObject as Model.Line;
790 2d09df82 gaqhf
            
791 016701e5 gaqhf
            return result;
792
        }
793 495bb8f5 gaqhf
        private bool CheckAngle(DataRow symbolRow)
794
        {
795
            return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2);
796
        }
797
        private bool CheckBalloon(Symbol symbol)
798
        {
799
            bool result = false;
800
801
            string text = string.Empty;
802
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
803
            if (symbolDefinitions.Count != 0)
804
            {
805
                text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
806
807
                if (text.Equals("VLP"))
808
                    text = "VPO";
809
                else if (text.Equals("IVP"))
810
                    text = "IPO";
811
812
                if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text))
813
                    result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId);
814
            }
815
816
            return result;
817
        }
818 53344e2c gaqhf
        #endregion
819 9dab7146 gaqhf
820 74a0c9d6 gaqhf
        #region Test Source
821
        public void test()
822
        {
823 14540282 gaqhf
            //InitGUI();
824 9dab7146 gaqhf
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
825 74a0c9d6 gaqhf
826 932933ed gaqhf
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }, null);
827
            return;
828 495bb8f5 gaqhf
            Symbol symbol = new Symbol();
829
            symbol.Aveva = new AvevaSymbolInfo();
830
            symbol.Aveva.Name = "INVB";
831
            symbol.Aveva.X = 50;
832
            symbol.Aveva.Y = 100;
833
834
            DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name));
835
            if (allRows.Length == 1)
836
            {
837
                DataRow symbolRow = allRows[0];
838
                bool bAngle = CheckAngle(symbolRow);
839
                bool bBalloon = CheckBalloon(symbol);
840
                bBalloon = false;
841
                long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon);
842
            }
843
844
            Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name);
845
            string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper();
846
            if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2))
847
            {
848
                bool bIsPromptRotate = true;
849
            }
850
            int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString());
851
852
            AssociateSymbolType eAssosiateSym = AssociateSymbolType.None;
853
            bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym);
854
            bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text);
855
856 9dab7146 gaqhf
            //DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" });
857 74a0c9d6 gaqhf
858 9dab7146 gaqhf
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
859 74a0c9d6 gaqhf
        }
860 9dab7146 gaqhf
        public static void TESTStatic()
861 74a0c9d6 gaqhf
        {
862 495bb8f5 gaqhf
            System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable();
863
            AutoModeling auto = new AutoModeling(null, avevaSymbolTable);
864 74a0c9d6 gaqhf
            auto.test();
865
        }
866
        #endregion
867
    }
868 73152510 gaqhf
869
870
    public static class AcDbLinqExtensionMethods
871
    {
872
        /// <summary>
873
        /// Get all references to the given BlockTableRecord, including 
874
        /// references to anonymous dynamic BlockTableRecords.
875
        /// </summary>
876
877
        public static IEnumerable<BlockReference> GetBlockReferences(
878
           this BlockTableRecord btr,
879
           OpenMode mode = OpenMode.ForRead,
880
           bool directOnly = true)
881
        {
882
            if (btr == null)
883
                throw new ArgumentNullException("btr");
884
            var tr = btr.Database.TransactionManager.TopTransaction;
885
            if (tr == null)
886
                throw new InvalidOperationException("No transaction");
887
            var ids = btr.GetBlockReferenceIds(directOnly, true);
888
            int cnt = ids.Count;
889
            for (int i = 0; i < cnt; i++)
890
            {
891
                yield return (BlockReference)
892
                   tr.GetObject(ids[i], mode, false, false);
893
            }
894
            if (btr.IsDynamicBlock)
895
            {
896
                BlockTableRecord btr2 = null;
897
                var blockIds = btr.GetAnonymousBlockIds();
898
                cnt = blockIds.Count;
899
                for (int i = 0; i < cnt; i++)
900
                {
901
                    btr2 = (BlockTableRecord)tr.GetObject(blockIds[i],
902
                       OpenMode.ForRead, false, false);
903
                    ids = btr2.GetBlockReferenceIds(directOnly, true);
904
                    int cnt2 = ids.Count;
905
                    for (int j = 0; j < cnt2; j++)
906
                    {
907
                        yield return (BlockReference)
908
                           tr.GetObject(ids[j], mode, false, false);
909
                    }
910
                }
911
            }
912
        }
913
    }
914 74a0c9d6 gaqhf
}
클립보드 이미지 추가 (최대 크기: 500 MB)