-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenTopLevelAsm.cs
More file actions
89 lines (83 loc) · 3.1 KB
/
OpenTopLevelAsm.cs
File metadata and controls
89 lines (83 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;
using Application = SolidEdge.Framework.Interop.Application;
namespace SolidEdgeTopLevelAsmOpener
{
[ComVisible(true)]
[COMServerAssociation(AssociationType.Directory)]
public class OpenTopLevelAsm : SharpContextMenu
{
private ContextMenuStrip menu = new ContextMenuStrip();
protected override bool CanShowMenu()
{
// Check that only one item is selected and that it is a directory
return SingleItem() && IsDirectory();
bool SingleItem() => SelectedItemPaths.Count() == 1;
bool IsDirectory() => File.GetAttributes(SelectedItemPaths.First()).HasFlag(FileAttributes.Directory);
}
protected override ContextMenuStrip CreateMenu()
{
// Mostly adapted from SharpShell example:
// https://www.codeproject.com/Articles/1035998/NET-Shell-Extensions-Adding-submenus-to-Shell-Cont
menu.Items.Clear();
// TODO: Find way in SharpShell to handle pre-existing dropdown and add to it.
ToolStripMenuItem SEMenu;
SEMenu = new ToolStripMenuItem
{
Text = "Solid Edge"
};
var openParentAssemblies = new ToolStripMenuItem
{
Text = "Open Top-Level Assemblies"
};
openParentAssemblies.Click += (sender, args) => openAsmFiles();
SEMenu.DropDownItems.Add(openParentAssemblies);
menu.Items.Clear();
menu.Items.Add(SEMenu);
return menu;
}
private void openAsmFiles()
{
// Check if any .asm files exist in selected path before launching SE.
var path = SelectedItemPaths.First();
var asmFiles = Directory.GetFiles(path, "*.asm");
Application SEApp = null;
if (asmFiles.Any())
{
// Find running SE instance or launch new one.
OleMessageFilter.Register();
if (SEAppHelper.SEIsRunningForeground(out SEApp))
{
SEApp.DoIdle();
}
else
{
SEApp = SEAppHelper.SEStart();
SEApp.DoIdle();
}
// !! Must initialize Array with new operator or will throw NullRefEx
Array topLevelAsms = new string[] { };
SEApp.GetListOfTopLevelAssembliesFromFolder(path, out topLevelAsms);
SEApp.DoIdle();
if (topLevelAsms != null)
{
foreach (string asm in topLevelAsms)
{
SEApp.Documents.Open(asm);
SEApp.DoIdle();
}
}
}
OleMessageFilter.Revoke();
if (SEApp != null)
{
Marshal.FinalReleaseComObject(SEApp);
}
}
}
}