A big thanks to Viru Aithal, from our DevTech India team, for providing the code that inspired this post.
Update: it turns out I didn't look deeply enough into the origins of the code behind this post. The code that inspired Viru's code that inspired mine came from our old friend Mike Tuersley, who's delivering a class on customizing the Options dialog at this year's AU (in just over a week). Thanks, Mike! :-)
One way that applications often want to integrate with AutoCAD is via the dialog displayed by the OPTIONS command. Luckily it's relatively easy to add your own custom tab to this dialog using .NET, allowing users to view and modify your application's settings. I'm going to take a similar approach to this topic as I did with task dialogs: part 1 (this post) focuses on a very simple implementation to show the basic capabilities of the mechanism, and part 2 will look at a more "real world" implementation that goes as far as providing access to and storing some actual (well, realistic, at least) application settings.
The first step when implementing your custom tab is to add a User Control to your project. As this activity goes beyond just copy & pasting code, here's a sample project demonstrating the technique shown in this post.
Once you have a User Control in your project (ours has the default name UserControl1), you can design it to add in some custom controls - whether buttons, checkboxes, combos, or more complex controls such as a property grid (something I'll show in the next post). Here's the design I settled for, containing just a few, simple controls, each added with its default name:
You can clearly adjust the layout, as you wish, with anchor points, docking, etc. It's worth setting the "modifier" for each of the controls you've added to public or internal, assuming you want to access their state directly from elsewhere in the project. The other way is to expose them via public properties, but that's really up to you.
Here's the code that goes behind this dialog, the important activity being to set the "dirty" flag for the control when a particular component control is selected:
using System;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
namespace OptionsDlg
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(
object sender,
EventArgs e
)
{
TabbedDialogExtension.SetDirty(this, true);
}
private void checkBox1_CheckedChanged(
object sender,
EventArgs e
)
{
TabbedDialogExtension.SetDirty(this, true);
}
private void comboBox1_SelectedIndexChanged(
object sender,
EventArgs e
)
{
TabbedDialogExtension.SetDirty(this, true);
}
}
}
Now for our rest of the implementation. We're going to add our custom tab on load of the application (see these two previous posts for the approach for running code on application load), so there's no need to implement a command. Here's the C# code:
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
[assembly:
ExtensionApplication(
typeof(OneNeedsOptions.Initialization)
)
]
namespace OneNeedsOptions
{
class Initialization : IExtensionApplication
{
static OptionsDlg.UserControl1 _uc;
public void Initialize()
{
Application.DisplayingOptionDialog +=
new TabbedDialogEventHandler(
Application_DisplayingOptionDialog
);
}
public void Terminate()
{
Application.DisplayingOptionDialog -=
new TabbedDialogEventHandler(
Application_DisplayingOptionDialog
);
}
private static void ButtonPressedMessage(string name)
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(name + " button pressed.\n");
}
private static void ShowSettings()
{
if (_uc != null)
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(
"Settings were: checkbox contains {0}," +
" combobox contains {1}\n",
_uc.checkBox1.Checked,
_uc.comboBox1.Text
);
}
}
private static void OnOK()
{
ButtonPressedMessage("OK");
ShowSettings();
}
private static void OnCancel()
{
ButtonPressedMessage("Cancel");
}
private static void OnHelp()
{
ButtonPressedMessage("Help");
}
private static void OnApply()
{
ButtonPressedMessage("Apply");
ShowSettings();
}
private static void Application_DisplayingOptionDialog(
object sender,
TabbedDialogEventArgs e
)
{
if (_uc == null)
_uc = new OptionsDlg.UserControl1();
TabbedDialogExtension tde =
new TabbedDialogExtension(
_uc,
new TabbedDialogAction(OnOK),
new TabbedDialogAction(OnCancel),
new TabbedDialogAction(OnHelp),
new TabbedDialogAction(OnApply)
);
e.AddTab("My Custom Settings", tde);
}
}
}
When we build the application, load it into AutoCAD and run the OPTIONS command, we should see our custom settings appear in our new tab:
As the various settings in the dialog get modified (even the button being clicked), you'll see the "Apply" button get enabled - a direct result of the settings on the control being considered "dirty". As you select the various buttons belonging to the dialog you should see messages on the command-line, as the respective events get fired.
In the next post we'll extend this example to provide access to persistent properties, as you would want to do from a real application.