Language/C#

C#

비비이잉 2022. 5. 9. 14:26
반응형
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Markdig;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Linq;
using System.Net;
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;

namespace AVIDefectImageReview
{

    public partial class ReportForm : Form
    {
        private List<float> height_list = new List<float>();

        private string readvalue_no;
        private DataRow dr;
        private DataRow dr_height;
        private int offset;
        private DataTable dt;
        private DataTable dt_height;
        private Tuple<int, int> Col_Row;
        private struct HeightInfo
        {
            public int size { get; set; }
            public List<Field> fields { get; set; }

            public struct Field
            {
                public string name { get; set; }
                public int offset { get; set; }
                public int type { get; set; }
            }
        }
        private const string KEY_SIZE = "Size";
        private const string KEY_NAME = "Name";
        private const string KEY_OFFSET = "Offset";
        private const string KEY_VARTYPE = "Vartype";

        public ReportForm()
        {
            InitializeComponent();
            CenterToScreen();
            dt = new DataTable();
            dt.Columns.Add("zone", typeof(long));
            dt.Columns.Add("x", typeof(float));
            dt.Columns.Add("y", typeof(float));
            dt.Columns.Add("ActualX", typeof(float));
            dt.Columns.Add("ActualY", typeof(float));
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("height", typeof(float));
            dt.Columns.Add("Col", typeof(float));
            dt.Columns.Add("Row", typeof(float));
            dt.Columns.Add("intCOl", typeof(int));
            dt.Columns.Add("intRow", typeof(int));


            string mdfile = $"{Properties.Settings.Default.HttpURL}:{Properties.Settings.Default.HttpURLPort}/review/images/T092292A/01/s_Height.dat.md";
            string datfile = $"{Properties.Settings.Default.HttpURL}:{Properties.Settings.Default.HttpURLPort}/review/images/T092292A/01/s_Height.dat";

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //download .md file .dat file
            using (WebClient client = new WebClient())
            {
                Console.WriteLine(mdfile);
                Console.WriteLine(datfile);

                client.DownloadFile(mdfile, @"C:/Users/raine/Desktop/s_Height_Test.dat.md");
                client.DownloadFile(datfile, @"C:/Users/raine/Desktop/s_Height_Test.dat");

            }
            stopwatch.Stop();
            Console.WriteLine("Download : " + stopwatch.ElapsedMilliseconds + "ms");

            Stopwatch stopwatch2 = new Stopwatch();
            stopwatch2.Start();
            string url = "C:/Users/raine/Desktop/s_Height_Test.dat.md";
            StreamReader SR = new System.IO.StreamReader(File.Open(url, FileMode.Open));
            string hey = Markdown.ToHtml(SR.ReadToEnd());
            Console.WriteLine($"To HTML : {hey}");
            var parts = Regex.Split(hey, @"Offset=(.+?) Vartype");
            int[] parts_real = new int[7];
            int count = 0;
            for (int i = 0; i < parts.Length; i++)
            {
                if (i % 2 == 1)
                {
                    string strValue = parts[i].ToString();
                    strValue = strValue.Replace("\"", "");
                    int val = Convert.ToInt32(strValue);
                    parts_real[count] = val; //숫자만 들어가있는 배열 0,4,8,12,16,20,24
                    count++;
                }

            }

            var vartypes = Regex.Split(hey, @"Vartype=(.+?)/");
            int[] vartype_real = new int[7];
            int cnt = 0;
            foreach (var type in vartypes)
                Console.WriteLine($"vartype : {type}");
            for (int i = 0; i < vartypes.Length; i++)
            {
                if (i % 2 == 1)
                {

                    string str = vartypes[i].ToString();
                    str = str.Replace("\"", "");
                    int val = Convert.ToInt32(str);
                    vartype_real[cnt] = val; //byte가 들어가있는 배열 17 4 4 4 3 4 
                    cnt++;
                }

            }
            stopwatch2.Stop();
            Console.WriteLine("Parsnig  : " + stopwatch2.ElapsedMilliseconds + "ms");


            byte[] rbuff = new byte[28];
            using (FileStream fileStream = new FileStream("C:/Users/raine/Desktop/s_Height_Test.dat", FileMode.Open))
            {

                BinaryReader br = new BinaryReader(fileStream, System.Text.Encoding.UTF8);
                for (int i = 0; i < br.BaseStream.Length / 28; i++)
                {
                    dr = dt.NewRow();
                    
                    br.Read(rbuff, 0, rbuff.Length); //28바이트를 다 읽은거

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(rbuff);
                    }

                    string readvalue = BitConverter.ToString(rbuff, 0);

                    readvalue_no = readvalue.Replace("-", "");

                    for (int j = 0; j < 7; j++)
                    {

                        if (5 - j < 0)
                            offset = 4;
                        else
                            offset = parts_real[6 - j] - parts_real[5 - j];

                        covert_to_var_type(vartype_real[6 - j], offset, j);

                        
                    }
                    dt.Rows.Add(dr);
                }
                Stopwatch stopwatch3 = new Stopwatch();
                stopwatch3.Start();
                metroGridWaferReview.DataSource = dt;
                Console.WriteLine($"dt.count :: {dt.Rows.Count}");
                using (StreamWriter outputFile = new StreamWriter(@"C:/Users/raine/Desktop/ColRow.txt"))
                {
                    for(int i=0; i<dt.Rows.Count; i++)
                    {
                        outputFile.WriteLine($"{dt.Rows[i][9]} {dt.Rows[i][10]}");
                    }
                }

                stopwatch3.Stop();
                Console.WriteLine("Datagridview  : " + stopwatch3.ElapsedMilliseconds + "ms");
               

                metroGridWaferReview.Columns[0].HeaderText = "Zone";
                metroGridWaferReview.Columns[1].HeaderText = "X";
                metroGridWaferReview.Columns[2].HeaderText = "Y";
                metroGridWaferReview.Columns[3].HeaderText = "ActualX";
                metroGridWaferReview.Columns[4].HeaderText = "ActualY";
                metroGridWaferReview.Columns[5].HeaderText = "id";
                metroGridWaferReview.Columns[6].HeaderText = "height";
                metroGridWaferReview.Focus();

                br.Close();

                var results = height_list.Distinct();


            }
            


        }
        private void covert_to_var_type(int vartype, int offset, int j)
        {

            if (vartype == 17)
            {
                string value = readvalue_no.Substring(8 * j, offset * 2);
                uint num = uint.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
                byte[] floatVals = BitConverter.GetBytes(num);
                ulong f = BitConverter.ToUInt32(floatVals, 0);
                dr[6 - j] = f;

            }
            if (vartype == 3)
            {
                string value = readvalue_no.Substring(8 * j, offset * 2);
                uint num = uint.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
                byte[] floatVals = BitConverter.GetBytes(num);
                int f = BitConverter.ToInt32(floatVals, 0);
                dr[6 - j] = f;   
            }
            else
            {
                if (j == 6)
                    return;
                string value = readvalue_no.Substring(8 * j, offset * 2);
                uint num = uint.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
                byte[] floatVals = BitConverter.GetBytes(num);
                float f = BitConverter.ToSingle(floatVals, 0);
                dr[6 - j] = f;
                if (j == 3)
                {
                    dr[7] = f / 5469.9;
                    dr[9] = Math.Truncate(f / 5469.9); 
                }
                if (j == 4)
                {
                    dr[8] = f / 5470;
                    dr[10] = Math.Truncate(f / 5470);
                }
                    
                
            }
            
                
  
        }
        private void ExportExcel()
        {
            try
            {

                string path = @"C:/Users/raine/Desktop/test.xlsx";
                var excelApp = new Excel.Application();
                excelApp.Workbooks.Add();
                Excel._Worksheet workSheet = excelApp.Worksheets.Add();
                
                
                for(var i =0; i< dt.Columns.Count; i++)
                {
                    workSheet.Cells[1,i+1] = dt.Columns[i].ColumnName; 
                }
                Console.WriteLine($"######{dt.Rows.Count}");
                Console.WriteLine(dt.Columns.Count);
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    for (var j = 0; j < dt.Columns.Count; j++)
                    {
                        workSheet.Cells[i+2, j+1] = dt.Rows[i][j];
                    }
                }
                //Console.WriteLine($"dt.Rows.Count);
                //Console.WriteLine(dt.Columns.Count);
                workSheet.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal);
                excelApp.Quit();
            }
            catch(Exception ex)
            {
                Console.WriteLine("@@@@");
                Console.WriteLine(ex.Message);

            }
        }
        private void ReportToExcel()
        {
            string path = @"C:/Users/raine/Desktop/test.xlsx";
            Excel.Application excelApp = new Excel.Application();
            
            Excel.Workbook wb = excelApp.Workbooks.Add(true);
            Excel._Worksheet workSheet = wb.Worksheets.get_Item(1) as Excel._Worksheet;
            for(int i=0; i< metroGridWaferReview.Columns.Count-1;i++)
            {
                workSheet.Cells[1, i + 1] = metroGridWaferReview.Columns[i].HeaderText;

            }
            for(int r=0; r< metroGridWaferReview.Rows.Count;r++)
            {
                for(int i=0; i<metroGridWaferReview.Columns.Count-1; i++)
                {
                    workSheet.Cells[r + 2, i + 1] = metroGridWaferReview.Rows[r].Cells[i].Value;
                }
            }
            wb.SaveAs(path);
            excelApp.Quit();
        }

       


    }






    
}
반응형

'Language > C#' 카테고리의 다른 글

struct list 수정 불가  (0) 2022.05.16
1  (0) 2022.05.09
C# 정규표현식  (0) 2022.04.29
.md .dat  (0) 2022.04.29
.dat file .md file 불러오고 읽기  (0) 2022.04.28