Results 1 to 2 of 2

Thread: Document Level Global variables in Revit API

  1. #1
    Member
    Join Date
    2008-06
    Posts
    18
    Login to Give a bone
    0

    Default Document Level Global variables in Revit API

    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.

    Code:
    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";
        }
    Last edited by RobertB; 2009-02-02 at 06:35 PM. Reason: Added code tags

  2. #2
    100 Club
    Join Date
    2004-02
    Location
    Brookline, MA
    Posts
    186
    Login to Give a bone
    0

    Default Re: Document Level Global variables in Revit API

    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:

    Code:
    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 ];
      }
    
    }
    }

Similar Threads

  1. Constant/Global Variables in acaddoc.lsp
    By stusic in forum AutoLISP
    Replies: 15
    Last Post: 2012-07-16, 11:40 AM
  2. listing global variables
    By bowtle in forum AutoLISP
    Replies: 6
    Last Post: 2009-07-09, 05:08 AM
  3. Lost Document level projects
    By sfaust in forum Revit - API
    Replies: 0
    Last Post: 2009-01-26, 07:10 PM
  4. Global Variables
    By ccowgill in forum AutoLISP
    Replies: 10
    Last Post: 2008-11-24, 07:23 PM
  5. associating global elevation and building elevation on a single level....
    By olakahahola in forum Revit Architecture - General
    Replies: 1
    Last Post: 2007-11-07, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •