Skip to content

Instantly share code, notes, and snippets.

@Romiko
Created February 18, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Romiko/4dbba2d5ea37a99b368b to your computer and use it in GitHub Desktop.
Save Romiko/4dbba2d5ea37a99b368b to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using DicomObjects;
using DicomObjects.Enums;
namespace SB.Shared.Dicom
{
public class DicomQueryManager : IDicomQueryManager
{
public string CurrentServer { get; set; }
private readonly string callingAeTitle;
private readonly string destination;
public bool Found { get; private set; }
public int Port { get; private set; }
public DicomQuery Query { get; set; }
public DicomDataSet DataSet { get; private set; }
public DicomDataSetCollection Data { get; set; }
public string AccessionNumber { get; set; }
public string StudyUid { get; set; }
public string StudyDescription { get; set; }
public int ImageCount{ get; set; }
public DicomQueryManager(string callingAeTitle, string currentServer, string destination, string port, string accessionNumber, string studyUid)
{
AccessionNumber = accessionNumber;
CurrentServer = currentServer;
this.callingAeTitle = callingAeTitle;
this.destination = destination;
int thePort;
if (!int.TryParse(port, out thePort))
Port = 5000;
Port = thePort;
Query = GetDicomQuery();
DataSet = Query.QueryDataSet();
DataSet.Add(Keyword.AccessionNumber, accessionNumber);
DataSet.Add(Keyword.StudyInstanceUID, studyUid);
DataSet.Add(Keyword.StudyDescription, null);
}
public DicomQueryManager(string callingAeTitle, string currentServer, string port, string accessionNumber, string studyUid) :
this(callingAeTitle, currentServer, string.Empty, port, accessionNumber, studyUid) { }
public DicomQueryManager(string callingAeTitle, string currentServer, string port, string accessionNumber) :
this(callingAeTitle, currentServer, string.Empty, port, accessionNumber, string.Empty) { }
public DicomQueryManager(string callingAeTitle, string currentServer, string port) :
this(callingAeTitle, currentServer, string.Empty, port, string.Empty, string.Empty) { }
public IDicomQueryManager BuildPreferred()
{
Query.Level = QueryLevel.IMAGE;
return this;
}
public IDicomQueryManager BuildMasterSeriesLevel()
{
Query.Level = QueryLevel.SERIES;
DataSet.Add(Keyword.Modality, string.Empty);
return this;
}
public IDicomQueryManager BuildMasterStudyLevel()
{
Query.Level = QueryLevel.STUDY;
return this;
}
public void AssignServer(string server)
{
Query.Node = server;
Query.CalledAE = server;
CurrentServer = server;
}
public void SetAccessionNumber(string accessionNumber)
{
AccessionNumber = accessionNumber;
DataSet.Add(Keyword.AccessionNumber, accessionNumber);
}
private DicomQuery GetDicomQuery()
{
var query = new DicomQuery
{
Node = CurrentServer,
Port = Port,
CallingAE = callingAeTitle,
CalledAE = CurrentServer,
Level = QueryLevel.IMAGE,
Root = QueryRoot.Study,
Destination = destination
};
return query;
}
public bool Find()
{
Data = Query.Find(DataSet);
switch (Query.Level)
{
case QueryLevel.IMAGE:
ImageCount = Data.Count;
break;
case QueryLevel.STUDY:
SetQueryResultsStudy();
break;
case QueryLevel.SERIES:
SetQueryResultsSeries();
break;
default:
throw new NotSupportedException("Only QueryLevel at Image and Study/Series level is supported.");
}
Found = Data.Count > 0;
Debug.WriteLine("Found is: {0}", Found);
return Found;
}
private void SetQueryResultsStudy()
{
var count = Data.Count;
if (count > 0)
{
var countAttribute = Data[0][Keyword.NumberOfStudyRelatedInstances];
if (countAttribute.ExistsWithValue)
ImageCount = int.Parse(countAttribute.Value.ToString());
SetIntrinsicProperties();
}
else
{
ImageCount = 0;
}
}
private void SetQueryResultsSeries()
{
var seriesCount = Data.Count;
if (seriesCount > 0)
{
for (var i = 0; i < seriesCount; i++)
{
var imagesInSeriesCount = Data[i][Keyword.NumberOfSeriesRelatedInstances];
if (imagesInSeriesCount.ExistsWithValue)
ImageCount += int.Parse(imagesInSeriesCount.Value.ToString());
}
SetIntrinsicProperties();
}
else
{
ImageCount = 0;
}
}
private void SetIntrinsicProperties()
{
if (!string.IsNullOrEmpty(AccessionNumber)) return;
var accessionNumberAttriubte = Data[0][Keyword.AccessionNumber];
if (accessionNumberAttriubte.ExistsWithValue)
AccessionNumber = (string)accessionNumberAttriubte.Value;
var studyDescription = Data[0][Keyword.StudyDescription];
StudyDescription = studyDescription.ExistsWithValue ? (string)studyDescription.Value : string.Empty;
var studyUi = Data[0][Keyword.StudyInstanceUID];
StudyUid = studyUi.ExistsWithValue ? (string)studyUi.Value : string.Empty;
}
public void Move()
{
Query.Move(DataSet);
}
public IEnumerable<DicomLicense> GetLicenses()
{
var licenses = DicomLicense.InstalledLicenses;
return licenses;
}
public void Dispose()
{
if(DataSet != null)
DataSet.Dispose();
DataSet = null;
if(Data != null)
Data.Dispose();
Data = null;
Query = null;
}
}
}
using System;
using System.Collections.Generic;
using DicomObjects;
namespace SB.Shared.Dicom
{
public interface IDicomQueryManager : IDisposable
{
DicomDataSet DataSet { get; }
string AccessionNumber { get; set; }
string CurrentServer { get; set; }
int Port { get; }
void AssignServer(string server);
void SetAccessionNumber(string accessionNumber);
bool Find();
bool Found { get; }
void Move();
DicomQuery Query { get; }
DicomDataSetCollection Data { get; set; }
IDicomQueryManager BuildPreferred();
IDicomQueryManager BuildMasterSeriesLevel();
IDicomQueryManager BuildMasterStudyLevel();
int ImageCount { get; set; }
string StudyUid { get; set; }
string StudyDescription { get; set; }
IEnumerable<DicomLicense> GetLicenses();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment