프로젝트

일반

사용자정보

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

hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 016701e5

이력 | 보기 | 이력해설 | 다운로드 (15.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
25 14540282 gaqhf
using AVEVA.PID.CustomizationUtility.DB;
26
using AVEVA.PID.CustomizationUtility.Model;
27
using AVEVA.PID.CustomizationUtility.Properties;
28 74a0c9d6 gaqhf
29
namespace AVEVA.PID.CustomizationUtility
30
{
31
    public class AutoModeling
32
    {
33 1c5bd296 gaqhf
        public static AutoModeling Running { get; set; }
34 14540282 gaqhf
        Model.Document document;
35 74a0c9d6 gaqhf
36 14540282 gaqhf
        public AutoModeling(Model.Document document)
37 74a0c9d6 gaqhf
        {
38 14540282 gaqhf
            this.document = document;
39 74a0c9d6 gaqhf
        }
40
41 1c5bd296 gaqhf
        public void CreateDrawing()
42 46756c13 gaqhf
        {
43 1c5bd296 gaqhf
            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 46756c13 gaqhf
            {
54 1c5bd296 gaqhf
                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 9dab7146 gaqhf
                    acadDocument.SendCommand("QSAVE ");
68 1c5bd296 gaqhf
                    Running = null;
69
                }
70 46756c13 gaqhf
            }
71 1c5bd296 gaqhf
            catch (System.Exception ex)
72
            {
73
                
74
            }
75
76
            GC.Collect();
77
        }
78
        public void Run()
79
        {
80 016701e5 gaqhf
            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 46756c13 gaqhf
        }
90
91 9dab7146 gaqhf
        #region Run Method
92
        private void RunLineModeling()
93 74a0c9d6 gaqhf
        {
94 9dab7146 gaqhf
            foreach (var item in document.LINES)
95 74a0c9d6 gaqhf
            {
96 53344e2c gaqhf
                if (item.Aveva.Handle == 0)
97
                    LineModeling(item);
98 74a0c9d6 gaqhf
            }
99 9dab7146 gaqhf
        }
100 016701e5 gaqhf
        private void RunSymbolModeling()
101
        {
102
103
        }
104 9dab7146 gaqhf
        #endregion
105 74a0c9d6 gaqhf
106 9dab7146 gaqhf
        #region Modeling Method
107
        private void LineModeling(Model.Line line)
108
        {
109 53344e2c gaqhf
            List<Model.Line> groupLine = new List<Model.Line>();
110 9dab7146 gaqhf
            List<string> points = new List<string>();
111 016701e5 gaqhf
            GetConnectedGroupLine(line, groupLine, false);
112 53344e2c gaqhf
            GetGroupLinePoints(groupLine, points);
113 9dab7146 gaqhf
114 53344e2c gaqhf
            long handle = 0;
115 9dab7146 gaqhf
            if (line.Aveva.Type == Model.Type.Pipe)
116 53344e2c gaqhf
                handle = DrawPipe(line.Aveva.Name, points);
117 9dab7146 gaqhf
            else if (line.Aveva.Type == Model.Type.Signal)
118 53344e2c gaqhf
                handle = DrawSignal(line.Aveva.Name, points);
119
120
            foreach (var item in groupLine)
121
                item.Aveva.Handle = handle;
122 74a0c9d6 gaqhf
        }
123 016701e5 gaqhf
124 9dab7146 gaqhf
        #endregion
125 74a0c9d6 gaqhf
126
        #region Drawing Method
127 9dab7146 gaqhf
        private long DrawPipe(string style, List<string> coordinates)
128 74a0c9d6 gaqhf
        {
129 9dab7146 gaqhf
            List<long> prevHandles = GetAllGroupHandles();
130 74a0c9d6 gaqhf
131 9dab7146 gaqhf
            GUIUtils.SetPipeStyle(style);
132 14540282 gaqhf
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
133 74a0c9d6 gaqhf
            Editor editor = acDoc.Editor;
134
            List<object> commandParam = new List<object>();
135
            //command name
136
            commandParam.Add("DRPIPE");
137
            //coordinate
138
            foreach (var item in coordinates)
139
                commandParam.Add(item);
140
            //enter
141
            commandParam.Add(null);
142
            //enter
143
            commandParam.Add(null);
144
145
            editor.Command(commandParam.ToArray());
146
147 9dab7146 gaqhf
            List<long> newHandles = GetAllGroupHandles();
148
            List<long> otherHandles = new List<long>();
149
            foreach (var item in newHandles)
150
                if (!prevHandles.Contains(item))
151
                    otherHandles.Add(item);
152 74a0c9d6 gaqhf
153 9dab7146 gaqhf
            if (otherHandles.Count == 1)
154
                return otherHandles[0];
155
            else
156
                return 0;
157 74a0c9d6 gaqhf
        }
158 9dab7146 gaqhf
        private long DrawPipe(string style, List<string> coordinates, ObjectId objectId)
159 74a0c9d6 gaqhf
        {
160 9dab7146 gaqhf
            List<long> prevHandles = GetAllGroupHandles();
161 14540282 gaqhf
            GUIUtils.SetPipeStyle(style);
162 74a0c9d6 gaqhf
163 14540282 gaqhf
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
164 74a0c9d6 gaqhf
            Editor editor = acDoc.Editor;
165
            List<object> commandParam = new List<object>();
166
            //command name
167
            commandParam.Add("DRPIPE");
168
            //coordinate
169
            foreach (var item in coordinates)
170
                commandParam.Add(item);
171
            //enter
172
            commandParam.Add(null);
173
174
            //Input Object ID
175
            commandParam.Add(objectId);
176
177
            //enter
178
            commandParam.Add(null);
179
180
            editor.Command(commandParam.ToArray());
181 9dab7146 gaqhf
182
            List<long> newHandles = GetAllGroupHandles();
183
            List<long> otherHandles = new List<long>();
184
            foreach (var item in newHandles)
185
                if (!prevHandles.Contains(item))
186
                    otherHandles.Add(item);
187
188
            if (otherHandles.Count == 1)
189
                return otherHandles[0];
190
            else
191
                return 0;
192
        }
193
        private long DrawSignal(string style, List<string> coordinates)
194
        {
195
            List<long> prevHandles = GetAllGroupHandles();
196
197
            GUIUtils.SetPipeStyle(style);
198
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
199
            Editor editor = acDoc.Editor;
200
            List<object> commandParam = new List<object>();
201
            //command name
202
            commandParam.Add("VPESIGNAL");
203
            //coordinate
204
            foreach (var item in coordinates)
205
                commandParam.Add(item);
206
            //enter
207
            commandParam.Add(null);
208
209
            editor.Command(commandParam.ToArray());
210
211
            List<long> newHandles = GetAllGroupHandles();
212
            List<long> otherHandles = new List<long>();
213
            foreach (var item in newHandles)
214
                if (!prevHandles.Contains(item))
215
                    otherHandles.Add(item);
216
217
            if (otherHandles.Count == 1)
218
                return otherHandles[0];
219
            else
220
                return 0;
221 74a0c9d6 gaqhf
        }
222 6b3cb476 gaqhf
        private void InsertSymbol(string insertSymbolName, double x, double y)
223
        {
224
            try
225
            {
226
                AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData();
227
                drawingData.InsertSymbolName = insertSymbolName;
228
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
229
                Editor editor = acDoc.Editor;
230
                List<object> commandParam = new List<object>();
231
                commandParam.Add("INSSYM");
232
                Point3d point = new Point3d(x, y, 0);
233
                commandParam.Add(point);
234
                commandParam.Add(null);
235
236
                editor.Command(commandParam.ToArray());
237
            }
238
            catch (System.Exception ex)
239
            {
240 74a0c9d6 gaqhf
241 6b3cb476 gaqhf
            }
242
        }
243 74a0c9d6 gaqhf
        #endregion
244
245 53344e2c gaqhf
        #region
246 9dab7146 gaqhf
        private List<long> GetAllGroupHandles()
247
        {
248
            List<long> handles = new List<long>();
249
250
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
251
            Database acCurDb = acDoc.Database;
252
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
253
            {
254
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
255
                foreach (var entry in gd)
256
                    handles.Add(entry.Value.Handle.Value);
257
                acTrans.Commit();
258
            }
259
            return handles;
260
        }
261
        private ObjectId GetFirstPolyLine(long groupHandle)
262
        {
263
            ObjectId result = ObjectId.Null;
264
265
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
266
            Database acCurDb = acDoc.Database;
267
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
268
            {
269
                DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead);
270
                foreach (var entry in gd)
271
                {
272
                    if (entry.Value.Handle.Value == groupHandle)
273
                    {
274
                        Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group;
275
                        foreach (var item in group.GetAllEntityIds())
276
                        {
277
                            if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline))
278
                            {
279
                                result = item;
280
                                break;
281
                            }
282
                        }
283
                        break;
284
                    }
285
                }
286
                acTrans.Commit();
287
            }
