프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ b90890eb

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

1
using System;
2
using System.Collections.Generic;
3
using System.Collections;
4
using System.Configuration;
5
using System.Data;
6
using System.Data.Common;
7
using System.Data.SqlClient;
8
using System.IO;
9

    
10
using Autodesk.AutoCAD.ApplicationServices;
11
using Autodesk.AutoCAD.ApplicationServices.Core;
12
using Autodesk.AutoCAD.DatabaseServices;
13
using Autodesk.AutoCAD.EditorInput;
14
using Autodesk.AutoCAD.Geometry;
15
using Autodesk.AutoCAD.Interop;
16
using Autodesk.AutoCAD.Interop.Common;
17
using Autodesk.AutoCAD.Runtime;
18
using Autodesk.AutoCAD.Windows;
19

    
20
using AVEVA.PID.Components;
21
using AVEVA.PID.GUI;
22
using AVEVA.PID.Common;
23
using AVEVA.PID.DWGSelector;
24

    
25
using AVEVA.PID.CustomizationUtility.DB;
26
using AVEVA.PID.CustomizationUtility.Model;
27
using AVEVA.PID.CustomizationUtility.Properties;
28

    
29
namespace AVEVA.PID.CustomizationUtility
30
{
31
    public class AutoModeling
32
    {
33
        public static AutoModeling Running { get; set; }
34
        Model.Document document;
35

    
36
        public AutoModeling(Model.Document document)
37
        {
38
            this.document = document;
39
        }
40

    
41
        public void CreateDrawing()
42
        {
43
            string outPath = Project_DB.GetDirectiveValue("DRGPTH");
44
            string tempPath = Path.GetTempPath();
45
            string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT");
46

    
47
            DWTSelector.strDrawingNumber = document.AvevaDrawingNumber;
48
            DWTSelector.strSheetNumber = document.AvevaSheetNumber;
49
            DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber;
50
            DWTSelector.strCadFilePath = outPath;
51

    
52
            try
53
            {
54
                string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false);
55
                Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
56
                Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text);
57

    
58
                Commands.addLatestPidvesrionToDictionary();
59
                AVVPropCmd.ActivateAcad();
60

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

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

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

    
110
        #region Modeling Method
111
        private void LineModeling(Model.Line line)
112
        {
113
            List<Model.Line> groupLine = new List<Model.Line>();
114
            List<string> points = new List<string>();
115
            GetConnectedGroupLine(line, groupLine, false);
116
            GetGroupLinePoints(groupLine, points);
117

    
118
            long handle = 0;
119
            if (line.Aveva.Type == Model.Type.Pipe)
120
                handle = DrawPipe(line.Aveva.Name, points);
121
            else if (line.Aveva.Type == Model.Type.Signal)
122
                handle = DrawSignal(line.Aveva.Name, points);
123

    
124
            foreach (var item in groupLine)
125
                item.Aveva.Handle = handle;
126
        }
127
        private void SymbolModeling(Symbol symbol)
128
        {
129

    
130
        }
131
        #endregion
132

    
133
        #region Drawing Method
134
        private long DrawPipe(string style, List<string> coordinates)
135
        {
136
            List<long> prevHandles = GetAllGroupHandles();
137

    
138
            GUIUtils.SetPipeStyle(style);
139
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
140
            Editor editor = acDoc.Editor;
141
            List<object> commandParam = new List<object>();
142
            //command name
143
            commandParam.Add("DRPIPE");
144
            //coordinate
145
            foreach (var item in coordinates)
146
                commandParam.Add(item);
147
            //enter
148
            commandParam.Add(null);
149
            //enter
150
            commandParam.Add(null);
151

    
152
            editor.Command(commandParam.ToArray());
153

    
154
            List<long> newHandles = GetAllGroupHandles();
155
            List<long> otherHandles = new List<long>();
156
            foreach (var item in newHandles)
157
                if (!prevHandles.Contains(item))
158
                    otherHandles.Add(item);
159

    
160
            if (otherHandles.Count == 1)
161
                return otherHandles[0];
162
            else
163
                return 0;
164
        }
165
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
166
        {
167
            List<long> prevHandles = GetAllGroupHandles();
168
            GUIUtils.SetPipeStyle(style);
169

    
170
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
171
            Editor editor = acDoc.Editor;
172
            List<object> commandParam = new List<object>();
173
            //command name
174
            commandParam.Add("DRPIPE");
175
            //coordinate
176
            foreach (var item in coordinates)
177
                commandParam.Add(item);
178
            //enter
179
            commandParam.Add(null);
180

    
181
            //Input Object ID
182
            commandParam.Add(objectId);
183

    
184
            //enter
185
            commandParam.Add(null);
186

    
187
            editor.Command(commandParam.ToArray());
188

    
189
            List<long> newHandles = GetAllGroupHandles();
190
            List<long> otherHandles = new List<long>();
191
            foreach (var item in newHandles)
192
                if (!prevHandles.Contains(item))
193
                    otherHandles.Add(item);
194

    
195
            if (otherHandles.Count == 1)
196
                return otherHandles[0];
197
            else
198
                return 0;
199
        }
200
        private long DrawSignal(string style, List<string> coordinates)
201
        {
202
            List<long> prevHandles = GetAllGroupHandles();
203

    
204
            GUIUtils.SetPipeStyle(style);
205
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
206
            Editor editor = acDoc.Editor;
207
            List<object> commandParam = new List<object>();
208
            //command name
209
            commandParam.Add("VPESIGNAL");
210
            //coordinate
211
            foreach (var item in coordinates)
212
                commandParam.Add(item);
213
            //enter
214
            commandParam.Add(null);
215

    
216
            editor.Command(commandParam.ToArray());
217

    
218
            List<long> newHandles = GetAllGroupHandles();
219
            List<long> otherHandles = new List<long>();
220
            foreach (var item in newHandles)
221
                if (!prevHandles.Contains(item))
222
                    otherHandles.Add(item);
223

    
224
            if (otherHandles.Count == 1)
225
                return otherHandles[0];
226
            else
227
                return 0;
228
        }
229
        private void InsertSymbol(string insertSymbolName, double x, double y)
230
        {
231
            try
232
            {
233
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
234
                drawingData.InsertSymbolName = insertSymbolName;
235
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
236
                Editor editor = acDoc.Editor;
237
                List<object> commandParam = new List<object>();
238
                commandParam.Add("INSSYM");
239
                Point3d point = new Point3d(x, y, 0);
240
                commandParam.Add(point);
241
                commandParam.Add(null);
242

    
243
                editor.Command(commandParam.ToArray());
244
            }
245
            catch (System.Exception ex)
246
            {
247

    
248
            }
249
        }
250
        #endregion
251

    
252
        #region
253
        private List<long> GetAllGroupHandles()
254
        {
255
            List<long> handles = new List<long>();
256

    
257
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
258
            Database acCurDb = acDoc.Database;
259
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
260
            {
261
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
262
                foreach (var entry in gd)
263
                    handles.Add(entry.Value.Handle.Value);
264
                acTrans.Commit();
265
            }
266
            return handles;
267
        }
268
        private ObjectId GetFirstPolyLine(long groupHandle)
269
        {
270
            ObjectId result = ObjectId.Null;
271

    
272
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
273
            Database acCurDb = acDoc.Database;
274
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
275
            {
276
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
277
                foreach (var entry in gd)
278
                {
279
                    if (entry.Value.Handle.Value == groupHandle)
280
                    {
281
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
282
                        foreach (var item in group.GetAllEntityIds())
283
                        {
284
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
285
                            {
286
                                result = item;
287
                                break;
288
                            }
289
                        }
290
                        break;
291
                    }
292
                }
293
                acTrans.Commit();
294
            }
295

    
296
            return result;
297
        }
298

    
299

    
300

    
301
        #endregion
302

    
303
        #region
304
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
305
        {
306
            if (!group.Contains(line))
307
            {
308
                if (isInsert)
309
                    group.Insert(0, line);
310
                else
311
                    group.Add(line);
312

    
313
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
314
                {
315
                    object connObj = line.CONNECTORS[0].ConnectedObject;
316
                    if (connObj.GetType() == typeof(Model.Line) &&
317
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
318
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
319
                    else if (connObj.GetType() == typeof(Model.Symbol))
320
                    {
321
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
322
                        if (connLine != null)
323
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
324
                    }
325
                }
326
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
327
                {
328
                    object connObj = line.CONNECTORS[1].ConnectedObject;
329
                    if (connObj.GetType() == typeof(Model.Line) &&
330
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
331
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
332
                    else if (connObj.GetType() == typeof(Model.Symbol))
333
                    {
334
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
335
                        if (connLine != null)
336
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
337
                    }
338
                }
339
            }
340
        }
341
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
342
        {
343
            for (int i = 0; i < groupLine.Count; i++)
344
            {
345
                Model.Line line = groupLine[i];
346
                if (i == 0)
347
                    points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
348
                else
349
                {
350
                    Model.Line prevLine = groupLine[i - 1];
351
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
352
                        prevLine.SlopeType == SlopeType.VERTICAL)
353
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
354
                    else if (line.SlopeType == SlopeType.VERTICAL &&
355
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
356
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
357
                    else
358
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
359
                }
360

    
361
                if (i == groupLine.Count - 1)
362
                    points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
363
            }
364
        }
365
        private bool IsExistEndBreak(object item1, object item2)
366
        {
367
            bool result = false;
368
            if (item1 != null && item2 != null)
369
            {
370
                EndBreak endBreak = document.EndBreaks.Find(x =>
371
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
372
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
373
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
374
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
375

    
376
                if (endBreak != null)
377
                    result = true;
378
            }
379

    
380
            return result;
381
        }
382
        private LineRun FindLineRun(Model.Line line)
383
        {
384
            List<LineRun> allLineRun = new List<LineRun>();
385
            foreach (var item in document.LINENUMBERS)
386
                allLineRun.AddRange(item.RUNS);
387
            foreach (var item in document.TRIMLINES)
388
                allLineRun.AddRange(item.RUNS);
389

    
390
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
391
        }
392
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
393
        {
394
            Model.Line result = null;
395
            LineRun run = FindLineRun(line);
396
            Connector connector = symbol.CONNECTORS.Find(x =>
397
            x.ConnectedObject != null &&
398
            x.ConnectedObject != line &&
399
            run.RUNITEMS.Contains(x.ConnectedObject) &&
400
            x.ConnectedObject.GetType() == typeof(Model.Line));
401

    
402
            if (connector != null)
403
                result = connector.ConnectedObject as Model.Line;
404

    
405
            return result;
406
        }
407
        #endregion
408

    
409
        #region Test Source
410
        public void test()
411
        {
412
            //InitGUI();
413
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
414

    
415
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" });
416

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

    
419
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
420
        }
421
        public static void TESTStatic()
422
        {
423
            AutoModeling auto = new AutoModeling(null);
424
            auto.test();
425
        }
426
        #endregion
427
    }
428
}
클립보드 이미지 추가 (최대 크기: 500 MB)