106 lines
3.9 KiB
C#
106 lines
3.9 KiB
C#
///
|
|
/// Copyright (c) 2020 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace SchematicDrawing
|
|
{
|
|
public class Pipe
|
|
{
|
|
public IDrawingItem StartItem;
|
|
public int StartNodeId;
|
|
public IDrawingItem EndItem;
|
|
public int EndNodeId;
|
|
public Sz Sz;
|
|
|
|
|
|
public Pipe(IDrawingItem startItem, int startNodeId, IDrawingItem endItem, int endNodeId, Sz sz)
|
|
{
|
|
StartItem = startItem;
|
|
StartNodeId = startNodeId;
|
|
EndItem = endItem;
|
|
EndNodeId = endNodeId;
|
|
Sz = sz;
|
|
}
|
|
|
|
public Pipe(string pipeStr, Sz sz, Dictionary<string, IDrawingItem> name2Item)
|
|
{
|
|
String2Items(pipeStr, name2Item, out StartItem, out StartNodeId, out EndItem, out EndNodeId);
|
|
Sz = sz;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return (StartItem != null && EndItem != null) ? string.Format("{0}~{1}~{2}~{3}", StartItem.Name, StartNodeId, EndItem.Name, EndNodeId) : string.Empty;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Calculates coordinates of the start- and the end-point
|
|
/// </summary>
|
|
/// <param name="x1">Start point X</param>
|
|
/// <param name="y1">Start point Y</param>
|
|
/// <param name="x2">End point X</param>
|
|
/// <param name="y2">End point Y</param>
|
|
/// <returns>true when calculation successful</returns>
|
|
public bool GetCoordinates(out int x1, out int y1, out int x2, out int y2)
|
|
{
|
|
DrawingShape startDShape = DrawingShape.GetDrawingShape(StartItem);
|
|
DrawingShape endDShape = DrawingShape.GetDrawingShape(EndItem);
|
|
|
|
if (StartItem != null && EndItem != null && startDShape != null && endDShape != null)
|
|
{
|
|
Node startNode = startDShape.GetRotatedFlippedNode(StartItem, StartNodeId);
|
|
Node endNode = endDShape.GetRotatedFlippedNode(EndItem, EndNodeId);
|
|
|
|
x1 = StartItem.X + startNode.X * Const.GridSize;
|
|
y1 = StartItem.Y + startNode.Y * Const.GridSize;
|
|
x2 = EndItem.X + endNode.X * Const.GridSize;
|
|
y2 = EndItem.Y + endNode.Y * Const.GridSize;
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
x1 = y1 = x2 = y2 = 0;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Converts pipe string to start and end items
|
|
/// </summary>
|
|
/// <param name="pipeStr">Edge string</param>
|
|
/// <param name="name2Item">Dictionary of items</param>
|
|
/// <param name="startItem">Start item</param>
|
|
/// <param name="endItem">End item</param>
|
|
/// <returns>true when conversion successful</returns>
|
|
public static bool String2Items(string pipeStr, Dictionary<string, IDrawingItem> name2Item, out IDrawingItem startItem, out int startNid, out IDrawingItem endItem, out int endNid)
|
|
{
|
|
string[] fields = pipeStr.Split(new char[] { '~' });
|
|
|
|
IDrawingItem start, end;
|
|
DrawingShape dshapeFrom, dshapeTo;
|
|
if (fields.Length == 4 && name2Item.TryGetValue(fields[0], out start)
|
|
&& (null != (dshapeFrom = DrawingShape.Shapes[DrawingShape.DrShIx(start.Shape, start.Sz)]))
|
|
&& int.TryParse(fields[1], out startNid) && (startNid >= 0) && (startNid < dshapeFrom.Nodes.Length)
|
|
&& name2Item.TryGetValue(fields[2], out end)
|
|
&& (null != (dshapeTo = DrawingShape.Shapes[DrawingShape.DrShIx(end.Shape, end.Sz)]))
|
|
&& int.TryParse(fields[3], out endNid) && (endNid >= 0) && (endNid < dshapeTo.Nodes.Length))
|
|
{
|
|
startItem = start;
|
|
endItem = end;
|
|
return true;
|
|
}
|
|
|
|
startItem = null;
|
|
endItem = null;
|
|
startNid = 0;
|
|
endNid = 0;
|
|
return false;
|
|
}
|
|
}
|
|
}
|