프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 1ff0105e

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

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows.Forms;
10
using System.Threading;
11
using System.IO;
12
using Microsoft.VisualBasic;
13
using Ingr.RAD2D;
14
using Converter.BaseModel;
15
using Converter.SPPID.Properties;
16
using Converter.SPPID.DB;
17
using Converter.SPPID.Util;
18
using Converter.SPPID.Form;
19
using Converter.SPPID.Model;
20
using Plaice;
21
using Llama;
22
using DevExpress.XtraSplashScreen;
23
using Newtonsoft.Json;
24
using System.Runtime.InteropServices;
25

    
26
namespace Converter.SPPID.Wrapper
27
{
28
    public partial class ConverterDocking : UserControl
29
    {
30
        Ingr.RAD2D.Application radApp;
31
        dynamic application;
32
        public ConverterDocking()
33
        {
34
            InitializeComponent();
35
            application = Interaction.GetObject("", "PIDAutomation.Application");
36
            WrapperApplication wApp = new WrapperApplication(application.Application);
37
            radApp = wApp.RADApplication;
38

    
39
            try
40
            {
41
                textEditDrawingX.EditValue = Settings.Default.DrawingX;
42
                textEditDrawingY.EditValue = Settings.Default.DrawingY;
43
            }
44
            catch (Exception ex)
45
            {
46
                StringBuilder sb = new StringBuilder();
47
                sb.AppendLine(ex.Message);
48
                sb.AppendLine(ex.StackTrace);
49
                MessageBox.Show(sb.ToString());
50
            }
51

    
52
#if DEBUG
53
            simpleButton1.Visible = true;
54

    
55
            
56
#endif
57
        }
58

    
59
        private void btnConverter_Click(object sender, EventArgs e)
60
        {
61
            ConverterForm converterForm = new ConverterForm();
62
            if (converterForm.ShowDialog() == DialogResult.OK)
63
            {
64
                try
65
                {
66
                    CloseOPCForm.Run();
67

    
68
                    for (int i = 0; i < converterForm.Documents.Count; i++)
69
                    {
70
                        SPPID_Document document = converterForm.Documents[i];
71
                        if (document.SetSPPIDMapping() && document.Enable)
72
                        {
73
                            AutoModeling modeling = new AutoModeling(document, application, radApp);
74
                            modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
75
                            modeling.Run();
76
                        }
77
                    }
78
                }
79
                catch (Exception ex)
80
                {
81
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
82
                }
83
                finally
84
                {
85
                    CloseOPCForm.Stop();
86
                }
87

    
88
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
89
            }
90
        }
91

    
92
        private void btnLinkOPC_Click(object sender, EventArgs e)
93
        {
94
            LMADataSource dataSource = new LMADataSource();
95
            LMDrawings drawings = new LMDrawings();
96

    
97
            //try
98
            {
99
                Project_Info _ProjectInfo = Project_Info.GetInstance();
100
                _ProjectInfo.DefaultPath = Settings.Default.LatestProjectPath;
101
                if (Project_DB.ConnTestAndCreateTable())
102
                {
103
                    DataTable dt = Project_DB.SelectSPPID_DB_INFO();
104
                    if (dt.Columns.Count > 0 && dt.Rows.Count > 0)
105
                        SPPIDUtil.ConvertToSPPIDInfo(dt.Rows[0][0].ToString());
106
                    else
107
                        SPPID_DBInfo.Clear();
108

    
109
                    SPPID_DBInfo sPPID_DBInfo = SPPID_DBInfo.GetInstance();
110
                    if (sPPID_DBInfo.Enable)
111
                    {
112
                        drawings.Collect(dataSource);
113

    
114
                        DataTable drawingTable = Project_DB.SelectDrawingInfo();
115
                        drawingTable.Columns.Add("EXIST", typeof(bool));
116
                        drawingTable.Columns.Add("SPPIDPATH", typeof(string));
117

    
118
                        foreach (LMDrawing item in drawings)
119
                        {
120
                            DataRow[] rows = drawingTable.Select(string.Format("DRAWINGNUMBER = '{0}'", item.Attributes["DrawingNumber"].get_Value()));
121
                            foreach (DataRow row in rows)
122
                            {
123
                                row["EXIST"] = true;
124
                                row["DRAWINGNAME"] = item.Attributes["Name"].get_Value();
125
                                row["SPPIDPATH"] = item.get_Path();
126
                            }
127
                        }
128

    
129
                        List<SPPID_Document> allDocuments = new List<SPPID_Document>();
130
                        foreach (DataRow row in drawingTable.Rows)
131
                            allDocuments.Add((SPPID_Document)row["DOCUMENT"]);
132

    
133
                        AutoModeling_OPC opc = new AutoModeling_OPC(allDocuments, application, radApp);
134
                        opc.Run();
135
                    }
136
                }
137
                else
138
                {
139
                    MessageBox.Show(Msg.ConnectionFail, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
140
                }
141
            }
142
            //catch (Exception ex)
143
            {
144
                //MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
145
            }
146
            //finally
147
            {
148
                ReleaseCOMObjects(dataSource);
149
                ReleaseCOMObjects(drawings);
150
            }
151
        }
152

    
153
        public void ReleaseCOMObjects(params object[] objVars)
154
        {
155
            int intNewRefCount = 0;
156
            foreach (object obj in objVars)
157
            {
158
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
159
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
160
            }
161
        }
162

    
163
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
164
        {
165
            if (radApp.ActiveSelectSet.Count > 0)
166
            {
167
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
168
                if (line2D != null)
169
                {
170
                    double minX = 0;
171
                    double minY = 0;
172
                    double maxX = 0;
173
                    double maxY = 0;
174
                    line2D.Range(out minX, out minY, out maxX, out maxY);
175

    
176
                    Settings.Default.DrawingX = maxX - minX;
177
                    Settings.Default.DrawingY = maxY - minY;
178
                    Settings.Default.Save();
179

    
180
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
181
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
182
                }
183
                else
184
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
185
            }
186
            else
187
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
188
        }
189
        bool first = true;
190
        LMDrawing currentDrawing;
191
        private void simpleButton1_Click(object sender, EventArgs e)
192
        {
193
            Placement _placement = new Placement();
194
            LMADataSource dataSource = _placement.PIDDataSource;//placement.PIDDataSource;
195
            if (first)
196
            {
197
                LMAFilter filter = new LMAFilter();
198
                LMACriterion criterion = new LMACriterion();
199
                filter.ItemType = "Drawing";
200
                criterion.SourceAttributeName = "Name";
201
                criterion.Operator = "=";
202
                criterion.set_ValueAttribute("11111");
203
                filter.get_Criteria().Add(criterion);
204

    
205
                LMDrawings drawings = new LMDrawings();
206
                drawings.Collect(dataSource, Filter: filter);
207

    
208
                currentDrawing = ((dynamic)drawings).Nth(1);
209

    
210

    
211
                LMAFilter filter1 = new LMAFilter();
212
                LMACriterion criterion1 = new LMACriterion();
213
                filter1.ItemType = "Relationship";
214
                criterion1.SourceAttributeName = "SP_DRAWINGID";
215
                criterion1.Operator = "=";
216
                criterion1.set_ValueAttribute(currentDrawing.Id);
217
                filter1.get_Criteria().Add(criterion1);
218

    
219
                LMRelationships relationships = new LMRelationships();
220
                relationships.Collect(dataSource, Filter: filter1);
221
                int cc = relationships.Count;
222
            }
223
            else
224
            {
225
                //current LMDrawing 가져오기
226
                LMAFilter filter = new LMAFilter();
227
                LMACriterion criterion = new LMACriterion();
228
                filter.ItemType = "Relationship";
229
                criterion.SourceAttributeName = "SP_DRAWINGID";
230
                criterion.Operator = "=";
231
                criterion.set_ValueAttribute(currentDrawing.Id);
232
                filter.get_Criteria().Add(criterion);
233

    
234
                LMRelationships relationships = new LMRelationships();
235
                relationships.Collect(dataSource, Filter: filter);
236

    
237
                foreach (LMRelationship relationship in relationships)
238
                {
239
                    foreach (LMInconsistency inconsistency in relationship.Inconsistencies)
240
                    {
241
                        if (inconsistency.get_InconsistencyTypeIndex() == 1)
242
                        {
243
                            LMModelItem modelItem1 = relationship.Item1RepresentationObject == null ? null : relationship.Item1RepresentationObject.ModelItemObject;
244
                            LMModelItem modelItem2 = relationship.Item2RepresentationObject == null ? null : relationship.Item2RepresentationObject.ModelItemObject;
245
                            string[] array = inconsistency.get_Name().ToString().Split(new char[] { '=' });
246
                            if (modelItem1 != null)
247
                            {
248
                                string attrName = array[0];
249
                                if (attrName.Contains("PipingPoint"))
250
                                {
251
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
252
                                    int index = 1;
253
                                    while (modelItem1.Attributes["PipingPoint" + index + "." + originalAttr] != null)
254
                                    {
255
                                        LMAAttribute attribute1 = modelItem1.Attributes["PipingPoint" + index + "." + originalAttr];
256
                                        if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
257
                                        {
258
                                            attribute1.set_Value(DBNull.Value);
259
                                        }
260
                                        modelItem1.Commit();
261
                                        index++;
262
                                    }
263
                                }
264
                                else
265
                                {
266
                                    LMAAttribute attribute1 = modelItem1.Attributes[attrName];
267
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
268
                                    {
269
                                        attribute1.set_Value(DBNull.Value);
270
                                    }
271
                                    modelItem1.Commit();
272
                                }
273
                            }
274
                            if (modelItem2 != null)
275
                            {
276
                                string attrName = array[1];
277
                                if (attrName.Contains("PipingPoint"))
278
                                {
279
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
280
                                    int index = 1;
281
                                    while (modelItem2.Attributes["PipingPoint" + index + "." + originalAttr] != null)
282
                                    {
283
                                        LMAAttribute attribute2 = modelItem2.Attributes["PipingPoint" + index + "." + originalAttr];
284
                                        if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
285
                                        {
286
                                            attribute2.set_Value(DBNull.Value);
287
                                        }
288
                                        modelItem2.Commit();
289
                                        index++;
290
                                    }
291
                                }
292
                                else
293
                                {
294
                                    LMAAttribute attribute2 = modelItem2.Attributes[attrName];
295
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
296
                                    {
297
                                        attribute2.set_Value(DBNull.Value);
298
                                    }
299
                                    modelItem2.Commit();
300
                                }
301
                            }
302
                            if (modelItem1 != null)
303
                                ReleaseCOMObjects(modelItem1);
304
                            if (modelItem2 != null)
305
                                ReleaseCOMObjects(modelItem2);
306
                            inconsistency.Commit();
307
                        }
308
                        else if (inconsistency.get_InconsistencyTypeIndex() == 3)
309
                        {
310
                            
311
                        }
312
                    }
313
                    relationship.Commit();
314
                }
315
            }
316
            first = false;
317

    
318
            LMModelItem modelItem = dataSource.GetModelItem("FDC835123FF941489A5327FE364E3F03");
319
            if (modelItem != null)
320
            {
321
                string attrValue = modelItem.Attributes["FlowDirection"].get_Value().ToString();
322
                foreach (LMRepresentation rep in modelItem.Representations)
323
                {
324
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
325
                    {
326
                        LMConnector connector = dataSource.GetConnector(rep.Id);
327
                        foreach (LMRelationship relationship in connector.Relation1Relationships)
328
                        {
329
                            // Item2가 Symbol
330
                            if (relationship.Item1RepresentationID != null && relationship.Item1RepresentationID == connector.Id &&
331
                                relationship.Item2RepresentationObject.get_RepresentationType() == "Symbol")
332
                            {
333
                                int symbolIndex = Convert.ToInt32(relationship.get_Item2Location());
334
                                int lineIndex = Convert.ToInt32(relationship.get_Item1Location());
335
                                LMModelItem symbolModelItem = relationship.Item2RepresentationObject.ModelItemObject;
336

    
337
                                string symbolAttr = "PipingPoint" + symbolIndex + ".FlowDirection";
338
                                if (lineIndex == 0 && attrValue == "End 1 is upstream (Inlet)")
339
                                {
340
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
341
                                }
342
                                else if (lineIndex == 0 && attrValue == "End 1 is downstream (Outlet)")
343
                                {
344
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
345
                                }
346
                                else if (lineIndex == 1 && attrValue == "End 1 is upstream (Inlet)")
347
                                {
348
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
349
                                }
350
                                else if (lineIndex == 1 && attrValue == "End 1 is downstream (Outlet)")
351
                                {
352
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
353
                                }
354

    
355
                                symbolModelItem.Commit();
356
                                ReleaseCOMObjects(symbolModelItem);
357
                            }
358
                            // Item1이 Symbol
359
                            else if (relationship.Item2RepresentationID != null && relationship.Item2RepresentationID == connector.Id &&
360
                                    relationship.Item1RepresentationObject.get_RepresentationType() == "Symbol")
361
                            {
362
                                int symbolIndex = Convert.ToInt32(relationship.get_Item1Location());
363
                                int lineIndex = Convert.ToInt32(relationship.get_Item2Location());
364
                                LMModelItem symbolModelItem = relationship.Item1RepresentationObject.ModelItemObject;
365

    
366
                                string symbolAttr = "PipingPoint" + symbolIndex + ".FlowDirection";
367
                                if (lineIndex == 0 && attrValue == "End 1 is upstream (Inlet)")
368
                                {
369
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
370
                                }
371
                                else if (lineIndex == 0 && attrValue == "End 1 is downstream (Outlet)")
372
                                {
373
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
374
                                }
375
                                else if (lineIndex == 1 && attrValue == "End 1 is upstream (Inlet)")
376
                                {
377
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
378
                                }
379
                                else if (lineIndex == 1 && attrValue == "End 1 is downstream (Outlet)")
380
                                {
381
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
382
                                }
383

    
384
                                symbolModelItem.Commit();
385
                                ReleaseCOMObjects(symbolModelItem);
386
                            }
387
                        }
388

    
389
                        foreach (LMRelationship relationship in connector.Relation2Relationships)
390
                        {
391
                            // Item2가 Symbol
392
                            if (relationship.Item1RepresentationID != null && relationship.Item1RepresentationID == connector.Id &&
393
                                relationship.Item2RepresentationObject.get_RepresentationType() == "Symbol")
394
                            {
395
                                int symbolIndex = Convert.ToInt32(relationship.get_Item2Location());
396
                                int lineIndex = Convert.ToInt32(relationship.get_Item1Location());
397
                                LMModelItem symbolModelItem = relationship.Item2RepresentationObject.ModelItemObject;
398

    
399
                                string symbolAttr = "PipingPoint" + symbolIndex + ".FlowDirection";
400
                                if (lineIndex == 0 && attrValue == "End 1 is upstream (Inlet)")
401
                                {
402
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
403
                                }
404
                                else if (lineIndex == 0 && attrValue == "End 1 is downstream (Outlet)")
405
                                {
406
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
407
                                }
408
                                else if (lineIndex == 1 && attrValue == "End 1 is upstream (Inlet)")
409
                                {
410
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
411
                                }
412
                                else if (lineIndex == 1 && attrValue == "End 1 is downstream (Outlet)")
413
                                {
414
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
415
                                }
416

    
417
                                symbolModelItem.Commit();
418
                                ReleaseCOMObjects(symbolModelItem);
419
                            }
420
                            // Item1이 Symbol
421
                            else if (relationship.Item2RepresentationID != null && relationship.Item2RepresentationID == connector.Id &&
422
                                    relationship.Item1RepresentationObject.get_RepresentationType() == "Symbol")
423
                            {
424
                                int symbolIndex = Convert.ToInt32(relationship.get_Item1Location());
425
                                int lineIndex = Convert.ToInt32(relationship.get_Item2Location());
426
                                LMModelItem symbolModelItem = relationship.Item1RepresentationObject.ModelItemObject;
427

    
428
                                string symbolAttr = "PipingPoint" + symbolIndex + ".FlowDirection";
429
                                if (lineIndex == 0 && attrValue == "End 1 is upstream (Inlet)")
430
                                {
431
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
432
                                }
433
                                else if (lineIndex == 0 && attrValue == "End 1 is downstream (Outlet)")
434
                                {
435
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
436
                                }
437
                                else if (lineIndex == 1 && attrValue == "End 1 is upstream (Inlet)")
438
                                {
439
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is upstream (Inlet)");
440
                                }
441
                                else if (lineIndex == 1 && attrValue == "End 1 is downstream (Outlet)")
442
                                {
443
                                    symbolModelItem.Attributes[symbolAttr].set_Value("End 1 is downstream (Outlet)");
444
                                }
445

    
446
                                symbolModelItem.Commit();
447
                                ReleaseCOMObjects(symbolModelItem);
448
                            }
449
                        }
450

    
451
                        ReleaseCOMObjects(connector);
452
                    }
453

    
454
                }
455
            }
456

    
457
            //foreach (LMRelationship relationship in currentDrawing.Relationships)
458
            //{
459
            //    foreach (LMInconsistency inconsistency in relationship.Inconsistencies)
460
            //    {
461
            //        if (inconsistency.get_InconsistencyTypeIndex() == 1)
462
            //        {
463
            //            LMAAttribute attribute1 = ((dynamic)relationship.Item1RepresentationObject).ModelItemObject.Attributes[inconsistency.get_PropNameItem1()];
464
            //            LMAAttribute attribute2 = ((dynamic)relationship.Item2RepresentationObject).ModelItemObject.Attributes[inconsistency.get_PropNameItem2()];
465
            //            if (!DBNull.Value.Equals(attribute1.get_Value()))
466
            //                attribute1.set_Value(DBNull.Value);
467
            //            if (!DBNull.Value.Equals(attribute2.get_Value()))
468
            //                attribute2.set_Value(DBNull.Value);
469
            //            inconsistency.Commit();
470
            //        }
471
            //    }
472
            //}
473

    
474
            //_LMAItem item = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
475
            //PlaceRunInputs placeRunInputs = new PlaceRunInputs();
476
            //placeRunInputs.AddPoint(0, 0);
477
            //placeRunInputs.AddPoint(0.01, 0);
478
            //placeRunInputs.AddPoint(0.01, -0.01);
479
            //LMConnector cc = _placement.PIDPlaceRun(item, placeRunInputs);
480

    
481
            //_LMAItem item2 = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
482
            //PlaceRunInputs placeRunInputs2 = new PlaceRunInputs();
483
            //placeRunInputs2.AddPoint(0.02, 0);
484
            //placeRunInputs2.AddConnectorTarget(cc, 0.01, 0);
485
            //LMConnector cc2 =_placement.PIDPlaceRun(item2, placeRunInputs2);
486

    
487
            //_LMAItem item3 = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
488
            //PlaceRunInputs placeRunInputs3 = new PlaceRunInputs();
489
            //placeRunInputs3.AddPoint(0.01, 0.01);
490
            //if (cc2.ConnectItem1SymbolObject != null)
491
            //{
492
            //    placeRunInputs3.AddSymbolTarget(cc2.ConnectItem1SymbolObject, 0.01, 0);
493
            //}
494
            //else if (cc2.ConnectItem2SymbolObject != null)
495
            //{
496
            //    placeRunInputs3.AddSymbolTarget(cc2.ConnectItem2SymbolObject, 0.01, 0);
497
            //}
498

    
499
            //_placement.PIDPlaceRun(item3, placeRunInputs3);
500

    
501
        }
502

    
503
        [DllImport("user32.dll")]
504
        public static extern int FindWindow(string lpClassName, string lpWindowName);
505

    
506
        [DllImport("user32.dll", SetLastError = true)]
507
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
508

    
509
    }
510
}
클립보드 이미지 추가 (최대 크기: 500 MB)