private void ReplacePaths(Canvas canvas, IEnumerable <Infrastructure.Data.PointsSequence> sequences, bool isClosed, VGKind vgKind) { // remove all old paths having the same VGKind var indicesToRemove = (from item in canvas.Children.Cast <object>().ZipIndex() where item.Value is Path let path = (Path)item.Value where GetVGKind(path) == vgKind select item.Index ).ToArray(); Array.Reverse(indicesToRemove); foreach (var index in indicesToRemove) { canvas.Children.RemoveAt(index); } // add new paths if (sequences != null) { var paths = from pointsSequence in sequences select CreatePath(pointsSequence, isClosed, vgKind); foreach (var path in paths) { canvas.Children.Add(path); } } }
private Path CreatePath(Infrastructure.Data.PointsSequence polylineData, bool isClosed, VGKind vgKind) { var points = (from pnt in polylineData.Points select new Point { X = pnt.X, Y = pnt.Y }).ToArray(); var geometry = new StreamGeometry(); using (var context = geometry.Open()) { context.BeginFigure(points[0], false, isClosed); context.PolyLineTo(points.Skip(1).ToList(), true, false); } var scaleTransform = new ScaleTransform(); scaleTransform.Bind(ScaleTransform.ScaleXProperty, () => polyRoot.ActualWidth, width => width / 2); scaleTransform.Bind(ScaleTransform.ScaleYProperty, () => polyRoot.ActualHeight, height => height / 2); var translateTransform = new TranslateTransform(); translateTransform.Bind(TranslateTransform.XProperty, () => polyRoot.ActualWidth, width => width / 2); translateTransform.Bind(TranslateTransform.YProperty, () => polyRoot.ActualHeight, height => height / 2); var transformGroup = new TransformGroup { Children = { scaleTransform, translateTransform } }; geometry.Transform = transformGroup; var path = new Path(); path.Data = geometry; path.DataContext = polylineData; path.Bind(Path.StrokeProperty, () => polylineData.CurveCategory, () => polylineData.ColorCodingIndex, (curveCategory, colorCodingIndex) => { if (colorCodingIndex != PointsSequence.INVALID_COLOR_CODING) { return(Constants.PRIMITIVE_CURVES_COLOR_CODING[colorCodingIndex]); } else { switch (curveCategory) { case CurveCategories.None: return(SKETCH_STROKE_UNCATEGORIZED); case CurveCategories.Feature: return(SKETCH_STROKE_FEATURE); case CurveCategories.Silhouette: return(SKETCH_STROKE_SILHOUETTE); default: return(Binding.DoNothing); } } }); path.Bind(Path.StrokeThicknessProperty, () => polylineData.IsSelected, () => polylineData.IsEmphasized, () => polylineData.CurveCategory, (isSelected, isEmphasized, curveCategory) => { if (isSelected || isEmphasized) { return(4.0); } if (curveCategory == CurveCategories.Feature) { return(2.0); } return(1.0); }); SetVGKind(path, vgKind); return(path); }
private static void SetVGKind(Path path, VGKind value) { path.SetValue(VGKindProperty, value); }