Search code examples
autodesk-forgerevit-apiautodesk-bim360autodesk-designautomation

Get Revit Workshared cloud model as C# Autodesk.Revit.DB Document class


I have a Revit plug-in, I want it to do some operations on a workshared cloud model.

I can't figure out how to get the Revit model as a Document class (Autodesk.Revit.DB) which is stored live on BIM360 cloud, not a local copy, nor downloaded copy.

Seems like I have to use different API's and there are multiple steps to this although I was expecting something relatively simpler, I quickly realised this actually may have multiple steps which I honestly can't figure out.

Is there a working relevant code example on git hub for this ?

Edit: I was able to find the below code but it doesn't compile because ForgeClient and OSSObjectsApi doesn't exist in the latest forge sdk package, how can I fix that ?

using System;
using System.IO;
using System.Threading.Tasks;
using Autodesk.Forge;
using Autodesk.Forge.Model;
using Autodesk.Forge.Client;
using Newtonsoft.Json.Linq;

namespace BIM360Downloader
{
    class Program
    {
        static void Main(string[] args)
        {
            // These are the client ID and client secret that you obtained
            // when you registered your application on the Forge developer portal.
            string clientId = "YOUR_CLIENT_ID";
            string clientSecret = "YOUR_CLIENT_SECRET";

            // Replace these with the project ID and file ID of the model you want to download.
            string projectId = "YOUR_PROJECT_ID";
            string fileId = "YOUR_FILE_ID";

            // Create a new Forge API client.
            ForgeClient client = new ForgeClient(clientId, clientSecret);

            // Get the access token for the client.
            TwoLeggedApi oauth = new TwoLeggedApi();
            dynamic token = oauth.Authenticate(clientId, clientSecret, "client_credentials", new Scope[] { Scope.DataRead });
            string accessToken = token.access_token;

            // Set the bearer token for the client.
            client.Configuration.AccessToken = accessToken;

            // Download the model from BIM 360.
            MemoryStream modelStream = DownloadModelAsync(client, projectId, fileId).Result;
            Console.WriteLine("Successfully downloaded model to memory stream.");
        }

        static async Task<MemoryStream> DownloadModelAsync(ForgeClient client, string projectId, string fileId)
        {
            // Set up the request to download the model.
            OSSObjectsApi objectsApi = new OSSObjectsApi();
            dynamic objectDetails = await objectsApi.GetObjectDetailsAsync(projectId, fileId);
            string bucketKey = objectDetails.bucketKey;

            // Download the model data.
            dynamic data = await objectsApi.GetObjectAsync(bucketKey, fileId);
            byte[] modelData = data.Body;

            // Create a new MemoryStream object to store the model data.
            MemoryStream modelStream = new MemoryStream(modelData);
            return modelStream;
        }
    }
}

Solution

  • To open a live Revit Could Model (RCM), you can use ModelPathUtils.ConvertCloudGUIDsToCloudPath() to convert the project and model guid into a ModelPath. You can then use this ModelPath to open the document using Application.OpenDocumentFile() method.

    Also read the sections Getting the CloudPath for a Model and SaveAsCloudModel Information from the Web Browser in this link on how to find the account, project, and model guids of the model you are interested in.

    var cloudModelPath = ModelPathUtils.ConvertCloudGUIDsToCloudPath(region, projectGuid, modelGuid);
    Document doc = app.OpenDocumentFile(cloudModelPath, new OpenOptions());
    

    This code will work in a desktop addin as long as a valid user is logged in and that user has access to the input model. While you have not explicitly mentioned that you need this to work on Design Automation for Revit, you have added the tag #autodesk-designautomation to your question. The good news is the same code above should work for Design Automation addin (app bundle), but there is an extra step on how to provide user context to the design automation job. Please refer to this blog post and github sample for Design Automation for RCM.