Static in your commands- User settings Part 1

by Guy Robinson 10. February 2009 12:18

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

About the Author

A .NET software Developer providing custom applications and commands for architecture firms exclusively working with Autodesk Revit and integration with any associated applications. All from a little place north of Whitianga, New Zealand.

Page List

Disclaimer

I'm self employed so the opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway☺

© Copyright2008

Creative Commons License
Blog content is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

With the following exception. All code snippets, application and libraries are licensed under a a Apache License Version 2.0

Autodesk Revit®

Autodesk: Revit is a product that is wholly owned by Autodesk. Any reference to Revit,Revit API, Revit Architecture, Revit MEP or Revit Structure on this site is made acknowledging this ownership. Refer to Autodesk's own web site and product pages for specific trademark and copyright information. Autodesk represents a great many products and every attempt will be made to respect their ownership whenever one of these other products is mentioned on this site.