Revit二次开发,获取模型版本信息

string path = @"C:\xxx.rfa";//模型地址
BasicFileInfo fileInfo
=BasicFileInfo.Extract(path);//引入Autodesk.Revit.DB.BasicFileInfo
MessageBox.Show(fileInfo.SavedInVersion); 

 该方法在Revit 2020下已失效。

 

以下这是在谷歌找到的模型版本信息获取方式,理论山兼容所有版本,亲测可用

  private string ReadFileVersion(string filePath)
        {
            string fileVersion = null;
            var rawData = GetRawBasicFileInfo(filePath);
            var rawString = System.Text.Encoding.Unicode.GetString(rawData);
            var fileInfoData = rawString.Split(new string[] { "\0", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in fileInfoData)
            {
                if (item.StartsWith("Revit Build"))
                {
                    int colonIndex = item.IndexOf("k");
                    if (colonIndex > -1)
                    {
                        fileVersion = item.Substring(colonIndex + 1);
                        int bracketsIndex = fileVersion.IndexOf("(");
                        fileVersion = fileVersion.Substring(0, bracketsIndex);
                    }
                }   
            }
            return fileVersion;
        }
        private const string StreamName = "BasicFileInfo";
        private static byte[] GetRawBasicFileInfo(string revitFileName)
        {
            if (!StructuredStorageUtils.IsFileStucturedStorage(
              revitFileName))
            {
                throw new NotSupportedException(
                  "File is not a structured storage file");
            }

            using (StructuredStorageRoot ssRoot =
                new StructuredStorageRoot(revitFileName))
            {
                if (!ssRoot.BaseRoot.StreamExists(StreamName))
                    throw new NotSupportedException(string.Format(
                      "File doesn't contain {0} stream", StreamName));

                StreamInfo imageStreamInfo =
                    ssRoot.BaseRoot.GetStreamInfo(StreamName);

                using (Stream stream = imageStreamInfo.GetStream(
                  FileMode.Open, FileAccess.Read))
                {
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    return buffer;
                }
            }
        }
        public static class StructuredStorageUtils
        {
            [DllImport("ole32.dll")]
            static extern int StgIsStorageFile(
            [MarshalAs( UnmanagedType.LPWStr )]string pwcsName);

            public static bool IsFileStucturedStorage(
              string fileName)
            {
                int res = StgIsStorageFile(fileName);

                if (res == 0)
                    return true;

                if (res == 1)
                    return false;

                throw new FileNotFoundException(
                  "File not found", fileName);
            }
        }

转自此篇播客:https://thebuildingcoder.typepad.com/blog/2013/01/basic-file-info-and-rvt-file-version.html
posted @ 2017-06-17 00:24  让风继续吹  阅读(1500)  评论(0编辑  收藏  举报