288
289
            return result;
290
        }
291 016701e5 gaqhf
292
293
294
        #endregion
295
296
        #region
297
        private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert)
298 53344e2c gaqhf
        {
299
            if (!group.Contains(line))
300
            {
301 016701e5 gaqhf
                if (isInsert)
302 53344e2c gaqhf
                    group.Insert(0, line);
303
                else
304
                    group.Add(line);
305
306 016701e5 gaqhf
                if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject))
307
                {
308
                    object connObj = line.CONNECTORS[0].ConnectedObject;
309
                    if (connObj.GetType() == typeof(Model.Line) &&
310
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
311
                        GetConnectedGroupLine(connObj as Model.Line, group, true);
312
                    else if (connObj.GetType() == typeof(Model.Symbol))
313
                    {
314
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
315
                        if (connLine != null)
316
                            GetConnectedGroupLine(connLine as Model.Line, group, true);
317
                    }
318
                }
319
                if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject))
320
                {
321
                    object connObj = line.CONNECTORS[1].ConnectedObject;
322
                    if (connObj.GetType() == typeof(Model.Line) &&
323
                        APIDUtils.IsConnectedLine(connObj as Model.Line, line))
324
                        GetConnectedGroupLine(connObj as Model.Line, group, false);
325
                    else if (connObj.GetType() == typeof(Model.Symbol))
326
                    {
327
                        Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol);
328
                        if (connLine != null)
329
                            GetConnectedGroupLine(connLine as Model.Line, group, false);
330
                    }
