Examining Annotations (2) Example for Delphi

Procedure TForm1.ExamineAnnotations2(objPresStateDS: TLEADDicomDS);
var
   strMsg: String;
   strRefSOPInstanceUID: String;
   hGraphicAnnotationItem: LongInt;
   strGraphicLayer: String;
begin
   // Pick one of the images referenced in the 'Presentation State Module' and
   // get its SOP Instance UID
   if(objPresStateDS.FindFirstPresStateRefSeriesItem () = DICOM_SUCCESS)then
   begin
      if(objPresStateDS.GetPresStateImageRefCount () > 0)then
         strRefSOPInstanceUID:= objPresStateDS.GetPresStateImageRefInstanceUID (0);
   end;

   // Find the SOP Class UID of that image
   if(objPresStateDS.FindPresStateRefImageItem(strRefSOPInstanceUID) = DICOM_SUCCESS)then
   begin
      if(objPresStateDS.MoveChildElement () = 0)then
      begin
         if(objPresStateDS.FindFirstElement (TAG_REFERENCED_SOP_CLASS_UID, True) = 0)then
         begin
            if(objPresStateDS.GetStringValue (0, 1) = 0)then
            begin
               strMsg:= objPresStateDS.StringValues [0];
               Application.MessageBox(PChar(strMsg), 'Referenced SOP Class UID', MB_OK);
            end;
         end;
      end;
   end;

   // Remove the reference to that image from the 'Presentation State Module'
   objPresStateDS.RemovePresStateImageRef(strRefSOPInstanceUID);
         
   // Pick a Graphic Annotation Item
   if(objPresStateDS.FindFirstGraphicAnnItem () = DICOM_SUCCESS)then
   begin
      hGraphicAnnotationItem:= objPresStateDS.DefaultInterface.Get_CurrentElement ().hElement;
      
      // Pick an image reference
      if(objPresStateDS.GetLayerImageRefCount() > 0)then
      begin
         strRefSOPInstanceUID:= objPresStateDS.GetLayerImageRefInstanceUID(0);
         objPresStateDS.FindLayerRefImageItem(strRefSOPInstanceUID);

         // Display one of the attributes of the Referenced Image Item
         if(objPresStateDS.MoveChildElement () = 0)then
         begin
            if(objPresStateDS.FindFirstElement (TAG_REFERENCED_SOP_CLASS_UID, True) = 0)then
               if(objPresStateDS.GetStringValue (0, 1) = 0)then
               begin
                  strMsg:= 'Referenced SOP Class UID: ' + objPresStateDS.StringValues [0];
                  Application.MessageBox(PChar(strMsg),
                                         'Referenced Image Item',
                                         MB_OK);
               end;
         end;

         objPresStateDS.SetCurrentElement (hGraphicAnnotationItem);

         // Remove the image reference
         objPresStateDS.RemoveLayerImageRef(strRefSOPInstanceUID);

         // Remove all references to images that are listed in this Graphic Annotation
         // Item(this way, the annotations defined by this Item will be applied to all
         // the images listed in the 'Presentation State Module')
         objPresStateDS.RemoveAllLayerImageRefs
      end;

      strGraphicLayer:= objPresStateDS.GetLayerName();

      // Display some of the layer//s attributes
      objPresStateDS.GetLayerAttributes (objPresStateDS.GetLayerIndex(strGraphicLayer));
      with (objPresStateDS.LayerAttributes) do
      begin
         strMsg:= 'Graphic Layer: ' + LayerName + Chr(13) +
                  'Graphic Layer Order: ' + IntToStr(LayerOrder) + Chr(13) +
                  'Graphic Layer Description: ' + LayerDescription;
         Application.MessageBox(PChar(strMsg),
                                'Graphic Layer Attributes',
                                MB_OK);
      end;
      
      // Change the layer of this Graphic Annotation Item(the specified new
      // layer should already be defined in the 'Graphic Layer Module')
      objPresStateDS.SetLayerName('ANOTHER_LAYER');
      
      // Pick a graphic annotation object
      if(objPresStateDS.GetGraphicObjectCount () > 0)then
      begin
         objPresStateDS.FindGraphicObjectItem (0);
         
         // Display one of the attributes of this object
         if(objPresStateDS.MoveChildElement () = 0)then
         begin
            // Graphic Type(0070,0023)
            if(objPresStateDS.FindFirstElement ($700023, True) = 0)then
            begin
               if(objPresStateDS.GetStringValue (0, 1) = 0)then
               begin
                  strMsg:= 'Graphic Type: ' + objPresStateDS.StringValues [0];
                  Application.MessageBox(PChar(strmsg),
                                         'Graphic Annotation Object',
                                         MB_OK);
               end;
            end;
         end;
         
         objPresStateDS.SetCurrentElement (hGraphicAnnotationItem);
         
         // Remove the object
         objPresStateDS.RemoveGraphicObject(0);

         // Remove all the graphic annotation objects defined by this Graphic
         // Annotation Item
         objPresStateDS.RemoveAllGraphicObjects();
         // Or:
         //objPresStateDS.RemoveLayerGraphicObjects
      end;
      
      // Pick a text annotation object
      if(objPresStateDS.GetTextObjectCount () > 0)then
      begin
         objPresStateDS.FindTextObjectItem (0);

         // Display one of the attributes of this object
         if(objPresStateDS.MoveChildElement () = 0)then
         begin
            // Unformatted Text Value(0070,0006)
            if(objPresStateDS.FindFirstElement ($700006, True) = 0)then
            begin
               if(objPresStateDS.GetStringValue (0, 1) = 0)then
               begin
                  strMsg:= 'Unformatted Text Value: ' + 
                           objPresStateDS.StringValues [0];
                  Application.MessageBox(PChar(strMsg),
                                         'Text Annotation Object',
                                         MB_OK);
               end;
            end;
         end;
         
         objPresStateDS.SetCurrentElement (hGraphicAnnotationItem);
         
         // Remove the object
         objPresStateDS.RemoveTextObject(0);
         
         // Remove all the text annotation objects defined by this Graphic
         // Annotation Item
         objPresStateDS.RemoveAllTextObjects
         // Or:
         //objPresStateDS.RemoveLayerTextObjects
      end;
   end;
   
   // Remove all references to images that are listed in all the Graphic Annotation
   // Items(this way, the annotations defined by all the Items will be applied to all
   // the images listed in the 'Presentation State Module')
   objPresStateDS.RemoveAllLayersImageRefs();
   
   // Remove all references to images from the 'Presentation State Module'
   objPresStateDS.RemoveAllPresStateImageRefs ();

   // Pick one of the layers
   if(objPresStateDS.LayerCount > 0)then
   begin
      objPresStateDS.FindLayerItem(0);
      
      // Display one of the attributes of this layer
      if(objPresStateDS.MoveChildElement () = 0)then
      begin
         // Graphic Layer(0070,0002)
         if(objPresStateDS.FindFirstElement ($700002, True) = 0)then
         begin
            if(objPresStateDS.GetStringValue (0, 1) = 0)then
            begin
               strMsg:= 'Graphic Layer: ' + objPresStateDS.StringValues [0]; 
               Application.MessageBox(PChar(strMsg),
                                      'Graphic Layer',
                                      MB_OK);
            end;
         end;
      end;
      
      // Remove the layer
      objPresStateDS.RemoveLayer(0, True);
      
      // Remove all the layers defined in the 'Graphic Layer Module'
      objPresStateDS.RemoveAllLayers(True);
   end;
end;