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.
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
.
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 :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.