分离完成 AIStudio.Wpf.DiagramModels 用于对接三分软件的模型

This commit is contained in:
艾竹
2023-02-28 23:04:16 +08:00
parent a015f9208b
commit 84d4035e6d
10 changed files with 278 additions and 160 deletions

View File

@@ -1,419 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace AIStudio.Wpf.Flowchart.Models
{
/// <summary>
///
/// </summary>
public class DiagramData
{
/// <summary>
/// Gets or sets the nodes.
/// </summary>
/// <value>
/// The nodes.
/// </value>
public DiagramNode[] Nodes
{
get; set;
}
/// <summary>
/// Gets or sets the links.
/// </summary>
/// <value>
/// The links.
/// </value>
public DiagramLink[] Links
{
get; set;
}
/// <summary>
/// Gets or sets the groups.
/// </summary>
/// <value>
/// The groups.
/// </value>
public DiagramGroup[] Groups
{
get; set;
}
/// <summary>
/// Initializes a new instance of the <see cref="DiagramData"/> class.
/// </summary>
public DiagramData()
{
Nodes = new DiagramNode[0];
Links = new DiagramLink[0];
Groups = new DiagramGroup[0];
}
}
/// <summary>
/// DiagramNode
/// </summary>
public class DiagramNode
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public string Id
{
get; set;
}
/// <summary>
/// Gets or sets the parent identifier.
/// </summary>
/// <value>
/// The parent identifier.
/// </value>
public string ParentId
{
get; set;
}
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name
{
get; set;
}
/// <summary>
/// Gets or sets the color.
/// </summary>
/// <value>
/// The color.
/// </value>
public string Color
{
get; set;
}
/// <summary>
/// Gets or sets the label.
/// </summary>
/// <value>
/// The label.
/// </value>
public string Label
{
get; set;
}
/// <summary>
/// Gets or sets the width.
/// </summary>
/// <value>
/// The width.
/// </value>
public double Width
{
get; set;
}
/// <summary>
/// Gets or sets the height.
/// </summary>
/// <value>
/// The height.
/// </value>
public double Height
{
get; set;
}
/// <summary>
/// Gets or sets the x.
/// </summary>
/// <value>
/// The x.
/// </value>
public double X
{
get; set;
}
/// <summary>
/// Gets or sets the y.
/// </summary>
/// <value>
/// The y.
/// </value>
public double Y
{
get; set;
}
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
public string Type
{
get; set;
}
/// <summary>
/// Gets or sets the index of the z.
/// </summary>
/// <value>
/// The index of the z.
/// </value>
public int ZIndex
{
get; set;
}
/// <summary>
/// Gets or sets the port alignment list.
/// </summary>
/// <value>
/// The port alignment list.
/// </value>
public List<string> PortAlignmentList
{
get; set;
}
}
/// <summary>
///
/// </summary>
public class DiagramLink
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public string Id
{
get; set;
}
/// <summary>
/// Gets or sets the color.
/// </summary>
/// <value>
/// The color.
/// </value>
public string Color
{
get; set;
}
/// <summary>
/// Gets or sets the color of the selected.
/// </summary>
/// <value>
/// The color of the selected.
/// </value>
public string SelectedColor
{
get; set;
}
/// <summary>
/// Gets or sets the width.
/// </summary>
/// <value>
/// The width.
/// </value>
public double Width
{
get; set;
}
/// <summary>
/// Gets or sets the label.
/// </summary>
/// <value>
/// The label.
/// </value>
public string Label
{
get; set;
}//TODO
/// <summary>
/// Gets or sets the source identifier.
/// </summary>
/// <value>
/// The source identifier.
/// </value>
public string SourceId
{
get; set;
}
/// <summary>
/// Gets or sets the target identifier.
/// </summary>
/// <value>
/// The target identifier.
/// </value>
public string TargetId
{
get; set;
}
/// <summary>
/// Gets or sets the source port alignment.
/// </summary>
/// <value>
/// The source port alignment.
/// </value>
public string SourcePortAlignment
{
get; set;
}
/// <summary>
/// Gets or sets the target port alignment.
/// </summary>
/// <value>
/// The target port alignment.
/// </value>
public string TargetPortAlignment
{
get; set;
}
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
public string Type
{
get; set;
}
/// <summary>
/// Gets or sets the router.
/// </summary>
/// <value>
/// The router.
/// </value>
public string Router
{
get; set;
}
/// <summary>
/// Gets or sets the path generator.
/// </summary>
/// <value>
/// The path generator.
/// </value>
public string PathGenerator
{
get; set;
}
/// <summary>
/// Gets or sets the source marker path.
/// </summary>
/// <value>
/// The source marker path.
/// </value>
public string SourceMarkerPath
{
get; set;
}
/// <summary>
/// Gets or sets the width of the source marker.
/// </summary>
/// <value>
/// The width of the source marker.
/// </value>
public double? SourceMarkerWidth
{
get; set;
}
/// <summary>
/// Gets or sets the target marker path.
/// </summary>
/// <value>
/// The target marker path.
/// </value>
public string TargetMarkerPath
{
get; set;
}
/// <summary>
/// Gets or sets the width of the target marker.
/// </summary>
/// <value>
/// The width of the target marker.
/// </value>
public double? TargetMarkerWidth
{
get; set;
}
}
/// <summary>
///
/// </summary>
/// <seealso cref="AIStudio.Util.DiagramEntity.DiagramNode" />
public class FlowchartNode : DiagramNode
{
/// <summary>
/// Gets or sets the kind.
/// </summary>
/// <value>
/// The kind.
/// </value>
public NodeKinds Kind
{
get; set;
}
/// <summary>
/// Gets or sets the user ids.
/// </summary>
/// <value>
/// The user ids.
/// </value>
public IEnumerable<string> UserIds
{
get; set;
}
/// <summary>
/// Gets or sets the role ids.
/// </summary>
/// <value>
/// The role ids.
/// </value>
public IEnumerable<string> RoleIds
{
get; set;
}
/// <summary>
/// Gets or sets the type of the act.
/// </summary>
/// <value>
/// The type of the act.
/// </value>
public string ActType
{
get; set;
}
}
/// <summary>
///
/// </summary>
public class DiagramGroup
{
/// <summary>
/// Gets or sets the flow node ids.
/// </summary>
/// <value>
/// The flow node ids.
/// </value>
public List<string> FlowNodeIds { get; set; } = new List<string>();
}
}

