Kean Walmsley


  • About the Author
    Kean on Google+

July 2014

Sun Mon Tue Wed Thu Fri Sat
    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    








« Adding custom properties to AutoCAD's rollover tooltip and its quick properties panel | Main | Adding a custom tab to AutoCAD's options dialog using .NET - Part 2 »

November 19, 2008

Adding a custom tab to AutoCAD's options dialog using .NET - Part 1

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:

User control layout for options tab

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:

A custom tab in AutoCAD's options dialog

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.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e2010535fb57a9970c

Listed below are links to weblogs that reference Adding a custom tab to AutoCAD's options dialog using .NET - Part 1:

blog comments powered by Disqus

10 Random Posts