PDA

View Full Version : Document Level Global variables in Revit API



aireq303
2009-01-16, 11:04 PM
I'm trying to figure out a way to save custom properties and objects in between my custom commands. I have be writing to/from a file at the start and end of each command, but I'm wondering if there is a better way.

A static variable in a static class sort of works, but it's scope is too big. After some basic testing it seems that the scope of a static variable is the entire application. However, the values I am saving need to be specific to the current document. I'd want something where if the user switches between two open documents the commands will be using a different set of static variables.

I've added the code I used to check the scope of a static variable below in the comments.


class TestStaticVariable : IExternalCommand
{
public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

DialogResult result = MessageBox.Show("SavedStateTest.state = " + SavedStateTest.state, "hi there", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
SavedStateTest.state = "you last choose YES";
}
else
{
SavedStateTest.state = "you last choose NO";
}
return IExternalCommand.Result.Succeeded;
}
}
static public class SavedStateTest
{
public static string state = "Only Instantiated";
}

mmason.65315
2009-01-19, 11:56 PM
You could have a static variable that is a dictionary of document names and the saved states... That way as you switched between documents and re-ran the command, each one would have a saved state within the application...

Something like:



public class TestStaticVariable : IExternalCommand
{
private static Dictionary<string, SavedStateObject> SavedStates;
private SavedStateObject _savedState;

public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

if (SavedStates == null) SavedStates = new Dictionary(string,SavedStateObject)();

if (SavedStates.ContainsKey( commandData.ActiveDocument.PathName ) )
{
_savedState = SavedStates[ commandData.ActiveDocument.PathName ];
}

}
}