private void UpdateImagesWithProperAimGraphic(List <aim_dotnet.Annotation> annotations, List <IGraphic> annotationsGraphic)
        {
            var hasChanges = false;

            if (annotationsGraphic != null)
            {
                foreach (var graphic in annotationsGraphic)
                {
                    Debug.Assert(graphic.ParentPresentationImage == null || graphic.ParentPresentationImage == ImageViewer.SelectedPresentationImage);

                    var currentOverlayGraphics = graphic.ParentPresentationImage as IOverlayGraphicsProvider;
                    if (currentOverlayGraphics != null && currentOverlayGraphics.OverlayGraphics != null)
                    {
                        hasChanges |= currentOverlayGraphics.OverlayGraphics.Remove(graphic);
                    }
                }
            }

            if (annotations != null)
            {
                foreach (var annotation in annotations)
                {
                    hasChanges |= AimHelpers.ReadGraphicsFromAnnotation(annotation, ImageViewer.SelectedPresentationImage);
                }
            }

            if (hasChanges)
            {
                ImageViewer.SelectedPresentationImage.Draw();
            }
        }
        // Helper. Removes given IGraphic from their images and adds given annotations to the images as AimGraphic
        private void UpdateImagesWithProperAimGraphic(List <aim_dotnet.Annotation> annotations, List <IGraphic> annotationsGraphic)
        {
            if (annotationsGraphic != null)
            {
                foreach (IGraphic graphic in annotationsGraphic)
                {
                    // For now, we should be creating annotations only for the current image
                    // Code below that reads annotations back depends on this.
                    // Otherwise we either need to keep track of images from which we delete graphics (could be different from the images for the annotations?)
                    // or to pass in a list of images.
                    Debug.Assert(graphic.ParentPresentationImage == null || graphic.ParentPresentationImage == this.ImageViewer.SelectedPresentationImage);

                    IOverlayGraphicsProvider currentOverlayGraphics = graphic.ParentPresentationImage as IOverlayGraphicsProvider;
                    if (currentOverlayGraphics != null && currentOverlayGraphics.OverlayGraphics != null)
                    {
                        currentOverlayGraphics.OverlayGraphics.Remove(graphic);
                    }
                }
            }

            if (annotations != null)
            {
                foreach (aim_dotnet.Annotation annotation in annotations)
                {
                    AimHelpers.ReadGraphicsFromAnnotation(annotation, this.ImageViewer.SelectedPresentationImage);
                }
            }
        }
        // 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 void OnDisplaySetChanged(object sender, DisplaySetChangedEventArgs e)
        {
            if (e.NewDisplaySet != null && e.NewDisplaySet.Visible)
            {
                var uidCnt = new Dictionary <string, int>();
                foreach (var img in e.NewDisplaySet.PresentationImages)
                {
                    var imageSopPrivider = img as IImageSopProvider;
                    if (imageSopPrivider != null)
                    {
                        var studyInstanceUID = imageSopPrivider.ImageSop.StudyInstanceUid;
                        if (uidCnt.ContainsKey(studyInstanceUID))
                        {
                            uidCnt[studyInstanceUID] += 1;
                        }
                        else
                        {
                            uidCnt[studyInstanceUID] = 1;
                        }
                    }
                }

                var annotationList = new List <aim_dotnet.Annotation>();
                var dcmModel       = new aim_dotnet.DcmModel();
                foreach (var studyInstanceUID in uidCnt.Keys)
                {
                    InitializeAnnotationList(studyInstanceUID);
                    var annotationSopsInfo = _annotationDictionary[studyInstanceUID];
                    if (annotationSopsInfo == null)
                    {
                        continue;
                    }

                    foreach (var information in annotationSopsInfo)
                    {
                        try
                        {
                            annotationList.AddRange(dcmModel.ReadAnnotationsFromFile(information.InstanceFileName));
                        }
                        catch (Exception ex)
                        {
                            Platform.Log(LogLevel.Error, ex);
                        }
                    }
                }

                foreach (var annotation in annotationList)
                {
                    foreach (var img in e.NewDisplaySet.PresentationImages)
                    {
                        AimHelpers.ReadGraphicsFromAnnotation(annotation, img);
                    }
                }
            }
        }
        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));
        }
