/// <summary> /// Applies user DICOM tag replacement rules to a dataset /// </summary> /// <param name="dicomDataSet">The dicom dataset.</param> /// <param name="replacement">The replacement.</param> public static void ApplyUserReplacement(DicomDataset dicomDataSet, TagReplacement replacement) { dicomDataSet = dicomDataSet ?? throw new ArgumentNullException(nameof(dicomDataSet)); replacement = replacement ?? throw new ArgumentNullException(nameof(replacement)); // We must do contains otherwise if the tag is present but empty it will return null var tag = replacement.DicomTagIndex.DicomTag; if (dicomDataSet.Contains(tag)) { var sourceTagValue = dicomDataSet.GetSingleValueOrDefault(tag, string.Empty); if (replacement.Operation == TagReplacementOperation.UpdateIfExists) { dicomDataSet.AddOrUpdate(tag, replacement.Value); } else if (replacement.Operation == TagReplacementOperation.AppendIfExists) { dicomDataSet.AddOrUpdate(tag, $"{sourceTagValue}{replacement.Value}"); } else { throw new InvalidOperationException(nameof(replacement)); } } }
/// <summary> /// Replaces the user tag. /// </summary> /// <param name="dicomDatasetDeAnonymized">The dicom dataset de anonymized.</param> /// <param name="tagReplacement">The tag replacement.</param> public static void ReplaceUserTag( DicomDataset dicomDatasetDeAnonymized, TagReplacement tagReplacement) { ApplyUserReplacement(dicomDatasetDeAnonymized, tagReplacement); // Handle sequences recursive foreach (var item in dicomDatasetDeAnonymized) { if (item is DicomSequence dicomSequence) { foreach (var dataset in dicomSequence.Items) { ReplaceUserTag(dataset, tagReplacement); } } } }