简体   繁体   English

如何使用Revit API获取Revit文件版本?

[英]How can I get the Revit file version using the Revit API?

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 ). 在Revit API中,我知道我可以获得当前正在运行的Revit实例的版本( ControlledApplication.VersionBuildControlledApplication.VersionNameControlledApplication.VersionNumber )。 However, I would like to get the version of a Revit file itself before I open it. 但是,我想在打开之前获取Revit文件的版本。 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. 这样我就可以停止自动升级对话框,该对话框显示用户在较新版本的Revit中打开较旧的Revit文件。 I'm using Revit 2013 and expecting files from 2011, 2012, and 2013. 我正在使用Revit 2013并期待2011年,2012年和2013年的文件。

Similar Autodesk Discussion Group Question 类似的Autodesk讨论组问题
Building Coder 建筑编码器

As you said Revit file format is a Structired Storage document and information you need is stored in the BasicFileInfo stream. 正如您所说,Revit文件格式是Structired Storage文档,您需要的信息存储在BasicFileInfo流中。

Here is the full console application demonstrating how to extract BasicFileInfo Data 是完整的控制台应用程序,演示如何提取BasicFileInfo数据

Unfortunately I don't know the format of the BasicFileInfoStream. 不幸的是我不知道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. 读取唯一的BasicFileInfo比读取整个文件要好得多。 Imagine if the Revit project is over 500 MB or more. 想象一下,如果Revit项目超过500 MB或更多。 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. 从我的示例中,在rawString中使用Regex而不是整个文件。

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. 在Revit 2013中,您可以使用BasicFileInfo类的SavedInCurrentVersion属性来确定文件是否以当前Revit版本保存。 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. 如果要获取保存文件的版本(如问题标题中所示),可以使用同一类的SavedInVersion属性。 You can use the BasicFileInfo.Extract method to get the BasicFileInfo . 您可以使用BasicFileInfo.Extract方法获取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 . “.rvt”文件以OLE格式存储,因此如果使用像Structured Storage Viewer这样的工具,您可以看到内容。 It will be located under BasicFileInfo . 它将位于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. 如果你想,那么你可能会使用.NET库来读取数据,但我使用的是StreamReaderRegex

Here is the regular expression: 这是正则表达式:

Revit\\sArchitecture\\s(?<Year>\\d{4})\\s(Build:\\s(?<Build>\\w*)\\((?<Processor>\\w{3})\\)\\) 的Revit \\ sArchitecture \\ S(<年份> \\ d {4}?)\\ S(构建:?\\ S(<生成> \\ W *)\\((<处理器> \\ 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. 但请注意,我的版本专门检查架构,因此您需要为MEP或Structural修改它。

UPDATED : 更新

  • If you are using Revit 2013 or newer then see my other answer . 如果您使用的是Revit 2013或更新版本,请参阅我的其他答案
  • 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). 如果您使用的是Revit 2012或更早版本,请将上面的Regex与Victor的答案结合起来,以便更好地阅读BasicFileInfo(而不是整个内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM