Advertisement
chekalin-v

Revit Basic File Info

Jan 23rd, 2013
3,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.01 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Packaging;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         private const string pathToRevitFile =
  12.             @"pathToRevitFile";
  13.  
  14.         private const string StreamName = "BasicFileInfo";
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             if (!StructuredStorageUtils.IsFileStucturedStorage(pathToRevitFile))
  19.                 throw new NotSupportedException("File is not a structured storage file");
  20.  
  21.             var rawData = GetRawBasicFileInfo(pathToRevitFile);
  22.  
  23.         BasicFileInfo basicFileInfo = new BasicFileInfo();
  24.  
  25.             using (var ms = new MemoryStream(rawData))
  26.             {
  27.                 using (var br = new BinaryReader(ms, System.Text.Encoding.Unicode))
  28.                 {
  29.                     basicFileInfo.A = br.ReadInt32();
  30.                     basicFileInfo.B = br.ReadInt32();
  31.                     basicFileInfo.C = br.ReadInt16();
  32.  
  33.                     var sLen = br.ReadInt32();
  34.  
  35.                     basicFileInfo.Path1
  36.                         = System.Text.Encoding.Unicode.GetString(br.ReadBytes(sLen * 2));
  37.  
  38.                     sLen = br.ReadInt32();
  39.                     basicFileInfo.Version = System.Text.Encoding.Unicode.GetString(br.ReadBytes(sLen * 2));
  40.  
  41.                     sLen = br.ReadInt32();
  42.                     basicFileInfo.Path2 = System.Text.Encoding.Unicode.GetString(br.ReadBytes(sLen * 2));
  43.                    
  44.                    
  45.                     basicFileInfo.Unknown = br.ReadBytes(5);
  46.  
  47.                     sLen = br.ReadInt32();
  48.                     basicFileInfo.UID = System.Text.Encoding.Unicode.GetString(br.ReadBytes(sLen * 2));
  49.                     sLen = br.ReadInt32();
  50.                     basicFileInfo.Localization = System.Text.Encoding.Unicode.GetString(br.ReadBytes(sLen * 2));
  51.  
  52.                    
  53.  
  54.                     //read to end
  55.  
  56.                     br.ReadBytes(2); // \r \n
  57.                     sLen = (int) (br.BaseStream.Length - br.BaseStream.Position) - 2;
  58.  
  59.                     var buffer =
  60.                         br.ReadBytes(sLen);
  61.  
  62.                     basicFileInfo.Data = Encoding.Unicode.GetString(buffer);
  63.  
  64.                     br.ReadBytes(2); // \r \n
  65.                 }
  66.             }
  67.  
  68.             var rawString = System.Text.Encoding.Unicode.GetString(rawData);
  69.  
  70.             var fileInfoData = rawString.Split(new string[] { "\0", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  71.  
  72.             foreach (var info in fileInfoData)
  73.             {
  74.                 Console.WriteLine(info);
  75.             }
  76.         }
  77.  
  78.         private static byte[] GetRawBasicFileInfo(string revitFileName)
  79.         {
  80.             if (!StructuredStorageUtils.IsFileStucturedStorage(revitFileName))
  81.                 throw new NotSupportedException("File is not a structured storage file");
  82.  
  83.  
  84.             using (StructuredStorageRoot ssRoot =
  85.                 new StructuredStorageRoot(pathToRevitFile))
  86.             {
  87.                 if (!ssRoot.BaseRoot.StreamExists(StreamName))
  88.                     throw new NotSupportedException(string.Format("File doesn't contain {0} stream", StreamName));
  89.  
  90.                 StreamInfo imageStreamInfo =
  91.                     ssRoot.BaseRoot.GetStreamInfo(StreamName);
  92.                 using (Stream stream = imageStreamInfo.GetStream(FileMode.Open, FileAccess.Read))
  93.                 {
  94.                     byte[] buffer = new byte[stream.Length];
  95.                     stream.Read(buffer, 0, buffer.Length);
  96.                     return buffer;
  97.                 }
  98.             }
  99.         }
  100.            
  101.     }
  102.  
  103.     public static class StructuredStorageUtils
  104.     {
  105.         [DllImport("ole32.dll")]
  106.         static extern int StgIsStorageFile(
  107.         [MarshalAs(UnmanagedType.LPWStr)]
  108.             string pwcsName);
  109.  
  110.         public static bool IsFileStucturedStorage(string fileName)
  111.         {
  112.             int res = StgIsStorageFile(fileName);
  113.             if (res == 0)
  114.                 return true;
  115.             if (res == 1)
  116.                 return false;
  117.             throw new FileNotFoundException("File not found", fileName);
  118.         }
  119.     }
  120.  
  121.     public class StructuredStorageException : Exception
  122.     {
  123.         public StructuredStorageException()
  124.         {
  125.  
  126.         }
  127.  
  128.         public StructuredStorageException(string message)
  129.             : base(message)
  130.         {
  131.  
  132.         }
  133.  
  134.         public StructuredStorageException(string message, Exception innerException)
  135.             : base(message, innerException)
  136.         {
  137.  
  138.         }
  139.     }
  140.  
  141.     public class StructuredStorageRoot : IDisposable
  142.     {
  143.         StorageInfo _storageRoot;
  144.  
  145.         public StructuredStorageRoot(Stream stream)
  146.         {
  147.  
  148.             try
  149.             {
  150.                 _storageRoot = (StorageInfo)InvokeStorageRootMethod(null,
  151.                                                                "CreateOnStream",
  152.                                                                stream);
  153.             }
  154.             catch (Exception ex)
  155.             {
  156.  
  157.                 throw new StructuredStorageException("Cannot get StructuredStorageRoot", ex);
  158.             }
  159.  
  160.         }
  161.  
  162.         public StructuredStorageRoot(string fileName)
  163.         {
  164.             try
  165.             {
  166.                 _storageRoot = (StorageInfo)InvokeStorageRootMethod(null,
  167.                     "Open", fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  168.             }
  169.             catch (Exception ex)
  170.             {
  171.  
  172.                 throw new StructuredStorageException("Cannot get StructuredStorageRoot", ex);
  173.             }
  174.  
  175.         }
  176.  
  177.         private static object InvokeStorageRootMethod(StorageInfo storageRoot, string methodName, params object[] methodArgs)
  178.         {
  179.             Type storageRootType = typeof(StorageInfo).Assembly.GetType("System.IO.Packaging.StorageRoot", true, false);
  180.             object result = storageRootType.InvokeMember(methodName,
  181.                 BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
  182.                 null, storageRoot, methodArgs);
  183.             return result;
  184.         }
  185.  
  186.         private void CloseStorageRoot()
  187.         {
  188.             InvokeStorageRootMethod(_storageRoot, "Close");
  189.         }
  190.  
  191.         #region Implementation of IDisposable
  192.  
  193.         public void Dispose()
  194.         {
  195.             CloseStorageRoot();
  196.         }
  197.  
  198.         #endregion
  199.  
  200.         public StorageInfo BaseRoot
  201.         {
  202.             get { return _storageRoot; }
  203.         }
  204.  
  205.     }
  206.  
  207.     public struct BasicFileInfo
  208.     {
  209.         public Int32 A { get; set; }
  210.         public Int32 B { get; set; }
  211.         public Int16 C { get; set; }
  212.         public string Path1 { get; set; }
  213.         public string Version { get; set; }
  214.         public string Path2 { get; set; }
  215.         public string Data { get; set; }
  216.         public byte[] Unknown { get; set; }
  217.         public string UID { get; set; }
  218.         public string Localization { get; set; }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement