Question

In the Revit API I know that I can get the version of the Revit instance that is currently running (ControlledApplication.VersionBuild,ControlledApplication.VersionName,ControlledApplication.VersionNumber). However, I would like to get the version of a Revit file itself before I open it. This way I could stop the automatic upgrade dialog that shows when a user opens an older Revit file in a newer version of Revit. I'm using Revit 2013 and expecting files from 2011, 2012, and 2013.

Similar Autodesk Discussion Group Question
Building Coder

Was it helpful?

Solution

As you said Revit file format is a Structired Storage document and information you need is stored in the BasicFileInfo stream.

Here is the full console application demonstrating how to extract BasicFileInfo Data

Unfortunately I don't know the format of the BasicFileInfoStream. But if you read it as string you can get version in which file was created.

Read the only BasicFileInfo is much better than read the whole file. Imagine if the Revit project is over 500 MB or more. When you call

string fileContents = streamReader.ReadToEnd();

you write the whole file to the memory.

Also, Regular expression on the huge file works slow.

I think you should use

var rawString = System.Text.Encoding.Unicode.GetString(rawData);

from my sample and use Regex in the rawString instead the whole file.

Hope it helps.

UPDATED: I completely forgot. In the Revit 2013 you can use SavedInCurrentVersion property of the BasicFileInfo class to determine whether a file was saved in current Revit version or not. If you want to get version in which file was saved (as in your question caption) you can use SavedInVersion property of the same class. You can use the BasicFileInfo.Extract method to get the BasicFileInfo.

OTHER TIPS

I took a closer look at the discussion group posting from my question and found that the version information is actually human readable among the other jibberish of the file. The ".rvt" file is stored in OLE format so you can see the content if use a tool like Structured Storage Viewer. It will be located under BasicFileInfo.

If you wanted to then you could probably use an OLE library for .NET to read the data but I used a StreamReader and a Regex instead.

Here is the regular expression:

Revit\sArchitecture\s(?<Year>\d{4})\s(Build:\s(?<Build>\w*)\((?<Processor>\w{3})\)\)

Here is the code:

private void ControlledApplication_DocumentOpening (object sender,
  DocumentOpeningEventArgs e)
{
  FileInfo revitFileToUpgrade = new FileInfo(e.PathName);
  Regex buildInfoRegex = new Regex(
    @"Revit\sArchitecture\s(?<Year>\d{4})\s\(Build:\s(?<Build>\w*)\((<Processor>\w{3})\)\)");

  using(StreamReader streamReader = 
    new StreamReader(e.PathName, Encoding.Unicode))
  {
    string fileContents = streamReader.ReadToEnd();

    Match buildInfo = buildInfoRegex.Match(fileContents);
    string year = buildInfo.Groups["Year"].Value;
    string build = buildInfo.Groups["Build"].Value;
    string processor = buildInfo.Groups["Processor"].Value;
  }
}

This will allow you to get the year, build number, and the type of processor. Notice, however, that my version checks specifically for Architecture so you will need to modify it for MEP or Structural.

UPDATED:

  • If you are using Revit 2013 or newer then see my other answer.
  • If you are using Revit 2012 or older then combine the Regex above with Victor's answer for a better way to read the BasicFileInfo (instead of the whole contents).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top