hytos / DTI_PID / SPPIDConverter_CustomCommand / ConverterForm.cs @ 4582b1d2
이력 | 보기 | 이력해설 | 다운로드 (21.8 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"); //Image.FromFile(@".\Close.png"); |
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 |
SPPIDUtill.LoadMapping(mapping); |
259 |
|
260 |
List<SPPID.Model.Document> documents = new List<SPPID.Model.Document>(); |
261 |
foreach (TreeNode node in treeViewDrawing.Nodes[0].Nodes) |
262 |
{ |
263 |
if (node.Checked) |
264 |
{ |
265 |
SPPID.Model.Document document = node.Tag as SPPID.Model.Document; |
266 |
if (document != null && document.Init()) |
267 |
documents.Add(document); |
268 |
} |
269 |
} |
270 |
|
271 |
autoModelingThread = new Thread(Func => |
272 |
{ |
273 |
try |
274 |
{ |
275 |
foreach (SPPID.Model.Document document in documents) |
276 |
{ |
277 |
AutoModeling auto = new AutoModeling(document); |
278 |
auto.SetListBoxLog(listBoxLog); |
279 |
auto.SetProgressBar(toolStripProgressBar); |
280 |
auto.Run(); |
281 |
break; |
282 |
} |
283 |
} |
284 |
catch (Exception ex) |
285 |
{ |
286 |
Log.WriteLine(ex); |
287 |
} |
288 |
}); |
289 |
autoModelingThread.Start(); |
290 |
} |
291 |
|
292 |
private void stopToolStripMenuItem_Click(object sender, EventArgs e) |
293 |
{ |
294 |
try |
295 |
{ |
296 |
if (autoModelingThread != null) |
297 |
autoModelingThread.Abort(); |
298 |
} |
299 |
catch (Exception ex) |
300 |
{ |
301 |
Log.WriteLine(ex); |
302 |
} |
303 |
} |
304 |
#endregion |
305 |
|
306 |
private void listBoxLog_DrawItem(object sender, DrawItemEventArgs e) |
307 |
{ |
308 |
if (e.Index >= 0) |
309 |
{ |
310 |
ListBoxItem item = listBoxLog.Items[e.Index] as ListBoxItem; |
311 |
if (item != null) |
312 |
{ |
313 |
e.Graphics.DrawString(item.Message, |
314 |
listBoxLog.Font, |
315 |
new SolidBrush(item.Color), |
316 |
0, |
317 |
e.Index * listBoxLog.ItemHeight); |
318 |
} |
319 |
} |
320 |
} |
321 |
|
322 |
#region TreeNode |
323 |
|
324 |
#region Hide TreeNode CheckBox |
325 |
private const int TVIF_STATE = 0x8; |
326 |
private const int TVIS_STATEIMAGEMASK = 0xF000; |
327 |
private const int TV_FIRST = 0x1100; |
328 |
private const int TVM_SETITEM = TV_FIRST + 63; |
329 |
|
330 |
[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)] |
331 |
private struct TVITEM |
332 |
{ |
333 |
public int mask; |
334 |
public IntPtr hItem; |
335 |
public int state; |
336 |
public int stateMask; |
337 |
[MarshalAs(UnmanagedType.LPTStr)] |
338 |
public string lpszText; |
339 |
public int cchTextMax; |
340 |
public int iImage; |
341 |
public int iSelectedImage; |
342 |
public int cChildren; |
343 |
public IntPtr lParam; |
344 |
} |
345 |
|
346 |
[DllImport("user32.dll", CharSet = CharSet.Auto)] |
347 |
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, |
348 |
ref TVITEM lParam); |
349 |
|
350 |
/// <summary> |
351 |
/// Hides the checkbox for the specified node on a TreeView control. |
352 |
/// </summary> |
353 |
private void HideCheckBox(TreeView tvw, TreeNode node) |
354 |
{ |
355 |
TVITEM tvi = new TVITEM(); |
356 |
tvi.hItem = node.Handle; |
357 |
tvi.mask = TVIF_STATE; |
358 |
tvi.stateMask = TVIS_STATEIMAGEMASK; |
359 |
tvi.state = 0; |
360 |
SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi); |
361 |
} |
362 |
#endregion |
363 |
|
364 |
private void SetDrawingTreeNode(string fileName) |
365 |
{ |
366 |
SPPID.Model.Document document = SPPID.Model.Document.Load(fileName, mapping); |
367 |
TreeNode drawingNode = new TreeNode(); |
368 |
if (document == null) |
369 |
{ |
370 |
drawingNode.Text = fileName; |
371 |
drawingNode.Name = fileName; |
372 |
drawingNode.ForeColor = Color.Red; |
373 |
treeViewDrawing.Nodes[0].Nodes.Add(drawingNode); |
374 |
} |
375 |
else |
376 |
{ |
377 |
drawingNode.Text = document.DWGNAME; |
378 |
drawingNode.Name = document.DWGNAME; |
379 |
drawingNode.Tag = document; |
380 |
treeViewDrawing.Nodes[0].Nodes.Add(drawingNode); |
381 |
} |
382 |
|
383 |
SetItemTreeNode(drawingNode); |
384 |
} |
385 |
|
386 |
private void SetItemTreeNode(TreeNode drawingNode) |
387 |
{ |
388 |
SPPID.Model.Document document = drawingNode.Tag as SPPID.Model.Document; |
389 |
if (document != null) |
390 |
{ |
391 |
foreach (LineNumber item in document.LINENUMBERS) |
392 |
{ |
393 |
TreeNode node = new TreeNode(); |
394 |
node.Name = item.NAME; |
395 |
node.Text = item.TEXT; |
396 |
node.Tag = item; |
397 |
drawingNode.Nodes.Add(node); |
398 |
HideCheckBox(treeViewDrawing, node); |
399 |
foreach (ItemAttribute attribute in item.ATTRIBUTES) |
400 |
{ |
401 |
TreeNode attrNode = new TreeNode(); |
402 |
attrNode.Name = attribute.Attribute; |
403 |
attrNode.Text = attribute.Attribute; |
404 |
attrNode.Tag = attribute; |
405 |
node.Nodes.Add(attrNode); |
406 |
HideCheckBox(treeViewDrawing, attrNode); |
407 |
} |
408 |
} |
409 |
|
410 |
foreach (Symbol item in document.SYMBOLS) |
411 |
{ |
412 |
TreeNode node = new TreeNode(); |
413 |
node.Name = item.PARENT; |
414 |
node.Text = item.NAME; |
415 |
node.Tag = item; |
416 |
drawingNode.Nodes.Add(node); |
417 |
HideCheckBox(treeViewDrawing, node); |
418 |
foreach (ItemAttribute attribute in item.ATTRIBUTES) |
419 |
{ |
420 |
TreeNode attrNode = new TreeNode(); |
421 |
attrNode.Name = attribute.Attribute; |
422 |
attrNode.Text = attribute.Attribute; |
423 |
attrNode.Tag = attribute; |
424 |
node.Nodes.Add(attrNode); |
425 |
HideCheckBox(treeViewDrawing, attrNode); |
426 |
} |
427 |
} |
428 |
|
429 |
foreach (Line item in document.LINES) |
430 |
{ |
431 |
TreeNode node = new TreeNode(); |
432 |
node.Name = item.TYPE; |
433 |
node.Text = item.TYPE; |
434 |
node.Tag = item; |
435 |
drawingNode.Nodes.Add(node); |
436 |
HideCheckBox(treeViewDrawing, node); |
437 |
foreach (ItemAttribute attribute in item.ATTRIBUTES) |
438 |
{ |
439 |
TreeNode attrNode = new TreeNode(); |
440 |
attrNode.Name = attribute.Attribute; |
441 |
attrNode.Text = attribute.Attribute; |
442 |
attrNode.Tag = attribute; |
443 |
node.Nodes.Add(attrNode); |
444 |
HideCheckBox(treeViewDrawing, attrNode); |
445 |
} |
446 |
} |
447 |
|
448 |
foreach (Text item in document.TEXTS) |
449 |
{ |
450 |
TreeNode node = new TreeNode(); |
451 |
node.Name = item.NAME; |
452 |
node.Text = item.NAME; |
453 |
node.Tag = item; |
454 |
drawingNode.Nodes.Add(node); |
455 |
HideCheckBox(treeViewDrawing, node); |
456 |
foreach (ItemAttribute attribute in item.ATTRIBUTES) |
457 |
{ |
458 |
TreeNode attrNode = new TreeNode(); |
459 |
attrNode.Name = attribute.Attribute; |
460 |
attrNode.Text = attribute.Attribute; |
461 |
attrNode.Tag = attribute; |
462 |
node.Nodes.Add(attrNode); |
463 |
HideCheckBox(treeViewDrawing, attrNode); |
464 |
} |
465 |
} |
466 |
} |
467 |
} |
468 |
|
469 |
private void RefreshItemNode() |
470 |
{ |
471 |
foreach (TreeNode dwgNode in treeViewDrawing.Nodes[0].Nodes) |
472 |
{ |
473 |
dwgNode.ForeColor = Color.Red; |
474 |
bool result = true; |
475 |
foreach (TreeNode docNode in dwgNode.Nodes) |
476 |
{ |
477 |
string name = SPPIDUtill.GetSPPIDMappingName(docNode.Name, mapping); |
478 |
if (!string.IsNullOrEmpty(name)) |
479 |
{ |
480 |
docNode.ForeColor = Color.Gray; |
481 |
} |
482 |
else |
483 |
{ |
484 |
docNode.ForeColor = Color.Red; |
485 |
result = false; |
486 |
} |
487 |
|
488 |
foreach (TreeNode attrNode in docNode.Nodes) |
489 |
{ |
490 |
name = SPPIDUtill.GetSPPIDMappingName(attrNode.Name, mapping); |
491 |
if (!string.IsNullOrEmpty(name)) |
492 |
{ |
493 |
attrNode.ForeColor = Color.Gray; |
494 |
} |
495 |
else |
496 |
{ |
497 |
attrNode.ForeColor = Color.Red; |
498 |
} |
499 |
} |
500 |
} |
501 |
|
502 |
if (result) |
503 |
{ |
504 |
dwgNode.ForeColor = Color.Black; |
505 |
} |
506 |
} |
507 |
} |
508 |
|
509 |
private List<string> GetNeedMapping() |
510 |
{ |
511 |
List<string> nameList = new List<string>(); |
512 |
foreach (TreeNode dwgNode in treeViewDrawing.Nodes[0].Nodes) |
513 |
{ |
514 |
foreach (TreeNode docNode in dwgNode.Nodes) |
515 |
{ |
516 |
string name = docNode.Name; |
517 |
string sppidName = SPPIDUtill.GetSPPIDMappingName(name, mapping); |
518 |
if (string.IsNullOrEmpty(sppidName) && !nameList.Contains(name)) |
519 |
nameList.Add(name); |
520 |
|
521 |
// Attribute |
522 |
foreach (TreeNode attrNode in docNode.Nodes) |
523 |
{ |
524 |
name = attrNode.Name; |
525 |
sppidName = SPPIDUtill.GetSPPIDMappingName(name, mapping); |
526 |
if (string.IsNullOrEmpty(sppidName) && !nameList.Contains(name)) |
527 |
nameList.Add(name); |
528 |
} |
529 |
} |
530 |
} |
531 |
|
532 |
return nameList; |
533 |
} |
534 |
|
535 |
private void treeViewDrawing_AfterCheck(object sender, TreeViewEventArgs e) |
536 |
{ |
537 |
treeViewDrawing.AfterCheck -= new TreeViewEventHandler(this.treeViewDrawing_AfterCheck); |
538 |
|
539 |
if (e.Node.Level == 0) |
540 |
{ |
541 |
foreach (TreeNode node in e.Node.Nodes) |
542 |
node.Checked = e.Node.Checked; |
543 |
} |
544 |
else if (e.Node.Level == 1) |
545 |
{ |
546 |
bool result = false; |
547 |
foreach (TreeNode node in e.Node.Parent.Nodes) |
548 |
{ |
549 |
if (node.Checked) |
550 |
{ |
551 |
result = true; |
552 |
break; |
553 |
} |
554 |
} |
555 |
|
556 |
if (result) |
557 |
e.Node.Parent.Checked = true; |
558 |
else |
559 |
e.Node.Parent.Checked = false; |
560 |
} |
561 |
|
562 |
treeViewDrawing.AfterCheck += new TreeViewEventHandler(this.treeViewDrawing_AfterCheck); |
563 |
} |
564 |
#endregion |
565 |
|
566 |
#endregion |
567 |
|
568 |
|
569 |
} |
570 |
} |