Posted on 10. February 2009

Static in your commands- User settings Part 1

Every well designed application will provide some level of user specific configuration. This is the first part in a series of posts explaining how to do this for Revit commands using 3 different techniques.  Revit commands are assemblies not applications so using standard .NET functionality (ie System.Configuration) has a number of problems. I’m going to start with a simple technique and then cover how to do it properly using .NET. Including why doing it properly doesn’t work and how to make it work. The first solution is simple to use but also the weakest solution.

The example command I’m going to use throughout these posts remembers the commands form position and size on the screen. The first technique using static fields however differs from the other techniques in that it will only remember the forms position and size during a Revit session. The other techniques will store these settings between sessions.

What is an AppDomain? Simplistically it’s the .NET equivalent of an operating systems process. This provides a level of code isolation both for the operating system and the host application, as well as the possibility of isolating assemblies within a .NET application. By definition every .NET application must contain at least one AppDomain. For each Revit session the current Revit .NET API (not VSTA) runs all commands within a single AppDomain.

One of the advantages of a single AppDomain, is it allows you to use static fields in your command classes to retain state when executing a command multiple times during a Revit session. An  assembly shared amongst multiple commands can also make use of this feature to share common settings amongst commands. From the code notice the forms size and position is not set in a constructor but the first time the command is run in a session by using static fields. This could also be done using a static constructor.

public class Command: IExternalCommand
    {
        // initialise for the first time the command is run in a session
        private static Point _location = new Point(200, 200);
        private static Size _size = new Size(300, 300);

The default size of the form is taken from the design size of the form. Make sure you set the form StartPosition to manual to avoid the other options overriding the forms location when initialised.

StartPosition

The code for this example command is here.

Comments are closed