Exemplo n.º 1
0
        public Bio2DA(ExportEntry export)
        {
            //Console.WriteLine("Loading " + export.ObjectName);
            Export = export;

            RowNames = new List<string>();
            if (export.ClassName == "Bio2DA")
            {
                const string rowLabelsVar = "m_sRowLabel";
                var props = export.GetProperty<ArrayProperty<NameProperty>>(rowLabelsVar);
                if (props != null)
                {
                    foreach (NameProperty n in props)
                    {
                        RowNames.Add(n.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Unable to find row names property!");
                    return;
                }
            }
            else
            {
                var props = export.GetProperty<ArrayProperty<IntProperty>>("m_lstRowNumbers");//Bio2DANumberedRows
                if (props != null)
                {
                    foreach (IntProperty n in props)
                    {
                        RowNames.Add(n.Value.ToString());
                    }
                }
                else
                {
                    Debug.WriteLine("Unable to find row names property (m_lstRowNumbers)!");
                    return;
                }
            }

            var binary = export.GetBinaryData<Bio2DABinary>();

            ColumnNames = new List<string>(binary.ColumnNames.Select(n => n.Name));
            Cells = new Bio2DACell[RowCount, ColumnCount];

            foreach ((int index, Bio2DABinary.Cell cell) in binary.Cells)
            {
                int row = index / ColumnCount;
                int col = index % ColumnCount;
                this[row, col] = cell.Type switch
                {
                    Bio2DABinary.DataType.INT => new Bio2DACell(cell.IntValue),
                    Bio2DABinary.DataType.NAME => new Bio2DACell(cell.NameValue, export.FileRef),
                    Bio2DABinary.DataType.FLOAT => new Bio2DACell(cell.FloatValue),
                    _ => throw new ArgumentOutOfRangeException()
                };
            }

            IsIndexed = binary.IsIndexed;
        }
Exemplo n.º 2
0
 internal void MarkAsUnmodified()
 {
     for (int rowindex = 0; rowindex < RowCount; rowindex++)
     {
         for (int colindex = 0; colindex < ColumnCount; colindex++)
         {
             Bio2DACell cell = Cells[rowindex, colindex];
             if (cell != null)
             {
                 cell.IsModified = false;
             }
         }
     }
 }
Exemplo n.º 3
0
        private void StartBio2DAScan()
        {
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            table2da = new Bio2DA(export);
            //Add columns
            for (int j = 0; j < table2da.ColumnNames.Count(); j++)
            {
                dataGridView1.Columns.Add(table2da.ColumnNames[j], table2da.ColumnNames[j]);
            }


            //Add rows
            for (int i = 0; i < table2da.RowNames.Count(); i++)
            {
                //defines row data. If you add columns, you need to add them here in order
                List <Object> rowData = new List <object>();
                for (int j = 0; j < table2da.ColumnNames.Count(); j++)
                {
                    Bio2DACell cell = table2da[i, j];
                    if (cell != null)
                    {
                        rowData.Add(cell.GetDisplayableValue());
                    }
                    else
                    {
                        rowData.Add(null);
                    }
                    //rowData.Add(table2da[i, j]);
                }
                dataGridView1.Rows.Add(rowData.ToArray());
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].HeaderCell.Value = table2da.RowNames[i];
            }

            //Add row headers
            for (int i = 0; i < table2da.RowNames.Count(); i++)
            {
                dataGridView1.Rows[i].HeaderCell.Value = table2da.RowNames[i];
            }
        }
Exemplo n.º 4
0
        private void Table_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count > 0)
            {
                var seelctedcell = dataGridView1.SelectedCells[0];

                Label_CellCoordinate.Text = seelctedcell.RowIndex + ", " + seelctedcell.ColumnIndex;
                Bio2DACell cell = table2da[seelctedcell.RowIndex, seelctedcell.ColumnIndex];
                if (cell != null)
                {
                    string text = "";
                    switch (cell.Type)
                    {
                    case Bio2DACell.TYPE_FLOAT:
                        text = "Float";
                        break;

                    case Bio2DACell.TYPE_NAME:
                        text = "Name";
                        break;

                    case Bio2DACell.TYPE_INT:
                        text = "Integer";
                        break;
                    }
                    Label_CellType.Text = text;
                }
                else
                {
                    Label_CellType.Text = "Null Cell";
                }
            }
            else
            {
                Label_CellCoordinate.Text = "Select a cell";
                Label_CellType.Text       = "Select a cell";
            }
        }
