프로젝트

일반

사용자정보

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

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 53344e2c

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

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

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

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

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

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

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

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

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

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

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

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

    
75
            GC.Collect();
76
        }
77
        public void Run()
78
        {
79
            RunLineModeling();
80
        }
81

    
82
        #region Run Method
83
        private void RunLineModeling()
84
        {
85
            foreach (var item in document.LINES)
86
            {
87
                if (item.Aveva.Handle == 0)
88
                    LineModeling(item);
89
            }
90
        }
91
        #endregion
92

    
93
        #region Modeling Method
94
        private void LineModeling(Model.Line line)
95
        {
96
            List<Model.Line> groupLine = new List<Model.Line>();
97
            List<string> points = new List<string>();
98
            GetConnectedGroupLine(line, null, groupLine);
99
            GetGroupLinePoints(groupLine, points);
100

    
101
            long handle = 0;
102
            if (line.Aveva.Type == Model.Type.Pipe)
103
                handle = DrawPipe(line.Aveva.Name, points);
104
            else if (line.Aveva.Type == Model.Type.Signal)
105
                handle = DrawSignal(line.Aveva.Name, points);
106

    
107
            foreach (var item in groupLine)
108
                item.Aveva.Handle = handle;
109
        }
110
        #endregion
111

    
112
        #region Drawing Method
113
        private long DrawPipe(string style, List<string> coordinates)
114
        {
115
            List<long> prevHandles = GetAllGroupHandles();
116

    
117
            GUIUtils.SetPipeStyle(style);
118
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
119
            Editor editor = acDoc.Editor;
120
            List<object> commandParam = new List<object>();
121
            //command name
122
            commandParam.Add("DRPIPE");
123
            //coordinate
124
            foreach (var item in coordinates)
125
                commandParam.Add(item);
126
            //enter
127
            commandParam.Add(null);
128
            //enter
129
            commandParam.Add(null);
130

    
131
            editor.Command(commandParam.ToArray());
132

    
133
            List<long> newHandles = GetAllGroupHandles();
134
            List<long> otherHandles = new List<long>();
135
            foreach (var item in newHandles)
136
                if (!prevHandles.Contains(item))
137
                    otherHandles.Add(item);
138

    
139
            if (otherHandles.Count == 1)
140
                return otherHandles[0];
141
            else
142
                return 0;
143
        }
144
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
145
        {
146
            List<long> prevHandles = GetAllGroupHandles();
147
            GUIUtils.SetPipeStyle(style);
148

    
149
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
150
            Editor editor = acDoc.Editor;
151
            List<object> commandParam = new List<object>();
152
            //command name
153
            commandParam.Add("DRPIPE");
154
            //coordinate
155
            foreach (var item in coordinates)
156
                commandParam.Add(item);
157
            //enter
158
            commandParam.Add(null);
159

    
160
            //Input Object ID
161
            commandParam.Add(objectId);
162

    
163
            //enter
164
            commandParam.Add(null);
165

    
166
            editor.Command(commandParam.ToArray());
167

    
168
            List<long> newHandles = GetAllGroupHandles();
169
            List<long> otherHandles = new List<long>();
170
            foreach (var item in newHandles)
171
                if (!prevHandles.Contains(item))
172
                    otherHandles.Add(item);
173

    
174
            if (otherHandles.Count == 1)
175
                return otherHandles[0];
176
            else
177
                return 0;
178
        }
179
        private long DrawSignal(string style, List<string> coordinates)
180
        {
181
            List<long> prevHandles = GetAllGroupHandles();
182

    
183
            GUIUtils.SetPipeStyle(style);
184
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
185
            Editor editor = acDoc.Editor;
186
            List<object> commandParam = new List<object>();
187
            //command name
188
            commandParam.Add("VPESIGNAL");
189
            //coordinate
190
            foreach (var item in coordinates)
191
                commandParam.Add(item);
192
            //enter
193
            commandParam.Add(null);
194

    
195
            editor.Command(commandParam.ToArray());
196

    
197
            List<long> newHandles = GetAllGroupHandles();
198
            List<long> otherHandles = new List<long>();
199
            foreach (var item in newHandles)
200
                if (!prevHandles.Contains(item))
201
                    otherHandles.Add(item);
202

    
203
            if (otherHandles.Count == 1)
204
                return otherHandles[0];
205
            else
206
                return 0;
207
        }
