private DpiState CalculateDpiActions(IDiagram diagram) { DpiState result = DpiState.Ok; var schemaManager = new DiagramPropertySchemaManager(diagram.Model); var dpiFactor = schemaManager.GetDpiFactor(diagram); if (dpiFactor > 0f) { if (dpiFactor > Dpi.Factor.Height + 0.25) { result = DpiState.TooBig; } else if (dpiFactor < Dpi.Factor.Height - 0.25) { result = DpiState.TooSmall; } } else { schemaManager.SetDpiFactor(diagram); var links = diagram.Links?.ToArray(); if (links?.Any() ?? false) { var propertyType = schemaManager.GetLinksSchema()?.GetPropertyType("Points"); if (propertyType != null) { var totalDistanceFactor = 0.0; var count = 0; foreach (var link in links) { var property = link.GetProperty(propertyType); if (property is IPropertyArray array) { var points = array.Value?.Select(x => float.Parse(x, NumberFormatInfo.InvariantInfo)) .ToArray(); if (points?.Any() ?? false) { var source = link.DataFlow.Source; var sourceShape = diagram.Entities.FirstOrDefault(x => x.AssociatedId == source.Id); if (sourceShape != null) { var position = sourceShape.Position; var x = points[0]; var y = points[1]; var distance = Math.Sqrt(Math.Pow(position.X - x, 2) + Math.Pow(position.Y - y, 2)); totalDistanceFactor += distance / Dpi.Factor.Width; count++; } } } } if (count > 0) { var distanceFactor = totalDistanceFactor / count; if (distanceFactor < 13f) { result = DpiState.TooSmall; } else if (distanceFactor > 26f) { result = DpiState.TooBig; } } } } } return(result); }
private void AddLink([NotNull] ILink link) { var from = GetEntity(link.DataFlow.Source); var to = GetEntity(link.DataFlow.Target); if (from != null && to != null) { var newLink = new GraphLink(link, _dpiState) { Loading = true, FromPort = from.Port, ToPort = to.Port }; if (_actions != null) { newLink.SetContextAwareActions(_actions); } _graph.Doc.Add(newLink); _links.Add(link.AssociatedId, newLink); newLink.SelectedLink += OnSelectedLink; newLink.SelectedThreatEvent += OnSelectedThreatEvent; var schemaManager = new DiagramPropertySchemaManager(link.DataFlow.Model); var pointsPropertyType = schemaManager.GetLinksSchema()?.GetPropertyType("Points"); var property = link.GetProperty(pointsPropertyType); if (property is IPropertyArray propertyArray) { var array = propertyArray.Value?.ToArray(); var count = array?.Length ?? 0; if (count > 0) { newLink.RealLink.ClearPoints(); for (int i = 0; i < count / 2; i++) { float x; float y; switch (_dpiState) { case DpiState.TooSmall: x = float.Parse(array[i * 2], NumberFormatInfo.InvariantInfo) * 2; y = float.Parse(array[i * 2 + 1], NumberFormatInfo.InvariantInfo) * 2; break; case DpiState.TooBig: x = float.Parse(array[i * 2], NumberFormatInfo.InvariantInfo) / 2; y = float.Parse(array[i * 2 + 1], NumberFormatInfo.InvariantInfo) / 2; break; default: x = float.Parse(array[i * 2], NumberFormatInfo.InvariantInfo); y = float.Parse(array[i * 2 + 1], NumberFormatInfo.InvariantInfo); break; } newLink.RealLink.AddPoint(new PointF(x, y)); } } } newLink.Loading = false; newLink.UpdateRoute(); } }
public void Initialize([NotNull] IThreatModel model) { var schemaManager = new DiagramPropertySchemaManager(model); schemaManager.GetLinksSchema(); }