Example #1
0
        private DicomNGetResponse GetPrinter(DicomNGetRequest request)
        {
            var ds = new DicomDataset();

            var sb = new StringBuilder();

            if (request.Attributes != null && request.Attributes.Length > 0)
            {
                foreach (var item in request.Attributes)
                {
                    sb.AppendFormat("GetPrinter attribute {0} requested", item);
                    sb.AppendLine();
                    var value = Printer.GetSingleValueOrDefault(item, "");
                    ds.Add(item, value);
                }

                Logger.Info(sb.ToString());
            }
            if (!ds.Any())
            {
                ds.Add(DicomTag.PrinterStatus, Printer.PrinterStatus);
                ds.Add(DicomTag.PrinterStatusInfo, "");
                ds.Add(DicomTag.PrinterName, Printer.PrinterName);
                ds.Add(DicomTag.Manufacturer, Printer.Manufacturer);
                ds.Add(DicomTag.DateOfLastCalibration, Printer.DateTimeOfLastCalibration.Date);
                ds.Add(DicomTag.TimeOfLastCalibration, Printer.DateTimeOfLastCalibration);
                ds.Add(DicomTag.ManufacturerModelName, Printer.ManufacturerModelName);
                ds.Add(DicomTag.DeviceSerialNumber, Printer.DeviceSerialNumber);
                ds.Add(DicomTag.SoftwareVersions, Printer.SoftwareVersions);
            }

            var response = new DicomNGetResponse(request, DicomStatus.Success)
            {
                Dataset = ds
            };

            Logger.Info(response.ToString(true));
            return(response);
        }
        /// <summary>
        /// Returns a basic type (string, double, int array, dictionary etc) for the given <paramref name="item"/> in the <paramref name="dataset"/>.
        /// </summary>
        /// <param name="dataset"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static object GetCSharpValue(DicomDataset dataset, DicomItem item)
        {
            if (dataset == null || !dataset.Any())
            {
                throw new ArgumentException("The DicomDataset is invalid as it is null or has no elements.");
            }

            if (item == null || item.Tag == null || item.ValueRepresentation == null)
            {
                throw new ArgumentException("The DicomItem is invalid as it is either null, has a null Tag, or null ValueRepresentation: " + item);
            }

            if (!dataset.Contains(item))
            {
                throw new ArgumentException("The DicomDataset does not contain the item");
            }

            if (item.Tag == DicomTag.PixelData)
            {
                return(null);
            }

            switch (item.ValueRepresentation.Code)
            {
            // AE - Application Entity
            case "AE":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // AS - Age String
            case "AS":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // AT - Attribute Tag
            case "AT":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // CS - Code String
            case "CS":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // DA - Date
            case "DA":
                return(GetValueFromDatasetWithMultiplicity <DateTime>(dataset, item.Tag));

            // DS - Decimal String
            case "DS":
                return(GetValueFromDatasetWithMultiplicity <decimal>(dataset, item.Tag));

            // DT - Date Time
            case "DT":
                return(GetValueFromDatasetWithMultiplicity <DateTime>(dataset, item.Tag));

            // FL - Floating Point Single
            case "FL":
                return(GetValueFromDatasetWithMultiplicity <float>(dataset, item.Tag));

            // FD - Floating Point Double
            case "FD":
                return(GetValueFromDatasetWithMultiplicity <double>(dataset, item.Tag));

            // IS - Integer String
            case "IS":
                return(GetValueFromDatasetWithMultiplicity <int>(dataset, item.Tag));

            // LO - Long String
            case "LO":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // LT - Long Text
            case "LT":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // OB - Other Byte String
            case "OB":
                return(GetValueFromDatasetWithMultiplicity <byte>(dataset, item.Tag));

            // OD - Other Double String
            case "OD":
                return(GetValueFromDatasetWithMultiplicity <double>(dataset, item.Tag));

            // OF - Other Float String
            case "OF":
                return(GetValueFromDatasetWithMultiplicity <float>(dataset, item.Tag));

            // OL - Other Long
            case "OL":
                return(GetValueFromDatasetWithMultiplicity <uint>(dataset, item.Tag));

            // OW - Other Word String
            case "OW":
                return(GetValueFromDatasetWithMultiplicity <ushort>(dataset, item.Tag));

            // PN - Person Name
            case "PN":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // SH - Short String
            case "SH":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // SL - Signed Long
            case "SL":
                return(GetValueFromDatasetWithMultiplicity <int>(dataset, item.Tag));

            // SQ - Sequence
            case "SQ":
                return(GetSequenceFromDataset(dataset, item.Tag));

            // SS - Signed Short
            case "SS":
                return(GetValueFromDatasetWithMultiplicity <short>(dataset, item.Tag));

            // ST - Short Text
            case "ST":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // TM - Time
            case "TM":

                var tm = GetValueFromDatasetWithMultiplicity <DateTime>(dataset, item.Tag);

                // Need to handle case where we couldn't parse to DateTime so returned string instead
                return(tm is DateTime
                            ? ConvertToTimeSpanArray(tm)
                            : tm);

            // UC - Unlimited Characters
            case "UC":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // UI - Unique Identifier
            case "UI":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // UL - Unsigned Long
            case "UL":
                return(GetValueFromDatasetWithMultiplicity <uint>(dataset, item.Tag));

            // UN - Unknown
            case "UN":
                return(GetValueFromDatasetWithMultiplicity <byte>(dataset, item.Tag));

            // UR - URL
            case "UR":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // US - Unsigned Short
            case "US":
                return(GetValueFromDatasetWithMultiplicity <ushort>(dataset, item.Tag));

            // UT - Unlimited Text
            case "UT":
                return(GetValueFromDatasetWithMultiplicity <string>(dataset, item.Tag));

            // NONE
            case "NONE":
                return(GetValueFromDatasetWithMultiplicity <object>(dataset, item.Tag));

            default:
                //return GetValueFromDatasetWithMultiplicity<object>(dataset, item.Tag);
                throw new Exception("Unknown VR code: " +
                                    item.ValueRepresentation.Code +
                                    "(" + item.ValueRepresentation.Name + ")");
            }
        }
        public static IEnumerable <string> Compare(DicomDataset a, DicomDataset b, bool ignoreTrailingNull = false)
        {
            if (a == null || b == null)
            {
                throw new ArgumentException("Dataset " + (a == null ? "A" : "B") + " was null");
            }

            var differences = new List <string>();

            if (!a.Any())
            {
                if (!b.Any())
                {
                    return(differences);
                }

                differences.Add("A contained no elements, but B did");
                return(differences);
            }

            if (!b.Any())
            {
                differences.Add("B contained no elements, but A did");
                return(differences);
            }

            if (a.Count() != b.Count())
            {
                differences.Add("A and B did not contain the same number of elements");
            }

            foreach (DicomItem item in a)
            {
                if (!b.Contains(item.Tag))
                {
                    differences.Add($"B did not contain tag {item.Tag} {item.Tag.DictionaryEntry.Keyword} from A");
                    continue;
                }

                if (item.ValueRepresentation.IsString)
                {
                    string before = a.GetString(item.Tag);
                    string after  = b.GetString(item.Tag);

                    if (string.Equals(before, after))
                    {
                        continue;
                    }

                    if (ignoreTrailingNull && Math.Abs(before.Length - after.Length) == 1)
                    {
                        string longest = before.Length > after.Length ? before : after;

                        // Check for a single trailing NUL character (int value == 0)
                        if (longest[longest.Length - 1] == 0)
                        {
                            continue;
                        }
                    }

                    differences.Add(string.Format("Tag {0} {1} {2} had value \"{3}\" in A and \"{4}\" in B",
                                                  item.Tag, item.ValueRepresentation, item.Tag.DictionaryEntry.Keyword, before, after));
                }
                else if (item.ValueRepresentation == DicomVR.SQ)
                {
                    DicomSequence seqA = a.GetSequence(item.Tag);
                    DicomSequence seqB = b.GetSequence(item.Tag);

                    if (seqA.Count() != seqB.Count())
                    {
                        differences.Add(string.Format("Sequence of tag {0} {1} had {2} elements in A, but {3} in B",
                                                      item.Tag, item.Tag.DictionaryEntry.Keyword, seqA.Count(), seqB.Count()));
                        continue;
                    }

                    for (var i = 0; i < seqA.Count(); ++i)
                    {
                        differences.AddRange(Compare(seqA.Items[i], seqB.Items[i]));
                    }
                }
                else
                {
                    object[] valA = a.GetValues <object>(item.Tag);
                    object[] valB = b.GetValues <object>(item.Tag);

                    if (!(valA.Any() || valB.Any()))
                    {
                        continue;
                    }

                    if (valA.Length != valB.Length)
                    {
                        differences.Add(string.Format("Tag {0} {1} {2} had {3} values in A and {4} values in B",
                                                      item.Tag, item.ValueRepresentation, item.Tag.DictionaryEntry.Keyword, valA.Length, valB.Length));
                    }

                    List <object> diffs = valA.Except(valB).ToList();

                    if (!diffs.Any())
                    {
                        continue;
                    }

                    differences.Add("\tDifferent values were: " + string.Join(", ", diffs));
                }
            }

            return(differences);
        }