LAUREL BRIDGE

LaurelBridge.DCF.Examples.ReadDataSetFlat Namespace

DICOM Connectivity Framework V3.4
The ReadDataSetFlat example demonstrates how to use DCF and the ReadDataSetFlat method to diagnose problems with sequence encoding.
Classes

  ClassDescription
Public classProgram
A program to demonstate how to use readDataSetFlat on a SR dataset.
Examples

ReadDataSetFlat Sample Code
public class Program
{
    /// <summary>
    /// Main entry point for ReadDataSetFlat.
    /// </summary>
    /// <param name="args">command line arguments</param>
    [STAThread]
    public static void Main(string[] args)
    {
        try
        {
            string fileName = args.Length > 0 ? args[0] : "BasicTextSR.dcm";
            using (DicomFileInput dfi = new DicomFileInput(fileName))
            {
                dfi.TrackElementOffsets = true;
                DicomDataSet dds = dfi.ReadDataSetFlat();
                Console.WriteLine("DataSet.IsFlat = {0}", dds.IsFlat);
                dds.DumperOptions = new DataSetDumperOptions { ShowElementOffsets = true };
                Console.WriteLine(dds);
                List<IDicomElement> elementList = new List<IDicomElement>(dds);
                IList<AttributeTag> tagsToDump = new AttributeTag[]
                {
                    Tags.SOPClassUID,
                    Tags.SOPInstanceUID,
                    Tags.StudyDate,
                    Tags.StudyTime,
                    Tags.SeriesTime,
                    Tags.CodeMeaning,
                    Tags.NumericValue,
                    Tags.TextValue,
                    Tags.ConceptNameCodeSequence,
                    Tags.ContentSequence,
                    Tags.ContentTemplateSequence,
                    //Tags.Item,
                    //Tags.ItemDelimitationItem,
                    //Tags.SequenceDelimitationItem,
                };
                Console.WriteLine("{0}Printing interesting elements in dataset:", Environment.NewLine);
                int indent = 0;
                foreach (IDicomElement el in elementList)
                {
                    // this relies on undefined length sequences and items
                    // (defined length sequences would have to calculate the end of the sequence or item based on the
                    // length of each element, which is also dependent on explicit or implicit transfer syntax;
                    // see DicomDeviceInput CalcHeaderLength and is left as an exercise for the reader ;-).
                    if (el.Tag.Equals(Tags.Item) && el.Length == (-1))
                        indent++;
                    else if (el.Tag.Equals(Tags.ItemDelimitationItem))
                        indent--;
                    if (tagsToDump.Contains(el.Tag))
                    {

                        Console.WriteLine("{0}{1}", GetIndent(indent), el);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception caught during execution: {0}", e);
        }

        if (System.Diagnostics.Debugger.IsAttached)
        {
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey();
        }
    }

    private static readonly Dictionary<int, string> _indent = new Dictionary<int, string>();

    private static string GetIndent(int indent)
    {
        if (indent <= 0)
            return string.Empty;
        if (!_indent.ContainsKey(indent))
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++)
                sb.Append("> ");
            _indent[indent] = sb.ToString();
        }
        return _indent[indent];
    }
}