//only returns null if going to the yard //called by TrainModel to get the next block that the train will be moving to, queries the Wayside for switch states if needed public Block getNextBlock(Block prevBlock, Block currBlock, int?lineId = null) { Block nextBlock = null; bool isSource = false; bool isTarget = false; if (prevBlock == null && currBlock == null) //coming from yard { return(findYardBlock((int)lineId)); } if (currBlock.parentSwitch != null) { if (currBlock.parentSwitch.sourceBlockId == currBlock.blockId) { isSource = true; } else if (currBlock.parentSwitch.targetBlockId1 == currBlock.blockId || currBlock.parentSwitch.targetBlockId2 == currBlock.blockId) { isTarget = true; } } if (prevBlock == null && currBlock.parentSwitch != null) //if already on 1st block from yard { if (isTarget) { return(findBlock((int)currBlock.parentSwitch.sourceBlockId)); } else if (isSource) { int targetId = (int)TrackControllerModule.getSwitchState(currBlock.parentSwitch.switchId); return(findBlock(targetId)); } } if (prevBlock != null && currBlock.prevBlockId == null && currBlock.nextBlockId == null) { return(null); } else if (prevBlock.parentSwitch != null && currBlock.parentSwitch != null) //if coming off a switch { if (currBlock.prevBlockId == null) { return(findBlock((int)currBlock.nextBlockId)); } else { return(findBlock((int)currBlock.prevBlockId)); } } else if (currBlock.parentSwitch != null && prevBlock.parentSwitch == null) //if entering a switch { if (isTarget) { return(findBlock((int)currBlock.parentSwitch.sourceBlockId)); } else if (isSource) { int targetId = (int)TrackControllerModule.getSwitchState(currBlock.parentSwitch.switchId); return(findBlock(targetId)); } } else //if no switches involved { if (prevBlock.nextBlockId != null && prevBlock.nextBlockId == currBlock.blockId) { return(findBlock((int)currBlock.nextBlockId)); } else if (prevBlock.prevBlockId != null && prevBlock.prevBlockId == currBlock.blockId) { return(findBlock((int)currBlock.prevBlockId)); } } return(nextBlock); }
/* * adds all block from DB to blockList * initializes a trainList * sets up train tables in CTC window * */ public void initializeTrackLayout(List <Line> trackLines) { systemListView.View = View.Details; systemListView.FullRowSelect = true; ListViewExtender extendo = new ListViewExtender(systemListView); ListViewButtonColumn blockClick = new ListViewButtonColumn(0); blockClick.Click += selBlock; blockClick.FixedWidth = true; extendo.AddColumn(blockClick); ListViewButtonColumn trainClick = new ListViewButtonColumn(2); trainClick.Click += selTrain; trainClick.FixedWidth = true; extendo.AddColumn(trainClick); myBlockList = new List <Block>(); myLineList = new List <Line>(); Block yard = new TrainProject.Block(0, 1); myBlockList.Add(yard); myTrainList = new List <Train>(); foreach (Line line in trackLines) { myLineList.Add(line); foreach (Section section in line.sections) { if (line.lineId == 2) //red line { foreach (Block block in section.blocks) { { if (block != null) { myBlockList.Add(block); block.isOccupied = false; block.lineId = 2; Console.WriteLine(block.line); block.section = section.name; } } } } } } //add yard ListViewItem yrd = new ListViewItem(); yrd.Text = "Yard"; systemListView.Items.Add(yrd); foreach (Block block in myBlockList) { if (block.lineId == 2) { String bl = block.blockNum.ToString(); ListViewItem item = new ListViewItem(); item.Text = block.blockNum.ToString(); item.SubItems.Add("Open"); item.SubItems.Add("-");//occupancy systemListView.Items.Add(item); if (block.parentSwitch != null) //check if block is associated with switch { if (block.blockId == block.parentSwitch.sourceBlockId) { int switchId = block.parentSwitch.switchId; int currState = (int)TrackControllerModule.getSwitchState(switchId); Block b = findBlock(currState); //int currState = (int)TrackControllerWindow.controllerModule.getSwitchState(switchId); if (currState == (b.blockId)) { currState = (b.blockNum); } item.SubItems.Add(currState.ToString()); } } else { item.SubItems.Add("-"); //switch state } item.SubItems.Add("-"); //crossing state } } //-----------------Green Line-------------------- systemListView2.View = View.Details; systemListView2.FullRowSelect = true; ListViewExtender extendo2 = new ListViewExtender(systemListView2); ListViewButtonColumn blockClick2 = new ListViewButtonColumn(0); blockClick2.Click += selBlock; blockClick2.FixedWidth = true; extendo2.AddColumn(blockClick2); ListViewButtonColumn trainClick2 = new ListViewButtonColumn(2); trainClick2.Click += selTrain; trainClick2.FixedWidth = true; extendo2.AddColumn(trainClick2); foreach (Line line in trackLines) { foreach (Section section in line.sections) { if (line.lineId == 1) //green line { foreach (Block block in section.blocks) { { if (block != null) { myBlockList.Add(block); block.isOccupied = false; block.lineId = 1; block.section = section.name; } } } } } } //add yard yrd = new ListViewItem(); yrd.Text = "Yard"; systemListView2.Items.Add(yrd); foreach (Block block in myBlockList) { if (block.lineId == 1) { String bl = block.blockNum.ToString(); ListViewItem item = new ListViewItem(); item.Text = block.blockNum.ToString(); item.SubItems.Add("Open"); item.SubItems.Add("-");//occupancy systemListView2.Items.Add(item); if (block.parentSwitch != null) //check if block is associated with switch { if (block.blockId == block.parentSwitch.sourceBlockId) { int switchId = block.parentSwitch.switchId; int currState = (int)TrackControllerModule.getSwitchState(switchId); Block b = findBlock(currState); //int currState = (int)TrackControllerWindow.controllerModule.getSwitchState(switchId); if (currState == (b.blockId)) { currState = (b.blockNum); } item.SubItems.Add(currState.ToString()); } } else { item.SubItems.Add("-"); //switch state } item.SubItems.Add("-"); //crossing state } } }