331
                }
332 53344e2c gaqhf
            }
333
        }
334
        private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points)
335
        {
336
            for (int i = 0; i < groupLine.Count; i++)
337
            {
338
                Model.Line line = groupLine[i];
339 016701e5 gaqhf
                if (i == 0)
340
                    points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
341
                else
342
                {
343
                    Model.Line prevLine = groupLine[i - 1];
344
                    if (line.SlopeType == SlopeType.HORIZONTAL &&
345
                        prevLine.SlopeType == SlopeType.VERTICAL)
346
                        points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y);
347
                    else if (line.SlopeType == SlopeType.VERTICAL &&
348
                        prevLine.SlopeType == SlopeType.HORIZONTAL)
349
                        points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y);
350
                    else
351
                        points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y);
352
                }
353
354 53344e2c gaqhf
                if (i == groupLine.Count - 1)
355
                    points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y);
356
            }
357
        }
358 016701e5 gaqhf
        private bool IsExistEndBreak(object item1, object item2)
359
        {
360
            bool result = false;
361
            if (item1 != null && item2 != null)
362
            {
363
                EndBreak endBreak = document.EndBreaks.Find(x =>
364
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 &&
365
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) ||
366
            (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 &&
367
            APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1));
368
369
                if (endBreak != null)
370
                    result = true;
371
            }
372
373
            return result;
374
        }
375
        private LineRun FindLineRun(Model.Line line)
376
        {
377
            List<LineRun> allLineRun = new List<LineRun>();
378
            foreach (var item in document.LINENUMBERS)
379
                allLineRun.AddRange(item.RUNS);
380
            foreach (var item in document.TRIMLINES)
381
                allLineRun.AddRange(item.RUNS);
382
383
            return allLineRun.Find(x => x.RUNITEMS.Contains(line));
384
        }
385
        private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol)
386
        {
387
            Model.Line result = null;
388
            LineRun run = FindLineRun(line);
389
            Connector connector = symbol.CONNECTORS.Find(x =>
390
            x.ConnectedObject != null &&
391
            x.ConnectedObject != line &&
392
            run.RUNITEMS.Contains(x.ConnectedObject) &&
393
            x.ConnectedObject.GetType() == typeof(Model.Line));
394
395
            if (connector != null)
396
                result = connector.ConnectedObject as Model.Line;
397
398
            return result;
399
        }
400 53344e2c gaqhf
        #endregion
401 9dab7146 gaqhf
402 74a0c9d6 gaqhf
        #region Test Source
403
        public void test()
404
        {
405 14540282 gaqhf
            //InitGUI();
406 9dab7146 gaqhf
            //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" });
407 74a0c9d6 gaqhf
408
            DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" });
409
410 9dab7146 gaqhf
            //DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" });
411 74a0c9d6 gaqhf
412 9dab7146 gaqhf
            //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" });
413 74a0c9d6 gaqhf
        }
414 9dab7146 gaqhf
        public static void TESTStatic()
415 74a0c9d6 gaqhf
        {
416 14540282 gaqhf
            AutoModeling auto = new AutoModeling(null);
417 74a0c9d6 gaqhf
            auto.test();
418
        }
419
        #endregion
420
    }
421
}
클립보드 이미지 추가 (최대 크기: 500 MB)