Skip to content

Commit

Permalink
CLI now is able to display the Dictionary and UID lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron533 committed Jul 14, 2024
1 parent c24d63a commit 06e8c83
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
4 changes: 4 additions & 0 deletions DicomGrep/ViewModels/SopClassLookupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public SopClassLookupViewModel() : base()
}
}

/// <summary>
/// Get all dicom UID definitions.
/// </summary>
/// <returns></returns>
private IEnumerable<DicomUID> GetAllDicomUIDDefs()
{
return typeof(DicomUID).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
Expand Down
55 changes: 50 additions & 5 deletions DicomGrepCli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.CommandLine;
using DicomGrepCore.Services;
using FellowOakDicom;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Text.RegularExpressions;

Expand All @@ -13,9 +15,9 @@ static async Task<int> Main(string[] args)

#region lookup options
// once see the lookup options, do not execute any search
var lookupOption = new Option<LookupIn>(
var lookupOption = new Option<LookupIn?>(
name: "--lookup",
description: "lookup the Dictionary or SOPClass UID"
description: "lookup the DICOM Dictionary or UID"
);
lookupOption.AddAlias("-l");
rootCommand.AddOption(lookupOption);
Expand All @@ -41,6 +43,7 @@ static async Task<int> Main(string[] args)
name: "--ignore-case",
description: "ignore case"
);
ignoreCaseOption.SetDefaultValue(true);
ignoreCaseOption.AddAlias("-i");
rootCommand.AddOption(ignoreCaseOption);

Expand Down Expand Up @@ -68,15 +71,57 @@ static async Task<int> Main(string[] args)
#endregion search options


rootCommand.SetHandler(
(lookupOptionValue, folderOptionValue, recursiveOptionValue, ignoreCaseOptionValue, tagOptionValue, valueOptionValue) =>
{
if (lookupOptionValue.HasValue)
{
PrintLookup(lookupOptionValue.Value);
}
},
lookupOption, folderOption, recursiveOption, ignoreCaseOption, tagOption, valueOption
);


return await rootCommand.InvokeAsync(args);
}


private static void PrintLookup(LookupIn lookupIn)
{
DictionaryService dictionaryService = new DictionaryService();
dictionaryService.ReadAndAppendCustomDictionaries();
switch (lookupIn)
{
case LookupIn.DICT:
//dictionaryService.GetAllDicomTagDefs();
//dictionaryService.GetAllPrivateTagDefs();
var publicTags = dictionaryService.GetAllDicomTagDefs().OrderBy(entry => entry.Tag.Group).ThenBy(entry => entry.Tag.Element).ToList();
var privateTags = dictionaryService.GetAllPrivateTagDefs().OrderBy(entry => entry.Tag.PrivateCreator).ThenBy(entry => entry.Tag.Group).ThenBy(entry => entry.Tag.Element).ToList();
Console.WriteLine("Tag|Name|Group|Element|IsRetired|IsPrivate|PrivateCreator");
foreach (var tag in publicTags.Concat(privateTags))
{
Console.WriteLine($"{tag.Tag}|{tag.Name}|{tag.Tag.Group}|{tag.Tag.Element}|{tag.IsRetired}|{tag.Tag.IsPrivate}|{tag.Tag.PrivateCreator}");
}
Console.WriteLine($"Total {publicTags.Count + privateTags.Count} tags. Public: {publicTags.Count}, Private: {privateTags.Count}");
break;
case LookupIn.UID:
var allUids = dictionaryService.GetAllDicomUIDDefs().ToList();
Console.WriteLine("UID|Name|Type|IsRetired|IsImageStorage|IsVolumeStorage|StorageCategory");
foreach (var uid in allUids)
{
Console.WriteLine($"{uid.UID}|{uid.Name}|{uid.Type}|{uid.IsRetired}|{uid.IsImageStorage}|{uid.IsVolumeStorage}|{uid.StorageCategory}");
}
Console.WriteLine($"Total {allUids.Count} UIDs.");
break;
default:
break;
}
}
}

enum LookupIn
{
DICT = 0,
SOP
UID
}
}
43 changes: 43 additions & 0 deletions DicomGrepCore/Services/DictionaryService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using FellowOakDicom;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -61,5 +63,46 @@ private bool ReadDictionary(out DicomDictionary dictionary, string filePath)
return false;
}
}

/// <summary>
/// Get all dicom UID definitions.
/// </summary>
/// <returns></returns>
public IEnumerable<DicomUID> GetAllDicomUIDDefs()
{
return typeof(DicomUID).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
.Select(f => f.GetValue(null)).Where(v => v is DicomUID).Cast<DicomUID>();
}

/// <summary>
/// Get all public DICOM tag definitions. Private tags are not included.
/// </summary>
/// <returns></returns>
public IEnumerable<DicomDictionaryEntry> GetAllDicomTagDefs()
{
return DicomDictionary.Default;
}

/// <summary>
/// Get all private DICOM tag definitions.
/// </summary>
/// <returns></returns>
public IEnumerable<DicomDictionaryEntry> GetAllPrivateTagDefs()
{
ConcurrentDictionary<string, DicomDictionary> _private = typeof(DicomDictionary)
.GetField("_private", BindingFlags.NonPublic | BindingFlags.Instance)?
.GetValue(DicomDictionary.Default)
as ConcurrentDictionary<string, DicomDictionary>;

// each vendor (private tag creator) can own multiple entries (tags)
IEnumerable<DicomDictionary> vendorsDictionary = _private.Select(item => item.Value);
foreach (var dictionary in vendorsDictionary)
{
foreach (var entry in dictionary)
{
yield return entry;
}
}
}
}
}

0 comments on commit 06e8c83

Please sign in to comment.