/// <summary> /// Store the DICOM file into a DICOM Media File in the current Information Model directory. /// The file contents might be later used for retrieval.The transfer syntax is also passed so that the TS specified /// in the dcm file is used while moving the file to a Storage SCP /// </summary> /// <param name="dicomFile">The DICOM file to store</param> protected void StoreDicomFile(DvtkData.Media.DicomFile dicomFile) { System.String iom = System.String.Empty; if (Name.Equals("PatientRootInformationModel")) { iom = "PR"; } else if (Name.Equals("StudyRootInformationModel")) { iom = "SR"; } else if (Name.Equals("PatientStudyOnlyInformationModel")) { iom = "PS"; } // generate a filename System.String filename; filename = System.String.Format("{0}\\DVTK_IOM_TMP_{1}_{2:00000000}.DCM", DataDirectory, iom, _fileIndex++); DvtkData.Media.DicomFile dicomMediaFile = new DvtkData.Media.DicomFile(); // set up the file head DvtkData.Media.FileHead FileHead = new DvtkData.Media.FileHead(); // add the Transfer Syntax UID //DvtkData.Dul.TransferSyntax transferSyntax = new DvtkData.Dul.TransferSyntax(DvtkData.Dul.TransferSyntax.Explicit_VR_Little_Endian.UID); FileHead.TransferSyntax = dicomFile.FileHead.TransferSyntax; // set up the file meta information DvtkData.Media.FileMetaInformation fileMetaInformation = new DvtkData.Media.FileMetaInformation(); // add the FMI version fileMetaInformation.AddAttribute(Tag.FILE_META_INFORMATION_VERSION.GroupNumber, Tag.FILE_META_INFORMATION_VERSION.ElementNumber, VR.OB, 1, 2); // add the SOP Class UID System.String sopClassUid = ""; DvtkData.Dimse.Attribute attribute = dicomFile.DataSet.GetAttribute(DvtkData.Dimse.Tag.SOP_CLASS_UID); if (attribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { sopClassUid = uniqueIdentifier.Values[0]; } } fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_CLASS_UID.GroupNumber, Tag.MEDIA_STORAGE_SOP_CLASS_UID.ElementNumber, VR.UI, sopClassUid); // add the SOP Instance UID System.String sopInstanceUid = ""; attribute = dicomFile.DataSet.GetAttribute(DvtkData.Dimse.Tag.SOP_INSTANCE_UID); if (attribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { sopInstanceUid = uniqueIdentifier.Values[0]; } } fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.GroupNumber, Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.ElementNumber, VR.UI, sopInstanceUid); // add the Transfer Syntax UID fileMetaInformation.AddAttribute(Tag.TRANSFER_SYNTAX_UID.GroupNumber, Tag.TRANSFER_SYNTAX_UID.ElementNumber, VR.UI, dicomFile.FileHead.TransferSyntax); // add the Implemenation Class UID DvtkData.Dimse.Attribute implentationUID = dicomFile.FileMetaInformation.GetAttribute(0x00020012); fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_CLASS_UID.GroupNumber, Tag.IMPLEMENTATION_CLASS_UID.ElementNumber, VR.UI, implentationUID); // add the Implementation Version Name DvtkData.Dimse.Attribute implementationVersion = dicomFile.FileMetaInformation.GetAttribute(0x00020013); fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_VERSION_NAME.GroupNumber, Tag.IMPLEMENTATION_VERSION_NAME.ElementNumber, VR.SH, implementationVersion); // set up the dicomMediaFile contents dicomMediaFile.FileHead = FileHead; dicomMediaFile.FileMetaInformation = fileMetaInformation; dicomMediaFile.DataSet = dicomFile.DataSet; // write the dicomMediaFile to file Dvtk.DvtkDataHelper.WriteDataSetToFile(dicomMediaFile, filename); // save the filename in the dataset dicomFile.DataSet.Filename = filename; }
/// <summary> /// Constructor. Encapsulated existing DvtkData FileMetaInformation instance and DvtkData FileHead instance. /// </summary> /// <param name="dvtkDataFileMetaInformation">The encapsulated DvtkData FileMetaInformation instance.</param> /// <param name="dvtkDataFileHead">The encapsulated FileHead instance.</param> internal FileMetaInformation(DvtkData.Media.FileMetaInformation dvtkDataFileMetaInformation, DvtkData.Media.FileHead dvtkDataFileHead) : base(dvtkDataFileMetaInformation) { this.dvtkDataFileHead = dvtkDataFileHead; }
/// <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)); }
void SaveFileAsDefinedTS(string transferSyntax) { if (DCMFile == "") { richTextBoxLog.AppendText("There is no Media file to save, please select Media file.\n"); return; } try { SaveFileDialog saveAsDlg = new SaveFileDialog(); saveAsDlg.Filter = "DCM files (*.dcm) |*.dcm | All files (*.*)|*.*"; FileInfo dcmFileInfo = new FileInfo(DCMFile); saveAsDlg.InitialDirectory = dcmFileInfo.DirectoryName; saveAsDlg.FileName = dcmFileInfo.Name; saveAsDlg.Title = "Save DICOM file..."; if (saveAsDlg.ShowDialog() == DialogResult.OK) { richTextBoxLog.AppendText("Saving the modified Media file.\n"); _NewDCMFileName = saveAsDlg.FileName.Trim(); if (DCMFile != _NewDCMFileName) _IsSavedInNewDCMFile = true; DvtkData.Media.DicomFile dicomMediaFile = new DvtkData.Media.DicomFile(); // set up the file head DvtkData.Media.FileHead fileHead = new DvtkData.Media.FileHead(); // add the Transfer Syntax UID DvtkData.Dul.TransferSyntax tSyntax = new DvtkData.Dul.TransferSyntax(transferSyntax); fileHead.TransferSyntax = tSyntax; // set up the file meta information DvtkData.Media.FileMetaInformation fileMetaInformation = new DvtkData.Media.FileMetaInformation(); // add the FMI version fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.FILE_META_INFORMATION_VERSION.GroupNumber, DvtkData.Dimse.Tag.FILE_META_INFORMATION_VERSION.ElementNumber, VR.OB, 1, 2); // add the SOP Class UID fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_CLASS_UID.GroupNumber, DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_CLASS_UID.ElementNumber, VR.UI, _FileMetaInfo.MediaStorageSOPClassUID); // add the SOP Instance UID fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.GroupNumber, DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.ElementNumber, VR.UI, _FileMetaInfo.MediaStorageSOPInstanceUID); // add the Transfer Syntax UID fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID.GroupNumber, DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID.ElementNumber, VR.UI, transferSyntax); // add the Implemenation Class UID string implClassUID = ""; if(_FileMetaInfo.Exists("0x00020012")) { // Get the Transfer syntax HLI.Attribute implClassUIDAttr = _FileMetaInfo["0x00020012"]; implClassUID = implClassUIDAttr.Values[0]; } fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.IMPLEMENTATION_CLASS_UID.GroupNumber, DvtkData.Dimse.Tag.IMPLEMENTATION_CLASS_UID.ElementNumber, VR.UI, implClassUID); // add the Implementation Version Name string implClassVersion = ""; if(_FileMetaInfo.Exists("0x00020013")) { // Get the Transfer syntax HLI.Attribute implClassVersionAttr = _FileMetaInfo["0x00020013"]; implClassVersion = implClassVersionAttr.Values[0]; } fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.IMPLEMENTATION_VERSION_NAME.GroupNumber, DvtkData.Dimse.Tag.IMPLEMENTATION_VERSION_NAME.ElementNumber, VR.SH, implClassVersion); // set up the dicomMediaFile contents dicomMediaFile.FileHead = fileHead; dicomMediaFile.FileMetaInformation = fileMetaInformation; dicomMediaFile.DataSet = _DCMdataset.DvtkDataDataSet; // write the dicomMediaFile to file Dvtk.DvtkDataHelper.WriteDataSetToFile(dicomMediaFile, _NewDCMFileName); string theLogText = string.Format("Dataset {0} saved successfully with transfer syntax: {1}.\n\n", _NewDCMFileName, transferSyntax); richTextBoxLog.AppendText(theLogText); richTextBoxLog.ScrollToCaret(); richTextBoxLog.Focus(); //Cleanup the FMI & Dataset IsDCMFileModified = false; _IsAttributeGroupLengthDefined = false; _DCMdataset = null; _FileMetaInfo = null; if (_IsSavedInNewDCMFile) { FileInfo newDcmFileInfo = new FileInfo(_NewDCMFileName); _IsNewDCMFileLoaded = true; this.dirListBox.Path = newDcmFileInfo.DirectoryName; this.fileListBox.Path = this.dirListBox.Path; this.fileListBox.SelectedItem = newDcmFileInfo.Name; this.dirListBox.Refresh(); this.fileListBox.Refresh(); //Load the new Media file LoadDCMFile(_NewDCMFileName); } else { _IsNewDCMFileLoaded = false; this.dirListBox.Path = dcmFileInfo.DirectoryName; this.fileListBox.Path = this.dirListBox.Path; this.fileListBox.SelectedItem = dcmFileInfo.Name; //Load the new Media file LoadDCMFile(DCMFile); } UpdateTitleBarText(); } } catch (Exception exception) { string theErrorText; theErrorText = string.Format("Media file {0} could not be saved:\n{1}\n\n", _NewDCMFileName, exception.Message); richTextBoxLog.AppendText(theErrorText); richTextBoxLog.ScrollToCaret(); richTextBoxLog.Focus(); IsDCMFileModified = false; UpdateTitleBarText(); } }
/// <summary> /// Store the DICOM file into a DICOM Media File in the current Information Model directory. /// The file contents might be later used for retrieval.The transfer syntax is also passed so that the TS specified /// in the dcm file is used while moving the file to a Storage SCP /// </summary> /// <param name="dicomFile">The DICOM file to store</param> protected void StoreDicomFile(DvtkData.Media.DicomFile dicomFile) { System.String iom = System.String.Empty; if (Name.Equals("PatientRootInformationModel")) { iom = "PR"; } else if (Name.Equals("StudyRootInformationModel")) { iom = "SR"; } else if (Name.Equals("PatientStudyOnlyInformationModel")) { iom = "PS"; } // generate a filename System.String filename; filename = System.String.Format("{0}\\DVTK_IOM_TMP_{1}_{2:00000000}.DCM", DataDirectory, iom, _fileIndex++); DvtkData.Media.DicomFile dicomMediaFile = new DvtkData.Media.DicomFile(); // set up the file head DvtkData.Media.FileHead FileHead = new DvtkData.Media.FileHead(); // add the Transfer Syntax UID //DvtkData.Dul.TransferSyntax transferSyntax = new DvtkData.Dul.TransferSyntax(DvtkData.Dul.TransferSyntax.Explicit_VR_Little_Endian.UID); FileHead.TransferSyntax = dicomFile.FileHead.TransferSyntax; // set up the file meta information DvtkData.Media.FileMetaInformation fileMetaInformation = new DvtkData.Media.FileMetaInformation(); // add the FMI version fileMetaInformation.AddAttribute(Tag.FILE_META_INFORMATION_VERSION.GroupNumber, Tag.FILE_META_INFORMATION_VERSION.ElementNumber, VR.OB, 1, 2); // add the SOP Class UID System.String sopClassUid = ""; DvtkData.Dimse.Attribute attribute = dicomFile.DataSet.GetAttribute(DvtkData.Dimse.Tag.SOP_CLASS_UID); if (attribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { sopClassUid = uniqueIdentifier.Values[0]; } } fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_CLASS_UID.GroupNumber, Tag.MEDIA_STORAGE_SOP_CLASS_UID.ElementNumber, VR.UI, sopClassUid); // add the SOP Instance UID System.String sopInstanceUid = ""; attribute = dicomFile.DataSet.GetAttribute(DvtkData.Dimse.Tag.SOP_INSTANCE_UID); if (attribute != null) { UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue; if (uniqueIdentifier.Values.Count > 0) { sopInstanceUid = uniqueIdentifier.Values[0]; } } fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.GroupNumber, Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.ElementNumber, VR.UI, sopInstanceUid); // add the Transfer Syntax UID fileMetaInformation.AddAttribute(Tag.TRANSFER_SYNTAX_UID.GroupNumber, Tag.TRANSFER_SYNTAX_UID.ElementNumber, VR.UI, dicomFile.FileHead.TransferSyntax); // add the Implemenation Class UID DvtkData.Dimse.Attribute implentationUID = dicomFile.FileMetaInformation.GetAttribute(0x00020012); fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_CLASS_UID.GroupNumber, Tag.IMPLEMENTATION_CLASS_UID.ElementNumber, VR.UI, implentationUID); // add the Implementation Version Name DvtkData.Dimse.Attribute implementationVersion = dicomFile.FileMetaInformation.GetAttribute(0x00020013); fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_VERSION_NAME.GroupNumber, Tag.IMPLEMENTATION_VERSION_NAME.ElementNumber, VR.SH, implementationVersion); // set up the dicomMediaFile contents dicomMediaFile.FileHead = FileHead; dicomMediaFile.FileMetaInformation = fileMetaInformation; dicomMediaFile.DataSet = dicomFile.DataSet; // write the dicomMediaFile to file Dvtk.DvtkDataHelper.WriteDataSetToFile(dicomMediaFile, filename); // save the filename in the dataset dicomFile.DataSet.Filename = filename; }