프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter_CustomCommand / ConverterForm.cs @ 1cad80c1

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

1
/*---------------------------------------------------------------------+\
2
|                                                                       |
3
|        Copyright 2016 Intergraph Corporation                          |
4
|        All Rights Reserved                                            |
5
|                                                                       |
6
|        Including software, file formats, and audio-visual displays;   |
7
|        may only be used pursuant to applicable software license       |
8
|        agreement; contains confidential and proprietary information of|
9
|        Intergraph and/or third parties which is protected by copyright|
10
|        and trade secret law and may not be provided or otherwise made |
11
|        available without proper authorization.                        |
12
|                                                                       |
13
|        Unpublished -- rights reserved under the Copyright Laws of the |
14
|        United States.                                                 |
15
|                                                                       |
16
|        Intergraph Corporation                                         |
17
|        Huntsville, Alabama         35894-0001                         |
18
|                                                                       |
19
\+---------------------------------------------------------------------*/
20

    
21
using System;
22
using System.Collections.Generic;
23
using System.Windows.Forms;
24
using Ingr.RAD2D;
25
using System.Runtime.InteropServices;
26
using System.Drawing;
27
using SPPID.Modeling;
28
using SPPID.Utill;
29
using SPPID.Model;
30
using System.Threading;
31
using Microsoft.Win32;
32

    
33
namespace CustomCommand
34
{
35
    /// <summary>
36
    /// This is the primary form.  The form is shown as modal.
37
    /// This command may be built as an executable (EXE) or as a dynamic link 
38
    /// library (DLL).
39
    /// 
40
    /// When you implement the code that dismisses your form, e.g., an OK
41
    /// button or a Stop Processing button, you should include the following:
42
    /// 
43
    ///     Hide the form, for example
44
    ///         converterForm.Hide
45
    ///     
46
    ///     Set the Application's Interactive property to True, for example
47
    ///         commandControl.Application.Interactive = True
48
    ///
49
    ///     Set Intergraph Command Control Done property to True, for example
50
    ///         commandControl.Done = True
51
    /// </summary>
52
    public partial class ConverterForm : Form
53
    {
54
        #region Default CustomCommand
55
        readonly string menuControlName = "ID2 to SPPID Converter";
56
        readonly string toolTipText = "ID2 to SPPID Converter";
57
        readonly Image image = (Image)Properties.Resources.ResourceManager.GetObject("ToolbarImage");
58
        /// <summary>
59
        /// Constructs and initializes the form.
60
        /// </summary>
61
        public ConverterForm()
62
        {
63
            InitializeComponent();
64
        }
65
        
66
        /// <summary>
67
        /// The OK Click Event is used to dismiss the form.
68
        /// </summary>
69
        /// <param name="sender">The sender of the event</param>
70
        /// <param name="e">Arguments passed during event</param>
71
        //private void cmdOK_Click(object sender, EventArgs e)
72
        //{
73
        //    commandControl.Application.RADApplication.Interactive = true;
74
        //    Hide();
75
        //    commandControl.Done = true;
76
        //}
77

    
78
        /// <summary>
79
        /// The Activate Event is where the command should show its form
80
        /// if it should be displayed.
81
        /// </summary>
82
        /// <param name="sender">The sender of the event</param>
83
        /// <param name="e">Arguments passed during event</param>
84
        private void commandControl_Activate(object sender, EventArgs e)
85
        {
86
            ShowDialog();
87
        }
88

    
89
        /// <summary>
90
        /// The Deactivate event is where the command should hide its form if it
91
        /// was displayed.  The command should not unload the form here.
92
        /// The command's form should be unloaded in the Class Module Terminate event.
93
        /// </summary>
94
        /// <param name="sender">The sender of the event</param>
95
        /// <param name="e">Arguments passed during event</param>
96
        private void commandControl_Deactivate(object sender, EventArgs e)
97
        {
98
            Hide();
99
        }
100

    
101
        /// <summary>
102
        /// The Initialize event is where the command should perform 1 time 
103
        /// initialization, for example it might save a reference to the 
104
        /// active document in private global data.
105
        /// </summary>
106
        /// <param name="sender">The sender of the event</param>
107
        /// <param name="e">Arguments passed during event</param>
108
        private void commandControl_Initialize(object sender, EventArgs e)
109
        {
110
            try
111
            {
112
                RegistryKey key = Registry.LocalMachine;
113
                RegistryKey software = key.OpenSubKey("SOFTWARE");
114
                RegistryKey DOFTECH = software.OpenSubKey("DOFTECH");
115
                if (DOFTECH != null)
116
                {
117
                    RegistryKey ID2 = DOFTECH.OpenSubKey("ID2");
118
                    if (ID2 != null)
119
                    {
120
                        SPPIDUtill.defaultPath = ID2.GetValue("Path").ToString();
121

    
122
                        Log.logPath = SPPIDUtill.defaultPath + @"Converter\SPPID Converter.log";
123
                        SPPIDUtill.mappingFilePath = SPPIDUtill.defaultPath + @"Converter\mapping.xml";
124
                        string dllPath = SPPIDUtill.defaultPath + @"Converter\SPPIDConverter_CustomCommand.dll";
125

    
126
                        SPPIDUtill.LoadMapping(mapping);
127
                        Ingr.RAD2D.Application application = commandControl.Application.RADApplication;
128

    
129
                        ToolBars toolBars = application.ToolBars;
130
                        bool find = true;
131
                        while (find)
132
                        {
133
                            find = false;
134
                            foreach (ToolbarControl item in toolBars["Main"].Controls)
135
                            {
136
                                if (item.DLLName.Contains("SPPIDConverter_CustomCommand.dll"))
137
                                {
138
                                    item.Delete();
139
                                    find = true;
140
                                    break;
141
                                }
142
                            }
143
                        }
144

    
145
                        ToolbarControl toolBarControl = toolBars["Main"].Controls.AppendMacroCommand(dllPath);
146
                        toolBarControl.ToolTipText = toolTipText;
147
                        //toolBarControl.Picture = image;
148

    
149
                        Menus menu = application.Menus;
150
                        find = true;
151
                        while (find)
152
                        {
153
                            find = false;
154
                            foreach (MenuControl item in menu["&Tools"].Controls)
155
                            {
156
                                if (item.Caption == menuControlName)
157
                                {
158
                                    item.Delete();
159
                                    find = true;
160
                                    break;
161
                                }
162
                            }
163
                        }
164
                        MenuControl menuControl = application.Menus["&Tools"].Controls.AppendMacroCommand(menuControlName, dllPath);
165
                        menuControl.ToolTipText = toolTipText;
166
                    }
167
                }
168
            }
169
            catch (Exception ex)
170
            {
171
                Log.WriteLine(ex);
172
            }
173
        }
174

    
175
        /// <summary>
176
        /// The Terminate event is where the command can clean up any command
177
        /// specific allocated resources.
178
        /// </summary>
179
        /// <param name="sender">The sender of the event</param>
180
        /// <param name="e">Arguments passed during event</param>
181
        public void commandControl_Terminate(object sender, EventArgs e)
182
        {
183
           
184
        }
185

    
186
        /// <summary>
187
        /// The primary form should not simply be unloaded. You should set the
188
        /// Intergraph Command Control Done property to True when you want the 
189
        /// form to be unloaded. Then unload the form in the dispose method.
190
        /// </summary>
191
        /// <param name="sender">The sender of the event</param>
192
        /// <param name="e">Arguments passed during event</param>
193
        private void ConverterForm_FormClosing(object sender, FormClosingEventArgs e)
194
        {
195
            commandControl.Application.RADApplication.Interactive = true; // Add by kyouho
196
            Hide();
197
            commandControl.Done = true;
198
            e.Cancel = true; //Do not let C# close out the form ... Let RAD close it.
199
        }
200
        #endregion
201

    
202
        #region ?? CustomCommand Source ????
203

    
204
        private Dictionary<string, string> mapping = new Dictionary<string, string>();
205
        Thread autoModelingThread;
206

    
207
        #region Botton Event
208
        private void openDocumentsToolStripMenuItem_Click(object sender, EventArgs e)
209
        {
210
            OpenFileDialog dia = new OpenFileDialog();
211
            dia.Multiselect = true;
212
            dia.Filter = "Xml Files(*.xml)|*.xml";
213
            if (dia.ShowDialog() == DialogResult.OK)
214
            {
215
                SPPIDUtill.LoadMapping(mapping);
216
                foreach (string fileName in dia.FileNames)
217
                    SetDrawingTreeNode(fileName);
218
                treeViewDrawing.Nodes[0].Expand();
219
                RefreshItemNode();
220
            }
221
        }
222

    
223
        private void itemMappingToolStripMenuItem_Click(object sender, EventArgs e)
224
        {
225
            List<string> needName = GetNeedMapping();
226
            MappingForm form = new MappingForm(mapping, needName);
227
            if (form.ShowDialog() == DialogResult.OK)
228
            {
229
                SPPIDUtill.SaveMapping(mapping);
230
                RefreshItemNode();
231
            }
232
        }
233

    
234
        private void importToolStripMenuItem_Click(object sender, EventArgs e)
235
        {
236
            OpenFileDialog dia = new OpenFileDialog();
237
            dia.Multiselect = false;
238
            dia.Filter = "Excel File(*.xlsx;*.xls)|*.xlsx;*.xls";
239
            if (dia.ShowDialog() == DialogResult.OK)
240
            {
241
                ExcelUtill.LoadMappingExcelFile(dia.FileName, mapping);
242
                try
243
                {
244
                    SPPIDUtill.SaveMapping(mapping);
245
                    RefreshItemNode();
246
                    MessageBox.Show("Success Load Excel File");
247
                }
248
                catch (Exception ex)
249
                {
250
                    Log.WriteLine(ex);
251
                    MessageBox.Show("Fail Save Mapping File");
252
                }
253
            }
254
        }
255

    
256
        private void convertToolStripMenuItem_Click(object sender, EventArgs e)
257
        {
258
            Ingr.RAD2D.Application application = commandControl.Application.RADApplication;
259

    
260
            SPPIDUtill.LoadMapping(mapping);
261

    
262
            List<SPPID.Model.Document> documents = new List<SPPID.Model.Document>();
263
            foreach (TreeNode node in treeViewDrawing.Nodes[0].Nodes)
264
            {
265
                if (node.Checked)
266
                {
267
                    SPPID.Model.Document document = node.Tag as SPPID.Model.Document;
268
                    if (document != null && document.Init())
269
                        documents.Add(document);
270
                }
271
            }
272

    
273
            //Ingr.RAD2D.Document SPPID_Doc = application.Documents.Add();
274
            //SPPID_Doc.SaveOnClose = false;
275
            //SPPID_Doc.SaveAs(@"C:\dasd.pid");
276
            //SPPID_Doc.Close(false);
277

    
278

    
279
            // Save ??? ????
280
            string folderPath = "";
281
            if (documents.Count > 0)
282
            {
283
                FolderBrowserDialog dia = new FolderBrowserDialog();
284
                dia.ShowNewFolderButton = true;
285
                if (dia.ShowDialog() == DialogResult.OK)
286
                    folderPath = dia.SelectedPath + @"\";
287
                else
288
                    return;
289
            }
290

    
291
            autoModelingThread = new Thread(Func =>
292
            {
293
                try
294
                {
295
                    foreach (SPPID.Model.Document document in documents)
296
                    {
297
                        Ingr.RAD2D.Document SPPID_Doc = application.Documents.Add();
298
                        SPPID_Doc.SaveOnClose = false;
299

    
300
                        AutoModeling auto = new AutoModeling(document);
301
                        auto.SetListBoxLog(listBoxLog);
302
                        auto.SetProgressBar(toolStripProgressBar);
303
                        auto.Run();
304

    
305
                        SPPID_Doc.SaveAs(@"C:\" + document.DWGNAME);
306
                        SPPID_Doc.Close(false);
307
                    }
308
                }
309
                catch (Exception ex)
310
                {
311
                    Log.WriteLine(ex);
312
                }
313
            });
314
            autoModelingThread.Start();
315
        }
316

    
317
        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
318
        {
319
            try
320
            {
321
                if (autoModelingThread != null)
322
                    autoModelingThread.Abort();
323
            }
324
            catch (Exception ex)
325
            {
326
                Log.WriteLine(ex);
327
            }
328
        }
329
        #endregion
330

    
331
        private void listBoxLog_DrawItem(object sender, DrawItemEventArgs e)
332
        {
333
            if (e.Index >= 0)
334
            {
335
                ListBoxItem item = listBoxLog.Items[e.Index] as ListBoxItem;
336
                if (item != null)
337
                {
338
                    e.Graphics.DrawString(item.Message,
339
                        listBoxLog.Font,
340
                        new SolidBrush(item.Color),
341
                        0,
342
                        e.Index * listBoxLog.ItemHeight);
343
                }
344
            }
345
        }
346

    
347
        #region TreeNode
348

    
349
        #region Hide TreeNode CheckBox
350
        private const int TVIF_STATE = 0x8;
351
        private const int TVIS_STATEIMAGEMASK = 0xF000;
352
        private const int TV_FIRST = 0x1100;
353
        private const int TVM_SETITEM = TV_FIRST + 63;
354

    
355
        [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
356
        private struct TVITEM
357
        {
358
            public int mask;
359
            public IntPtr hItem;
360
            public int state;
361
            public int stateMask;
362
            [MarshalAs(UnmanagedType.LPTStr)]
363
            public string lpszText;
364
            public int cchTextMax;
365
            public int iImage;
366
            public int iSelectedImage;
367
            public int cChildren;
368
            public IntPtr lParam;
369
        }
370

    
371
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
372
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam,
373
                                                 ref TVITEM lParam);
374

    
375
        /// <summary>
376
        /// Hides the checkbox for the specified node on a TreeView control.
377
        /// </summary>
378
        private void HideCheckBox(TreeView tvw, TreeNode node)
379
        {
380
            TVITEM tvi = new TVITEM();
381
            tvi.hItem = node.Handle;
382
            tvi.mask = TVIF_STATE;
383
            tvi.stateMask = TVIS_STATEIMAGEMASK;
384
            tvi.state = 0;
385
            SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
386
        }
387
        #endregion
388

    
389
        private void SetDrawingTreeNode(string fileName)
390
        {
391
            SPPID.Model.Document document = SPPID.Model.Document.Load(fileName, mapping);
392

    
393
            TreeNode drawingNode = new TreeNode();
394
            if (document == null)
395
            {
396
                drawingNode.Text = fileName;
397
                drawingNode.Name = fileName;
398
                drawingNode.ForeColor = Color.Red;
399
                treeViewDrawing.Nodes[0].Nodes.Add(drawingNode);
400
            }
401
            else
402
            {
403
                foreach (TreeNode treeNode in treeViewDrawing.Nodes[0].Nodes)
404
                {
405
                    if (treeNode.Text == document.DWGNAME)
406
                    {
407
                        treeViewDrawing.Nodes[0].Nodes.Remove(treeNode);
408
                        break;
409
                    }
410
                }
411

    
412
                drawingNode.Text = document.DWGNAME;
413
                drawingNode.Name = document.DWGNAME;
414
                drawingNode.Tag = document;
415
                treeViewDrawing.Nodes[0].Nodes.Add(drawingNode);
416
            }
417

    
418
            SetItemTreeNode(drawingNode);
419
        }
420

    
421
        private void SetItemTreeNode(TreeNode drawingNode)
422
        {
423
            SPPID.Model.Document document = drawingNode.Tag as SPPID.Model.Document;
424
            if (document != null)
425
            {
426
                foreach (LineNumber item in document.LINENUMBERS)
427
                {
428
                    TreeNode node = new TreeNode();
429
                    node.Name = LineNumber.LineNumberSymbolPath;
430
                    node.Text = item.TEXT;
431
                    node.Tag = item;
432
                    drawingNode.Nodes.Add(node);
433
                    HideCheckBox(treeViewDrawing, node);
434
                    foreach (ItemAttribute attribute in item.ATTRIBUTES)
435
                    {
436
                        TreeNode attrNode = new TreeNode();
437
                        attrNode.Name = attribute.Attribute;
438
                        attrNode.Text = attribute.Attribute;
439
                        attrNode.Tag = attribute;
440
                        node.Nodes.Add(attrNode);
441
                        HideCheckBox(treeViewDrawing, attrNode);
442
                    }
443
                }
444

    
445
                foreach (Symbol item in document.SYMBOLS)
446
                {
447
                    TreeNode node = new TreeNode();
448
                    node.Name = item.PARENT;
449
                    node.Text = item.NAME;
450
                    node.Tag = item;
451
                    drawingNode.Nodes.Add(node);
452
                    HideCheckBox(treeViewDrawing, node);
453
                    foreach (ItemAttribute attribute in item.ATTRIBUTES)
454
                    {
455
                        TreeNode attrNode = new TreeNode();
456
                        attrNode.Name = attribute.Attribute;
457
                        attrNode.Text = attribute.Attribute;
458
                        attrNode.Tag = attribute;
459
                        node.Nodes.Add(attrNode);
460
                        HideCheckBox(treeViewDrawing, attrNode);
461
                    }
462
                }
463

    
464
                foreach (Line item in document.LINES)
465
                {
466
                    TreeNode node = new TreeNode();
467
                    node.Name = item.TYPE;
468
                    node.Text = item.TYPE;
469
                    node.Tag = item;
470
                    drawingNode.Nodes.Add(node);
471
                    HideCheckBox(treeViewDrawing, node);
472
                    foreach (ItemAttribute attribute in item.ATTRIBUTES)
473
                    {
474
                        TreeNode attrNode = new TreeNode();
475
                        attrNode.Name = attribute.Attribute;
476
                        attrNode.Text = attribute.Attribute;
477
                        attrNode.Tag = attribute;
478
                        node.Nodes.Add(attrNode);
479
                        HideCheckBox(treeViewDrawing, attrNode);
480
                    }
481
                }
482

    
483
                foreach (Text item in document.TEXTS)
484
                {
485
                    TreeNode node = new TreeNode();
486
                    node.Name = item.NAME;
487
                    node.Text = item.NAME;
488
                    node.Tag = item;
489
                    drawingNode.Nodes.Add(node);
490
                    HideCheckBox(treeViewDrawing, node);
491
                    foreach (ItemAttribute attribute in item.ATTRIBUTES)
492
                    {
493
                        TreeNode attrNode = new TreeNode();
494
                        attrNode.Name = attribute.Attribute;
495
                        attrNode.Text = attribute.Attribute;
496
                        attrNode.Tag = attribute;
497
                        node.Nodes.Add(attrNode);
498
                        HideCheckBox(treeViewDrawing, attrNode);
499
                    }
500
                }
501
            }
502
        }
503

    
504
        private void RefreshItemNode()
505
        {
506
            foreach (TreeNode docNode in treeViewDrawing.Nodes[0].Nodes)
507
            {
508
                docNode.ForeColor = Color.Red;
509
                bool result = true;
510
                foreach (TreeNode itemNode in docNode.Nodes)
511
                {
512
                    string name = itemNode.Name;
513
                    if (name == "SIZE")
514
                    {
515
                        string sppidName = SPPIDUtill.GetSPPIDMappingName(SPPID.Model.Text.pipingCompSize, mapping);
516
                        if (!string.IsNullOrEmpty(sppidName))
517
                        {
518
                            itemNode.ForeColor = Color.Gray;
519
                        }
520
                        else
521
                        {
522
                            itemNode.ForeColor = Color.Red;
523
                            result = false;
524
                        }
525
                        sppidName = SPPIDUtill.GetSPPIDMappingName(SPPID.Model.Text.instrumentSize, mapping);
526
                        if (!string.IsNullOrEmpty(sppidName))
527
                        {
528
                            itemNode.ForeColor = Color.Gray;
529
                        }
530
                        else
531
                        {
532
                            itemNode.ForeColor = Color.Red;
533
                            result = false;
534
                        }
535
                    }
536
                    else
537
                    {
538
                        string sppidName = SPPIDUtill.GetSPPIDMappingName(name, mapping);
539
                        if (!string.IsNullOrEmpty(sppidName))
540
                        {
541
                            itemNode.ForeColor = Color.Gray;
542
                        }
543
                        else
544
                        {
545
                            itemNode.ForeColor = Color.Red;
546
                            result = false;
547
                        }
548
                    }
549

    
550
                    foreach (TreeNode attrNode in itemNode.Nodes)
551
                    {
552
                        name = attrNode.Name;
553
                        string sppidName = SPPIDUtill.GetSPPIDMappingName(name, mapping);
554
                        if (!string.IsNullOrEmpty(sppidName))
555
                        {
556
                            attrNode.ForeColor = Color.Gray;
557
                        }
558
                        else
559
                        {
560
                            attrNode.ForeColor = Color.Red;
561
                        }
562
                    }
563
                }
564

    
565
                if (result)
566
                {
567
                    docNode.ForeColor = Color.Black;
568
                }
569
            }
570
        }
571

    
572
        private List<string> GetNeedMapping()
573
        {
574
            List<string> nameList = new List<string>();
575
            foreach (TreeNode dwgNode in treeViewDrawing.Nodes[0].Nodes)
576
            {
577
                foreach (TreeNode docNode in dwgNode.Nodes)
578
                {
579
                    string name = docNode.Name;
580
                    if (name == "SIZE")
581
                    {
582
                        string sppidName = SPPIDUtill.GetSPPIDMappingName(SPPID.Model.Text.pipingCompSize, mapping);
583
                        if (string.IsNullOrEmpty(sppidName) && !nameList.Contains(name))
584
                            nameList.Add(SPPID.Model.Text.pipingCompSize);
585
                        sppidName = SPPIDUtill.GetSPPIDMappingName(SPPID.Model.Text.instrumentSize, mapping);
586
                        if (string.IsNullOrEmpty(sppidName) && !nameList.Contains(name))
587
                            nameList.Add(SPPID.Model.Text.instrumentSize);
588

    
589
                    }
590
                    else
591
                    {
592
                        string sppidName = SPPIDUtill.GetSPPIDMappingName(name, mapping);
593
                        if (string.IsNullOrEmpty(sppidName) && !nameList.Contains(name))
594
                            nameList.Add(name);
595
                    }
596

    
597
                    // Attribute
598
                    foreach (TreeNode attrNode in docNode.Nodes)
599
                    {
600
                        name = attrNode.Name;
601
                        string sppidName = SPPIDUtill.GetSPPIDMappingName(name, mapping);
602
                        if (string.IsNullOrEmpty(sppidName) && !nameList.Contains(name))
603
                            nameList.Add(name);
604
                    }
605
                }
606
            }
607

    
608
            return nameList;
609
        }
610

    
611
        private void treeViewDrawing_AfterCheck(object sender, TreeViewEventArgs e)
612
        {
613
            treeViewDrawing.AfterCheck -= new TreeViewEventHandler(this.treeViewDrawing_AfterCheck);
614

    
615
            if (e.Node.Level == 0)
616
            {
617
                foreach (TreeNode node in e.Node.Nodes)
618
                    node.Checked = e.Node.Checked;
619
            }
620
            else if (e.Node.Level == 1)
621
            {
622
                bool result = false;
623
                foreach (TreeNode node in e.Node.Parent.Nodes)
624
                {
625
                    if (node.Checked)
626
                    {
627
                        result = true;
628
                        break;
629
                    }
630
                }
631

    
632
                if (result)
633
                    e.Node.Parent.Checked = true;
634
                else
635
                    e.Node.Parent.Checked = false;
636
            }
637

    
638
            treeViewDrawing.AfterCheck += new TreeViewEventHandler(this.treeViewDrawing_AfterCheck);
639
        }
640
        #endregion
641

    
642
        #endregion
643

    
644

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