Getting Data From the Central File While Working in the Local File

A while back I was thinking about using the API while working in a local file to collect data about the central file. This came from Steve’s post about warnings after Synchronize with Central if rooms have been added in your local file that are in the same region as rooms in the central file (that presumably were created by some other user since your last Synchronize).

Instead of being surprised by this warning as part of a complex and possibly time-consuming Synchronize, maybe it would be nicer to run some quick checks via the API. To do this, we would start with the local file open, open the central file, collect some data, and then return the user to the local file. But because of these two restrictions, several intermediate steps are needed.


public void GetCentralFileData()
    Autodesk.Revit.ApplicationServices.Application app = this.Application;
    Document localFileDocument = this.ActiveUIDocument.Document;

    // Get an instance of the ModelPath class describing the central file
    ModelPath modelPath = localFileDocument.GetWorksharingCentralModelPath();

    // Get the string of the path for the central and local files 
    string centralFilePath = ModelPathUtils.ConvertModelPathToUserVisiblePath(modelPath);
    string localFilePath = localFileDocument.PathName;

    // Local and central can't both be open at the same time in the same session
    // But trying to call doc.Close() here doesn't work. It throws an InvalidOperationException
    // when attempting to close the currently active document
    // So before trying to close the currently open local file, first create and open a temporary document
    Document tempDoc = app.NewProjectDocument(@"C:\ProgramData\Autodesk\RAC 2013\Templates\US Imperial\default.rte");
    // But NewProjectDocument does not make the new document active, so the local file (which is the active document) still can't be closed

    // Save the temporary file into the TEMP folder
    string tempFileName = Path.Combine(Environment.GetEnvironmentVariable("TEMP"),"tempFile.rvt");
    // Delete an existing file if there is one from a previous run of this macro

    // Use OpenAndActivateDocument to make the temp file the active document
    Autodesk.Revit.UI.UIApplication uiapp = new UIApplication(app);

    // Now that the temp file is open and active, the local file can be closed

    // With the local file closed, we can now finally open the central file
    // OpenDocumentFile opens the document into memory but does not make it visible in the Revit UI.
    // Use OpenAndActivateDocument if you want the document to become active
    Document centralFileDoc = app.OpenDocumentFile(centralFilePath);

    int centralFileWallCount = new FilteredElementCollector(centralFileDoc).OfClass(typeof(Wall)).Count();

    // Open the temp file
    UIDocument tempfileUIDoc = uiapp.OpenAndActivateDocument(tempFileName);

    // Close the central file

    // Open the local file

    // Close the temp file

    // Finally we are back to having only the local file open!
    // Display the data about the central file that was collected when the central file was open
    TaskDialog.Show("Central File Data", centralFileWallCount + " walls in " + centralFilePath);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s