public void AddLine(int type, string colorname, string [] stationsname, int[] positionx, int[] positiony) { int i, j, idmark, extra = 0, tempid, tempid1; int[] stations; int[] changestations; List <int> templist = new List <int>(); stations = new int [stationsname.Length]; int [] templine = new int[1]; if (LineCount > LinesArray.Length) { return; } LineCount++; tempid = FreeLineNum[0]; FreeLineNum.Remove(tempid); templine[0] = tempid; XmlDocument XmlStation = new XmlDocument(); XmlStation.Load(StationXml); XmlNode StationRoot = XmlStation.SelectSingleNode("All_Stations"); XmlNodeList StationList = StationRoot.ChildNodes; for (i = 0; i < stationsname.Length; i++) { idmark = 0; for (j = 1; j < StationMax; j++) { if (StationsArray[j] != null && StationsArray[j].Name == stationsname[i]) { idmark = j; break; } } if (idmark > 0) { if (StationsArray[idmark].Lines.Count == 1) { ChangeStationCount++; if (StationsArray[idmark].Lines[0] == tempid) { extra = idmark; } else { int line = StationsArray[idmark].Lines[0]; LinesArray[line].ChangeStations.Add(idmark); XmlDocument XmlLine1 = new XmlDocument(); XmlLine1.Load(LineXml); XmlNodeList linelist1 = XmlLine1.SelectSingleNode("All_Lines").ChildNodes; foreach (XmlNode linenode1 in linelist1) { XmlElement lineele1 = (XmlElement)linenode1; if (lineele1.GetAttribute("Line_ID") == line.ToString()) { XmlNodeList nodelist1 = lineele1.ChildNodes; foreach (XmlNode nodenode1 in nodelist1) { XmlElement nodeele1 = (XmlElement)nodenode1; if (nodeele1.Name == "Line_ChangeStations") { nodeele1.InnerText = LinesArray[line].ChangeStationsToString(); break; } } break; } } XmlLine1.Save(LineXml); StationsArray[idmark].Lines.Add(tempid); } } else { if (StationsArray[idmark].Lines.Contains(tempid) == false) { StationsArray[idmark].Lines.Add(tempid); } } stations[i] = idmark; templist.Add(idmark); foreach (XmlNode stationnode in StationList) { XmlElement stationele = (XmlElement)stationnode; if (stationele.GetAttribute("Station_ID") == idmark.ToString()) { XmlNodeList nodelist = stationele.ChildNodes; foreach (XmlNode nodenode in nodelist) { XmlElement nodeele = (XmlElement)nodenode; if (nodeele.Name == "Station_Lines") { nodeele.InnerText = StationsArray[idmark].LinesToString(); break; } } break; } } } else { if (StationCount + 1 >= StationsArray.Length) { return; } StationCount++; tempid1 = FreeStationNum[0]; FreeStationNum.Remove(tempid1); StationsArray[tempid1] = new Station(positionx[i], positiony[i], stationsname[i], templine); stations[i] = tempid1; XmlElement stationnode = XmlStation.CreateElement("Station"); stationnode.SetAttribute("Station_ID", tempid1.ToString()); XmlElement stationsub1 = XmlStation.CreateElement("Station_Name"); stationsub1.InnerText = stationsname[i]; stationnode.AppendChild(stationsub1); XmlElement stationsub2 = XmlStation.CreateElement("Station_Position"); stationsub2.InnerText = StationsArray[tempid1].PositionToString(); stationnode.AppendChild(stationsub2); XmlElement stationsub3 = XmlStation.CreateElement("Station_Lines"); stationsub3.InnerText = StationsArray[tempid1].LinesToString(); stationnode.AppendChild(stationsub3); StationRoot.AppendChild(stationnode); } } XmlStation.Save(StationXml); changestations = new int[templist.Count]; for (j = 0; j < templist.Count; j++) { changestations[j] = templist[j]; } LinesArray[tempid] = new Line(type, extra, colorname, stations, changestations); XmlDocument XmlLine = new XmlDocument(); XmlLine.Load(LineXml); XmlNode LineRoot = XmlLine.SelectSingleNode("All_Lines"); XmlNodeList LineList = LineRoot.ChildNodes; XmlElement linenode = XmlLine.CreateElement("Line"); linenode.SetAttribute("Line_ID", tempid.ToString()); XmlElement linesub1 = XmlLine.CreateElement("Line_Color"); linesub1.InnerText = colorname; linenode.AppendChild(linesub1); XmlElement linesub2 = XmlLine.CreateElement("Line_Type"); linesub2.InnerText = type.ToString(); linenode.AppendChild(linesub2); XmlElement linesub3 = XmlLine.CreateElement("Line_Stations"); linesub3.InnerText = LinesArray[tempid].StationsToString(); linenode.AppendChild(linesub3); XmlElement linesub4 = XmlLine.CreateElement("Line_ChangeStations"); linesub4.InnerText = LinesArray[tempid].ChangeStationsToString(); linenode.AppendChild(linesub4); XmlElement linesub5 = XmlLine.CreateElement("Line_Extral"); linesub5.InnerText = extra.ToString(); linenode.AppendChild(linesub5); LineRoot.AppendChild(linenode); XmlLine.Save(LineXml); }
public void AddStation(int x, int y, string name, int [] lines) { int prox, proy, extraMark = 0, closeMark = 0, i = 0, found = 0, exed = 0, prodistan = 0, distan = 0, todistan = 0, tempid = 0; if (StationCount + 1 >= StationsArray.Length) { return; } StationCount++; if (lines.Length > 1) { ChangeStationCount++; } tempid = FreeStationNum[0]; FreeStationNum.Remove(tempid); XmlDocument XmlLine = new XmlDocument(); XmlLine.Load(LineXml); XmlNodeList linelist = XmlLine.SelectSingleNode("All_Lines").ChildNodes; foreach (int line in lines) { extraMark = 0; closeMark = 0; i = 0; found = 0; exed = 0; prodistan = 0; distan = 0; todistan = 0; if (LinesArray[line].Stations.Count == 1) { closeMark = 1; } else { prox = StationsArray[LinesArray[line].Stations[0]].X; proy = StationsArray[LinesArray[line].Stations[0]].Y; prodistan = (x - prox) * (x - prox) + (y - proy) * (y - proy); for (i = 1; i < LinesArray[line].Stations.Count; i++) { if (found == 1) { break; } if (LinesArray[line].Stations[i] == LinesArray[line].Extral) { exed++; } distan = (StationsArray[LinesArray[line].Stations[i]].X - prox) * (StationsArray[LinesArray[line].Stations[i]].X - prox) + (StationsArray[LinesArray[line].Stations[i]].Y - proy) * (StationsArray[LinesArray[line].Stations[i]].Y - proy); prox = StationsArray[LinesArray[line].Stations[i]].X; proy = StationsArray[LinesArray[line].Stations[i]].Y; todistan = (x - prox) * (x - prox) + (y - proy) * (y - proy); if (exed == 2) { extraMark = i; prodistan = todistan; exed--; continue; } if (todistan <= distan && prodistan <= distan) { closeMark = i; found = 1; } prodistan = todistan; } if (closeMark == 0) { closeMark = LinesArray[line].Stations.Count; if (LinesArray[line].Type == 2) { prox = StationsArray[LinesArray[line].Stations[extraMark - 1]].X; proy = StationsArray[LinesArray[line].Stations[extraMark - 1]].Y; prodistan = (x - prox) * (x - prox) + (y - proy) * (y - proy); prox = StationsArray[LinesArray[line].Stations[LinesArray[line].Stations.Count - 1]].X; proy = StationsArray[LinesArray[line].Stations[LinesArray[line].Stations.Count - 1]].Y; todistan = (x - prox) * (x - prox) + (y - proy) * (y - proy); if (prodistan < todistan) { closeMark = extraMark; } ; } prox = StationsArray[LinesArray[line].Stations[0]].X; proy = StationsArray[LinesArray[line].Stations[0]].Y; prodistan = (x - prox) * (x - prox) + (y - proy) * (y - proy); prox = StationsArray[LinesArray[line].Stations[closeMark - 1]].X; proy = StationsArray[LinesArray[line].Stations[closeMark - 1]].Y; todistan = (x - prox) * (x - prox) + (y - proy) * (y - proy); if (prodistan < todistan) { closeMark = 0; } } } LinesArray[line].Stations.Insert(closeMark, tempid); if (lines.Length > 1) { LinesArray[line].ChangeStations.Add(tempid); } foreach (XmlNode linenode in linelist) { XmlElement lineele = (XmlElement)linenode; if (lineele.GetAttribute("Line_ID") == line.ToString()) { XmlNodeList nodelist = lineele.ChildNodes; foreach (XmlNode nodenode in nodelist) { XmlElement nodeele = (XmlElement)nodenode; if (nodeele.Name == "Line_Stations") { nodeele.InnerText = LinesArray[line].StationsToString(); } if (nodeele.Name == "Line_ChangeStations") { if (lines.Length > 1) { nodeele.InnerText = LinesArray[line].ChangeStationsToString(); } } } break; } } } XmlLine.Save(LineXml); StationsArray[tempid] = new Station(x, y, name, lines); XmlDocument XmlStation = new XmlDocument(); XmlStation.Load(StationXml); XmlNode StationRoot = XmlStation.SelectSingleNode("All_Stations"); XmlElement stationnode = XmlStation.CreateElement("Station"); stationnode.SetAttribute("Station_ID", tempid.ToString()); XmlElement stationsub1 = XmlStation.CreateElement("Station_Name"); stationsub1.InnerText = name; stationnode.AppendChild(stationsub1); XmlElement stationsub2 = XmlStation.CreateElement("Station_Position"); stationsub2.InnerText = StationsArray[tempid].PositionToString();; stationnode.AppendChild(stationsub2); XmlElement stationsub3 = XmlStation.CreateElement("Station_Lines"); stationsub3.InnerText = StationsArray[tempid].LinesToString(); stationnode.AppendChild(stationsub3); StationRoot.AppendChild(stationnode); XmlStation.Save(StationXml); }