示例#1
0
 public void RemoveShape(XDrawingShape shape)
 {
     if (SelectedShape == shape)
     {
         SelectedControlPoint = null;
         SelectedShape        = null;
     }
     shape.Dispose();
     m_Shapes.Remove(shape);
 }
示例#2
0
        /// <summary>
        /// Imports a drawing from xml.
        /// </summary>
        /// <param name="xElement">Xml element too start searching</param>
        /// <param name="clearDrawing">remove all shapes before import</param>
        /// <returns>true if all xml element could be loaded, false if unknown (skiped) elements have been found</returns>
        /// <remarks>
        /// Searches for the 1st DrawingImage, if none is found look for Canvas
        /// </remarks>
        public bool Import(XElement xElement, bool clearDrawing)
        {
            if (clearDrawing)
            {
                ClearDrawing();
            }

            // look for DrawingImage
            var xRoot = (from e in xElement.DescendantsAndSelf("DrawingImage")
                         select e).FirstOrDefault();

            if (xRoot != null)
            {
                // we need the 1st DrawingGroup
                xRoot = (from e in xElement.Descendants("DrawingGroup")
                         select e).FirstOrDefault();

                if (xRoot != null)
                {
                    foreach (var xGD in xRoot.Elements("GeometryDrawing"))
                    {
                        XDrawingShape shape = XDrawingShape.LoadFromGeometryDrawing(this, xGD);
                        if (shape != null)
                        {
                            m_Shapes.Add(shape);
                            Canvas.Children.Insert(Canvas.Children.IndexOf(m_ControlPointPath), shape.Path);
                        }
                    }
                }
            }
            else
            {
                // DrawingImage not found, look for Canvas
                xRoot = (from e in xElement.DescendantsAndSelf("Canvas")
                         select e).FirstOrDefault();

                if (xRoot != null)
                {
                    foreach (var xPath in xRoot.Elements("Path"))
                    {
                        XDrawingShape shape = XDrawingShape.LoadFromPath(this, xPath);
                        if (shape != null)
                        {
                            m_Shapes.Add(shape);
                            Canvas.Children.Insert(Canvas.Children.IndexOf(m_ControlPointPath), shape.Path);
                        }
                    }
                }
            }

            return(true);
        }
示例#3
0
 public ControlPoint(XDrawing drawing, XDrawingShape shape,
                     DependencyObject obj, DependencyProperty prop,
                     int dings, bool visible, bool isSelectable)
 {
     ConnectedTo    = null;
     Drawing        = drawing;
     Shape          = shape;
     m_Object       = obj;
     m_Prop         = prop;
     m_Dings        = dings;
     m_IsSelectable = isSelectable;
     if (m_Prop != null)
     {
         DependencyPropertyDescriptor.FromProperty(m_Prop, m_Object.GetType()).AddValueChanged(m_Object, PropertyChanged);
     }
     if (visible)
     {
         m_CPGeom = new EllipseGeometry(GetPoint(), 5.0 / Drawing.ZoomFactor, 5.0 / Drawing.ZoomFactor);
         Drawing.ControlPointGroup.Children.Add(m_CPGeom);
     }
 }
示例#4
0
        private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (Clipboard.ContainsText() && Clipboard.GetText().StartsWith("<GeometryDrawing"))
            {
                try
                {
                    var xaml = Clipboard.GetText();
                    var xDoc = XDocument.Parse(xaml);

                    XDrawingShape shape = XDrawingShape.LoadFromGeometryDrawing(this, xDoc.Root);
                    if (shape != null)
                    {
                        m_Shapes.Add(shape);
                        Canvas.Children.Insert(Canvas.Children.IndexOf(m_ControlPointPath), shape.Path);
                        SelectedShape = shape;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception while pasting");
                }
            }
        }
示例#5
0
        private static XDrawingShape CreateShape(XDrawing drawing, Brush fillBrush, string penThickness, string penDashCap,
                                                 string penStartLineCap, string penEndLineCap, string penLineJoin, string penMiterLimit,
                                                 Brush penBrush, string penDashArray, string penDashOffset, XElement xGeometry)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            var path = new Path();

            path.Fill = fillBrush;
            if (penThickness != null)
            {
                path.StrokeThickness = Double.Parse(penThickness, nfi);
            }
            if (penDashCap != null)
            {
                path.StrokeDashCap = (PenLineCap)Enum.Parse(typeof(PenLineCap), penDashCap);
            }
            if (penStartLineCap != null)
            {
                path.StrokeStartLineCap = (PenLineCap)Enum.Parse(typeof(PenLineCap), penStartLineCap);
            }
            if (penEndLineCap != null)
            {
                path.StrokeEndLineCap = (PenLineCap)Enum.Parse(typeof(PenLineCap), penEndLineCap);
            }
            if (penLineJoin != null)
            {
                path.StrokeLineJoin = (PenLineJoin)Enum.Parse(typeof(PenLineJoin), penLineJoin);
            }
            if (penMiterLimit != null)
            {
                path.StrokeMiterLimit = Double.Parse(penMiterLimit, nfi);
            }
            path.Stroke = penBrush;
            if (penDashArray != null)
            {
                XDrawingShape.StringToDashArray(penDashArray, path.StrokeDashArray);
            }
            if (penDashOffset != null)
            {
                path.StrokeDashOffset = Double.Parse(penDashOffset, nfi);
            }

            var types = typeof(XDrawingShape).Assembly.GetTypes();

            foreach (var type in types)
            {
                if (type.IsSubclassOf(typeof(XDrawingShape)))
                {
                    var attributes = type.GetCustomAttributes(typeof(XDrawGeometryAttribute), false) as XDrawGeometryAttribute[];
                    var geomName   = xGeometry.Name.LocalName;

                    // check if we have a TextGeometry, which is no "real" XAML node type
                    if (String.CompareOrdinal(geomName, "GeometryGroup") == 0)
                    {
                        var c = xGeometry.Parent.FirstNode as XComment;

                        if (c != null && c.Value.StartsWith("<TextGeometry"))
                        {
                            geomName = "TextGeometry";
                        }
                    }

                    if (attributes.Length >= 1 && String.CompareOrdinal(geomName, attributes[0].GeometryName) == 0)
                    {
                        var mi = type.GetMethod(
                            "CreateFromXml",
                            BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
                            null,
                            new Type[] { typeof(XDrawing), typeof(Path), typeof(XElement) },
                            null);
                        if (mi == null)
                        {
                            throw new Exception("shape type does not support static method CreateFromXml");
                        }
                        return(mi.Invoke(null, new object[] { drawing, path, xGeometry }) as XDrawingShape);
                    }
                }
            }

            throw new Exception("Unknown geometry type: " + xGeometry.Name);
        }