Question

im trying to add a new tag to my DicomFile.DataSet in ClearCanvas.

I notice there is the method "DicomFile.DataSet.RemoveAttribute" but no "AddAtribute" method. So I have been looking at the method "LoadDicomFields" & "SaveDicomFields" but so far can't seem to get them to work. Ive tried to pass in a "DicomFieldAttribute" to these methods, but to no avail.

What am I missing here? Or what do I need to do to add a new tag to the DataSet.

DicomFieldAttribute c = new DicomFieldAttribute(tag);
List<DicomFieldAttribute> cs = new List<DicomFieldAttribute>();
cs.Add(c);
DicomFile.DataSet.LoadDicomFields(cs);
DicomFile.DataSet.SaveDicomFields(cs);
if(DicomFile.DataSet.Contains(tag))
{
   tag = 0; //BreakPoint never reached here
}

Or I tried this as well::

DicomFieldAttribute c = new DicomFieldAttribute(tag);
DicomFile.DataSet.LoadDicomFields(c);
DicomFile.DataSet.SaveDicomFields(c);
if(DicomFile.DataSet.Contains(tag))
{
   tag = 0; //BreakPoint never reached here
}

Ive been stuck on what would seem to be a trivial task.

Was it helpful?

Solution

You're confusing a bit the use of attributes. The DicomFiledAttribute is a .NET attribute that can be placed on members of a class so that the class is automatically populated with values from a DicomAttributeCollection or or to have the class automatically populated with values from the DicomAttribute Collection. Ie, given a test class like this:


public class TestClass
{
    [DicomField(DicomTags.SopClassUid, DefaultValue = DicomFieldDefault.Default)]
    public DicomUid SopClassUid = null;

    [DicomField(DicomTags.SopInstanceUid, DefaultValue = DicomFieldDefault.Default)]
    public DicomUid SOPInstanceUID = null;

    [DicomField(DicomTags.StudyDate, DefaultValue = DicomFieldDefault.Default)]
    public DateTime StudyDate;
}

You could populate an instance of the class like this:


DicomFile file = new DicomFile("filename.dcm");
file.Load();
TestClass testInstance = new TestClass();

file.DataSet.LoadDicomFields(testInstance);
// testInstance should now be populated with the values from file

If you're interested in just populating some DICOM tags, the DicomAttributeCollection has an indexer in it. The indexer will automatically create a DicomAttribute instance if it doesn't already exist, for the tag requested via the indexer. So, to populate a value, you can do soemthing like this:



DicomFile file = new DicomFile("filename.dcm");

file.DataSet[DicomTags.SopInstanceUid].SetStringValue("1.1.1");

If you want to create the DicomAttribute yourself, you can do something like this:


DicomAttribute attrib = new DicomAttributeUI(DicomTags.SopInstanceUid);
attrib.SetStringValue("1.1.1");

DicomFile file = new DicomFile("filename.dcm");
file.DataSet[DicomTags.SopInstanceUid] = attrib;

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