Retrieving Active Users in a Revit Central Model File (File-Based)

Retrieving Active Users in a Revit Central Model File (File-Based)

DesignGroup01
Enthusiast Enthusiast
1,483 Views
4 Replies
Message 1 of 5

Retrieving Active Users in a Revit Central Model File (File-Based)

DesignGroup01
Enthusiast
Enthusiast

Hello All,

I am working on a file-based central model in Revit and am looking for a way to retrieve a list of active users currently working in the central model file. Specifically, I want to track which users are accessing the central model at any given time.

The scenario is as follows:

  • We have a central model that multiple users are working on.
  • Each user opens the central model and creates a local copy of the file.
  • I want to programmatically extract the list of active users who currently have the central model file open.

Is there any method or functionality within the Revit API that allows me to access this information? Ideally, I would like to get a list of users who are actively working on the file or have the central model open.

I have explored some options such as worksets and session users, but I haven’t been able to find a direct API method to fetch this list.

Any guidance or suggestions would be greatly appreciated!

Thank you in advance!

0 Likes
1,484 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

Hello. I asked Gemini for you, and it replies:

   

You're tackling a common challenge in multi-user Revit workflows. Unfortunately, the Revit API does not provide a direct method to retrieve a list of active users currently working on a file-based central model. This information is managed at the file system level and not exposed through the API. Worksets and session users relate to in-Revit collaboration features (worksharing) and don't reflect file access.

However, we can explore some workarounds, understanding they have limitations:

1. File Locking Detection (Most Reliable but Imperfect):

The most reliable approach is to check for file locks. When a user opens a central model, Revit creates lock files (e.g., .lock, ~$ files on Windows). We can use .NET's System.IO functionality to detect these locks.

C#
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public static class RevitUtils
{
    public static List<string> GetUsersLockingCentralModel(string centralModelPath)
    {
        List<string> lockingUsers = new List<string>();

        if (!File.Exists(centralModelPath))
        {
            return lockingUsers; // Central model doesn't exist
        }

        string lockFilePath = Path.Combine(Path.GetDirectoryName(centralModelPath), "~$" + Path.GetFileName(centralModelPath));
        if (File.Exists(lockFilePath))
        {
            try
            {
                using (var stream = new FileStream(lockFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var reader = new StreamReader(stream))
                {
                    string firstLine = reader.ReadLine();
                    if (!string.IsNullOrEmpty(firstLine))
                    {
                        // The first line often contains the username. This is *not* guaranteed by Autodesk though!
                        lockingUsers.Add(firstLine);
                    }
                }

            }
            catch (Exception ex)
            {
                // Handle exceptions (e.g., file access issues)
                System.Diagnostics.Debug.WriteLine($"Error reading lock file: {ex.Message}");
            }

        }

        //Check for .lock file (less common with Revit but can happen)
        string legacyLockFilePath = centralModelPath + ".lock";

        if (File.Exists(legacyLockFilePath))
        {
            lockingUsers.Add("Lock File Exists"); // Less info available here.
        }

        return lockingUsers;
    }
}

// Example usage:
string centralModelPath = @"C:\Path\To\Your\CentralModel.rvt";
List<string> activeUsers = RevitUtils.GetUsersLockingCentralModel(centralModelPath);

if (activeUsers.Any())
{
    foreach(string user in activeUsers)
    {
        TaskDialog.Show("Active Users", $"User: {user} has the central model open");
    }

}
else
{
    TaskDialog.Show("Active Users", "No users currently have the central model open.");
}

Key Improvements and Explanations:

  • Handling ~$ and .lock Files: The code now checks for both the primary ~$ lock file and the less common .lockfile (sometimes seen in older Revit versions or specific scenarios).
  • Reading Lock File Content: The code attempts to read the first line of the ~$ lock file. Often, this line contains the username. However, this is not officially documented or guaranteed by Autodesk. It's an observation based on common practice. Therefore, relying on this is not 100% reliable.
  • Error Handling: A try-catch block is added to handle potential IOExceptions (e.g., if the lock file is already being accessed by another process).
  • Clearer Return Value: The function returns a List<string> to handle cases where multiple locks might be present (though rare).
  • Example Usage with TaskDialog: I have included a simple example of how to use the function and display the results using a TaskDialog for better user feedback within Revit.

Limitations:

  • Accuracy: Relying on file locks only indicates that a Revit process has the file open. It doesn't guarantee active work. A user might have Revit open with the model loaded but be inactive.
  • Username Extraction Unreliable: As mentioned, extracting the username from the lock file is not officially supported and should be treated as an approximation.
  • File System Dependency: This approach is inherently tied to the file system and might not be suitable for cloud-based or other non-standard file storage solutions.

2. Revit Server or Cloud Worksharing (If Applicable):

If you're using Revit Server or BIM 360/Autodesk Construction Cloud (ACC), there are more robust ways to track user activity, often through server logs or cloud platform APIs. These are outside the scope of file-based worksharing but are worth considering if you have those infrastructure components.

Conclusion:

While a perfect solution doesn't exist within the Revit API for file-based central models, the file locking method provides a reasonably effective workaround. Remember to clearly communicate the limitations of this approach to your users.

I hope this detailed explanation and enhanced code example are helpful. Please let me know if you have any further questions.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

ctm_mka
Collaborator
Collaborator

so kind of a hack approach, but it works, for both local and cloud based central models. consists of two apps. First, installed on every user machines, upon  opening the project, writes/appends to a text file to a network location, file name is the users name an contains the model name(s) only. Upon closing the model, deletes that file. Second part is the reader, this may or may not be on everyone's machines depending on how you want to use the data.  we have a revit side app that parses through those text files and displays everyone in the model you are in, and then and admin side client that does that same, but for all active models and users.

Message 4 of 5

eric.stimmel
Advocate
Advocate

You could compile a list of all the users that have elements checked out in the model. It's not going to be perfect (some users may have the model open, but nothing checked out while others may not have relinquished properly and even if they are not in the model, they will show up in the list), but it's a pretty good approximation of who is in the model.

0 Likes
Message 5 of 5

TripleM-Dev.net
Advisor
Advisor

Workshare monitor from Autodesk itself?

If you have a local open, it will show all users that have the model open and status (syncing, last sync etc)

 

Any reason you would want to access it with API manually?

 

0 Likes