Beispiel #6
0
        private void OnDisplaySetChanged(object sender, DisplaySetChangedEventArgs e)
        {
            // toDO - init annotation dictionary here!
            Console.WriteLine("OnDisplaySetChanged");

            // TODO - add/remove DisplaySet.PresentationImages.ItemAdded event handlers

            if (e.NewDisplaySet != null && e.NewDisplaySet.Visible)
            {
                Dictionary <string, int> uidCnt = new Dictionary <string, int>();
                foreach (IPresentationImage img in e.NewDisplaySet.PresentationImages)
                {
                    IImageSopProvider imageSopPrivider = img as IImageSopProvider;
                    if (imageSopPrivider != null)
                    {
                        string studyInstanceUID = imageSopPrivider.ImageSop.StudyInstanceUid;
                        if (uidCnt.ContainsKey(studyInstanceUID))
                        {
                            uidCnt[studyInstanceUID] += 1;
                        }
                        else
                        {
                            uidCnt[studyInstanceUID] = 1;
                        }
                    }
                }

                // 1. Init annotation objects
                List <aim_dotnet.Annotation> annotationList = new List <aim_dotnet.Annotation>();
                aim_dotnet.DcmModel          dcmModel       = new aim_dotnet.DcmModel();
                foreach (string studyInstanceUID in uidCnt.Keys)
                {
                    InitializeAnnotationList(studyInstanceUID);
                    List <AimSopInstanceInformation> annotationSopsInfo = _annotationDictionary[studyInstanceUID];
                    if (annotationSopsInfo == null)
                    {
                        continue;
                    }

                    foreach (AimSopInstanceInformation information in annotationSopsInfo)
                    {
                        try
                        {
                            annotationList.AddRange(dcmModel.ReadAnnotationsFromFile(information.InstanceFileName));
                        }
                        catch (Exception ex)
                        {
                            Platform.Log(LogLevel.Error, ex);
                        }
                    }
                }

                // 2. put markup on the images
                foreach (aim_dotnet.Annotation annotation in annotationList)
                {
                    foreach (IPresentationImage img in e.NewDisplaySet.PresentationImages)
                    {
                        AimHelpers.ReadGraphicsFromAnnotation(annotation, img);
                    }
                }
            }
        }
        public void CreateAnnotation()
        {
            if (!CreateAnnotationEnabled)
            {
                return;
            }

            if (base.HasValidationErrors)
            {
                Platform.Log(LogLevel.Info, "Cannot create annotation. Validation error(s): " + Validation.GetErrorsString(this));
                ShowValidation(true);
                return;
            }

            if (!LocalDataStoreActivityMonitor.IsConnected)
            {
                DesktopWindow.ShowMessageBox("Failed to save annotation. Not connected to the local data store. Is workstation service running?", MessageBoxActions.Ok);
                return;
            }

            // Get new annotations
            List <IGraphic> annotationsGraphic;
            var             annotations = CreateAnnotationsFromUserInput(out annotationsGraphic);

            _annotationGraphics = annotationsGraphic;
            if (annotations.Count == 0)
            {
                Platform.Log(LogLevel.Warn, "CreateAnnotation resulted in no annotations being created");
                return;
            }

            EventsHelper.Fire(AnnotationCreating, this, EventArgs.Empty);

            var isAnyOperationPerformed = false;

            if (AimSettings.Default.StoreXmlAnnotationsLocally)
            {
                var destinationFolder = AimSettings.Default.ActualAnnotationStoreFolder;
                try
                {
                    if (!System.IO.Directory.Exists(destinationFolder))
                    {
                        System.IO.Directory.CreateDirectory(destinationFolder);
                    }

                    var annFiles = AimHelpers.WriteXmlAnnotationsToFolder(annotations, destinationFolder);
                    if (annotations.Count != annFiles.Length)
                    {
                        Platform.Log(LogLevel.Error, "Only {0} annotations(s) out of {1} written to \"{0}\"", annFiles.Length, annotations.Count, destinationFolder);
                    }

                    isAnyOperationPerformed = annFiles.Length > 0;
                }
                catch (Exception ex)
                {
                    Platform.Log(LogLevel.Error, ex, "Failed to store annotations to local folder \"{0}\"", destinationFolder);
                }
            }

            if (AimSettings.Default.SendNewXmlAnnotationsToGrid)
            {
                if (SendAnnotationsToGrid(annotations))
                {
                    isAnyOperationPerformed = true;
                }
                else
                {
                    Platform.Log(LogLevel.Error, "Failed to send annotations to the caGrid");
                }
            }

            if (LocalDataStoreActivityMonitor.IsConnected)
            {
                isAnyOperationPerformed = SendAnnotationsToLocalStorageAndPacs(annotations) || isAnyOperationPerformed;
            }
            else
            {
                DesktopWindow.ShowMessageBox("Failed to save annotation. Not connected to the local data store. Is workstation service running?", MessageBoxActions.Ok);
            }

            if (isAnyOperationPerformed)
            {
                UpdateImagesWithProperAimGraphic(annotations, annotationsGraphic);

                // Reset all controls on the template
                //ResetAnnotationData();
            }
        }
        // Main method called by templates to create annotaion(s)
        public void CreateAnnotation()
        {
            if (!this.CreateAnnotationEnabled)
            {
                return;
            }

            if (base.HasValidationErrors)
            {
                this.ShowValidation(true);
                return;
            }

            // We should bail out here even we only want to store it locally and/or to the grid.
            // There will be no way for us to display the new annotation back to the user after we remove the original markup.
            if (!LocalDataStoreActivityMonitor.IsConnected)
            {
                this.DesktopWindow.ShowMessageBox("Failed to save annotation. Not connected to the local data store. Is workstation service running?", MessageBoxActions.Ok);
                return;
            }

            // Get new annotations
            List <IGraphic> annotationsGraphic;
            List <aim_dotnet.Annotation> annotations = this.CreateAnnotationsFromUserInput(out annotationsGraphic);

            if (annotations.Count == 0)
            {
                Platform.Log(LogLevel.Warn, "CreateAnnotation resulted in no annotations being created");
                return;
            }

            bool isAnyOperationPerformed = false;

            // Save AIM XML to the Desktop
            if (AimSettings.Default.SaveXmlToDesktop)
            {
                int annCnt = AimHelpers.WriteXmlAnnotationsToFolder(annotations, Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
                if (annotations.Count != annCnt)
                {
                    Platform.Log(LogLevel.Error, "Only {0} annotations(s) out of {1} written to the Desktop", annCnt, annotations.Count);
                }

                isAnyOperationPerformed = annCnt > 0 || isAnyOperationPerformed;
            }

            // Save AIM XML Annotations locally
            if (AimSettings.Default.StoreXmlAnnotationsLocally)
            {
                string destinationFolder
                    = AimSettings.Default.StoreXmlInMyDocuments
                                                ?
                      System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "AIM Annotations")
                                                :
                      AimSettings.Default.LocalAnnotationsFolder;
                try
                {
                    if (!System.IO.Directory.Exists(destinationFolder))
                    {
                        System.IO.Directory.CreateDirectory(destinationFolder);
                    }

                    int annCnt = AimHelpers.WriteXmlAnnotationsToFolder(annotations, destinationFolder);
                    if (annotations.Count != annCnt)
                    {
                        Platform.Log(LogLevel.Error, "Only {0} annotations(s) out of {1} written to \"{0}\"", annCnt, annotations.Count, destinationFolder);
                    }

                    isAnyOperationPerformed = annCnt > 0 || isAnyOperationPerformed;
                }
                catch (Exception ex)
                {
                    Platform.Log(LogLevel.Error, ex, "Failed to store annotations to local folder \"{0}\"", destinationFolder);
                }
            }

            // RSNA code. Real one is at the start of the method
            if (LocalDataStoreActivityMonitor.IsConnected)
            {
                isAnyOperationPerformed = this.SendAnnotationsToLocalStorageAndPacs(annotations) || isAnyOperationPerformed;
            }
            else
            {
                this.DesktopWindow.ShowMessageBox("Failed to save annotation. Not connected to the local data store. Is workstation service running?", MessageBoxActions.Ok);
            }

            if (isAnyOperationPerformed)
            {
                // Remove all IGraphic used to create annotations and add them back as AimGraphic
                this.UpdateImagesWithProperAimGraphic(annotations, annotationsGraphic);

                // Reset all controls on the template
                this.ResetAnnotationData();
            }
        }