208
        private void InsertSymbol(string insertSymbolName, double x, double y)
209
        {
210
            try
211
            {
212
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
213
                drawingData.InsertSymbolName = insertSymbolName;
214
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
215
                Editor editor = acDoc.Editor;
216
                List<object> commandParam = new List<object>();
217
                commandParam.Add("INSSYM");
218
                Point3d point = new Point3d(x, y, 0);
219
                commandParam.Add(point);
220
                commandParam.Add(null);
221

    
222
                editor.Command(commandParam.ToArray());
223
            }
224
            catch (System.Exception ex)
225
            {
226

    
227
            }
228
        }
229
        #endregion
230

    
231
        #region
232
        private List<long> GetAllGroupHandles()
233
        {
234
            List<long> handles = new List<long>();
235

    
236
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
237
            Database acCurDb = acDoc.Database;
238
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
239
            {
240
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
241
                foreach (var entry in gd)
242
                    handles.Add(entry.Value.Handle.Value);
243
                acTrans.Commit();
244
            }
245
            return handles;
246
        }
247
        private ObjectId GetFirstPolyLine(long groupHandle)
248
        {
249
            ObjectId result = ObjectId.Null;
250

    
251
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
252
            Database acCurDb = acDoc.Database;
253
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
254
            {
255
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
256
                foreach (var entry in gd)
257
                {
258
                    if (entry.Value.Handle.Value == groupHandle)
259
                    {
260
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
261
                        foreach (var item in group.GetAllEntityIds())
262
                        {
263
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
264
                            {
265
                                result = item;
266
                                break;
267
                            }
268
                        }
269
                        break;
270
                    }
271
                }
272
                acTrans.Commit();
273
            }
274

    
275
            return result;
276
        }
277
        private void GetConnectedGroupLine(Model.Line line, Model.Line prevLine, List<Model.Line> group)
278
        {
279
            if (!group.Contains(line))
280
            {
281
                if (prevLine != null && prevLine.CONNECTORS[0].ConnectedObject == line)
282
                    group.Insert(0, line);
283
                else if (prevLine != null && prevLine.CONNECTORS[1].ConnectedObject == line)
284
                    group.Add(line);
285
                else
286
                    group.Add(line);
287

    
288
                if (line.CONNECTORS[0].ConnectedObject != null &&
289
                    line.CONNECTORS[0].ConnectedObject.GetType() == typeof(Model.Line) &&
290
                    APIDUtils.IsConnectedLine(line.CONNECTORS[0].ConnectedObject as Model.Line, line))
291
                    GetConnectedGroupLine(line.CONNECTORS[0].ConnectedObject as Model.Line, line, group);
292
                if (line.CONNECTORS[1].ConnectedObject != null &&
293
                    line.CONNECTORS[1].ConnectedObject.GetType() == typeof(Model.Line) &&
294
                    APIDUtils.IsConnectedLine(line.CONNECTORS[1].ConnectedObject as Model.Line, line))
295
                    GetConnectedGroupLine(line.CONNECTORS[1].ConnectedObject as Model.Line, line, group);
296
            }
297
        }
298
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
299
        {
300
            for (int i = 0; i < groupLine.Count; i++)
301
            {
302
                Model.Line line = groupLine[i];
303
                points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
304
                if (i == groupLine.Count - 1)
305
                    points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
306
            }
307
        }
308
        #endregion
309

    
310
        #region Test Source
311
        public void test()
312
        {
313
            //InitGUI();
314
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
315

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

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

    
320
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
321
        }
322
        public static void TESTStatic()
323
        {
324
            AutoModeling auto = new AutoModeling(null);
325
            auto.test();
326
        }
327
        #endregion
328
    }
329
}
클립보드 이미지 추가 (최대 크기: 500 MB)