elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Set Data of path in code

17 Jul 2015, 08:28

How do I do this in Unity C#?
Path arcPath = new Path();
arcPath.Stroke = new SolidColorBrush(Colors.Blue);
arcPath.StrokeThickness = 1;	
arcPath.Data = Geometry.Parse("M 80,200 A 100,50 45 1 0 100,50 Z");
The Data part has to be changed dynamically. Parse is not supported.

Edit:
This works, but it crashes NoesisGUI, and then the Editor:
using UnityEngine;
using System.Collections;
using Noesis;

	public class ND : MonoBehaviour {

	Path path;
	float point0 = 0;

	void Start (){
		
		NoesisGUIPanel panel = GetComponent<NoesisGUIPanel>();
		FrameworkElement root = panel.GetContent();
		root.Parent.UseLayoutRounding = false; 
		Noesis.Canvas canvas = (Noesis.Canvas)root.FindName("layer1");

		path = new Path();
		path.Stroke = new SolidColorBrush(Colors.Green);
		path.StrokeThickness = 1;	

		canvas.Children.Add(path);
	}

	void Update(){
		point0 = (Mathf.Sin(Time.time * 0.5f) * 30f) + 30;
		string pathString = "M 17.57812541,76.56250172 62.22098358,83.25893044 81.75223403,68.47098367 80.35714471," + point0;
		path.SetBinding(Path.DataProperty, new Binding(){Source=pathString});
	}
}
 
elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Re: Set Data of path in code

17 Jul 2015, 12:21

I managed to do this with StreamGeometry instead. There still might be a bug though, as I don't think it should crash.

Here is the new working code:
using UnityEngine;
using System.Collections;
using Noesis;

public class ND : MonoBehaviour {

	StreamGeometry streamGeometry;
	float point0 = 0;

	void Start (){
		
		NoesisGUIPanel panel = GetComponent<NoesisGUIPanel>();
		FrameworkElement root = panel.GetContent();
		root.Parent.UseLayoutRounding = false; 
		Noesis.Canvas canvas = (Noesis.Canvas)root.FindName("layer1");

		// Create a path to draw a geometry with.
		Path path = new Path();
		path.Stroke = new SolidColorBrush(Colors.Green);
		path.StrokeThickness = 1;

		// Create a StreamGeometry to use to specify myPath.
		streamGeometry = new StreamGeometry();
		streamGeometry.FillRule = FillRule.EvenOdd;
		path.Data = streamGeometry;

		canvas.Children.Add(path);
	}

	void Update(){

		point0 = (Mathf.Sin(Time.time * 0.5f) * 30f) + 30;
		string pathString = "M 17.57812541,76.56250172 62.22098358,83.25893044 81.75223403,68.47098367 80.35714471," + point0;
		streamGeometry.SetData(pathString);
	}
}
Note that instead of using SetData() and a path string, you can also construct the shape step by step:
float dynamic = (Mathf.Sin(Time.time * 0.5f) * 30f) + 30;
		
using(StreamGeometryContext ctx = streamGeometry.Open()){

	ctx.BeginFigure(new Point(10, 90), true);
	ctx.LineTo(new Point(dynamic, 90));
	ctx.ArcTo(new Point(60, 60), new Size(new Point(10, 10)), 0, false, SweepDirection.Counterclockwise);
}
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Set Data of path in code

17 Jul 2015, 13:30

Geometry.Parse() is not supported.
Although it is a slow method to generate a procedural geometry, we can add this in a future release.
I managed to do this with StreamGeometry instead. There still might be a bug though, as I don't think it should crash.
Could you please create a ticket in our bugtracker to track this issue? Thanks.
Note that instead of using SetData() and a path string, you can also construct the shape step by step:
Using the StreamGeomtryContext is the recommended way to update a StreamGeometry, because using SetData() involves parsing a string which is significantly slower than using the context method.
 
elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Re: Set Data of path in code

17 Jul 2015, 15:49

Thanks. Will file a bug report for the crash.

Edit:
Can not reproduce now... Strange.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Set Data of path in code

22 Jul 2015, 11:49

In case you can reproduce the crash again, please file a report in our bugtracker.
Meanwhile I marked this post regarding how to set Path.Data in code as solved.

Who is online

Users browsing this forum: Bing [Bot] and 1 guest