Esempio n. 1
0
        /// <summary>
        /// Remove all unneeded groups with clipping set that doesn't clip anything
        /// In sum this is a very, very expensive operation. But it helps to
        /// create cleaner and faster code without unnecessary clipping
        /// </summary>
        private static Geometry RemoveClipping(GraphicVisual visual)
        {
            Geometry geometry = null;

            switch (visual)
            {
            case GraphicGroup group:
            {
                var childrenGeometry = new PathGeometry();
                geometry = childrenGeometry;

                foreach (var childVisual in group.Children)
                {
                    var childgeometry = RemoveClipping(childVisual);
                    childrenGeometry.AddGeometry(childgeometry);
                }

                if (group.Clip != null)
                {
                    var groupClipGeometry = GeometryBinaryGenerator.GenerateGeometry(group.Clip);
                    var intersection      = groupClipGeometry.FillContainsWithDetail(childrenGeometry, 0.0001, ToleranceType.Absolute);

                    if (intersection == IntersectionDetail.FullyContains)
                    {
                        group.Clip = null;
                    }
                    else
                    if (intersection == IntersectionDetail.Intersects)
                    {
                        var pen1 = new Pen(Brushes.Black, 2);
                        groupClipGeometry = groupClipGeometry.GetWidenedPathGeometry(pen1);
                        groupClipGeometry = groupClipGeometry.GetOutlinedPathGeometry();

                        var pen2 = new Pen(Brushes.Black, 1);
                        childrenGeometry = childrenGeometry.GetWidenedPathGeometry(pen2);
                        childrenGeometry = childrenGeometry.GetOutlinedPathGeometry();

                        intersection = groupClipGeometry.FillContainsWithDetail(childrenGeometry, 0.0001, ToleranceType.Absolute);

                        if (intersection == IntersectionDetail.FullyContains)
                        {
                            group.Clip = null;
                        }
                    }
                }

                break;
            }

            case GraphicPath graphicPath:
            {
                geometry = GeometryBinaryGenerator.GenerateGeometry(graphicPath.Geometry);
                break;
            }
            }

            return(geometry);
        }
        /// <summary>
        /// Create a GeometryDrawing from a single graphic path
        /// </summary>
        private static Drawing GeneratePath(GraphicPath graphicPath)
        {
            var geometryDrawing = new GeometryDrawing();

            geometryDrawing.Geometry = GeometryBinaryGenerator.GenerateGeometry(graphicPath.Geometry);
            SetColors(graphicPath, geometryDrawing);

            return(geometryDrawing);
        }
        /// <summary>
        /// Generates a WPF drawing from the specified geometry tree.
        /// </summary>
        private static Drawing GenerateDrawing(GraphicVisual visual)
        {
            Drawing drawing = null;

            switch (visual)
            {
            case GraphicGroup group:
            {
                var drawingGroup = new DrawingGroup();
                drawing = drawingGroup;
                drawingGroup.Opacity = group.Opacity;

                if (group.Clip != null)
                {
                    drawingGroup.ClipGeometry = GeometryBinaryGenerator.GenerateGeometry(group.Clip);
                }

                foreach (var childVisual in group.Children)
                {
                    var childDrawing = GenerateDrawing(childVisual);
                    drawingGroup.Children.Add(childDrawing);
                }

                break;
            }

            case GraphicPath graphicPath:
            {
                drawing = GeneratePath(graphicPath);

                break;
            }
            }

            return(drawing);
        }