Exemplo n.º 5
0
        private void Table_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count > 0)
            {
                var seelctedcell = dataGridView1.SelectedCells[0];

                Label_CellCoordinate.Text = seelctedcell.RowIndex + ", " + seelctedcell.ColumnIndex;
                Bio2DACell cell = table2da[seelctedcell.RowIndex, seelctedcell.ColumnIndex];
                if (cell != null)
                {
                    Label_CellType.Text = cell.GetTypeString();
                }
                else
                {
                    Label_CellType.Text = "Null Cell";
                }
            }
            else
            {
                Label_CellCoordinate.Text = "Select a cell";
                Label_CellType.Text       = "Select a cell";
            }
        }
Exemplo n.º 6
0
        public Bio2DA(IExportEntry export)
        {
            Console.WriteLine("Loading " + export.ObjectName);
            this.export = export;
            IMEPackage pcc = export.FileRef;

            byte[] data = export.Data;

            RowNames = new List <string>();
            if (export.ClassName == "Bio2DA")
            {
                string rowLabelsVar = "m_sRowLabel";
                var    properties   = export.GetProperties();
                var    props        = export.GetProperty <ArrayProperty <NameProperty> >(rowLabelsVar);
                if (props != null)
                {
                    foreach (NameProperty n in props)
                    {
                        RowNames.Add(n.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Unable to find row names property!");
                    Debugger.Break();
                    return;
                }
            }
            else
            {
                string rowLabelsVar = "m_lstRowNumbers"; //Bio2DANumberedRows
                var    props        = export.GetProperty <ArrayProperty <IntProperty> >(rowLabelsVar);
                if (props != null)
                {
                    foreach (IntProperty n in props)
                    {
                        RowNames.Add(n.Value.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Unable to find row names property!");
                    Debugger.Break();
                    return;
                }
            }

            //Get Columns
            ColumnNames = new List <string>();
            int colcount         = BitConverter.ToInt32(data, data.Length - 4);
            int currentcoloffset = 0;

            while (colcount >= 0)
            {
                currentcoloffset += 4;
                int colindex = BitConverter.ToInt32(data, data.Length - currentcoloffset);
                currentcoloffset += 8; //names in this case don't use nameindex values.
                int    nameindex = BitConverter.ToInt32(data, data.Length - currentcoloffset);
                string name      = pcc.getNameEntry(nameindex);
                ColumnNames.Insert(0, name);
                colcount--;
            }
            Cells = new Bio2DACell[RowNames.Count(), ColumnNames.Count()];

            currentcoloffset += 4;  //column count.
            int infilecolcount = BitConverter.ToInt32(data, data.Length - currentcoloffset);

            //start of binary data
            int binstartoffset = export.propsEnd(); //arrayheader + nonenamesize + number of items in this list
            int curroffset     = binstartoffset;

            int cellcount = BitConverter.ToInt32(data, curroffset);

            if (cellcount > 0)
            {
                curroffset += 4;
                for (int rowindex = 0; rowindex < RowNames.Count(); rowindex++)
                {
                    for (int colindex = 0; colindex < ColumnNames.Count() && curroffset < data.Length - currentcoloffset; colindex++)
                    {
                        byte dataType = 255;
                        dataType = data[curroffset];
                        curroffset++;
                        int    dataSize = dataType == Bio2DACell.TYPE_NAME ? 8 : 4;
                        byte[] celldata = new byte[dataSize];
                        Buffer.BlockCopy(data, curroffset, celldata, 0, dataSize);
                        Bio2DACell cell = new Bio2DACell(pcc, curroffset, dataType, celldata);
                        Cells[rowindex, colindex] = cell;
                        curroffset += dataSize;
                    }
                }
                CellCount = RowNames.Count() * ColumnNames.Count();
            }
            else
            {
                IsIndexed   = true;
                curroffset += 4; //theres a 0 here for some reason
                cellcount   = BitConverter.ToInt32(data, curroffset);
                curroffset += 4;

                //curroffset += 4;
                while (CellCount < cellcount)
                {
                    int index = BitConverter.ToInt32(data, curroffset);
                    int row   = index / ColumnNames.Count();
                    int col   = index % ColumnNames.Count();
                    curroffset += 4;
                    byte dataType = data[curroffset];
                    int  dataSize = dataType == Bio2DACell.TYPE_NAME ? 8 : 4;
                    curroffset++;
                    byte[] celldata = new byte[dataSize];
                    Buffer.BlockCopy(data, curroffset, celldata, 0, dataSize);
                    Bio2DACell cell = new Bio2DACell(pcc, curroffset, dataType, celldata);
                    Cells[row, col] = cell;
                    CellCount++;
                    curroffset += dataSize;
                }
            }
            Console.WriteLine("Finished loading " + export.ObjectName);
        }
Exemplo n.º 7
0
        public void Write2DAToExport()
        {
            using (var stream = new MemoryStream())
            {
                //Cell count
                if (IsIndexed)
                {
                    //Indexed ones seem to have 0 at start
                    stream.WriteBytes(BitConverter.GetBytes(0));
                }
                stream.WriteBytes(BitConverter.GetBytes(CellCount));

                //Write cell data
                for (int rowindex = 0; rowindex < RowNames.Count(); rowindex++)
                {
                    for (int colindex = 0; colindex < ColumnNames.Count(); colindex++)
                    {
                        Bio2DACell cell = Cells[rowindex, colindex];
                        if (cell != null)
                        {
                            if (IsIndexed)
                            {
                                //write index
                                int index = (rowindex * ColumnNames.Count()) + colindex; //+1 because they are not zero based indexes since they are numerals
                                stream.WriteBytes(BitConverter.GetBytes(index));
                            }
                            stream.WriteByte(cell.Type);
                            stream.WriteBytes(cell.Data);
                        }
                        else
                        {
                            if (IsIndexed)
                            {
                                //this is a blank cell. It is not present in the table.
                                continue;
                            }
                            else
                            {
                                Console.WriteLine("THIS SHOULDN'T OCCUR!");
                            }
                        }
                    }
                }

                //Write Columns
                if (!IsIndexed)
                {
                    stream.WriteBytes(BitConverter.GetBytes(0)); //seems to be a 0 before column definitions
                }
                //Console.WriteLine("Columns defs start at " + stream.Position.ToString("X6"));
                stream.WriteBytes(BitConverter.GetBytes(ColumnNames.Count()));
                for (int colindex = 0; colindex < ColumnNames.Count(); colindex++)
                {
                    //Console.WriteLine("Writing column definition " + columnNames[colindex]);
                    int nameIndexForCol = export.FileRef.findName(ColumnNames[colindex]);
                    stream.WriteBytes(BitConverter.GetBytes(nameIndexForCol));
                    stream.WriteBytes(BitConverter.GetBytes(0)); //second half of name reference in 2da is always zero since they're always indexed at 0
                    stream.WriteBytes(BitConverter.GetBytes(colindex));
                }

                int    propsEnd      = export.propsEnd();
                byte[] binarydata    = stream.ToArray();
                byte[] propertydata  = export.Data.Take(propsEnd).ToArray();
                var    newExportData = new byte[propertydata.Length + binarydata.Length];
                propertydata.CopyTo(newExportData, 0);
                binarydata.CopyTo(newExportData, propertydata.Length);
                //Console.WriteLine("Old data size: " + export.Data.Length);
                //Console.WriteLine("NEw data size: " + newExportData.Length);
                if (export.Data.Length != newExportData.Length)
                {
                    Console.WriteLine("FILES ARE WRONG SIZE");
                    Debugger.Break();
                }
                export.Data = newExportData;
            }
        }
Exemplo n.º 8
0
        public static Bio2DA ReadExcelTo2DA(IExportEntry export, string Filename)
        {
            var          Workbook   = new XLWorkbook(Filename);
            IXLWorksheet iWorksheet = null;

            if (Workbook.Worksheets.Count() > 1)
            {
                try
                {
                    iWorksheet = Workbook.Worksheet("Import");
                }
                catch
                {
                    MessageBox.Show("Import Sheet not found");
                    return(null);
                }
            }
            else
            {
                iWorksheet = Workbook.Worksheet(1);
            }

            //Do we want to limit user to importing same column structure as existing?  Who would be stupid enough to do something else??? ME.
            // - Kinkojiro, 2019

            //STEP 1 Clear existing data
            Bio2DA bio2da = new Bio2DA();

            bio2da.export = export;

            //STEP 2 Read columns and row names

            //Column names
            IXLRow hRow = iWorksheet.Row(1);

            foreach (IXLCell cell in hRow.Cells(hRow.FirstCellUsed().Address.ColumnNumber, hRow.LastCellUsed().Address.ColumnNumber))
            {
                if (cell.Address.ColumnNumber > 1) //ignore excel column 1
                {
                    bio2da.ColumnNames.Add(cell.Value.ToString());
                }
            }

            //Row names
            IXLColumn column = iWorksheet.Column(1);

            foreach (IXLCell cell in column.Cells())
            {
                if (cell.Address.RowNumber > 1) //ignore excel row 1
                {
                    bio2da.RowNames.Add(cell.Value.ToString());
                }
            }

            //Populate the Bio2DA now that we know the size
            bio2da.Cells = new Bio2DACell[bio2da.RowCount, bio2da.ColumnCount];


            //Step 3 Populate the table.
            //indices here are excel based. Subtract two to get Bio2DA based.
            for (int rowIndex = 2; rowIndex < (bio2da.RowCount + 2); rowIndex++)
            {
                for (int columnIndex = 2; columnIndex < bio2da.ColumnCount + 2; columnIndex++)
                {
                    IXLCell xlCell         = iWorksheet.Cell(rowIndex, columnIndex);
                    string  xlCellContents = xlCell.Value.ToString();
                    if (!string.IsNullOrEmpty(xlCellContents))
                    {
                        Bio2DACell newCell = new Bio2DACell();
                        if (int.TryParse(xlCellContents, out int intVal))
                        {
                            newCell.Type = Bio2DACell.Bio2DADataType.TYPE_INT;
                            newCell.Data = BitConverter.GetBytes(intVal);
                        }
                        else if (float.TryParse(xlCellContents, out float floatVal))
                        {
                            newCell.Type = Bio2DACell.Bio2DADataType.TYPE_FLOAT;
                            newCell.Data = BitConverter.GetBytes(floatVal);
                        }
                        else
                        {
                            newCell.Type = Bio2DACell.Bio2DADataType.TYPE_NAME;
                            newCell.Pcc  = export.FileRef;                                                            //for displaying, if this displays before the export is reloaded and 2da is refreshed
                            newCell.Data = BitConverter.GetBytes((long)export.FileRef.FindNameOrAdd(xlCellContents)); //long because names are 8 bytes not 4
                        }
                        bio2da[rowIndex - 2, columnIndex - 2] = newCell;
                    }
                    else
                    {
                        bio2da.IsIndexed = true;  //Null cells = indexing
                    }
                }
            }
            return(bio2da);
        }
Exemplo n.º 9
0
        public void Write2DAToExport()
        {
            using (var stream = new MemoryStream())
            {
                //Cell count
                if (IsIndexed)
                {
                    //Indexed ones seem to have 0 at start
                    stream.WriteBytes(BitConverter.GetBytes(0));
                }
                stream.WriteBytes(BitConverter.GetBytes(PopulatedCellCount));

                //Write cell data
                for (int rowindex = 0; rowindex < RowCount; rowindex++)
                {
                    for (int colindex = 0; colindex < ColumnCount; colindex++)
                    {
                        Bio2DACell cell = Cells[rowindex, colindex];
                        if (cell != null)
                        {
                            if (IsIndexed)
                            {
                                //write index
                                int index = (rowindex * ColumnCount) + colindex; //+1 because they are not zero based indexes since they are numerals
                                stream.WriteBytes(BitConverter.GetBytes(index));
                            }
                            stream.WriteByte((byte)cell.Type);
                            stream.WriteBytes(cell.Data);
                        }
                        else
                        {
                            if (IsIndexed)
                            {
                                //this is a blank cell. It is not present in the table.
                                continue;
                            }
                            else
                            {
                                Debug.WriteLine("THIS SHOULDN'T OCCUR!");
                                Debugger.Break();
                                throw new Exception("A non-indexed Bio2DA cannot have null cells.");
                            }
                        }
                    }
                }

                //Write Columns
                if (!IsIndexed)
                {
                    stream.WriteBytes(BitConverter.GetBytes(0)); //seems to be a 0 before column definitions
                }
                //Console.WriteLine("Columns defs start at " + stream.Position.ToString("X6"));
                stream.WriteBytes(BitConverter.GetBytes(ColumnCount));
                for (int colindex = 0; colindex < ColumnCount; colindex++)
                {
                    //Console.WriteLine("Writing column definition " + columnNames[colindex]);
                    int nameIndexForCol = export.FileRef.FindNameOrAdd(ColumnNames[colindex]);
                    stream.WriteBytes(BitConverter.GetBytes(nameIndexForCol));
                    stream.WriteBytes(BitConverter.GetBytes(0)); //second half of name reference in 2da is always zero since they're always indexed at 0
                    stream.WriteBytes(BitConverter.GetBytes(colindex));
                }

                int    propsEnd   = export.propsEnd();
                byte[] binarydata = stream.ToArray();

                //Todo: Rewrite properties here
                PropertyCollection props = new PropertyCollection();
                if (export.ClassName == "Bio2DA")
                {
                    var indicies = new ArrayProperty <NameProperty>(ArrayType.Name, "m_sRowLabel");
                    foreach (var rowname in RowNames)
                    {
                        indicies.Add(new NameProperty {
                            Value = rowname
                        });
                    }
                    props.Add(indicies);
                }
                else
                {
                    var indices = new ArrayProperty <IntProperty>(ArrayType.Int, "m_lstRowNumbers");
                    foreach (var rowname in RowNames)
                    {
                        indices.Add(new IntProperty(int.Parse(rowname)));
                    }
                    props.Add(indices);
                }

                MemoryStream propsStream = new MemoryStream();
                props.WriteTo(propsStream, export.FileRef);
                MemoryStream currentDataStream   = new MemoryStream(export.Data);
                byte[]       propertydata        = propsStream.ToArray();
                int          propertyStartOffset = export.GetPropertyStart();
                var          newExportData       = new byte[propertyStartOffset + propertydata.Length + binarydata.Length];
                Buffer.BlockCopy(export.Data, 0, newExportData, 0, propertyStartOffset);
                propertydata.CopyTo(newExportData, propertyStartOffset);
                binarydata.CopyTo(newExportData, propertyStartOffset + propertydata.Length);
                //Console.WriteLine("Old data size: " + export.Data.Length);
                //Console.WriteLine("NEw data size: " + newExportData.Length);

                //This assumes the input and output data sizes are the same. We should not assume this with new functionality
                //if (export.Data.Length != newExportData.Length)
                //{
                //    Debug.WriteLine("FILES ARE WRONG SIZE");
                //    Debugger.Break();
                //}
                export.Data = newExportData;
            }
        }