View File

@@ -1,103 +0,0 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace AIStudio.Wpf.Flowchart.Models
{
public class DiagramNodeConverter : DATACreationConverter<DiagramNode>
{
protected override DiagramNode Create(Type objectType, JObject jObject)
{
//第一种方法:判断属性值来确认是哪个派生类
if (FieldExists("Type", jObject, out string type))
{
if (type == "FlowchartNode")
{
return new FlowchartNode();
}
else
{
return new DiagramNode();
}
}
else
{
return new DiagramNode();
}
//第二种方法:判断字段是否存在来确认是哪个派生类
}
private bool FieldExists(string fieldName, JObject jObject, out string entityName)
{
entityName = jObject[fieldName] == null ? "" : jObject[fieldName].ToString();
return jObject[fieldName] != null;
}
}
public class DiagramLinkConverter : DATACreationConverter<DiagramLink>
{
protected override DiagramLink Create(Type objectType, JObject jObject)
{
//第一种方法:判断属性值来确认是哪个派生类
if (FieldExists("Type", jObject, out string type))
{
return new DiagramLink();
}
else
{
return new DiagramLink();
}
//第二种方法:判断字段是否存在来确认是哪个派生类
}
private bool FieldExists(string fieldName, JObject jObject, out string entityName)
{
entityName = jObject[fieldName] == null ? "" : jObject[fieldName].ToString();
return jObject[fieldName] != null;
}
}
public abstract class DATACreationConverter<T> : JsonConverter
{
/// <summary>
/// Create an instance of objectType, based properties in the JSON object
/// </summary>
/// <param name="objectType">type of object expected</param>
/// <param name="jObject">
/// contents of JSON object that will be deserialized
/// </param>
/// <returns></returns>
protected abstract T Create(Type objectType, JObject jObject);
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
// Load JObject from stream
JObject jObject = JObject.Load(reader);
// Create target object based on JObject
T target = Create(objectType, jObject);
// Populate the object properties
serializer.Populate(jObject.CreateReader(), target);
return target;
}
public override void WriteJson(JsonWriter writer,
object value,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,261 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using AIStudio.Wpf.DiagramDesigner;
using Newtonsoft.Json;
using System.Linq;
using System.Windows.Media;
using AIStudio.Wpf.Flowchart.ViewModels;
namespace AIStudio.Wpf.Flowchart.Models
{
public static class DiagramDataExtention
{
#region ToJson
public static string ToJson(this IDiagramViewModel diagram)
{
var json = JsonConvert.SerializeObject(new {
Nodes = diagram.Items.OfType<DesignerItemViewModelBase>().Select(p => p.ToDiagramNode()).Where(p => p != null),
Links = diagram.Items.OfType<ConnectionViewModel>().Select(p => p.ToDiagramLink()).Where(p => p != null)
}, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return json;
}
public static DiagramNode ToDiagramNode(this DesignerItemViewModelBase nodeModel)
{
DiagramNode diagramNode;
if (nodeModel is FlowNode flowNode)
{
var flowchartNode = new FlowchartNode();
diagramNode = flowchartNode;
diagramNode.Name = flowNode.Name;
flowchartNode.Color = flowNode.Color;
flowchartNode.Kind = flowNode.Kind;
if (nodeModel is MiddleFlowNode middleflowNode)
{
flowchartNode.UserIds = middleflowNode.UserIds;
flowchartNode.RoleIds = middleflowNode.RoleIds;
flowchartNode.ActType = middleflowNode.ActType;
}
}
else
{
return null;
}
diagramNode.Id = nodeModel.Id.ToString();
if (nodeModel.ParentId != Guid.Empty)
{
diagramNode.ParentId = nodeModel.ParentId.ToString();
}
diagramNode.Label = nodeModel.Text;
diagramNode.Width = nodeModel.ItemWidth * ScreenHelper.ScreenScale;
diagramNode.Height = nodeModel.ItemHeight * ScreenHelper.ScreenScale;
diagramNode.X = nodeModel.Left * ScreenHelper.ScreenScale;
diagramNode.Y = nodeModel.Top * ScreenHelper.ScreenScale;
diagramNode.ZIndex = nodeModel.ZIndex;
diagramNode.Type = diagramNode.GetType().Name;
diagramNode.PortAlignmentList = nodeModel.Connectors.Select(p => p.Orientation.ToString()).ToList();
return diagramNode;
}
public static DiagramLink ToDiagramLink(this ConnectionViewModel linkModel)
{
DiagramLink diagramLink = new DiagramLink();
diagramLink.Id = linkModel.Id.ToString();
diagramLink.Color = SerializeHelper.SerializeColor(linkModel.ColorViewModel.LineColor.Color);
diagramLink.SelectedColor = SerializeHelper.SerializeColor(Colors.Black);
diagramLink.Width = linkModel.ColorViewModel.LineWidth;
diagramLink.Label = linkModel.Text;
if (linkModel.IsFullConnection)
{
diagramLink.SourceId = linkModel.SourceConnectorInfo.DataItem.Id.ToString();
diagramLink.TargetId = linkModel.SinkConnectorInfoFully.DataItem.Id.ToString();
//线条形状与箭头待处理
//diagramLink.Router = baseLinkModel.Router?.Method.Name;
//diagramLink.PathGenerator = baseLinkModel.PathGenerator?.Method.Name;
//diagramLink.SourceMarkerPath = baseLinkModel.SourceMarker?.Path;
//diagramLink.SourceMarkerWidth = baseLinkModel.SourceMarker?.Width;
//diagramLink.TargetMarkerPath = baseLinkModel.TargetMarker?.Path;
//diagramLink.TargetMarkerWidth = baseLinkModel.TargetMarker?.Width;
diagramLink.Type = diagramLink.GetType().Name;
diagramLink.SourcePortAlignment = linkModel.SourceConnectorInfo.Orientation.ToString();
diagramLink.TargetPortAlignment = linkModel.SinkConnectorInfoFully.Orientation.ToString();
}
else
{
return null;
}
return diagramLink;
}
#endregion
#region ToObject
public static void ToObject(this IDiagramViewModel diagram, string json)
{
var data = JsonConvert.DeserializeObject<DiagramData>(json, new JsonConverter[] { new DiagramNodeConverter(), new DiagramLinkConverter() });
if (data != null)
{
ToObject(diagram, data);
}
}
public static void ToObject(this IDiagramViewModel diagram, DiagramData data)
{
diagram.Items.Clear();
List<DesignerItemViewModelBase> nodes = new List<DesignerItemViewModelBase>();
if (data.Nodes != null)
{
foreach (var node in data.Nodes)
{
var nodemodel = node.ToNodelModel(diagram);
nodes.Add(nodemodel);
diagram.Items.Add(nodemodel);
}
}
if (data.Links != null)
{
foreach (var link in data.Links)
{
var source = nodes.FirstOrDefault(p => p.Id == new Guid(link.SourceId));
var target = nodes.FirstOrDefault(p => p.Id == new Guid(link.TargetId));
var linkmodel = link.ToLinkModel(diagram, source, target);
diagram.Items.Add(linkmodel);
}
}
}
private static DesignerItemViewModelBase ToNodelModel(this DiagramNode diagramNode, IDiagramViewModel diagram)
{
DesignerItemViewModelBase nodeModel;
if (diagramNode is FlowchartNode flowchartNode)
{
FlowNode flowNode = null;
switch (flowchartNode.Kind)
{
case NodeKinds.Start:
{
var flowchartNodelModel = new StartFlowNode();
flowNode = flowchartNodelModel;
break;
}
case NodeKinds.End:
{
var flowchartNodelModel = new EndFlowNode();
flowNode = flowchartNodelModel;
break;
}
case NodeKinds.Decide:
{
var flowchartNodelModel = new DecideFlowNode();
flowNode = flowchartNodelModel;
break;
}
case NodeKinds.COBegin:
{
var flowchartNodelModel = new COBeginFlowNode();
flowNode = flowchartNodelModel;
break;
}
case NodeKinds.COEnd:
{
var flowchartNodelModel = new COEndFlowNode();
flowNode = flowchartNodelModel;
break;
}
case NodeKinds.Middle:
{
var flowchartNodelModel = new MiddleFlowNode();
flowNode = flowchartNodelModel;
flowchartNodelModel.UserIds = flowchartNode.UserIds?.ToList();
flowchartNodelModel.RoleIds = flowchartNode.RoleIds?.ToList();
flowchartNodelModel.ActType = flowchartNode.ActType;
break;
}
default:
{
var flowNodelModel = new FlowNode(NodeKinds.Normal);
flowNode = flowNodelModel;
break;
}
}
nodeModel = flowNode;
flowNode.Name = flowchartNode.Name;
flowNode.Color = flowchartNode.Color;
flowNode.Kind = flowchartNode.Kind;
}
else
{
nodeModel = new TextDesignerItemViewModel();
}
nodeModel.Id = new Guid(diagramNode.Id);
if (!string.IsNullOrEmpty(diagramNode.ParentId))
{
nodeModel.ParentId = new Guid(diagramNode.ParentId);
}
nodeModel.Root = diagram;
nodeModel.Text = diagramNode.Label;
nodeModel.ItemWidth = diagramNode.Width / ScreenHelper.ScreenScale;
nodeModel.ItemHeight = diagramNode.Height / ScreenHelper.ScreenScale;
nodeModel.Left = diagramNode.X / ScreenHelper.ScreenScale;
nodeModel.Top = diagramNode.Y / ScreenHelper.ScreenScale;
nodeModel.ZIndex = diagramNode.ZIndex;
diagramNode.PortAlignmentList?.ForEach(p => nodeModel.AddConnector(new FullyCreatedConnectorInfo(nodeModel, p.ToEnum<ConnectorOrientation>())));
return nodeModel;
}
public static ConnectionViewModel ToLinkModel(this DiagramLink diagramLink, IDiagramViewModel diagram, DesignerItemViewModelBase sourceNode, DesignerItemViewModelBase targetNode)
{
FullyCreatedConnectorInfo sourceConnectorInfo = sourceNode.Connectors.FirstOrDefault(p => p.Orientation.ToString() == diagramLink.SourcePortAlignment);
FullyCreatedConnectorInfo sinkConnectorInfo = targetNode.Connectors.FirstOrDefault(p => p.Orientation.ToString() == diagramLink.TargetPortAlignment);
ConnectionViewModel linkModel = new ConnectionViewModel(diagram, sourceConnectorInfo, sinkConnectorInfo, diagram.DrawModeViewModel?.LineDrawMode ?? DrawMode.ConnectingLineSmooth, diagram.DrawModeViewModel?.LineRouterMode ?? RouterMode.RouterNormal);
linkModel.Id = new Guid(diagramLink.Id);
linkModel.ColorViewModel.LineColor.Color = SerializeHelper.DeserializeColor(diagramLink.Color);
linkModel.ColorViewModel.LineWidth = diagramLink.Width;
if (!string.IsNullOrEmpty(diagramLink.Label))
{
linkModel.AddLabel(diagramLink.Label);
}
//线条形状与箭头待处理
//switch (diagramLink.Router)
//{
// case "Normal": linkModel.Router = Routers.Normal; break;
// case "Orthogonal": linkModel.Router = Routers.Orthogonal; break;
//}
//switch (diagramLink.PathGenerator)
//{
// case "Smooth": linkModel.PathGenerator = PathGenerators.Smooth; break;
// case "Straight": linkModel.PathGenerator = PathGenerators.Straight; break;
//}
//if (!string.IsNullOrEmpty(diagramLink.SourceMarkerPath))
//{
// linkModel.SourceMarker = new LinkMarker(diagramLink.SourceMarkerPath, diagramLink.SourceMarkerWidth ?? 10.0);
//}
//if (!string.IsNullOrEmpty(diagramLink.TargetMarkerPath))
//{
// linkModel.TargetMarker = new LinkMarker(diagramLink.TargetMarkerPath, diagramLink.TargetMarkerWidth ?? 10.0);
//}
return linkModel;
}
#endregion
}
}

View File

@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AIStudio.Wpf.DiagramModels;
using AIStudio.Wpf.DiagramModels.ViewModels;
using AIStudio.Wpf.Flowchart.ViewModels;
namespace AIStudio.Wpf.Flowchart.Models
{
/// <summary>
///
/// </summary>
/// <seealso cref="AIStudio.Util.DiagramEntity.DiagramNode" />
public class FlowchartNode : DiagramNode
{
/// <summary>
/// Gets or sets the kind.
/// </summary>
/// <value>
/// The kind.
/// </value>
public NodeKinds Kind
{
get; set;
}
/// <summary>
/// Gets or sets the user ids.
/// </summary>
/// <value>
/// The user ids.
/// </value>
public IEnumerable<string> UserIds
{
get; set;
}
/// <summary>
/// Gets or sets the role ids.
/// </summary>
/// <value>
/// The role ids.
/// </value>
public IEnumerable<string> RoleIds
{
get; set;
}
/// <summary>
/// Gets or sets the type of the act.
/// </summary>
/// <value>
/// The type of the act.
/// </value>
public string ActType
{
get; set;
}
public override DiagramItemViewModel ToNodel()
{
FlowNode flowNode = null;
switch (Kind)
{
case NodeKinds.Start:
{
flowNode = new StartFlowNode();
break;
}
case NodeKinds.End:
{
flowNode = new EndFlowNode();
break;
}
case NodeKinds.Decide:
{
flowNode = new DecideFlowNode();
break;
}
case NodeKinds.COBegin:
{
flowNode = new COBeginFlowNode();
break;
}
case NodeKinds.COEnd:
{
flowNode = new COEndFlowNode();
break;
}
case NodeKinds.Middle:
{
var flowchartNodelModel = new MiddleFlowNode();
flowNode = flowchartNodelModel;
flowchartNodelModel.UserIds = UserIds?.ToList();
flowchartNodelModel.RoleIds = RoleIds?.ToList();
flowchartNodelModel.ActType = ActType;
break;
}
default:
{
var flowNodelModel = new FlowNode(NodeKinds.Normal);
flowNode = flowNodelModel;
break;
}
}
flowNode.Name = Name;
flowNode.Color = Color;
flowNode.Kind = Kind;
return flowNode;
}
}
}