将路径转换为几何形状 [英] Convert path to geometric shape

查看:26
本文介绍了将路径转换为几何形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我试图总结并提出基本问题和我的想法,但到目前为止还行不通:S

Hey everyone I have tried to sum up and come with the basic question and my idea which does not work so far :S

基本上我的问题是:用户将元素添加到一起,我想根据这些图形创建一个新元素,以便可以为用户定义的元素创建新路径.假设我有一个正方形和一个三角形.用户将其组合成一所房子.现在我想让房子成为用户的一个元素.为此,我需要元素的路径,我该如何创建?

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?

我的想法使用的图形元素基于路径字符串.因此,我希望将这些转换为以后可以使用的几何元素.我在下面的答案中使用了 André Meneses 提供的代码,代码复制在这里:>

My Idea The figure elements which are used are based on path strings. Therefore I want these to be converted to a geometry element that I can use later on. I use the code supplied by André Meneses in the answer below, the code replicated here:

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 not working, exception is thrown
                //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;
    }

我这样做是为了让几何图形遵循 此链接描述.上面的问题是我无法访问新几何元素的 x 或 y 位置,那么我该如何指定这个位置?

This I do to get a Geometry to follow the example from this link describes. The problem with the above is that I cannot access x or y positions, of the new geometry element, so how do I specify this position?

对于这个职位,我认为 这个链接 可能是一个解决方案,只是还没有让它工作?:)

For the position I think this link might be a solution, just haven't gotten it working yet? :)

完成后,我将它添加到基于 before 链接的几何组,这样我就可以获得新元素的路径.但是几何组的边界为 0.因此,为此,我需要为各个几何元素定义 x 和 y,然后这可能会解决 geomtrygroup 问题,或者我必须在此之后查看:) 问题已经存在太久了:|

When this is done I add it to a geometrygroup based on the before link, such that I can get a path, for the new element. But the geometrygroup has 0 as bounds. So for this to work I need to define x and y for the individual geometry elements, and then this might solve the geomtrygroup problem or I then have to look at this after :) Question has been standing for toooo long :|

下面的文字是旧的问题和想法

我有一个字符串,我想在后面的代码中将其转换为几何形状.所以我在 Stackoverflow 上找到了这个 WPF C# 路径:如何从带有路径数据的字符串到代码中的几何图形(不在 XAML 中)

I have a string that I would like to convert to a geometric shape in the code behind. So I found this on Stackoverflow WPF C# Path: How to get from a string with Path Data to Geometry in Code (not in XAML)

此链接建议您可以使用以下代码使用解析将字符串转换为路径:

This link suggests that one can convert a string to path using parse with the following code:

var path = new Path();
path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 400,175 H 280");

然而,解析在 Windows Phone 上不可用.我的其他努力并没有解决这个问题.我尝试使用 pathGeometry 但似乎无法将字符串设置为路径?

However parse is not available on Windows Phone. My other efforts has not solved the issue. I tried with pathGeometry but did not seem to be possible to set a string as the path?

所以我的问题是如何在不绑定到视图元素的代码中将字符串转换为几何形状.

So my problem is how to convert a string to a geometric shape in code behind not binding to an element on view.

第一步所以我成功地用以下内容创建了一条路径

First Step So I succeeded in creating a path with the following

var pathTesting = new System.Windows.Shapes.Path();
var b = new System.Windows.Data.Binding
{
    Source = DecorationOnShield[i].Gear.Path
};
System.Windows.Data.BindingOperations.SetBinding(pathTesting, System.Windows.Shapes.Path.DataProperty, b);

现在我正在尝试将路径转换为几何形状.

Now I am trying to convert the path to a geometric shape.

额外

我的想法是和这个链接描述的一样.示例显示的位置:

My idea is to do the same as this link describes. Where the example shows :

var blackRectGeometry = new RectangleGeometry();
Rect rct = new Rect();
rct.X = 80;
rct.Y = 167;
rct.Width = 150;
rct.Height = 30;
blackRectGeometry.Rect = rct;

但不是矩形,我想使用 pat 形式的任意形状h,但仍然可以设置诸如 坐标之类的信息size.

But instead of rectangle I would like to use an arbitrary shape in form of a path, but still be able to set information such as coordinates and size.

额外

我想定义一个用户控件,它由一个看起来像这样的路径组成:

I was thinking of defining a usercontrol which consisted of a path only looking like this:

<UserControl x:Class="UndoRedoShield.View.Geometry"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
mc:Ignorable="d"
Canvas.Left="{Binding Gear.x}" Canvas.Top="{Binding Gear.y}">


<Path Data="{Binding Gear.Path}" Fill="{Binding Gear.Color}" Stretch="Fill" UseLayoutRounding="False" Height="{Binding Gear.Height}" Width="{Binding Gear.Width}" Opacity="{Binding Gear.Opacity}">
    <Path.RenderTransform>
        <ScaleTransform ScaleX="{Binding Gear.Scale}" ScaleY="{Binding Gear.Scale}"/>
    </Path.RenderTransform>

</Path>

</UserControl>

但是无法以任何方式使用它来处理几何.任何人都知道使用这种方法吗?任何方法表示赞赏!:)

But have not been able to use it in any way regarding geometry. Anyone have any idea using this method? Any method is appreciated ! :)

额外的:)

是否可以用uielements创建一个几何图形,使得渲染的usercontrol可以转换为几何Path?

Is it possible to create a geometric shape out of uielements, such that a usercontrol that is rendered Can be converted to a geometric Path?

进展

我发现 此链接 我可以从路径创建几何图形的地方.路径具有属性宽度和高度.

I found this link Where I can create a geometry from a path. The path has the property width and height.

但我在几何或路径中没有的属性如下:

But the properties I do not have in geometry or path is the following:

  1. 画布左
  2. 画布顶部
  3. Canvas.ZIndex(我认为当我将它添加到 GeometryGroup 时这是可能的)

好像可以通过Path.Data的bounds属性来实现.但不是 ZIndex.所以这个还是需要用geometryGroup来测试,需要把Geometry加入到GeometryGroup中.

Seems like this can be done through the bounds property of the Path.Data. But not ZIndex. So this still needs to be tested with geometryGroup, and the Geometry needs to be added to the GeometryGroup.

推荐答案

前段时间,在为此寻找解决方案时,我最终创建了这个函数.也许它会帮助你.

Some time ago, while searching for a solution for this and I ended up creating this function. Maybe it will help you.

    public static Geometry PathMarkupToGeometry(string pathMarkup)
    {
        try
        {
            string xaml =
            "<Path " +
            "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
            "<Path.Data>" + pathMarkup + "</Path.Data></Path>";
            var path = XamlReader.Load(xaml) as Path;
            // Detach the PathGeometry from the Path
            if (path != null)
            {
                Geometry geometry = path.Data;
                path.Data = null;
                return geometry;
            }
            return null;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        return null;
    }

然后我这样调用这个函数:

Then I call this function like this:

     var arrow = new Path
     {
        Data = DesignHelpers.PathMarkupToGeometry("M-1,0 L0,1 L1,0"),
        Fill = new SolidColorBrush(Colors.Black),
        Stretch = Stretch.Fill,
        Height = 12,
        Width = 18,
        HorizontalAlignment = HorizontalAlignment.Center
    };

我不知道这是否完全符合您的需求,但也许可以提供帮助.

I don't know if this will fit your needs exactly but maybe it can help.

这篇关于将路径转换为几何形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