// Creates AIM Annotation(s) based on user template/image selection and our internal preferences
        // Returns a list of the created annotations and a list of IGraphic elements used to create the annotations.
        // The list of graphic elements is used later to replace these elements with AimGraphics
        private List <aim_dotnet.Annotation> CreateAnnotationsFromUserInput(out List <IGraphic> annotationsGraphic)
        {
            AimAnnotationCreationContext aimContext = new AimAnnotationCreationContext(AnnotationKind.AK_ImageAnnotation, AnnotationTypeCode, AnnotationName);

            aimContext.SelectedAnatomicEntities    = this.SelectedAnatomicEntities;
            aimContext.SelectedImagingObservations = this.SelectedImagingObservations;
            aimContext.SelectedInferences          = this.SelectedInferences;
            aimContext.AnnotationUser = (!string.IsNullOrEmpty(this.UserName) && !string.IsNullOrEmpty(this.LoginName))
                                                        ?
                                        new User
            {
                Name        = this.UserName,
                LoginName   = this.LoginName,
                RoleInTrial = this.RoleInTrial,
                NumberWithinRoleOfClinicalTrial = this.NumberWithinRoleInTrial >= 0 ? this.NumberWithinRoleInTrial : -1
            }
                                                        : null;
            aimContext.includeCalculations = true;             // !this.VasariTemplateVisible; // no calculations for VASARI
            // !!! Create annotation for the current image only !!!
            aimContext.SOPImageUIDs = new List <string> {
                ((IImageSopProvider)this.ImageViewer.SelectedPresentationImage).ImageSop.SopInstanceUid
            };
            //            aim_dotnet.Annotation annotation = AIMHelpers.CreateAimAnnotation(this.ImageViewer.SelectedPresentationImage, aimContext);
            return(AimHelpers.CreateAimAnnotations(this.ImageViewer.SelectedPresentationImage.ParentDisplaySet.PresentationImages, aimContext, out annotationsGraphic));
        }
        private List <aim_dotnet.Annotation> CreateAnnotationsFromUserInput(out List <IGraphic> annotationsGraphic)
        {
            var aimContext = new AimAnnotationCreationContext(AnnotationKind.AK_ImageAnnotation, AnnotationTypeCode, AnnotationName);

            aimContext.SelectedAnatomicEntities    = SelectedAnatomicEntities;
            aimContext.SelectedImagingObservations = SelectedImagingObservations;
            aimContext.SelectedInferences          = SelectedInferences;
            aimContext.AnnotationUser = (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(LoginName))
                                                                                        ?
                                        new User
            {
                Name        = UserName,
                LoginName   = LoginName,
                RoleInTrial = RoleInTrial,
                NumberWithinRoleOfClinicalTrial = NumberWithinRoleInTrial >= 0 ? NumberWithinRoleInTrial : -1
            }
                                                                                        : null;
            aimContext.includeCalculations = true;
            aimContext.SOPImageUIDs        = new List <string> {
                ((IImageSopProvider)ImageViewer.SelectedPresentationImage).ImageSop.SopInstanceUid
            };
            return(AimHelpers.CreateAimAnnotations(ImageViewer.SelectedPresentationImage.ParentDisplaySet.PresentationImages, aimContext, out annotationsGraphic));
        }
        private static aim_dotnet.ImageAnnotation CreateImageAnnotation(IImageSopProvider imageSop, AimAnnotationCreationContext creationContext)
        {
            // Create Basic Image Annotation
            Platform.CheckTrue(creationContext.SelectedAnnotationKind == aim_dotnet.AnnotationKind.AK_ImageAnnotation, "ImageAnnotationKind");
            Platform.CheckForEmptyString(creationContext.AnnotationName, "AnnotationName");
            var imgAnnotation = new aim_dotnet.ImageAnnotation();
            imgAnnotation.CodeValue = creationContext.AnnotationTypeCode.CodeValue;
            imgAnnotation.CodeMeaning = creationContext.AnnotationTypeCode.CodeMeaning;
            imgAnnotation.CodingSchemeDesignator = creationContext.AnnotationTypeCode.CodingSchemeDesignator;
            imgAnnotation.CodingSchemeVersion = creationContext.AnnotationTypeCode.CodingSchemeVersion;
            imgAnnotation.UniqueIdentifier = DicomUid.GenerateUid().UID;
            imgAnnotation.Name = creationContext.AnnotationName;
            imgAnnotation.DateTime = DateTime.Now;
            imgAnnotation.Patient = CreatePatient(imageSop);
            imgAnnotation.Equipment = CreateEquipment();
            if (creationContext.AnnotationUser != null)
                imgAnnotation.User = new aim_dotnet.User(creationContext.AnnotationUser);
            if (!string.IsNullOrEmpty(creationContext.AnnotationComment))
                imgAnnotation.Comment = creationContext.AnnotationComment;
            if (creationContext.SelectedAnatomicEntities != null)
                imgAnnotation.AnatomyEntityCollection = new List<aim_dotnet.AnatomicEntity>(creationContext.SelectedAnatomicEntities);
            if (creationContext.SelectedImagingObservations != null)
                imgAnnotation.ImagingObservationCollection = new List<aim_dotnet.ImagingObservation>(creationContext.SelectedImagingObservations);
            if (creationContext.SelectedInferences != null)
                imgAnnotation.InferenceCollection = new List<aim_dotnet.Inference>(creationContext.SelectedInferences);

            return imgAnnotation;
        }
        public static List<aim_dotnet.Annotation> CreateAimAnnotations(IEnumerable<IPresentationImage> presentationImages, AimAnnotationCreationContext creationContext, out List<IGraphic> annotationsGraphic)
        {
            var annotations = new List<aim_dotnet.Annotation>();
            annotationsGraphic = new List<IGraphic>();
            if (creationContext.SelectedAnnotationKind == aim_dotnet.AnnotationKind.AK_ImageAnnotation)
            {
                var annotationSOPs = creationContext.SOPImageUIDs;
                foreach (var currentImage in presentationImages)
                {
                    var currentImageSOP = currentImage as IImageSopProvider;
                    if (currentImageSOP == null)
                        continue;
                    if (annotationSOPs.Count > 0 && !annotationSOPs.Contains(currentImageSOP.ImageSop.SopInstanceUid))
                        continue;

                    var currentOverlayGraphics = currentImage as IOverlayGraphicsProvider;
                    if (currentOverlayGraphics != null)
                    {
                        // Create one annotation per calculation/geo shape
                        var shapeIdentifier = 0;
                        var geoShapes = new List<aim_dotnet.IGeometricShape>();
                        var calculations = creationContext.includeCalculations ? new List<aim_dotnet.Calculation>() : null;
                        var textAnnotations = new List<aim_dotnet.TextAnnotation>();
                        foreach (var graphic in currentOverlayGraphics.OverlayGraphics)
                        {
                            var currentRoiGraphic = graphic as RoiGraphic;
                            if (currentRoiGraphic != null && currentRoiGraphic.Roi != null)
                            {
                                // Create Geo Shape for this ROI
                                var geoShape = Create2DGeoShape(currentRoiGraphic, currentImageSOP.ImageSop.SopInstanceUid, currentImageSOP.Frame.FrameNumber);
                                if (geoShape != null)
                                {
                                    geoShape.ShapeIdetifier = shapeIdentifier++;
                                    if (calculations != null)
                                        calculations.AddRange(CreateCalculations(currentRoiGraphic, geoShape.ShapeIdetifier));
                                    geoShapes.Add(geoShape);
                                    annotationsGraphic.Add(graphic);
                                }
                            }
                            else if (graphic is ContextMenuControlGraphic)
                            {
                                var contextMenuControlGraphic = (ContextMenuControlGraphic) graphic;
                                if (contextMenuControlGraphic.Subject != null)
                                {
                                    var textAnnotation =
                                        CreateTextAnnotation(contextMenuControlGraphic.Subject, currentImageSOP.ImageSop.SopInstanceUid, currentImageSOP.Frame.FrameNumber);
                                    if (textAnnotation != null)
                                    {
                                        if (textAnnotation.ConnectorPoints != null)
                                            textAnnotation.ConnectorPoints.ShapeIdetifier = shapeIdentifier++;
                                        textAnnotations.Add(textAnnotation);
                                        annotationsGraphic.Add(graphic);
                                    }
                                }
                            }
                        }

                        if (annotationSOPs.Count > 0 || geoShapes.Count > 0 || (calculations != null && calculations.Count > 0) || textAnnotations.Count > 0)
                        {
                            var imgAnn = CreateImageAnnotation(currentImageSOP, creationContext);
                            if (geoShapes.Count > 0)
                                imgAnn.GeometricShapeCollection = geoShapes;
                            if (calculations != null && calculations.Count > 0)
                                imgAnn.CalculationCollection = calculations;
                            if (textAnnotations.Count > 0)
                                imgAnn.TextAnnotationCollection = textAnnotations;
                            AddDicomImageReference(imgAnn, currentImageSOP);
                            annotations.Add(imgAnn);
                        }
                    }
                }

                return annotations;
            }

            throw new NotImplementedException("Creation of AnnotationOfAnnotation objects is not yet supported ");
        }
		public static List<aim_dotnet.Annotation> CreateAimAnnotations(IEnumerable<IPresentationImage> presentationImages, AimAnnotationCreationContext creationContext, out List<IGraphic> annotationsGraphic)
		{
			List<aim_dotnet.Annotation> annotations = new List<aim_dotnet.Annotation>();
			annotationsGraphic = new List<IGraphic>(); // list that includes all IGraphic objects used to create new annotations

			if (creationContext.SelectedAnnotationKind == aim_dotnet.AnnotationKind.AK_ImageAnnotation)
			{
				List<string> annotationSOPs = creationContext.SOPImageUIDs;
					// list of UIDs to include in the annotation. If empty, include all images from the same set which have markup.

				foreach (IPresentationImage currentImage in presentationImages)
				{
					IImageSopProvider currentImageSOP = currentImage as IImageSopProvider;
					if (currentImageSOP == null) // not an image?
						continue;

					// Skip the image if it's not included in our image annotation set
					if (annotationSOPs.Count > 0 && !annotationSOPs.Contains(currentImageSOP.ImageSop.SopInstanceUid))
						continue;

					// Find all markups on the image
					IOverlayGraphicsProvider currentOverlayGraphics = currentImage as IOverlayGraphicsProvider;
					if (currentOverlayGraphics != null) // all images should implement IOverlayGraphicsProvider
					{
						// Create one annotation per calculation/geo shape
						int shapeIdentifier = 0;
						List<aim_dotnet.IGeometricShape> geoShapes = new List<aim_dotnet.IGeometricShape>();
						List<aim_dotnet.Calculation> calculations = creationContext.includeCalculations ? new List<aim_dotnet.Calculation>() : null;
						foreach (IGraphic graphic in currentOverlayGraphics.OverlayGraphics)
						{
							RoiGraphic currentRoiGraphic = graphic as RoiGraphic;
							if (currentRoiGraphic != null && currentRoiGraphic.Roi != null)
							{
								// Create Geo Shape for this ROI
								aim_dotnet.IGeometricShape geoShape = Create2DGeoShape(currentRoiGraphic, currentImageSOP.ImageSop.SopInstanceUid, currentImageSOP.Frame.FrameNumber);
								if (geoShape != null)
								{
									// Shape Identifier is just a count
									geoShape.ShapeIdetifier = shapeIdentifier++;

									// Get calcualtions for this ROI, if required
									if (calculations != null)
										calculations.AddRange(CreateCalculations(currentRoiGraphic, geoShape.ShapeIdetifier));

									geoShapes.Add(geoShape);
									annotationsGraphic.Add(graphic);
								}
							}
						}
						// Create annotation if:
						// 1. we are explicitly asked to create an annotation for this SOP, or
						// 2. image has markup(s), or
						// 3. image has calculation(s)
						if (annotationSOPs.Count > 0 || geoShapes.Count > 0 || (calculations != null && calculations.Count > 0))
						{
							aim_dotnet.ImageAnnotation imgAnn = CreateImageAnnotation(currentImageSOP, creationContext);

							if (geoShapes.Count > 0)
								imgAnn.GeometricShapeCollection = geoShapes;

							if (calculations != null && calculations.Count > 0)
								imgAnn.CalculationCollection = calculations;

							// Add current image reference to the annotation
							AddDicomImageReference(imgAnn, currentImageSOP);

							annotations.Add(imgAnn);
						}
					}
				}

				return annotations;
			}
			else
			{
				throw new NotImplementedException("Creation of AnnotationOfAnnotation objects is not yet supported ");
			}
		}