Example #1
0
        public static FilmSize FromDicomString(string dicomString)
        {
            try
            {
                if (string.IsNullOrEmpty(dicomString))
                {
                    return(null);
                }

                if (dicomString == "A3")
                {
                    return new FilmSize
                           {
                               _dicomString = dicomString,
                               _sizeUnit    = FilmSizeUnit.Centimeter,
                               _width       = 29.7f,
                               _height      = 42.0f
                           }
                }
                ;

                if (dicomString == "A4")
                {
                    return new FilmSize
                           {
                               _dicomString = dicomString,
                               _sizeUnit    = FilmSizeUnit.Centimeter,
                               _width       = 21.0f,
                               _height      = 29.7f
                           }
                }
                ;

                var indexOfX = dicomString.IndexOf('X');
                if (indexOfX < 3)                 // at least one char for width, and 2 chars for units
                {
                    throw new InvalidFilmSizeException(dicomString);
                }

                var firstUnit  = dicomString.Substring(indexOfX - 2, 2);
                var secondUnit = dicomString.Substring(dicomString.Length - 2);
                if (firstUnit != "CM" && firstUnit != "IN")
                {
                    throw new InvalidFilmSizeException(dicomString);
                }
                if (firstUnit != secondUnit)
                {
                    throw new InvalidFilmSizeException(dicomString);
                }

                var xSeparatedDimension = dicomString.Replace("IN", "").Replace("CM", "").Replace('_', '.');
                var dimensions          = StringUtilities.SplitQuoted(xSeparatedDimension, "X");

                var filmSize = new FilmSize
                {
                    _dicomString = dicomString,
                    _sizeUnit    = firstUnit == "IN" ? FilmSizeUnit.Inch : FilmSizeUnit.Centimeter,
                    _width       = float.Parse(dimensions[0]),
                    _height      = float.Parse(dimensions[1]),
                };
                return(filmSize);
            }
            catch (NotSupportedException)
            {
                throw;
            }
            catch (InvalidFilmSizeException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new InvalidFilmSizeException(dicomString, e);
            }
        }