-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAddinServer.cs
More file actions
122 lines (103 loc) · 4.12 KB
/
AddinServer.cs
File metadata and controls
122 lines (103 loc) · 4.12 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
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
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Inventor;
using Inventor.AddinTemplate.Buttons;
namespace Inventor.AddinTemplate
{
/// <summary>
/// This is the primary AddIn Server class that implements the ApplicationAddInServer interface
/// that all Inventor AddIns are required to implement. The communication between Inventor and
/// the AddIn is via the methods on this interface.
/// </summary>
[Guid("25E7585D-00D9-47C6-8E1B-734A2186383A")]
public class AddinServer : Inventor.ApplicationAddInServer
{
// The Inventor application instance
public static Inventor.Application InventorApp;
public static Guid AddinGuid = new("25E7585D-00D9-47C6-8E1B-734A2186383A");
List<InventorButton> _buttons;
#region ApplicationAddInServer Members
/// <summary>
/// This method is called by Inventor when it loads the addin.
/// The AddInSiteObject provides access to the Inventor Application object.
/// The FirstTime flag indicates if the addin is loaded for the first time.
/// </summary>
public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
{
InventorApp = addInSiteObject.Application;
InventorApp.ApplicationEvents.OnApplicationOptionChange += UpdateButtons;
try
{
// If the addin is loaded for the first time, initialize the UI components
if (firstTime)
{
InitializeUIComponents();
}
}
catch (Exception ex)
{
MessageBox.Show($"Could not load Inventor.AddinTemplate.\n\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Initializes the UI components of the addin.
/// </summary>
private void InitializeUIComponents()
{
_buttons = Assembly.GetAssembly(typeof(InventorButton)).GetTypes()
.Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InventorButton)))
.Select(Activator.CreateInstance)
.Cast<InventorButton>()
.Where(button => button.Enabled)
.OrderBy(button => button.SequenceNumber)
.ToList();
_buttons.ForEach(b => b.Initialize());
}
/// <summary>
/// Updates the buttons when the application options change.
/// </summary>
private void UpdateButtons(EventTimingEnum beforeOrAfter, NameValueMap context, out HandlingCodeEnum handlingCode)
{
if (beforeOrAfter == EventTimingEnum.kAfter)
{
_buttons.ForEach(b => b.Dispose());
InitializeUIComponents();
handlingCode = HandlingCodeEnum.kEventHandled;
}
handlingCode = HandlingCodeEnum.kEventNotHandled;
}
/// <summary>
/// This method is called by Inventor when the AddIn is unloaded.
/// The AddIn will be unloaded either manually by the user or
/// when the Inventor session is terminated.
/// </summary>
public void Deactivate()
{
InventorApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
/// <summary>
/// This method is now obsolete, you should use the
/// ControlDefinition functionality for implementing commands.
/// </summary>
public void ExecuteCommand(int commandID)
{
}
/// <summary>
/// This property is provided to allow the AddIn to expose an API
/// of its own to other programs. Typically, this would be done by
/// implementing the AddIn's API interface in a class and returning
/// that class object through this property.
/// </summary>
public object Automation
{
get
{
return null;
}
}
#endregion
}
}