Question

Currently I building a small desktop application that can handle DICOM files. I am coding in C# and .NET and using the ClearCanvas library. One thing that I need to do is to be able to display the full contents of the file including all of the sequences. But the sequences are done in a recursive manner, so each sequence can have more sequences inside of it. Right now my code can access the first two levels, but I am just doing this as a tester, since I need to be able to access the nth level of sequences. So I need to somehow automate this. This is what my code looks like right now for the first two levels.

DicomSequenceItem[] seq = attrs2[i].Values as DicomSequenceItem[];
if (seq != null)
{
for (int j = 0; j < seq.Length; j++)
{
      for (int n = 0; n < seq[j].Count; n++)
      {
           DicomSequenceItem[] level2 = seq[j].ElementAt(n).Values as DicomSequenceItem[];
           if(seq[j].ElementAt(n).GetValueType().ToString().Equals("ClearCanvas.Dicom.DicomSequenceItem"))
           {               
                for (int k = 0; k < level2.Length; k++)
                {
                     for (int l = 0; l < level2[k].Count; l++)
                     {
                          text += "\t\t" + level2[k].ElementAt(l) + "\r\n";
                     }
                }
            }
            else
            {
                text += "\t" + seq[j].ElementAt(n) + "\r\n";
            }
       }
}
}

Any help (code samples) would be greatly appreciated.

Thanks!

Was it helpful?

Solution

Here's a simple recursive routine to traverse through the tags in an attribute collection, including stepping recursively through any Sequence elements that may be in the collection:

    void Dump(DicomAttributeCollection collection, string prefix, StringBuilder sb)
    {     
        foreach (DicomAttribute attribute in collection)
        {
            var attribSQ = attribute as DicomAttributeSQ;
            if (attribSQ != null)
            {                    
                for (int i=0; i< attribSQ.Count; i++) 
                {
                    sb.AppendLine(prefix + "SQ Item: " + attribSQ.ToString());

                    DicomSequenceItem sqItem = attribSQ[i];
                    Dump(sqItem, prefix + "\t", sb);
                }
            }
            else
            {
                sb.AppendLine(prefix + attribute.ToString());
            }
        }
    }

DicomAttributeCollection is Enumerable, so you can just use a foreach loop to go through all of the attributes in the collection. The attributes themselves are stored in a SortedDictionary, so they will also be in ascending tag order when enumerated.

Note that if you downloaded the source code for the ClearCanvas libraries, you also could look at the real Dump() method that is part of the DicomAttributeCollection class. It traverses through a collection and writes to a StringBuilder instance all of the tags in the collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top