private IEnumerable<MaterialViewModel> ReadMaterialsFromXlsx(
Dictionary<string, Material> materials,
Dictionary<string,
FillPatternElement> fillPatterns)
{
Excel.Application excel
= new Excel.Application();
excel.Visible = false;
Excel.Workbook workbook = excel.Workbooks.Open(
_input_file_name, 0, true, 5, "", "", true,
Excel.XlPlatform.xlWindows, "\t", false,
false, 0, true, 1, 0 );
Excel.Worksheet worksheet = (Excel.Worksheet)
workbook.Worksheets.get_Item( 1 );
Excel.Range range = worksheet.UsedRange;
int iRow = 5;
while( null != range.Cells[iRow, 1].Value2 )
{
string matName = (string) range.Cells[iRow, 1].Value2;
matName += " " + (string) range.Cells[iRow, 2].Value2;
matName += " " + (string) range.Cells[iRow, 3].Value2;
if( !string.IsNullOrEmpty( matName ) )
{
double red = (double) range.Cells[iRow, 4].Value2;
double green = (double) range.Cells[iRow, 5].Value2;
double blue = (double) range.Cells[iRow, 6].Value2;
double transparency = (double) range.Cells[iRow, 8].Value2;
string surPatternName = (string) range.Cells[iRow, 9].Value2;
string cutPatternName = (string) range.Cells[iRow, 10].Value2;
string csi = (string) range.Cells[iRow, 11].Value2;
FillPatternElement fillPatternElement;
FillPattern surfacePattern = null, cutPattern = null;
if( fillPatterns.TryGetValue( surPatternName, out fillPatternElement ) )
surfacePattern = fillPatternElement.GetFillPattern();
if( fillPatterns.TryGetValue( cutPatternName, out fillPatternElement ) )
cutPattern = fillPatternElement.GetFillPattern();
var status = Status.Normal;
if( !materials.ContainsKey( csi ) )
status = Status.BaseMaterialClassNotFound;
if( materials.ContainsKey( matName ) )
status = Status.ProjectAlreadyContainsMaterialWithTheSameName;
yield return new MaterialViewModel
{
Name = matName,
BaseMaterialClass = csi,
Color = new Color(
Byte.Parse( red.ToString() ),
Byte.Parse( green.ToString() ),
Byte.Parse( blue.ToString() ) ),
Transparency = transparency,
SurfacePattern = surfacePattern,
CutPattern = cutPattern,
Status = status,
AddToProject = status == Status.Normal
};
}
++iRow;
}
workbook.Close( true, null, null );
excel.Quit();
}