Search code examples
fo-dicom

Update Dicom Tags in a nested sequence


I would like to update (300A,004A) and (300A,00B8) dicom tags both are type string strings.

The challenge is that these tags are in a nested sequence and multiple instances exist. Need to ensure I update the correct "item" in the sequence.

Here is my code:

  string[] files = Directory.GetFiles(dirPath);
    foreach (var item in files)
    
    {
            DicomFile dicomFile = DicomFile.Open(item, FileReadOption.ReadAll);
    
             var dicomDataset = dicomFile.Dataset;
            
            //Count number of items found in the sequence
    
            int count = dicomDataset.GetSequence(DicomTag.ToleranceTableSequence).Items[0].GetSequence(DicomTag.BeamLimitingDeviceToleranceSequence).Items.Count();
    
    //go thru items until you find the correct one that contains "ASYMX
    
            for (int i = 0; i < count; i++)
    
          {
    
    var Dev1 = dicomDataset.GetSequence(DicomTag.ToleranceTableSequence).Items[0].GetSequence(DicomTag.BeamLimitingDeviceToleranceSequence).Items[i].GetSingleValueOrDefault<string>(DicomTag.RTBeamLimitingDeviceType, "");
    
                if (Dev1 == "ASYMX")
    
                {
    
    **//Code works up to here because it found the item that contains ASYMX**
    
    
           //Now, I need to update those tags
      dicomFile.Dataset.AddOrUpdate<string>(DicomTag.BeamLimitingDevicePositionTolerance, "Valu1");
    
    
                      dicomFile.Dataset.AddOrUpdate<string>(DicomTag.RTBeamLimitingDeviceType, "ASYMX2");
    
          dicomFile.Save(item);
                
                     }
    
    
               }
    
    
         }
           
    
        else
        
        {
    
    
            //Console.WriteLine("TBD");
    
    
        }

The code compiles correctly but file does not update


Solution

  • If you want to update a tag inside a sequence item, you have to do that in that item (which is a Dataset itself) instead of in the dataset root. In your code, you are adding the tags to the dataset root (which you should be able to see).

    Something like this should work (untested):

        ...
        for (int i = 0; i < count; i++)    
        {
            // get the item with the tags to be changed
            var item = dicomDataset.GetSequence(DicomTag.ToleranceTableSequence).Items[0].GetSequence(DicomTag.BeamLimitingDeviceToleranceSequence).Items[i];
            var dev1 = item.GetSingleValueOrDefault<string>(DicomTag.RTBeamLimitingDeviceType, "");
            if (dev1 == "ASYMX")
            {
                // update the tag in that item
                item.AddOrUpdate<string>(DicomTag.BeamLimitingDevicePositionTolerance, "Value1");
                item.AddOrUpdate<string>(DicomTag.RTBeamLimitingDeviceType, "ASYMX2");
            }
        }
        dicomFile.Save(dataset);