//This code displays the data collected from existing grid(span or not) then displays it on a series of lcd
//panels with the same name "Profile LCD Panel" or variations of that. Just some basic stats. Qon donated the
//span code.
//######################################################################
//configuration variables:
const string pbName = "Programmable block profiler"; //block that runs the script.
const string LCDdisplays = "Profile LCD Panel"; //real names of panels: Profile LCD Panel 1, Profile LCD Panel 2,...
//names are like that for sorting purposes, or else data is randomly displayed.
const int displayChars = 800; //number of characters to put on the panel
const int rowWidth = 30; //number of chars per row
//the two const values there make a chunk of text 800 long and 30ish wide.
//the number of lines in the text depends on the actual text length.
void Main()
{
bool spanConnected = true; //span grids, no span = no pistons or rotors.
IMyTerminalBlock progBlock = GridTerminalSystem.GetBlockWithName(pbName);
var gridSize = progBlock.CubeGrid; //only check same size blocks
StringBuilder tester = new StringBuilder();
Dictionary<String,int> collate = new Dictionary<String, int>();
List<String> Names = new List<String>();
List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(allBlocks); //capture all blocks
for (int i = 0; i<allBlocks.Count; i++) { //count through the blocks
if (allBlocks[i].CubeGrid == progBlock.CubeGrid || spanConnected) { //look ma, one less instruction!
String BlockName = allBlocks[i].BlockDefinition.ToString(); //Gather Block info
if (allBlocks[i].CubeGrid.GridSizeEnum == gridSize.GridSizeEnum) {
int count = 1;
int Amount = 0;
if (collate.TryGetValue(BlockName, out Amount))
{
Amount += count;
collate[BlockName] = Amount;
} else {
collate.Add(BlockName, count);
Names.Add(BlockName); //use this list for key retrieval, no other way.
}
}//if Contains
}//if CubeGrid
} //format data to text
tester.Append(DateTime.Now.ToString() + " Block Profile\n");
for (int i = 0; i < Names.Count; i++) {
var truncIndex = Names[i].IndexOf('/') + 1;
var formatName = Names[i].Remove(0,truncIndex);
var newstring = formatName + ": " + collate[Names[i]].ToString().PadLeft(rowWidth - formatName.Length, '_') + "\n"; //good match or
if (formatName.Length < 5) {//not enough data ?
truncIndex = Names[i].IndexOf('_') + 1;
formatName = Names[i].Remove(0,truncIndex);
newstring = ": " + collate[Names[i]].ToString().PadLeft(rowWidth - formatName.Length + 1, '_') + "\n";
truncIndex = formatName.IndexOf('/');
newstring = formatName.Remove(truncIndex) + newstring;
}//if
tester.Append(newstring);
}//for
//code segment below displays text in multiple lcds, no text it shows the graphic selected instead
List<IMyTerminalBlock> lcd = new List<IMyTerminalBlock>();
GridTerminalSystem.SearchBlocksOfName(LCDdisplays, lcd); //display(s)
lcd = BlockSort(lcd);//sort by name.
int startIndex = 0; //keep track of where we left off
for (int i = 0; i < lcd.Count; i++) { //break up the text, chunklet style
var Display = lcd[i] as IMyTextPanel;
if (startIndex < tester.Length-1) { // is the limit less than the data?
var textBreak = startIndex + displayChars; //start at the limit and go over :)
for (int j = textBreak; j < tester.Length; j++) { //possible to exceed the string length with the line above.
textBreak = j; //set it to current count
if (tester[j] == '\n') {break;}
}//for
if (textBreak > tester.Length) textBreak = tester.Length - 1; //this can happen with the loop entry.
Display.WritePublicText(tester.ToString(startIndex,textBreak-startIndex));
startIndex = textBreak+1; //advance
Display.ShowTextureOnScreen();
Display.ShowPublicTextOnScreen();
} else {
Display.ShowTextureOnScreen(); //display selected texture instead of data
}// if startIndex
}//for
}//main
List<IMyTerminalBlock> BlockSort (List<IMyTerminalBlock> blocks)
{
List<IMyTerminalBlock> ListSort = new List<IMyTerminalBlock>();
List<string> blockNames = new List<string>();
for (int i = 0; i < blocks.Count; i++)
{
blockNames.Add(blocks[i].DisplayNameText.ToString());
}
blockNames.Sort();
for (int i = 0; i < blockNames.Count; i++)
{
for (int j = 0; j < blocks.Count; j++)
{
if (blockNames[i] == blocks[j].DisplayNameText.ToString() && !ListSort.Contains(blocks[j]))
{
ListSort.Add(blocks[j]);
}
}
}
return ListSort;
}