AutoCAD .NET Addin Wizard (AcadNetAddinWizard) is far more than just some templates scattered around. It is a real-wizard-sense wizard, i.e. multi paged, configurable, flexible and intelligent. It also includes various project item wizards, coders, and widgets for AutoCAD .NET programming.
In this article, let us see how the OptionsDialog Extensioner of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can programmatically add a custom tab to the AutoCAD Options dialog through the TabbedDialogExtension class.
The OptionsDialog Extensioner can be found from the Add New Item UI of both Visual C# and VB.NET of Visual Studio both full-fledged and Express in version both 2008 and 2010. Let’s take the C# of Visual Studio 2010 for an example. The AutoCAD Addin category will appear under the Visual C# Items node of the Installed Templates and its various item wizards including the OptionsDialog Extensioner appear in turn in the middle pane:
Here are wizard pages and some sample fill-outs of the OptionsDialog Extensioner:
The auto-generated class and code may look like the following:
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcWindowsNS = Autodesk.AutoCAD.Windows;
namespace Acad2012NetDemoAddinCS
{
/// <summary>
/// The settings object that is associated with the UserControl.
/// </summary>
public class OptionsDialogExtensioner1_Settings
{
public string Text { get; set; } // Just an example property
//TODO: add more setting properties.
}
/// <summary>
/// The class that can be used to extend the AutoCAD Options dialog.
/// </summary>
public class OptionsDialogExtensioner1
{
public void Register()
{
MgdAcApplication.DisplayingOptionDialog += AcadApplication_DisplayingOptionDialog;
}
public void UnRegister()
{
MgdAcApplication.DisplayingOptionDialog -= AcadApplication_DisplayingOptionDialog;
}
// Make the settings object public and static so that the outside world can access to it.
public static OptionsDialogExtensioner1_Settings TabSettings = new OptionsDialogExtensioner1_Settings();
private UserControl1 mCustomControl;
TabbedDialogExtension mOptDiaExt;
private void AcadApplication_DisplayingOptionDialog(object sender, TabbedDialogEventArgs e)
{
mCustomControl = new UserControl1();
//TODO: e.g. set default values of controls in the mCustomControl.
mOptDiaExt = new TabbedDialogExtension(mCustomControl, okCallback, cancelCallback, helpCallback, applyCallback);
e.AddTab("MyOption", mOptDiaExt);
foreach (Control control in mCustomControl.Controls)
{
control.Validated += control_Validated;
}
}
private void control_Validated(object sender, EventArgs e)
{
TabbedDialogExtension.SetDirty((sender as Control).Parent, true);
}
private void okCallback()
{
applyCallback();
MessageBox.Show(TabSettings.Text); // Should be removed in the final code.
}
private void cancelCallback()
{
//TODO: e.g. do some cleanup.
}
private void helpCallback()
{
//TODO: e.g. launch a help file and open a specific topic.
}
private void applyCallback()
{
//TODO: e.g. write the control values to a setting file or some registry keys/values.
TabSettings.Text = mCustomControl.Name; // Just an example
}
}
}
The OptionsDialog Extensioner will be registered and unregistered in the chosen extension application implementation.
public class ExtApp : IExtensionApplication
{
#region IExtensionApplication Members
public void Initialize()
{
//TODO: add code to run when the ExtApp initializes. Here are a few examples:
// Checking some host information like build #, a patch or a particular Arx/Dbx/Dll;
// Creating/Opening some files to use in the whole life of the assembly, e.g. logs;
// Adding some ribbon tabs, panels, and/or buttons, when necessary;
// Loading some dependents explicitly which are not taken care of automatically;
// Subscribing to some events which are important for the whole session;
// Etc.
//mLongTransactionManagerEvents1.Register();
//mDynamicLinkerEvents1.Register();
mOptionsDialogExtensioner1.Register();
}
public void Terminate()
{
//TODO: add code to clean up things when the ExtApp terminates. For example:
// Closing the log files;
// Deleting the custom ribbon tabs/panels/buttons;
// Unloading those dependents;
// Un-subscribing to those events;
// Etc.
//mLongTransactionManagerEvents1.UnRegister();
//mDynamicLinkerEvents1.UnRegister();
mOptionsDialogExtensioner1.UnRegister();
}
#endregion
//LongTransactionManagerEvents1 mLongTransactionManagerEvents1 = new LongTransactionManagerEvents1();
//DynamicLinkerEvents1 mDynamicLinkerEvents1 = new DynamicLinkerEvents1();
OptionsDialogExtensioner1 mOptionsDialogExtensioner1 = new OptionsDialogExtensioner1();
}
Next is to launch AutoCAD, NetLoad the assembly, and launch the Options dialog to have a look at the custom tab (TabbedDialogExtension). In case the project itself is also created by the AutoCAD .NET Addin Wizard (AcadNetAddinWizard) and so does the test command, a single F5 press will get us all the way there.
Here is a sample custom tab of the Options dialog beging extended by the TabbedDialogExtension. The real look depends on what the Custom Control is designed.
Give the OptionsDialog Extensioner of the leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) a try and you will not feel any regret.
Posted by: |