/// <summary> /// Send all the images found in the given storage directory. /// </summary> /// <param name="storageDirectory">Given storage directory - containing storage DCM files.</param> /// <param name="modalityWorklistItem">Worklist Item used to provide overruling values for /// the Image headers.</param> /// <param name="withSingleAssociation">Boolean indicating whether the images should be sent in a single /// association or not.</param> /// <returns>Boolean indicating success or failure.</returns> public bool SendModalityImagesStored(System.String storageDirectory, DicomQueryItem modalityWorklistItem, bool withSingleAssociation) { if ((storageDirectory.Length == 0) || (modalityWorklistItem == null)) { return(false); } // Use a hash table to store the instance uid mappings // - the instance uids for the study, series and sop are going to be updated for all the datasets // read from the storageDirectory Hashtable instanceMapper = new Hashtable(); // Get the directory info for the storage directory - and make sure that it exists DirectoryInfo directoryInfo = new DirectoryInfo(storageDirectory); if (directoryInfo.Exists == false) { System.String message = System.String.Format("storageDirectory:\"{0}\" - does not exist.", storageDirectory); throw new System.Exception(message); } // Get a trigger DicomTrigger trigger = new DicomTrigger(TransactionNameEnum.RAD_8); trigger.HandleInSingleAssociation = withSingleAssociation; // Interate over all the DCM files found in the storage directory // - update the instances found with the worklist item contents and // set up the triggers for the Image Archive foreach (FileInfo fileInfo in directoryInfo.GetFiles()) { if ((fileInfo.Extension.ToLower().Equals(".dcm")) || (fileInfo.Extension == System.String.Empty)) { // Read the file meta information - it must be present for this actor DvtkData.Media.FileMetaInformation fileMetaInformation = Dvtk.DvtkDataHelper.ReadFMIFromFile(fileInfo.FullName); if (fileMetaInformation == null) { continue; } // Try to get the transfer syntax uid // - start with Implicit VR Little Endian System.String transferSyntaxUid = "1.2.840.10008.1.2"; DvtkData.Dimse.Attribute attribute = fileMetaInformation.GetAttribute(DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID); if (attribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { transferSyntaxUid = uniqueIdentifier.Values[0]; } } // Read the dataset from the DCM file DvtkData.Dimse.DataSet dataset = Dvtk.DvtkDataHelper.ReadDataSetFromFile(fileInfo.FullName); if (dataset == null) { continue; } // Remove any Group Lengths from the dataset dataset.RemoveGroupLengthAttributes(); // Try to get the series instance uid System.String oldSeriesInstanceUid = System.String.Empty; DvtkData.Dimse.Attribute seriesInstanceUidAttribute = dataset.GetAttribute(Tag.SERIES_INSTANCE_UID); if (seriesInstanceUidAttribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)seriesInstanceUidAttribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { oldSeriesInstanceUid = uniqueIdentifier.Values[0]; } // Remove the old series instance from the dataset dataset.Remove(seriesInstanceUidAttribute); } // See if a mapping exists System.String newSeriesInstanceUid = (System.String)instanceMapper[oldSeriesInstanceUid]; if (newSeriesInstanceUid == null) { // Add a mapping DefaultValueManager.UpdateInstantiatedDefaultTagValues(AffectedEntityEnum.SeriesEntity); newSeriesInstanceUid = DefaultValueManager.GetInstantiatedValue(Tag.SERIES_INSTANCE_UID); instanceMapper.Add(oldSeriesInstanceUid, newSeriesInstanceUid); } // Add the new series instance uid to the dataset dataset.AddAttribute(Tag.SERIES_INSTANCE_UID.GroupNumber, Tag.SERIES_INSTANCE_UID.ElementNumber, VR.UI, newSeriesInstanceUid); // Try to get the sop instance uid System.String oldSopInstanceUid = System.String.Empty; DvtkData.Dimse.Attribute sopInstanceUidAttribute = dataset.GetAttribute(Tag.SOP_INSTANCE_UID); if (sopInstanceUidAttribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)sopInstanceUidAttribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { oldSopInstanceUid = uniqueIdentifier.Values[0]; } // Remove the old sop instance from the dataset dataset.Remove(sopInstanceUidAttribute); } // See if a mapping exists System.String newSopInstanceUid = (System.String)instanceMapper[oldSopInstanceUid]; if (newSopInstanceUid == null) { // Add a mapping DefaultValueManager.UpdateInstantiatedDefaultTagValues(AffectedEntityEnum.InstanceEntity); newSopInstanceUid = DefaultValueManager.GetInstantiatedValue(Tag.SOP_INSTANCE_UID); instanceMapper.Add(oldSopInstanceUid, newSopInstanceUid); } // Add the new sop instance uid to the dataset dataset.AddAttribute(Tag.SOP_INSTANCE_UID.GroupNumber, Tag.SOP_INSTANCE_UID.ElementNumber, VR.UI, newSopInstanceUid); // Use modality Worklist Item to help construct the Storage Message // Get the attribute values to copy DvtkHighLevelInterface.Comparator.Comparator worklistItemComparator = new DvtkHighLevelInterface.Comparator.Comparator("WorklistItemComparator"); DicomComparator dicomWorklistItemComparator = worklistItemComparator.InitializeDicomComparator(modalityWorklistItem.DicomMessage); // Also try to use the MPPS InProgress Message to help construct the Storage Message DvtkHighLevelInterface.Comparator.Comparator mppsInProgressComparator = new DvtkHighLevelInterface.Comparator.Comparator("MppsInProgressComparator"); DicomComparator dicomMppsInProgressComparator = null; if (_nCreateSetMppsInProgress != null) { dicomMppsInProgressComparator = mppsInProgressComparator.InitializeDicomComparator(_nCreateSetMppsInProgress); } // Create the Storage message DvtkHighLevelInterface.Comparator.Comparator storageComparator = new DvtkHighLevelInterface.Comparator.Comparator("StorageComparator"); DvtkHighLevelInterface.Dicom.Messages.DicomMessage cStoreInstance = new DvtkHighLevelInterface.Dicom.Messages.DicomMessage(DvtkData.Dimse.DimseCommand.CSTORERQ); GenerateTriggers.MakeCStoreInstance(DefaultValueManager, cStoreInstance, dataset); storageComparator.PopulateDicomMessage(cStoreInstance, dicomWorklistItemComparator); if (dicomMppsInProgressComparator != null) { storageComparator.PopulateDicomMessage(cStoreInstance, dicomMppsInProgressComparator); } // Try to get the sop class uid System.String sopClassUid = System.String.Empty; DvtkData.Dimse.Attribute sopClassUidAttribute = dataset.GetAttribute(Tag.SOP_CLASS_UID); if (sopClassUidAttribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)sopClassUidAttribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { sopClassUid = uniqueIdentifier.Values[0]; } } // Add trigger item to the trigger trigger.AddItem(cStoreInstance, sopClassUid, transferSyntaxUid); } } // RAD-8 - trigger the ImageArchive return(TriggerActorInstances(ActorTypeEnum.ImageArchive, trigger, true)); }
private static void AddTagsToDataset(TagValueCollection tags, DvtkData.Dimse.DataSet dataset) { // iterate over the tags foreach (DicomTagValue tag in tags) { if (tag.ParentSequenceTag != Tag.UNDEFINED) { // try to get the sequence tag in the dataset DvtkData.Dimse.Attribute sequenceAttribute = dataset.GetAttribute(tag.ParentSequenceTag); if ((sequenceAttribute != null) && (sequenceAttribute.ValueRepresentation == DvtkData.Dimse.VR.SQ)) { SequenceOfItems sequenceOfItems = (SequenceOfItems)sequenceAttribute.DicomValue; if (sequenceOfItems.Sequence.Count == 1) { SequenceItem item = sequenceOfItems.Sequence[0]; if (item != null) { VR vr = VR.UN; // try to get the attribute in the item DvtkData.Dimse.Attribute attribute = item.GetAttribute(tag.Tag); if (attribute != null) { vr = attribute.ValueRepresentation; item.Remove(attribute); } // add the query value item.AddAttribute(tag.Tag.GroupNumber, tag.Tag.ElementNumber, vr, tag.Value); } } } } else { VR vr = VR.UN; // try to get the attribute in the dataset DvtkData.Dimse.Attribute attribute = dataset.GetAttribute(tag.Tag); if (attribute != null) { vr = attribute.ValueRepresentation; dataset.Remove(attribute); } // special check for the SPECIFIC CHARACTER SET attribute if (tag.Tag == Tag.SPECIFIC_CHARACTER_SET) { vr = VR.CS; } // add the query value dataset.AddAttribute(tag.Tag.GroupNumber, tag.Tag.ElementNumber, vr, tag.Value); } } }
private bool CopyToDicomMessage(DvtkData.Dimse.DicomMessage dicomMessage, DicomComparator sourceComparator) { bool messagePopulated = true; // Check if both templates have been initialized correctly if ((this.Template == null) || (sourceComparator.Template == null)) { return(false); } // Iterate over this comparator foreach (DicomComparisonTag thisComparisonTag in this.Template.ComparisonTags) { // try to get the equivalent tag in the sourceComparator DicomComparisonTag sourceComparisonTag = sourceComparator.Template.ComparisonTags.Find(thisComparisonTag.Tag); if (sourceComparisonTag != null) { System.String stringValue = sourceComparisonTag.DataFormat.ToDicomFormat(); DvtkData.Dimse.DataSet dataset = dicomMessage.DataSet; if (dataset != null) { // we need to see if the parent sequence has been set up in the dataset if (thisComparisonTag.ParentSequenceTag != Tag.UNDEFINED) { // set up the parent sequence and add it to the dataset SequenceOfItems sequenceOfItems = null; DvtkData.Dimse.Attribute sequenceAttribute = dataset.GetAttribute(thisComparisonTag.ParentSequenceTag); if (sequenceAttribute == null) { // add in an empty item DvtkData.Dimse.SequenceItem item = new SequenceItem(); dataset.AddAttribute(thisComparisonTag.ParentSequenceTag.GroupNumber, thisComparisonTag.ParentSequenceTag.ElementNumber, VR.SQ, item); sequenceAttribute = dataset.GetAttribute(thisComparisonTag.ParentSequenceTag); } // get the sequence item and add in the required attribute sequenceOfItems = (SequenceOfItems)sequenceAttribute.DicomValue; if (sequenceOfItems.Sequence.Count == 1) { DvtkData.Dimse.SequenceItem item = sequenceOfItems.Sequence[0]; if (item != null) { // add the attribute to the item if (sourceComparisonTag.Vr == VR.SQ) { // add in an empty item // TODO - fix this properly DvtkData.Dimse.SequenceItem item1 = new SequenceItem(); item.AddAttribute(sourceComparisonTag.Tag.GroupNumber, sourceComparisonTag.Tag.ElementNumber, VR.SQ, item1); } else { // if the attribute already exists - then we need to remove it // - it was probably set to the default value DvtkData.Dimse.Attribute attribute = item.GetAttribute(sourceComparisonTag.Tag); if (attribute != null) { item.Remove(attribute); } // add the attribute to the item item.AddAttribute(sourceComparisonTag.Tag.GroupNumber, sourceComparisonTag.Tag.ElementNumber, sourceComparisonTag.Vr, stringValue); } } } } else { // if the attribute already exists - then we need to remove it // - it was probably set to the default value DvtkData.Dimse.Attribute attribute = dataset.GetAttribute(sourceComparisonTag.Tag); if (attribute != null) { dataset.Remove(attribute); } // add the attribute at the top level dataset.AddAttribute(sourceComparisonTag.Tag.GroupNumber, sourceComparisonTag.Tag.ElementNumber, sourceComparisonTag.Vr, stringValue); } } } } return(messagePopulated); }