Advertisement

Saturday, March 2, 2019

Using Forge to Check Version of Revit Files Hosted in the Cloud

From the Autodesk Forge website:

Check the version of a Revit file hosted on the cloud

By Augusto Goncalves
February 21, 2019

With Design Automation for Revit we can select which engine to use, 2018 or 2019. And it's important to choose the correct one in order to avoid opening with an older version or saving with a newer version, e.g. if you have a 2018 file and open and save with 2019 engine.

Jeremy Tammik did an interesting blog post about it, in summary reading the file information that is encoded inside the bytes. This approach still valid, but if the file is on the cloud, we don't want to download it (it may be too big). But we don't need, actually, just need the first few KBs.

The following code sample downloads a few KBs, then if it finds "Autodesk", it created a dictionary with the values.

private async Task<Dictionary<string, string>> GetInfo(string storageLocation, string accessToken)
{
    // this only works for Revit files
    if (storageLocation.IndexOf(".rvt") == -1) return null;

    // prepare to download the file...
    WebClient webClient = new System.Net.WebClient();
    webClient.Headers.Add("Authorization", "Bearer " + accessToken);
    System.IO.Stream stream = webClient.OpenRead(storageLocation);
    byte[] buffer = new byte[ 10 * 1024]; // 10kb
    int res = 0;
    int count = 0;
    do
    {
        if (count++ > 10) return null; // should be right on the begining of the file... 

        // get 10kb of data..
        res = await stream.ReadAsync(buffer, 0, 10 * 1024);
        string rawString = System.Text.Encoding.BigEndianUnicode.GetString(buffer);

        // does it have "Autodesk" on it?
        if (!rawString.Contains("Autodesk")) continue;
        var fileInfoData = rawString.Split(new string[] { "\0", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

        // let's build a Dictionary from this data
        Dictionary<string, string> values = new Dictionary<string, string>();
        foreach (var info in fileInfoData)
            if (info.Contains(":"))
                values.Add(info.Split(":")[0], info.Split(":")[1]);

        return values; // stop downloading the file...
    } while (res != 0);

    // found nothing!
    return null;
}

The result will look like. Note the list of values may vary depending on which features were used to create the file.




See the original posting on the Autodesk Forge website »

Questions? Contact the original publisher »

No comments: