Skip to content

Latest commit

 

History

History
96 lines (74 loc) · 4.33 KB

File metadata and controls

96 lines (74 loc) · 4.33 KB

WinForms DiagramControl - Complex Expressions and Custom Functions in Shape Templates

This example uses complex expressions and custom functions to calculate the ShapeTemplate's Parameter value. You can use custom expressions or functions to implement complex logic for Parameters (for example, a circular motion).

In this example, the custom Sector shape allows users to specify its angle:

image

Implementation Details

ShapeTemplates accept functions that implement the ICustomFunctionOperator interface. Criteria Language Syntax allows you to calculate shape Parameters.

To create a custom calculation function, you must:

  1. Create a function class that implements the ICustomFunctionOperator interface.
  2. Use the CriteriaOperator.RegisterCustomFunction method to register this function.
public class CreateArcPoint : ICustomFunctionOperator {
	private static readonly CreateArcPoint instance = new CreateArcPoint();

	public static void Register() {
		CriteriaOperator.RegisterCustomFunction(instance);
	}
	public static void Unregister() {
		CriteriaOperator.UnregisterCustomFunction(instance);
	}

	public string Name => nameof(CreateArcPoint);

	public Type ResultType(params Type[] operands) {
		return typeof(double);
	}

	public object Evaluate(params object[] operands) {
		if (operands.Length == 3
			&& operands[0] is double X
			&& operands[1] is double Y
			&& operands[2] is string axe) {

			if (axe is "X")
				return X > 0.5 ? X : 0.5;
			return X > 0.5 ? Y : 1;
		}
		else
			return null;
	}
}
<ShapeTemplate Id="Sector" DefaultSize="120, 120">
	<Start X="0.5" Y="0" />
	<Line X="0.5" Y="0.5"/>
	<Line X="(COS([P0]) + 1)/2" Y="(SIN([P0]) + 1)/2" />
	<Arc X="CreateArcPoint((COS([P0]) + 1)/2, (SIN([P0]) + 1)/2, 'X')"
		 Y="CreateArcPoint((COS([P0]) + 1)/2, (SIN([P0]) + 1)/2, 'Y')"
		 Direction="Counterclockwise"
		 Size="CreateSize([W]/2, [H]/2)"/>

	<Arc X="0.5" Y="0" Direction="Counterclockwise" Size="CreateSize([W]/2, [H]/2)" />
	<ConnectionPoints>
		<ShapePoint X="0.5" Y="1" />
		<ShapePoint X="1" Y="0.5" />
		<ShapePoint X="0.5" Y="0" />
		<ShapePoint X="0" Y="0.5" />
	</ConnectionPoints>
	<Parameters>
		<Parameter DefaultValue="0"
			   Point="CreatePoint((COS([P]) + 1)/2*[W], (SIN([P]) + 1)/2*[H])"
			   Value="atn2([P.Y]/[H]*2-1, [P.X]/[W]*2-1)"
			   Min="-3.14" Max="3.14" />
	</Parameters>
</ShapeTemplate>

Files to Review

Documentation

More Examples