locked
Convert GeometryGroup to a path

    Question

  • This is a question regarding extracting a path from a GeometryGroup, it is a bit in relation to another question, but not the same. Lets say I have the following code:

    //TEST ---------------------------------------------------------
                        System.Windows.Shapes.Path myPath = new System.Windows.Shapes.Path();
                        SolidColorBrush blackBrush = new SolidColorBrush();
                        myPath.StrokeThickness = 1;
                        SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                        mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255);
                        myPath.Fill = mySolidColorBrush;
    
                        // Create the line geometry to add to the Path
                        LineGeometry myLineGeometry = new LineGeometry();
                        myLineGeometry.StartPoint = new Point(10, 10);
                        myLineGeometry.EndPoint = new Point(50, 30);
    
                        // Create the ellipse geometry to add to the Path
                        EllipseGeometry myEllipseGeometry = new EllipseGeometry();
                        myEllipseGeometry.Center = new Point(40, 70);
                        myEllipseGeometry.RadiusX = 30;
                        myEllipseGeometry.RadiusY = 30;
    
                        // Create a rectangle geometry to add to the Path
                        RectangleGeometry myRectGeometry = new RectangleGeometry();
                        myRectGeometry.Rect = new Rect(30, 55, 100, 30);
    
                        // Add all the geometries to a GeometryGroup.
                        GeometryGroup myGeometryGroup = new GeometryGroup();
                        myGeometryGroup.Children.Add(myLineGeometry);
                        myGeometryGroup.Children.Add(myEllipseGeometry);
                        myGeometryGroup.Children.Add(myRectGeometry);
                        string temp = myGeometryGroup.ToString();
                        myPath.Data = myGeometryGroup;

    This creates something like the below picture

    This is possible to display by using the variable myPath. Lets say I want to store this path of the new object, is it possible can I extract it from a GeometryGroup? It is possible with individual Geometries, would it not be possible to comebine the geometries to extract one path?

    My final goal is to create geometries of paths, and combine these to one path in c# code is this possible? I have looked at it for some months, see my stack question. But have not been able to solve it yet, any ideas?

    Saturday, August 9, 2014 1:10 PM

All replies

  • HI JOTM123,

    >is it possible can I extract it from a GeometryGroup? It is possible with individual Geometries, would it not be possible to comebine the geometries to extract one path?

    I assume you want to save the path to storage and then use them to create geometry when necessary. http://stackoverflow.com/questions/18197696/isolated-storage-saving-multiple-objects. Note you need to change the code to fit your platforms.

    >My final goal is to create geometries of paths, and combine these to one path in c# code is this possible?

    No, there is no existing XAML control for this requirement. But you can use C# or other language to create geometries of paths at runtime.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 12, 2014 6:29 AM
    Moderator
  • I assume you want to save the path to storage and then use them to create geometry when necessary. http://stackoverflow.com/questions/18197696/isolated-storage-saving-multiple-objects. Note you need to change the code to fit your platforms.

    Okay maybe I should just try and save it in isolate storage and create the geometrygroup everytime. I think that will work looking in hindsight. I allready have code for this, I think I looked blindly at creating a new path. Thank you for this. 
    Regarding

    No, there is no existing XAML control for this requirement. But you can use C# or other language to create geometries of paths at runtime.

    Yes it is true that I in theory can create these geometries from paths at runtime, it however does not work for me. Using this code:

    public static Geometry PathMarkupToGeometry(ShieldGearViewModel shieldGearModelRec)
            {
                string pathMarkup = shieldGearModelRec.Gear.Path;
                try
                {
                    string xaml =
                    "<Path " +
                    "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
                    "<Path.Data>" + pathMarkup + "</Path.Data></Path>";
                    var path = System.Windows.Markup.XamlReader.Load(xaml) as System.Windows.Shapes.Path;
                    // Detach the PathGeometry from the Path
                    if (path != null)
                    {
                        path.Height = shieldGearModelRec.Gear.Height;
                        path.Width = shieldGearModelRec.Gear.Width;
                        path.Fill = new SolidColorBrush(Colors.Green);
                        path.Stretch = Stretch.Fill;
                        Geometry geometry = path.Data;
    //Test which did not work
                        Rect transRect = new Rect(shieldGearModelRec.Gear.x, shieldGearModelRec.Gear.y, shieldGearModelRec.Gear.Width, shieldGearModelRec.Gear.Height);
                        //geometry.Transform.TransformBounds(transRect);
                        path.Data = null;
                        return geometry;
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
                return null;
            }

    I can greate a geometry that has the correct size(width and height). But when I add this to geometrygroup its bounds is set to zero and is not shown in the geometry group. As can be seen with the elements RectangleGeometry and EllipseGeometry in the code, in my initial question. Furthermore to add it to a geometry group you should be able to specify position of the element you insert in the geometrygroup, to make it possible to create a object from a series of paths.

    what is the issue ? Is it using geometries where I instead should use pathgeometries? I cannot understand why the geometry looks correct from the included code with path, width and height but disappears when I add it to the geometrygroup 

    Tuesday, August 12, 2014 8:09 AM