/
power-sm.cs
139 lines (107 loc) · 4.28 KB
/
power-sm.cs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using System.Reflection;
using SolidWorksTools.File;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
namespace PowerSM
{
[Guid("B2A0B714-0984-435A-A2DF-D3AA3E8E806D")]
[ComVisible(true)]
public class PowerSM: SwAddin
{
/* Notes to developers:
- Make sure Embed Interop Types for the reference solidworks.interop.sldworks in the properties window is set to true.
- From project properties, select the target platform to 64x under the build tab
- Check Register for COM Interop under the build tab // need to run vs as admin when building
*/
int AddInCookie;
SldWorks swApp;
#region Connect To SolidWorks
public bool ConnectToSW(object swAppObj, int SessionCookie)
{
swApp = (SldWorks)swAppObj;
AddInCookie = SessionCookie;
swApp.SetAddinCallbackInfo(0, this, AddInCookie);
BuildMenu();
return true;
}
public bool DisconnectFromSW()
{
DestroyMenu();
GC.Collect();
return true;
}
#endregion
#region COMRegistration
[ComRegisterFunction()]
private static void RegisterAssembly(Type t)
{
string KeyPath = string.Format(@"SOFTWARE\SolidWorks\AddIns\{0:b}", t.GUID);
RegistryKey rk = Registry.LocalMachine.CreateSubKey(KeyPath);
rk.SetValue(null, 1); // 1: Add-in will load at start-up
rk.SetValue("Title", "Power-SM"); // Title
rk.SetValue("Description", "SolidWorks Add-in for Sheet Metal"); // Description
#region Bitmap handling region
BitmapHandler iBmp = new BitmapHandler();
Assembly thisAssembly;
thisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var rm = new System.Resources.ResourceManager("PowerSM.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
Bitmap add_in = (Bitmap)rm.GetObject("add_icon");
// Copy the bitmap to a suitable permanent location with a meaningful filename
String addInPath = System.IO.Path.GetDirectoryName(thisAssembly.Location);
String iconPath = System.IO.Path.Combine(addInPath, "add_icon.bmp");
add_in.Save(iconPath);
#endregion
// Register the icon location
rk.SetValue("Icon Path", iconPath);
}
[ComUnregisterFunction()]
private static void UnRegisterAssembly(Type t)
{
string KeyPath = string.Format(@"Software\SolidWorks\Addins\{0:b}", t.GUID);
Registry.LocalMachine.DeleteSubKey(KeyPath);
}
#endregion
#region UIMethod
private void BuildMenu()
{
int DocType = (int)swDocumentTypes_e.swDocNONE;
swApp.AddMenu(DocType, "PowerSM", 1);
swApp.AddMenuItem4(DocType, AddInCookie, "Power Geometry Tool@PowerSM", 1, "GeometryToolMethod", "3", "Power Geometry Tool", "");
}
private void DestroyMenu()
{
int DocType = (int)swDocumentTypes_e.swDocNONE;
swApp.RemoveMenu(DocType, "PowerSM", "GeometryToolMethod");
swApp.RemoveMenu(DocType, "Power Geometry Tool@PowerSM", "GeometryToolMethod");
}
#endregion
#region Add-in Implementation
// Callback methods must be public, otherwise call from menu item fails
public void GeometryToolMethod()
{
var swFrame = new swFrame(Process.GetProcessById(swApp.GetProcessID()).MainWindowHandle);
var f = new PowerGeometryForm(swApp);
f.StartPosition = FormStartPosition.CenterParent;
f.ShowDialog(swFrame);
}
public void ConvertToolMethod()
{
}
#endregion
}
public class swFrame : IWin32Window
{
public IntPtr Handle { get; set; }
public swFrame(IntPtr handle)
{
Handle = handle;
}
}
}