//internal static string GetAimHtml(aim4_dotnet.AnnotationEntity annotation, aim4_dotnet.User aimUserInfo)
        internal static string GetAimHtml(Aim4AnnotationInstance annotationInstance)
        {
            var annotation = annotationInstance == null ? null : annotationInstance.AimAnnotationEntity;

            if (annotation == null)
            {
                return(GetEmptyHtml());
            }

            aim4_dotnet.User aimUserInfo = null;
            if (annotationInstance.ParentAimDocument is Aim4DocumentInstance)
            {
                aimUserInfo = ((Aim4DocumentInstance)annotationInstance.ParentAimDocument).AnnotationCollection.User;
            }

            if (annotation is aim4_dotnet.ImageAnnotation)
            {
                return(GetImageAnnotationHtml((aim4_dotnet.ImageAnnotation)annotation, aimUserInfo));
            }
            if (annotation is aim4_dotnet.AnnotationOfAnnotation)
            {
                return(GetAnnotationOfAnnotationHtml((aim4_dotnet.AnnotationOfAnnotation)annotation, aimUserInfo));
            }


            Platform.Log(LogLevel.Error, "Annotation Display Formatting (HTML): unknown annotation type");
            Debug.Assert(false, "Uknown Annotation Type");
            return("Unknown Annotation Type");
        }
        public static AimTemplateTree AimAnnotationToAimTemplateTree(Aim4AnnotationInstance annotationInstance, AimTemplateTree template)
        {
            if (annotationInstance == null || annotationInstance.AimAnnotationEntity == null)
                return null;

            var annotation = annotationInstance.AimAnnotationEntity;
            if (annotation.ImagingPhysicalEntityCollection != null)
            {
                foreach (var imgPhysEnt in annotation.ImagingPhysicalEntityCollection)
                {
                    if (IsNullCodeList(imgPhysEnt.TypeCode))
                        continue;

                    AimTemplateTreeAnatomicEntityNode matchingTreeNode = FromImagingPhysicalEntity(imgPhysEnt, template);
                    if (matchingTreeNode == null)
                        return null;
                }
            }
            if (annotation.ImagingObservationEntityCollection != null)
            {
                foreach (var io in annotation.ImagingObservationEntityCollection)
                {
                    if (IsNullCodeList(io.TypeCode))
                        continue;

                    AimTemplateTreeImagingObservationNode matchingTreeNode = FromImagingObservationEntity(io, template);
                    if (matchingTreeNode == null)
                        return null;
                }
            }
            if (annotation.CalculationEntityCollection != null)
            {
                //foreach (var calc in annotation.CalculationCollection)
                //{
                //    AimTemplateTreeCalculationNode matchingTreeNode =
                //        template.TemplateNodes.OfType<AimTemplateTreeCalculationNode>().FirstOrDefault(
                //            treeCalc => treeCalc.Label == calc.Description);
                //    if (matchingTreeNode != null)
                //    {
                //        AimTemplateTreeCalculationType matchingCalculationType =
                //            matchingTreeNode.CalculationTypes.FirstOrDefault(
                //                treeCalc => ToStandardCodeSequence(treeCalc).CodeValue == calc.CodeValue);
                //        if (matchingCalculationType != null)
                //        {
                //            matchingTreeNode.SelectedCalculationType = matchingCalculationType;
                //        }
                //        else
                //            return null;

                //        //if (matchingTreeNode.HasConfidence && calc..HasValue)
                //        //    matchingTreeNode.ConfidenceValue = calc.AnnotatorConfidence.Value;
                //    }
                //    else
                //        return null;
                //}
            }
            if (annotation.InferenceEntityCollection != null)
            {
                foreach (var inference in annotation.InferenceEntityCollection)
                {
                    if (IsNullCodeList(inference.TypeCode))
                        continue;

                    foreach (var inferenceNode in template.TemplateNodes.OfType<AimTemplateTreeInferenceNode>())
                    {
                        AimTemplateTreeAllowedTerm matchingAllowedTerm =
                            inferenceNode.AllowedTerms.FirstOrDefault(term => DoTheyMatch(term, inference.TypeCode));
                        if (matchingAllowedTerm != null)
                            matchingAllowedTerm.Selected = true;
                    }
                }
            }
            template.Markup.Clear();
            if (annotation is aim4_dotnet.ImageAnnotation)
            {
                var imageAnnotation = (aim4_dotnet.ImageAnnotation)annotation;
                var annotationName = string.IsNullOrEmpty(imageAnnotation.Name) ? "" : imageAnnotation.Name;

                if (imageAnnotation.MarkupEntityCollection != null)
                {
                    foreach (var markupEntity in imageAnnotation.MarkupEntityCollection)
                    {
                        if (markupEntity is aim4_dotnet.GeometricShapeEntity)
                        {
                            var markup = ToMarkup2D((aim4_dotnet.GeometricShapeEntity)markupEntity, annotationName);
                            if (markup != null)
                                template.Markup.Add(markup);
                        }
                        else if (markupEntity is aim4_dotnet.TextAnnotationEntity)
                        {
                            var textAnnotationEntity = (aim4_dotnet.TextAnnotationEntity) markupEntity;
                            if (textAnnotationEntity.GeometricShapeEntity != null)
                            {
                                var calloutText = AimHelpers.FormatPointCalloutText(annotationName, textAnnotationEntity.Text);
                                aim4_dotnet.TwoDimensionGeometricShapeEntity geoShapeEntity = null;
                                if (textAnnotationEntity.GeometricShapeEntity is aim4_dotnet.TwoDimensionGeometricShapeEntity)
                                {
                                    var twoDGeoShape = (aim4_dotnet.TwoDimensionGeometricShapeEntity) textAnnotationEntity.GeometricShapeEntity;
                                    switch (twoDGeoShape.TwoDimensionSpatialCoordinateCollection.Count)
                                    {
                                        case 1:
                                            {
                                                var markup = new MarkupPoint();
                                                markup.Name = annotationName;
                                                markup.IncludeInAnnotation = twoDGeoShape.IncludeFlag;
                                                markup.Point = AsPointF(twoDGeoShape.TwoDimensionSpatialCoordinateCollection[0]);
                                                markup.CalloutText = calloutText;
                                                markup.CalloutLocation = new PointF(markup.Point.X, markup.Point.Y);
                                                SetMarkupImageReference(markup, twoDGeoShape);
                                                markup.UseCrosshair = AimSettings.Default.UseCrosshairsForTextCallouts;
                                                template.Markup.Add(markup);
                                            }
                                            break;
                                        case 2:
                                            {
                                                var markup = new MarkupPoint();
                                                markup.Name = annotationName;
                                                markup.IncludeInAnnotation = twoDGeoShape.IncludeFlag;
                                                markup.Point = AsPointF(twoDGeoShape.TwoDimensionSpatialCoordinateCollection[0]);
                                                markup.CalloutText = calloutText;
                                                markup.CalloutLocation = AsPointF(twoDGeoShape.TwoDimensionSpatialCoordinateCollection[1]);
                                                SetMarkupImageReference(markup, twoDGeoShape);
                                                markup.UseCrosshair = AimSettings.Default.UseCrosshairsForTextCallouts;
                                                template.Markup.Add(markup);
                                            }
                                            break;
                                        default:
                                            Platform.Log(LogLevel.Error, "TextAnnotation has [{0}] Connector Points", twoDGeoShape.TwoDimensionSpatialCoordinateCollection.Count);
                                            break;
                                    }
                                }
                                else if (textAnnotationEntity.GeometricShapeEntity is aim4_dotnet.ThreeDimensionGeometricShapeEntity)
                                {
                                    // TODO: implement 2D markup conversion!
                                    // TODO: convert to the 2D ImageReferenceUid and referencedFrameNumber!

                                    var threeDGeoShape = (aim4_dotnet.ThreeDimensionGeometricShapeEntity) textAnnotationEntity.GeometricShapeEntity;
                                    switch (threeDGeoShape.ThreeDimensionSpatialCoordinateCollection.Count)
                                    {
                                        case 1:
                                            throw new NotImplementedException();
                                            break;
                                        case 2:
                                            throw new NotImplementedException();
                                            break;
                                        default:
                                            Platform.Log(LogLevel.Error, "TextAnnotation has [{0}] 3D Connector Points", threeDGeoShape.ThreeDimensionSpatialCoordinateCollection.Count);
                                            break;
                                    }
                                }
                            }
                        }
                        else
                            Debug.Assert(false, "Unexpected markup type");
                    }
                }
            }
            return template;
        }
        //internal static string GetAimHtml(aim4_dotnet.AnnotationEntity annotation, aim4_dotnet.User aimUserInfo)
        internal static string GetAimHtml(Aim4AnnotationInstance annotationInstance)
        {
            var annotation = annotationInstance == null ? null : annotationInstance.AimAnnotationEntity;
            if (annotation == null)
                return GetEmptyHtml();

            aim4_dotnet.User aimUserInfo = null;
            if (annotationInstance.ParentAimDocument is Aim4DocumentInstance)
                aimUserInfo = ((Aim4DocumentInstance) annotationInstance.ParentAimDocument).AnnotationCollection.User;

            if (annotation is aim4_dotnet.ImageAnnotation)
                return GetImageAnnotationHtml((aim4_dotnet.ImageAnnotation)annotation, aimUserInfo);
            if (annotation is aim4_dotnet.AnnotationOfAnnotation)
                return GetAnnotationOfAnnotationHtml((aim4_dotnet.AnnotationOfAnnotation)annotation, aimUserInfo);

            Platform.Log(LogLevel.Error, "Annotation Display Formatting (HTML): unknown annotation type");
            Debug.Assert(false, "Uknown Annotation Type");
            return "Unknown Annotation Type";
        }