locked
Creating Geometry Elements based on Paths

    Question

  • Hello I have had this problem for many months with the question evolving on stackoverflow QuestionOnStack. But the question has become so broad because of I did not know the correct question in the start. Firstly to specify the frame of the question.

    The user has many different shapes, all created as usercontrols. The shapes are based on path's such that they can be defined in all shapes by our designer. This works fine.

    The user than drags a number of elements into a canvas, and creates a shape. I then want to convert this shape to one shape, i.e. get one path for this new element. 

    For this I create A geometry element of the individual usercontrols. I have succeeded in creating the elements based on:

    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;
            }

    Then I add these into a geometry collection to enable it to become a path. Here is my first problem. I need to specify x y coordinates of the elements when I add them to the group? and create a bound for the geometrygroup. But this I have not succeeded in.

    Basicly my question is:
    The user adds elements together, and I want to create a new element based on these figures, such that a new path can be made for the users defined element. Lets say I have a square and a triangle. The user combines this to a house. Now I want to make the house an element for the user. For this I need the path of the element, how do I create this?

    Friday, August 08, 2014 10:58 AM

All replies

  • HI JOTM123,

    >For this I need the path of the element, how do I create this?

    Blend can easily combine any shapes together to form a new shape. Per my understanding, you are doing what blend does. Have a look at this tool, http://msdn.microsoft.com/en-us/library/cc296376.aspx.

    If I misunderstand, please post more details about this question.

    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.

    Monday, August 11, 2014 6:59 AM
    Moderator
  • You are correct that blend can do this from drawings and drag. But this does not help me. My question is that code like:

     //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.FillRule = FillRule.Nonzero;
                        myGeometryGroup.Children.Add(myLineGeometry);
                        myGeometryGroup.Children.Add(myEllipseGeometry);
                        myGeometryGroup.Children.Add(myRectGeometry);
                        //myGeometryGroup.Children.Add(geometry);
                        myPath.Data = myGeometryGroup;

    Produces a shape I can add to a canvas as a path. Which results in a picture like:

    But when I add an element to myGeometryGroup based on a path like I show in the code in the question, I get bounds equal to zero for the object such that it is not displayed. Therefore the inserted picture stays the same. So the first question is how do I add a shape to geometrygroup initialized by a path, (extra information I have is width, height, x, y, and z which can also be used for initialization).

    The second question is how do I programmatically from a GeometryGroup extract a path of the entire geometry added to the geometry group.

    Is this at all possible or should I look into other methods, I mean it should be possible to add path elements together to get a new path. I found something like combinegeometries but it is not available in Windows Phone.


    Monday, August 11, 2014 7:46 AM
  • HI JOTM123,

    What platform you are using? Per my understanding, System.Windows.Shapes.Path is not in store app. I assume you are doing this in windows phone Silverlight project. If so, I am going to move this thread into windows phone forum for effective response.

    Thank you for understanding.

    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.

    Wednesday, August 20, 2014 2:16 AM
    Moderator