Exemplo n.º 1
0
		public static ImageProperty Create(DicomAttribute attribute, string category, string name, string description, string separator)
		{
			// always use the hex value as the identifier, so that private and unknown tags aren't all mapped to the same identifier
			string identifier = attribute.Tag.HexString;

			if (category == null)
				category = string.Empty;

			if (string.IsNullOrEmpty(name))
				name = GetTagName(attribute.Tag);

			if (string.IsNullOrEmpty(description))
				description = GetTagDescription(attribute.Tag);

			if (attribute.IsNull || attribute.IsEmpty)
				return new ImageProperty(identifier, category, name, description, string.Empty);

			if (String.IsNullOrEmpty(separator))
				separator = ", ";

			object value;
			if (attribute.Tag.VR.Name == DicomVr.DAvr.Name)
			{
				value = StringUtilities.Combine(attribute.Values as string[], separator,
				                                delegate(string dateString)
				                                	{
				                                		DateTime? date = DateParser.Parse(dateString);
				                                		if (!date.HasValue)
				                                			return null;
				                                		else
				                                			return Format.Date(date.Value);
				                                	}, true);
			}
			else if (attribute.Tag.VR.Name == DicomVr.TMvr.Name)
			{
				value = StringUtilities.Combine(attribute.Values as string[], separator,
				                                delegate(string timeString)
				                                	{
				                                		DateTime? time = TimeParser.Parse(timeString);
				                                		if (!time.HasValue)
				                                			return null;
				                                		else
				                                			return Format.Time(time.Value);
				                                	}, true);
			}
			else if (attribute.Tag.VR.Name == DicomVr.DTvr.Name)
			{
				value = StringUtilities.Combine(attribute.Values as string[], separator,
				                                delegate(string dateTimeString)
				                                	{
				                                		DateTime? dateTime = DateTimeParser.Parse(dateTimeString);
				                                		if (!dateTime.HasValue)
				                                			return null;
				                                		else
				                                			return Format.Time(dateTime.Value);
				                                	}, true);
			}
			else if (attribute.Tag.VR.Name == DicomVr.PNvr.Name)
			{
				value = StringUtilities.Combine(attribute.Values as string[], separator,
				                                delegate(string nameString)
				                                	{
				                                		PersonName personName = new PersonName(nameString ?? "");
				                                		return personName.FormattedName;
				                                	}, true);
			}
			else if (attribute.Tag.VR == DicomVr.SQvr)
			{
				value = string.Empty;

				var values = attribute.Values as DicomSequenceItem[];
				if (values != null && values.Length > 0)
				{
					// handle simple use case by listing only the attributes of the first sequence item
					// since user can always use DICOM editor for more complex use cases
					var subproperties = new List<IImageProperty>();
					foreach (var subattribute in values[0])
						subproperties.Add(Create(subattribute, string.Empty, null, null, null));
					subproperties.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase));
					value = subproperties.ToArray();
				}
			}
			else if (attribute.Tag.VR.IsTextVR && attribute.GetValueType() == typeof(string[]))
			{
				value = StringUtilities.Combine(attribute.Values as string[], separator, true);
			}
			else
			{
				value = attribute.ToString();
			}

			return new ImageProperty(identifier, category, name, description, value);
		}