Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.IO;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Security.AccessControl;
- using System.Security.Policy;
- using System.Text;
- using System.Xml;
- namespace ConsoleApplication10 {
- class Program {
- static void Main(string[] args) {
- PathLine.CustomEndCap = new AdjustableArrowCap(10, 10);
- Connector[] connectors = ReadData();
- foreach(Connector connector in connectors) {
- if(connector.Preset == 5)
- continue;
- Connector actual = Calculate(connector.StartId, connector.EndId, connector.DX, connector.DY, connector.Name, connector);
- CheckConnectors(connector, actual);
- }
- }
- const int Rotation0 = 0;
- const int Rotation90 = 5400000;
- const int Rotation180 = 10800000;
- const int Rotation270 = 16200000;
- const int Rotation360 = 21600000;
- static Connector Calculate(int startId, int endId, int dx, int dy, string name, Connector original) {
- int firstShapeX = 2 + dx;
- int firstShapeY = 2 + dy;
- int secondShapeX = 2 - dx;
- int secondShapeY = 2 - dy;
- int startX = firstShapeX + GetDXByIdx(startId);
- int startY = firstShapeY + GetDYByIdx(startId);
- int finishX = secondShapeX + GetDXByIdx(endId);
- int finishY = secondShapeY + GetDYByIdx(endId);
- PathInfo[,] map = new PathInfo[5, 5];
- for(int i = 0; i < 5; i++) {
- for(int j = 0; j < 5; j++) {
- map[i, j] = new PathInfo(i, j);
- map[i, j].Length = int.MaxValue;
- map[i, j].Bends = int.MaxValue;
- }
- }
- map[startX, startY].Length = 0;
- map[startX, startY].Bends = 0;
- map[startX, startY].DX = GetDXByIdx(startId);
- map[startX, startY].DY = GetDYByIdx(startId);
- HashSet<PathInfo> close = new HashSet<PathInfo>();
- MySortedList open = new MySortedList();
- close.Add(map[firstShapeX, firstShapeY]);
- close.Add(map[secondShapeX, secondShapeY]);
- open.Add(map[startX, startY]);
- int[,] offsets = { { -1, 0 }, { 1, 0 }, { 0, 1 }, { 0, -1 } };
- while(open.Count > 0) {
- PathInfo current = open.GetMinAndRemove();
- close.Add(current);
- for(int i = 0; i < 4; i++) {
- int newX = current.X + offsets[i, 0];
- int newY = current.Y + offsets[i, 1];
- if(!IsPoint(newX, newY))
- continue;
- PathInfo other = map[newX, newY];
- if(close.Contains(other))
- continue;
- if(!open.Contains(other))
- open.Add(other);
- int newLength = current.Length + 1;
- int newBends = offsets[i, 0] == current.DX && offsets[i, 1] == current.DY ? current.Bends : current.Bends + 1;
- if(other.Length > newLength || (other.Length == newLength && other.Bends > newBends)) {
- other.Length = newLength;
- other.Bends = newBends;
- other.DX = offsets[i, 0];
- other.DY = offsets[i, 1];
- }
- }
- }
- List<Point> path = GetPath(map, startX, startY, finishX, finishY, firstShapeX, firstShapeY, secondShapeX, secondShapeY);
- path = OptimizePath(path);
- DrawPictureByMapAndSave(name, finishX, finishY, startX, startY, path, firstShapeX, firstShapeY, secondShapeX, secondShapeY);
- Connector connector = new Connector();
- connector.DX = dx;
- connector.DY = dy;
- connector.StartId = startId;
- connector.EndId = endId;
- int preset = map[finishX, finishY].Bends + 1;
- if(map[finishX, finishY].DX != -GetDXByIdx(endId) || map[finishX, finishY].DY != -GetDYByIdx(endId))
- preset++;
- if(preset == 1)
- preset = 3;
- connector.Preset = preset;
- connector.Rotation = GetRotation(connector);
- switch(connector.Preset) {
- case 2:
- SetupProperties2(path, connector, (int) (connector.Rotation / 60000));
- break;
- case 3:
- FillAdjust3(path, connector);
- SetupProperties3(path, connector, (int) (connector.Rotation / 60000));
- break;
- case 4:
- FilltAdjust4(path, connector);
- SetupProperties4(path, connector, (int) (connector.Rotation / 60000));
- break;
- case 5:
- FillAdjust5(connector);
- break;
- }
- int tempLeft = (int)((firstShapeX + 0.5 + GetDXByIdx(startId) / 2f) * ShapeSize);
- int tempTop = (int)((firstShapeY + 0.5 + GetDYByIdx(startId) / 2f) * ShapeSize);
- int tempRight = (int)((secondShapeX + 0.5 + GetDXByIdx(endId) / 2f) * ShapeSize);
- int tempBottom = (int)((secondShapeY + 0.5 + GetDYByIdx(endId) / 2f) * ShapeSize);
- int left = Math.Min(tempLeft, tempRight);
- int top = Math.Min(tempTop, tempBottom);
- int width = Math.Abs(tempRight - tempLeft);
- int height = Math.Abs(tempBottom - tempTop);
- if(original.Rotation == Rotation90 || original.Rotation == Rotation270) {
- left = left + width / 2 - height / 2;
- top = top + height / 2 - width / 2;
- int temp = width;
- width = height;
- height = temp;
- }
- DrawPictureByConnector(name, left, top, width, height, connector, firstShapeX, firstShapeY, secondShapeX, secondShapeY);
- if(connector.FlipH)
- connector.Rotation = Rotation360 - connector.Rotation;
- if(connector.FlipV)
- connector.Rotation = Rotation360 - connector.Rotation;
- connector.Rotation %= Rotation360;
- return connector;
- }
- static long GetRotation(Connector connector) {
- if (connector.DX == 1) {
- switch (connector.DY) {
- case -1:
- return (3 - connector.StartId) * 90 * 60000;
- case 1:
- return (360 - (3 - connector.StartId) * 90) % 360 * 60000;
- }
- throw new Exception();
- }
- switch (connector.StartId) {
- case 0:
- case 2:
- return 90 * 60000;
- case 3:
- return 0 * 60000;
- case 1:
- return 180 * 60000;
- }
- throw new Exception();
- }
- #region FillDefaulAdjust
- static void FillAdjust5(Connector connector) {
- }
- static void FilltAdjust4(List<Point> path, Connector connector) {
- int[] signedLength = new int[path.Count - 1];
- int abssum = 0;
- int[] how = new int[4];
- for(int i = 0; i < signedLength.Length; i++) {
- signedLength[i] = path[i + 1].X - path[i].X + path[i + 1].Y - path[i].Y;
- abssum += signedLength[i];
- how[Math.Abs(signedLength[i])]++;
- }
- abssum = Math.Abs(abssum);
- if(how[1] == 4) {
- if(signedLength[1] == signedLength[3]) {
- connector.AdjustValues.Add(-36000);
- connector.AdjustValues.Add(75000);
- }
- else if(signedLength[0] == signedLength[2]) {
- connector.AdjustValues.Add(25000);
- connector.AdjustValues.Add(136000);
- }
- else
- throw new Exception();
- }
- else if(how[3] == 2) {
- connector.AdjustValues.Add(-9000);
- connector.AdjustValues.Add(109000);
- }
- else if(how[3] == 1) {
- if(Math.Abs(signedLength[1]) == 3) {
- if(abssum == 2) {
- connector.AdjustValues.Add(-36000);
- connector.AdjustValues.Add(109000);
- }
- else if(abssum == 4 || abssum == 0) {
- connector.AdjustValues.Add(25000);
- connector.AdjustValues.Add(109000);
- }
- else
- throw new Exception();
- }
- else if(Math.Abs(signedLength[2]) == 3) {
- if(abssum == 2) {
- connector.AdjustValues.Add(-9000);
- connector.AdjustValues.Add(136000);
- }
- else if(abssum == 4 || abssum == 0) {
- connector.AdjustValues.Add(-9000);
- connector.AdjustValues.Add(75000);
- }
- else
- throw new Exception();
- }
- }
- else
- throw new Exception();
- }
- static void FillAdjust3(List<Point> path, Connector connector) {
- switch(path.Count) {
- case 2:
- connector.AdjustValues.Add(50000);
- break;
- case 4:
- int[] signedLength = new int[3];
- for(int i = 0; i < 3; i++) {
- signedLength[i] = path[i + 1].X - path[i].X + path[i + 1].Y - path[i].Y;
- }
- if(signedLength[0] == signedLength[2]) {
- connector.AdjustValues.Add(50000);
- }
- else if(signedLength[0] == -signedLength[2]) {
- connector.AdjustValues.Add(1800000); //тут надо делать ширину коннектора равной 1 поинт == 20 твипсов
- }
- else if(Math.Abs(signedLength[0]) == 1) {
- connector.AdjustValues.Add(-12000); //эти adjustValues и т.д. не подходят в общем случае, надо добиваться отступа от шейпов в 360 твипсов
- }
- else if(Math.Abs(signedLength[0]) == 3) {
- connector.AdjustValues.Add(112000);
- }
- else
- throw new Exception();
- break;
- default:
- throw new Exception();
- }
- }
- #endregion
- #region SetupProperties
- static void SetupProperties2(List<Point> path, Connector connector, int rotation) {
- int[] expectedAngles = GetAngles(path);
- int[] actualAngles = GetActualAngles2();
- for(int i = 0; i < actualAngles.Length; i++) {
- actualAngles[i] += rotation;
- actualAngles[i] %= 360;
- }
- connector.Rotation = rotation * 60000;
- for(int i = 0; i < expectedAngles.Length; i++) {
- FixAngleByFlip(expectedAngles[i], actualAngles[i], connector, actualAngles);
- }
- }
- static void SetupProperties3(List<Point> path, Connector connector, int rotation) {
- int[] actualAngles = GetActualAngles3(path);
- int[] expectedAngles = GetAngles(path);
- if(actualAngles.Length != expectedAngles.Length)
- throw new Exception();
- for(int i = 0; i < actualAngles.Length; i++) {
- actualAngles[i] += rotation;
- actualAngles[i] %= 360;
- }
- connector.Rotation = rotation * 60000;
- for(int i = 0; i < actualAngles.Length; i++) {
- FixAngleByFlip(expectedAngles[i], actualAngles[i], connector, actualAngles);
- }
- }
- static void SetupProperties4(List<Point> path, Connector connector, int rotation) {
- int[] actualAngles = GetActualAngles4(connector);
- int[] expectedAngles = GetAngles(path);
- if(actualAngles.Length != expectedAngles.Length)
- throw new Exception();
- if(rotation == 270) {
- rotation = 90;
- }
- for(int i = 0; i < actualAngles.Length; i++) {
- actualAngles[i] += rotation;
- actualAngles[i] %= 360;
- }
- connector.Rotation = rotation * 60000;
- for(int i = 0; i < actualAngles.Length; i++) {
- FixAngleByFlip(expectedAngles[i], actualAngles[i], connector, actualAngles);
- }
- }
- #endregion
- static void FixAngleByFlip(int expected, int actual, Connector connector, int[] actualAngles) {
- if(expected == actual)
- return;
- if(expected == 0 || expected == 180) {
- MakeFlipH(connector, actualAngles);
- }
- else {
- MakeFlipV(connector, actualAngles);
- }
- }
- static int GetRotationByIdx(int idx) {
- switch(idx) {
- case 0:
- return 270;
- case 1:
- return 180;
- case 2:
- return 90;
- case 3:
- return 0;
- default:
- throw new Exception();
- }
- }
- #region MakeFlip
- static void MakeFlipV(Connector connector, int[] actualAngles) {
- connector.FlipV = true;
- for(int i = 0; i < actualAngles.Length; i++) {
- actualAngles[i] = FlipV(actualAngles[i]);
- }
- }
- static void MakeFlipH(Connector connector, int[] actualAngles) {
- connector.FlipH = true;
- for(int i = 0; i < actualAngles.Length; i++) {
- actualAngles[i] = FlipH(actualAngles[i]);
- }
- }
- static int FlipV(int angle) {
- if(angle == 90 || angle == 270)
- return (angle + 180) % 360;
- return angle;
- }
- static int FlipH(int angle) {
- if(angle == 0 || angle == 180)
- return (angle + 180) % 360;
- return angle;
- }
- #endregion
- #region GetAngles
- static int[] GetActualAngles2() {
- return new[] { 0, 90 };
- }
- static int[] GetActualAngles3(List<Point> path) {
- switch(path.Count) {
- case 2:
- return new[] { 0 };
- case 4:
- int[] signedLength = new int[3];
- for(int i = 0; i < 3; i++) {
- signedLength[i] = path[i + 1].X - path[i].X + path[i + 1].Y - path[i].Y;
- }
- if(signedLength[0] == signedLength[2]) {
- return new[] { 0, 90, 0 };
- }
- if(signedLength[0] == -signedLength[2]) {
- return new[] { 0, 90, 180 };
- }
- if(Math.Abs(signedLength[0]) == 1) {
- return new[] { 180, 90, 0 };
- }
- if(Math.Abs(signedLength[0]) == 3) {
- return new[] { 0, 90, 180 };
- }
- throw new Exception();
- default:
- throw new Exception();
- }
- }
- static int[] GetActualAngles4(Connector connector) {
- int[] result = new int[4];
- result[0] = connector.AdjustValues[0] == 25000 ? 0 : 180;
- result[1] = 90;
- result[2] = 0;
- result[3] = connector.AdjustValues[1] == 75000 ? 90 : 270;
- return result;
- }
- static int[] GetAngles(List<Point> path) {
- int[] result = new int[path.Count - 1];
- for(int i = 0; i < path.Count - 1; i++) {
- int dx = path[i + 1].X - path[i].X;
- int dy = path[i + 1].Y - path[i].Y;
- result[i] = dy == 0 ? dx > 0 ? 0 : 180 : dy > 0 ? 90 : 270;
- }
- return result;
- }
- #endregion
- #region Path Stuff
- static List<Point> OptimizePath(List<Point> path) {
- if(path.Count == 1)
- return path;
- List<Point> result = new List<Point>();
- Point currentStart = path[0];
- result.Add(currentStart);
- Point currentFinish = path[1];
- int dx = currentFinish.X - currentStart.X;
- int dy = currentFinish.Y - currentStart.Y;
- for(int i = 2; i < path.Count; i++) {
- if(dx == path[i].X - currentFinish.X && dy == path[i].Y - currentFinish.Y) {
- currentFinish = path[i];
- }
- else {
- result.Add(currentFinish);
- currentStart = currentFinish;
- currentFinish = path[i];
- dx = currentFinish.X - currentStart.X;
- dy = currentFinish.Y - currentStart.Y;
- }
- }
- result.Add(currentFinish);
- return result;
- }
- static List<Point> GetPath(PathInfo[,] map, int startX, int startY, int finishX, int finishY, int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY) {
- List<Point> result = new List<Point>();
- result.Add(new Point(secondShapeX, secondShapeY));
- int x = finishX;
- int y = finishY;
- result.Add(new Point(x, y));
- while(!(x == startX && y == startY)) {
- int nextX = x - map[x, y].DX;
- int nextY = y - map[x, y].DY;
- x = nextX;
- y = nextY;
- result.Add(new Point(x, y));
- }
- result.Add(new Point(firstShapeX, firstShapeY));
- result.Reverse();
- return result;
- }
- #endregion
- const int ShapeSize = 100;
- static readonly Pen PathLine = new Pen(Color.Blue, 2);
- static readonly Brush ShapesBrush = new SolidBrush(Color.Green);
- static readonly Font ShapesFont = new Font("Courier", 16);
- static readonly SolidBrush FontBrush = new SolidBrush(Color.Red);
- static readonly Pen CellsPen = new Pen(Color.White, 2);
- static void DrawPictureByConnector(string name, long left, long top, long width, long height, Connector connector, int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY) {
- Bitmap bmp = new Bitmap(ShapeSize * 5 + 1, ShapeSize * 5 + 1);
- Graphics gr = Graphics.FromImage(bmp);
- DrawShapes(firstShapeX, firstShapeY, secondShapeX, secondShapeY, gr);
- DrawCells(gr);
- float angle = connector.Rotation / 60000f;
- Matrix matrix = new Matrix();
- long offsetX = 2 * left + width;
- long offsetY = 2 * top + height;
- if(connector.FlipV) {
- matrix.Multiply(new Matrix(1, 0, 0, -1, 0, 0), MatrixOrder.Append);
- matrix.Translate(0, offsetY, MatrixOrder.Append);
- }
- if(connector.FlipH) {
- matrix.Multiply(new Matrix(-1, 0, 0, 1, 0, 0), MatrixOrder.Append);
- matrix.Translate(offsetX, 0, MatrixOrder.Append);
- }
- PointF center = new PointF(left + width / 2f, top + height / 2f);
- matrix.RotateAt(angle, center);
- gr.Transform = matrix;
- switch(connector.Preset) {
- case 2:
- DrawPreset2(gr, left, top, width, height, connector);
- break;
- case 3:
- DrawPreset3(gr, left, top, width, height, connector);
- break;
- case 4:
- DrawPreset4(gr, left, top, width, height, connector);
- break;
- case 5:
- DrawPreset5();
- break;
- }
- gr.Transform = new Matrix();
- bmp.Save(@"ByPath\\" + name + ".bmp");
- }
- #region DrawPreset
- static void DrawPreset5() {
- }
- static void DrawPreset4(Graphics graphics, long left, long top, long width, long height, Connector connector) {
- long adjustValue1 = connector.AdjustValues[0];
- long adjustValue2 = connector.AdjustValues[1];
- float x1 = width * adjustValue1 / 100000f;
- float y2 = height * adjustValue2 / 100000f;
- graphics.DrawLine(PathLine, left, top, left + x1, top);
- graphics.DrawLine(PathLine, left + x1, top, left + x1, top + y2);
- graphics.DrawLine(PathLine, left + x1, top + y2, left + width, top + y2);
- graphics.DrawLine(PathLine, left + width, top + y2, left + width, top + height);
- }
- static void DrawPreset3(Graphics graphics, long left, long top, long width, long height, Connector connector) {
- long adjustValue = connector.AdjustValues[0];
- const int d = ShapeSize / 2;
- switch(adjustValue) {
- case -12000:
- adjustValue = -d * 100000 / width;
- break;
- case 50000:
- break;
- case 112000:
- adjustValue = (d + width) * 100000 / width;
- break;
- case 1800000:
- width = 1;
- adjustValue = d * 100000 / width;
- break;
- }
- float x1 = width * adjustValue / 100000f;
- graphics.DrawLine(PathLine, left, top, left + x1, top);
- graphics.DrawLine(PathLine, left + x1, top, left + x1, top + height);
- graphics.DrawLine(PathLine, left + x1, top + height, left + width, top + height);
- }
- static void DrawPreset2(Graphics graphics, long left, long top, long width, long height, Connector connector) {
- graphics.DrawLine(PathLine, left, top, left + width, top);
- graphics.DrawLine(PathLine, left + width, top, left + width, top + height);
- }
- #endregion
- static void DrawPictureByMapAndSave(string name, int finishX, int finishY, int startX, int startY, List<Point> path, int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY) {
- Bitmap bmp = new Bitmap(ShapeSize * 5 + 1, ShapeSize * 5 + 1);
- Graphics gr = Graphics.FromImage(bmp);
- List<Point> fullPath = new List<Point>();
- //fullPath.Add(new Point(firstShapeX, firstShapeY));
- fullPath.AddRange(path);
- //fullPath.Add(new Point(secondShapeX, secondShapeY));
- for(int index = 0; index < fullPath.Count - 1; index++) {
- Point point = fullPath[index];
- Point nextPoint = fullPath[index + 1];
- int x = point.X;
- int y = point.Y;
- int nextX = nextPoint.X;
- int nextY = nextPoint.Y;
- gr.DrawLine(PathLine, x * ShapeSize + ShapeSize / 2, y * ShapeSize + ShapeSize / 2, nextX * ShapeSize + ShapeSize / 2, nextY * ShapeSize + ShapeSize / 2);
- }
- DrawShapes(firstShapeX, firstShapeY, secondShapeX, secondShapeY, gr);
- DrawCells(gr);
- bmp.Save(@"Pictures\\" + name + ".bmp");
- }
- static void DrawShapes(int firstShapeX, int firstShapeY, int secondShapeX, int secondShapeY, Graphics gr) {
- SizeF textSize = gr.MeasureString("1", ShapesFont);
- gr.FillRectangle(ShapesBrush, firstShapeX * ShapeSize, firstShapeY * ShapeSize, ShapeSize, ShapeSize);
- gr.FillRectangle(ShapesBrush, secondShapeX * ShapeSize, secondShapeY * ShapeSize, ShapeSize, ShapeSize);
- gr.DrawString("1", ShapesFont, FontBrush, firstShapeX * ShapeSize + (ShapeSize - textSize.Width) / 2, firstShapeY * ShapeSize + (ShapeSize - textSize.Height) / 2);
- gr.DrawString("2", ShapesFont, FontBrush, secondShapeX * ShapeSize + (ShapeSize - textSize.Width) / 2, secondShapeY * ShapeSize + (ShapeSize - textSize.Height) / 2);
- }
- static void DrawCells(Graphics gr) {
- CellsPen.DashStyle = DashStyle.Dash;
- for(int i = 0; i < 6; i++) {
- gr.DrawLine(CellsPen, 0, ShapeSize * i, ShapeSize * 5 + 1, ShapeSize * i);
- gr.DrawLine(CellsPen, ShapeSize * i, 0, ShapeSize * i, ShapeSize * 5 + 1);
- }
- }
- static bool IsPoint(int x, int y) {
- return x >= 0 && y >= 0 && x < 5 && y < 5;
- }
- public static int GetDXByIdx(int idx) {
- switch(idx) {
- case 0:
- return 0;
- case 1:
- return -1;
- case 2:
- return 0;
- case 3:
- return 1;
- }
- throw new Exception();
- }
- public static int GetDYByIdx(int idx) {
- switch(idx) {
- case 0:
- return -1;
- case 1:
- return 0;
- case 2:
- return 1;
- case 3:
- return 0;
- }
- throw new Exception();
- }
- static void CheckConnectors(Connector expected, Connector actual) {
- //if(expected.FlipH != actual.FlipH)
- // Log("FlipH: Expected: {0} != Actual: {1}, Name:{2}", expected.FlipH, actual.FlipH, expected.Name);
- //if(expected.FlipV != actual.FlipV)
- // Log("FlipV: Expected: {0} != Actual: {1}, Name:{2}", expected.FlipV, actual.FlipV, expected.Name);
- if(expected.Rotation != actual.Rotation)
- Log("Rotation: Expected: {0} != Actual: {1}, Name:{2}", expected.Rotation, actual.Rotation, expected.Name);
- //if(expected.Preset != actual.Preset)
- // Log("Preset: Expected: {0} != Actual: {1}, Name:{2}", expected.Preset, actual.Preset, expected.Name);
- //if(expected.AdjustValues.Count != actual.AdjustValues.Count)
- // Log("AdjustValues.Count: Expected: {0} != Actual: {1}, Name:{2}", expected.AdjustValues.Count, actual.AdjustValues.Count, expected.Name);
- //else {
- // for(int i = 0; i < expected.AdjustValues.Count; i++) {
- // if(expected.AdjustValues[i] != actual.AdjustValues[i])
- // Log("AdjustValue {0}: {1} != {2}", i, expected.AdjustValues[i], actual.AdjustValues[i]);
- // }
- //}
- }
- static void Log(string message, params object[] par) {
- //throw new Exception(message);
- Console.WriteLine(message, par);
- }
- static Connector[] ReadData() {
- const string fileName = "1.xml";
- XmlDocument document = new XmlDocument();
- document.Load(fileName);
- XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
- nsmgr.AddNamespace("xdr", "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing");
- nsmgr.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
- XmlNodeList cxnSpPrs = document.SelectNodes("*//xdr:nvCxnSpPr", nsmgr);
- Connector[] connectors = new Connector[96];
- for(int i = 0; i < cxnSpPrs.Count; i++) {
- Connector connector = new Connector();
- connectors[i] = connector;
- XmlNode cxnSpPr = cxnSpPrs[i];
- connector.Name = cxnSpPr.ChildNodes[0].Attributes["name"].Value;
- connector.StartId = int.Parse(cxnSpPr.ChildNodes[1].ChildNodes[0].Attributes["idx"].Value);
- connector.EndId = int.Parse(cxnSpPr.ChildNodes[1].ChildNodes[1].Attributes["idx"].Value);
- XmlNode spPr = cxnSpPr.NextSibling;
- XmlNode xfrm = spPr.ChildNodes[0];
- XmlNode prstGeom = spPr.ChildNodes[1];
- XmlAttribute rotationAttribute = xfrm.Attributes["rot"];
- if(rotationAttribute != null)
- connector.Rotation = int.Parse(rotationAttribute.Value);
- if(xfrm.Attributes["flipH"] != null)
- connector.FlipH = true;
- if(xfrm.Attributes["flipV"] != null)
- connector.FlipV = true;
- connector.Preset = prstGeom.Attributes["prst"].Value.Last() - '0';
- Dictionary<string, long> adjustas = new Dictionary<string, long>();
- foreach(XmlNode gdNode in prstGeom.ChildNodes[0].ChildNodes) {
- adjustas.Add(gdNode.Attributes["name"].Value, long.Parse(gdNode.Attributes["fmla"].Value.Replace("val ", String.Empty)));
- }
- long value;
- switch(connector.Preset) {
- case 3:
- if(!adjustas.TryGetValue("adj1", out value))
- value = 50000;
- connector.AdjustValues.Add(value);
- break;
- case 4:
- if(!adjustas.TryGetValue("adj1", out value))
- value = 50000;
- connector.AdjustValues.Add(value);
- if(!adjustas.TryGetValue("adj2", out value))
- value = 50000;
- connector.AdjustValues.Add(value);
- break;
- case 5:
- if(!adjustas.TryGetValue("adj1", out value))
- value = 50000;
- connector.AdjustValues.Add(value);
- if(!adjustas.TryGetValue("adj2", out value))
- value = 50000;
- connector.AdjustValues.Add(value);
- if(!adjustas.TryGetValue("adj3", out value))
- value = 50000;
- connector.AdjustValues.Add(value);
- break;
- }
- }
- XmlNodeList xfrms = document.SelectNodes("*//a:off", nsmgr);
- for(int i = 0; i < 96; i++) {
- XmlNode fromNode = xfrms[i * 3];
- XmlNode toNode = xfrms[i * 3 + 1];
- long fromX = long.Parse(fromNode.Attributes["x"].Value);
- long toX = long.Parse(toNode.Attributes["x"].Value);
- long fromY = long.Parse(fromNode.Attributes["y"].Value);
- long toY = long.Parse(toNode.Attributes["y"].Value);
- connectors[i].DX = fromX.CompareTo(toX);
- connectors[i].DY = fromY.CompareTo(toY);
- }
- return connectors;
- }
- }
- class Connector {
- public int StartId { get; set; }
- public int EndId { get; set; }
- public string Name { get; set; }
- public long Rotation { get; set; }
- public bool FlipH { get; set; }
- public bool FlipV { get; set; }
- public int Preset { get; set; }
- public List<long> AdjustValues { get; set; }
- public int DX { get; set; }
- public int DY { get; set; }
- public Connector() {
- AdjustValues = new List<long>();
- }
- }
- class PathInfo : IEquatable<PathInfo>, IComparable<PathInfo> {
- public int X { get; private set; }
- public int Y { get; private set; }
- public int Length { get; set; }
- public int Bends { get; set; }
- public int DX { get; set; }
- public int DY { get; set; }
- public PathInfo(int x, int y) {
- X = x;
- Y = y;
- }
- #region Overrides of ValueType
- public int CompareTo(PathInfo other) {
- int result = Length.CompareTo(other.Length);
- if(result == 0)
- result = Bends.CompareTo(other.Bends);
- return result;
- }
- public override bool Equals(object obj) {
- if(ReferenceEquals(null, obj))
- return false;
- return obj is PathInfo && Equals((PathInfo)obj);
- }
- #region Equality members
- public bool Equals(PathInfo other) {
- return X == other.X && Y == other.Y;
- }
- public override int GetHashCode() {
- return (X << 4) + Y;
- }
- #endregion
- #endregion
- }
- class MySortedList {
- readonly HashSet<PathInfo> innerList;
- public int Count { get { return innerList.Count; } }
- public MySortedList() {
- innerList = new HashSet<PathInfo>();
- }
- public void Add(PathInfo value) {
- innerList.Add(value);
- }
- public bool Contains(PathInfo value) {
- return innerList.Contains(value);
- }
- public PathInfo GetMinAndRemove() {
- if(Count == 0)
- throw new Exception();
- bool first = true;
- PathInfo min = null;
- foreach(PathInfo pathInfo in innerList) {
- if(first) {
- min = pathInfo;
- first = false;
- }
- else {
- if(pathInfo.CompareTo(min) == -1)
- min = pathInfo;
- }
- }
- innerList.Remove(min);
- return min;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement