/// <summary>
        /// Reads BasicFileInfo from a Revit file.
        /// </summary>
        /// <param name="pathToFile">The path to Revit file.</param>
        /// <returns><see cref="RevitFileVersionInfo"/> object</returns>
        public static RevitFileVersionInfo ReadFromFile(string pathToFile)
        {
            var content = OleDataReader.GetRawString(pathToFile, nameof(RevitFileVersionInfo), Encoding.Unicode);
            var match   = RevitBuildRegex.Match(content);

            return(new RevitFileVersionInfo(match.Groups["version"]?.ToString()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a <see cref="RevitFileInfo" /> instance from the given file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="readProperties">if set to <c>true</c> [read properties].</param>
        /// <param name="readTypes">if set to <c>true</c> [read types].</param>
        /// <returns></returns>
        /// <exception cref="T:System.ArgumentException">filePath is invalid.</exception>
        public static RevitFileInfo GetFromFile(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
            {
                throw new ArgumentException("Please supply a valid path to a Revit document", nameof(filePath));
            }

            var rfi = new RevitFileInfo
            {
                FilePath = filePath
            };

            // Read basic file info from metadata
            try
            {
                var basicInfo  = OleDataReader.GetRawBytes(filePath, RevitFileMap.OleStreams.BASIC_FILE_INFO);
                var properties = GetProperties(basicInfo);
                rfi.ParseDocumentInfo(properties);
                rfi.ParseLocale(properties);
                rfi.ParseRevit(properties);
                rfi.ParseUsername(properties);
            }
            catch (Exception)
            {
                // TODO log the error
            }

            // Read family types from part atom
            try
            {
                rfi.PartAtom = PartAtom.GetFromFile(filePath);
                if (rfi.Format <= 0)
                {
                    rfi.Format = rfi.PartAtom?.Link?.Files?.FirstOrDefault()?.ProductVersion ?? -1;
                }
            }
            catch (Exception)
            {
                // TODO log the error
            }

            return(rfi);
        }
Ejemplo n.º 3
0
        public static int GetFormatFromFile(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
            {
                throw new ArgumentException("Please supply a valid path to a Revit document", nameof(filePath));
            }

            try
            {
                var basicInfo           = OleDataReader.GetRawBytes(filePath, RevitFileMap.OleStreams.BASIC_FILE_INFO);
                var properties          = GetProperties(basicInfo);
                var formatFromBasicInfo = properties.GetFormat();
                if (formatFromBasicInfo > 0)
                {
                    return(formatFromBasicInfo);
                }
            }
            catch
            {
                // TODO ad logging
                // Trying to read
            }

            try
            {
                var partAtom           = PartAtom.GetFromFile(filePath);
                var formatFromPartAtom = partAtom?.Link?.Files?.FirstOrDefault()?.ProductVersion;
                if (formatFromPartAtom != null)
                {
                    return(formatFromPartAtom.GetValueOrDefault());
                }
            }
            catch (Exception ex)
            {
                throw new InvalidDataException($"Couldn't read format information from the followin file: '{filePath}'", ex);
            }

            return(-1);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the properties.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        internal static Dictionary <string, string> GetProperties(string filePath)
        {
            var basicInfo = OleDataReader.GetRawBytes(filePath, RevitFileMap.OleStreams.BASIC_FILE_INFO);
            var fileProps = new Dictionary <string, string>();

            var asd = new Regex(@"\p{IsCJKUnifiedIdeographs}");

            foreach (var encoding in RevitFileMap.SupportedEncodings)
            {
                var basicInfoString = encoding.GetString(basicInfo)?.TrimEnd('\u0a0d');
                if (asd.IsMatch(basicInfoString))
                {
                    continue;
                }

                using var basicInfoReader = new StringReader(basicInfoString?.Replace("\0", string.Empty));

                // ReSharper disable once RedundantAssignment
                var stringLine = basicInfoReader.ReadLine(); // skip the first line
                while (!string.IsNullOrWhiteSpace(stringLine = basicInfoReader.ReadLine()))
                {
                    var parts = stringLine.Split(new[] { ":" }, StringSplitOptions.None);
                    if (parts.Length != 2 || IsInvalidProperty(parts.FirstOrDefault()))
                    {
                        continue;
                    }
                    fileProps.Add(parts[0].Trim(), parts[1].Trim());
                }

                if (fileProps.Any())
                {
                    break;
                }
            }

            return(fileProps);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Extracts the stream.
        /// </summary>
        /// <param name="pathToFile">The path to file.</param>
        /// <returns></returns>
        /// <exception cref="InvalidDataException"></exception>
        public override MemoryStream ExtractStream(string pathToFile)
        {
            try
            {
                var thumbnailBytes = OleDataReader.GetRawBytes(pathToFile, RevitFileMap.OleStorage.IMAGE_STREAM);
                // Validate preview data or go out
                if ((thumbnailBytes == null) || (thumbnailBytes.Length <= 0))
                {
                    return(null);
                }

                // read past the Revit meta-data to the start of the PNG image
                var startingOffset = GetPngOffset(thumbnailBytes);
                if (startingOffset == 0)
                {
                    return(null);
                }

                var previewUpperBound = thumbnailBytes.GetUpperBound(0);
                var pngDataBuffer     = new byte[previewUpperBound - startingOffset + 1];

                using (var ms = new MemoryStream(thumbnailBytes))
                {
                    ms.Position = startingOffset;
                    ms.Read(pngDataBuffer, 0, pngDataBuffer.Length);

                    // read the PNG image data into a byte array
                    var outms = new MemoryStream(pngDataBuffer);
                    return(outms);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidDataException($"Failed to extract the thumbnail of the following Revit file \"{pathToFile}\"", ex);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets from file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        public static PartAtom GetFromFile(string filePath)
        {
            var partAtom = OleDataReader.GetData <PartAtom>(filePath, nameof(PartAtom), Encoding.UTF8);

            return(partAtom);
        }