55using System . IO ;
66using System . Linq ;
77using System . Text ;
8+ using System . Text . RegularExpressions ;
9+ using static CodeCave . Revit . Toolkit . RevitFileInfoExtensions ;
810
9- namespace CodeCave . Revit . Toolkit
11+ namespace CodeCave . Revit . Toolkit . OLE
1012{
1113 public class RevitFileInfo
1214 {
15+ static RevitFileInfo ( )
16+ {
17+ #if ! NET45
18+ Encoding . RegisterProvider ( CodePagesEncodingProvider . Instance ) ;
19+ #endif
20+ }
21+
1322 #region Properties
1423
1524 /// <summary>
@@ -58,22 +67,34 @@ public class RevitFileInfo
5867 /// </value>
5968 public FamilyType [ ] Types => PartAtom ? . Family ? . Parts ;
6069
61- public Version Version { get ; internal set ; }
70+ public string ProductVendor { get ; internal set ; } = "Autodesk" ;
6271
63- public string Name { get ; internal set ; }
72+ public string ProductVersion { get ; internal set ; }
6473
65- public string Vendor { get ; internal set ; }
74+ public string ProductName { get ; internal set ; } = "Revit" ;
6675
67- public object Architecture { get ; internal set ; }
76+ public int Format { get ; internal set ; }
6877
69- public bool Is64Bit { get ; internal set ; }
78+ public bool Is64Bit { get ; internal set ; } = true ;
7079
7180 public string FilePath { get ; private set ; }
7281
7382 #endregion
7483
7584 #region Methods
7685
86+ public static int GetFormatFromFile ( string filePath )
87+ {
88+ if ( string . IsNullOrWhiteSpace ( filePath ) || ! File . Exists ( filePath ) )
89+ throw new ArgumentException ( "Please supply a valid path to a Revit document" , nameof ( filePath ) ) ;
90+
91+ var properties = GetProperties ( filePath ) ;
92+ if ( properties . TryGetValue ( KnownRevitInfoProps . FORMAT , out var formatRaw ) && int . TryParse ( formatRaw , out var format ) )
93+ return format ;
94+
95+ throw new InvalidDataException ( $ "Couldn't read format information from the followin file: '{ filePath } '") ;
96+ }
97+
7798 /// <summary>
7899 /// Gets a <see cref="RevitFileInfo" /> instance from the given file path.
79100 /// </summary>
@@ -84,8 +105,8 @@ public class RevitFileInfo
84105 /// <exception cref="T:System.ArgumentException">filePath is invalid</exception>
85106 public static RevitFileInfo GetFromFile ( string filePath )
86107 {
87- if ( string . IsNullOrWhiteSpace ( filePath ) )
88- throw new ArgumentException ( "filePath is invalid" ) ;
108+ if ( string . IsNullOrWhiteSpace ( filePath ) || ! File . Exists ( filePath ) )
109+ throw new ArgumentException ( "Please supply a valid path to a Revit document" , nameof ( filePath ) ) ;
89110
90111 var rfi = new RevitFileInfo
91112 {
@@ -130,28 +151,25 @@ public static RevitFileInfo GetFromFile(string filePath)
130151 /// <returns></returns>
131152 internal static Dictionary < string , string > GetProperties ( string filePath )
132153 {
133- var basicInfo = OleDataReader . GetRawBytes ( filePath , "BasicFileInfo" ) ;
154+ var basicInfo = OleDataReader . GetRawBytes ( filePath , RevitFileMap . OleStreams . BASIC_FILE_INFO ) ;
134155 var fileProps = new Dictionary < string , string > ( ) ;
135- var supportedEncodeings = new [ ]
136- {
137- Encoding . Unicode ,
138- Encoding . BigEndianUnicode ,
139- Encoding . UTF8 ,
140- Encoding . ASCII ,
141- Encoding . Default ,
142- } ;
143156
144- foreach ( var encoding in supportedEncodeings )
157+ var asd = new Regex ( @"\p{IsCJKUnifiedIdeographs}" ) ;
158+
159+ foreach ( var encoding in RevitFileMap . SupportedEncodings )
145160 {
146161 var basicInfoString = encoding . GetString ( basicInfo ) ? . TrimEnd ( '\u0a0d ' ) ;
147- using var basicInfoReader = new StringReader ( basicInfoString ) ;
162+ if ( asd . IsMatch ( basicInfoString ) )
163+ continue ;
164+
165+ using var basicInfoReader = new StringReader ( basicInfoString ? . Replace ( "\0 " , string . Empty ) ) ;
148166
149167 // ReSharper disable once RedundantAssignment
150168 var stringLine = basicInfoReader . ReadLine ( ) ; // skip the first line
151169 while ( ! string . IsNullOrWhiteSpace ( stringLine = basicInfoReader . ReadLine ( ) ) )
152170 {
153- var parts = stringLine . Split ( new [ ] { ":" } , 2 , StringSplitOptions . None ) ;
154- if ( parts . Length != 2 )
171+ var parts = stringLine . Split ( new [ ] { ":" } , StringSplitOptions . None ) ;
172+ if ( parts . Length != 2 || IsInvalidProperty ( parts . FirstOrDefault ( ) ) )
155173 continue ;
156174 fileProps . Add ( parts [ 0 ] . Trim ( ) , parts [ 1 ] . Trim ( ) ) ;
157175 }
@@ -163,6 +181,11 @@ internal static Dictionary<string, string> GetProperties(string filePath)
163181 return fileProps ;
164182 }
165183
184+ private static bool IsInvalidProperty ( string input )
185+ {
186+ return input ? . Any ( c => char . IsControl ( c ) ) ?? true ;
187+ }
188+
166189 #endregion
167190 }
168191}
0 commit comments