Advertisement
Guest User

Untitled

a guest
May 27th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using Euresys.Open_eVision_2_5;
  12. using System.Diagnostics;
  13. using OfficeOpenXml;
  14. using System.IO;
  15. using System.Reflection;
  16.  
  17. namespace WindowsFormsApp1
  18. {
  19.     public partial class Form2 : Form
  20.     {
  21.         PictureBox p;
  22.         Euresys.Open_eVision_2_5.EWorldShape shape1;
  23.         Euresys.Open_eVision_2_5.EImageBW8 srcImage;
  24.         List<EShape> shapes;
  25.         int length;
  26.         List<EImageBW8> images;
  27.         string modelPath;
  28.  
  29.         public Form2(string modelPath, List<EImageBW8> images)
  30.         {
  31.             InitializeComponent();
  32.  
  33.             this.modelPath = modelPath;
  34.             this.images = images;
  35.  
  36.             /*groupBoxPoint.Controls.Add(NumMeasuredPoints);
  37.             groupBoxPoint.Controls.Add(Tolerance);
  38.             groupBoxPoint.Controls.Add(Angle);
  39.  
  40.             groupBoxLine.Controls.Add(LCenterX);
  41.             groupBoxLine.Controls.Add(LCenterY);
  42.             groupBoxLine.Controls.Add(LAngle);
  43.  
  44.             groupBoxCircle.Controls.Add(CenterX);
  45.             groupBoxCircle.Controls.Add(CenterY);
  46.             groupBoxCircle.Controls.Add(Diameter);
  47.  
  48.             groupBoxRectangle.Controls.Add(RCenterX);
  49.             groupBoxRectangle.Controls.Add(RCenterY);
  50.             groupBoxRectangle.Controls.Add(RAngle);
  51.             groupBoxRectangle.Controls.Add(RSizeX);
  52.             groupBoxRectangle.Controls.Add(RSizeY);
  53.  
  54.             groupBoxWedge.Controls.Add(WCenterX);
  55.             groupBoxWedge.Controls.Add(WCenterY);
  56.             groupBoxWedge.Controls.Add(WBreadth);
  57.             groupBoxWedge.Controls.Add(WAngle);
  58.             groupBoxWedge.Controls.Add(WAmplitude);*/
  59.  
  60.  
  61.             /*p = new PictureBox();
  62.             p.Location = new Point(102, 36);
  63.             p.Size = new Size(250, 375);
  64.             this.Controls.Add(p);
  65.             p.Load();*/
  66.         }
  67.  
  68.         //public PictureBox GetPictureBox { get => pictureBox1; }
  69.  
  70.         /* EImageBW8 Draw(List<EImageBW8> images)
  71.          {
  72.              foreach (EImageBW8 img in images)
  73.              {
  74.  
  75.                  Graphics graphics = Graphics.FromImage(img.);
  76.              }
  77.              return Drawresults(img);
  78.          }*/
  79.        
  80.  
  81.         void Draw(string ImageName, int imIdx, EShape eShape)
  82.         {
  83.             Bitmap b = new Bitmap(ImageName);
  84.             //Bitmap b = new Bitmap("C:\\Users\\csiky.dora\\Desktop\\GyakornokiTeszt\\p.bmp");
  85.            
  86.             Graphics g = Graphics.FromImage(b);
  87.            
  88.             ECircleGauge c = new ECircleGauge();
  89.            
  90.             eShape.Draw(g);
  91.  
  92.             b.Save("C:\\Users\\csiky.dora\\Desktop\\miniteszt\\proba2" + imIdx + ".bmp");
  93.  
  94.             //b.s
  95.             //g.DrawImage(b, ulCorner);
  96.         }
  97.  
  98.  
  99.         private void Save_Click(object sender, EventArgs e)
  100.         {
  101.            
  102.             //try
  103.             //{
  104.                 SaveFileDialog sfd = new SaveFileDialog();                           // select the model file
  105.                 sfd.Filter = "Excel files|*.xls;*xlsx;*.xlsm";
  106.                 if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  107.                 {
  108.                 FileInfo fi = new FileInfo(sfd.FileName);
  109.                 ProcessModel(images, fi);
  110.             }
  111.                
  112.             //}
  113.  
  114.             /*catch (Exception en)
  115.             {
  116.                Console.WriteLine("Select );
  117.             }*/
  118.  
  119.         }
  120.         public void ProcessModel(List<EImageBW8> images, FileInfo outputFile)
  121.         {
  122.             EWorldShape shape = new EWorldShape();
  123.             shape.Load(modelPath, true);
  124.  
  125.             //EImageEncoder e = new EImageEncoder();
  126.             //ECodedImage2 cim = new ECodedImage2();
  127.  
  128.             //e.Encode(images[0], cim);
  129.             //Image img = Image.FromHbitmap(images[0].GetImagePtr());
  130.  
  131.             int rowIdx = 2;
  132.             int imIdx = 0;
  133.  
  134.             //Draw(images[imIdx].Title);
  135.  
  136.             using (ExcelPackage package = new ExcelPackage())
  137.             {
  138.                 ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Results");
  139.                 foreach (var im in images)
  140.                 {
  141.                     shape.Process(im, true);
  142.                     List<EShape> ShapeList = DepthSearch(shape);
  143.                     //shape.lo
  144.  
  145.                     Draw(images[imIdx].Title, imIdx + 1, ShapeList[0]);
  146.  
  147.                     fillData(package, ShapeList, images[imIdx].Title, rowIdx);
  148.                     rowIdx++;
  149.                     imIdx++;
  150.  
  151.                    
  152.                 }
  153.                 //package.Save();
  154.                 package.SaveAs(outputFile);
  155.             }
  156.         }
  157.  
  158.         private List<EShape> DepthSearch(EShape start)
  159.         {
  160.             var visited = new HashSet<EShape>();            
  161.  
  162.             EShape act;
  163.  
  164.             var stack = new Stack<EShape>();
  165.             stack.Push(start);
  166.             List<EShape> shapeList = new List<EShape>();
  167.  
  168.             while (stack.Count > 0)
  169.             {
  170.                 var vertex = stack.Pop();
  171.  
  172.                 if (visited.Contains(vertex))
  173.                     continue;
  174.  
  175.                 visited.Add(vertex);
  176.                 shapeList.Add(vertex);
  177.                 //Debug.WriteLine(vertex.Name);
  178.  
  179.                 //search for not visited gauges
  180.                 for (uint i = 0; i < vertex.NumDaughters; i++)
  181.                 {
  182.                     act = vertex.GetDaughter(i);
  183.                     if (!visited.Contains(act))
  184.                         stack.Push(act);
  185.                 }
  186.             }
  187.             return shapeList;
  188.             //int length = images.Count();
  189.             //open Form2
  190.             //Form2 Results = new Form2(shapeList, length, images);
  191.             //Results.ShowDialog();
  192.         }
  193.  
  194.         //  private void fillData(List<string> propertyNames, FileInfo fileName, List<EShape> ShapeList)
  195.         public void fillData(ExcelPackage package, List<EShape> ShapeList, string imName, int row)
  196.         {
  197.             // Add a new worksheet to the empty workbook
  198.             ExcelWorksheet worksheet = package.Workbook.Worksheets["Results"];
  199.  
  200.             /*             //Add the headers
  201.                          for (int i = 0; i < propertyNames.Count; i++)
  202.                          {
  203.                              worksheet.Cells[1, i + 1].Value = propertyNames[i];
  204.                          }*/
  205.  
  206.             //fill records
  207.             //            int actIdx = 2;
  208.             //int p = 2;
  209.  
  210.             worksheet.Cells[row, 1].Value = imName;
  211.             int cols = 1;
  212.             GroupBox gb = new GroupBox();
  213.  
  214.             int o = 1;
  215.             foreach (var pic in ShapeList)
  216.             {
  217.                 PropertyInfo[] pi;// = typeof(EShape).GetProperties();
  218.                 List<PropertyInfo> prop = new List<PropertyInfo>();// = pi.ToList();
  219.                 Type actType = pic.GetType();
  220.                 if (pic is EPointGauge)
  221.                 {
  222.                     pi = typeof(EPointGauge).GetProperties();
  223.                     prop = pi.ToList();
  224.                     actType = typeof(EPointGauge);
  225.                     gb = groupBoxPoint;
  226.                 }
  227.                 else if (pic is ELineGauge)
  228.                 {
  229.                     pi = typeof(ELineGauge).GetProperties();
  230.                     prop = pi.ToList();
  231.                     actType = typeof(ELineGauge);
  232.                     gb = groupBoxLine;
  233.                 }
  234.                 else if (pic is ECircleGauge)
  235.                 {
  236.                     pi = typeof(ECircleGauge).GetProperties();
  237.                     prop = pi.ToList();
  238.                     actType = typeof(ECircleGauge);
  239.                     gb = groupBoxCircle;
  240.                 }
  241.                 else if (pic is ERectangleGauge)
  242.                 {
  243.                     pi = typeof(ERectangleGauge).GetProperties();
  244.                     prop = pi.ToList();
  245.                     actType = typeof(ERectangleGauge);
  246.                     gb = groupBoxRectangle;
  247.                 }
  248.                 else if (pic is EWedgeGauge)
  249.                 {
  250.                     pi = typeof(EWedgeGauge).GetProperties();
  251.                     prop = pi.ToList();
  252.                     actType = typeof(EWedgeGauge);
  253.                     gb = groupBoxWedge;
  254.                 }
  255.                 else continue;
  256.  
  257.                 List<string> proplist = new List<string>();
  258.                 List<int> ixlist = new List<int>();
  259.  
  260.                 var checkboxes = gb.Controls.OfType<CheckBox>();
  261.                 foreach (var cb in checkboxes)
  262.                 {
  263.                     if (cb.Checked == true)
  264.                     {
  265.                         proplist.Add(cb.Text);
  266.                     }
  267.                 }
  268.                
  269.                 // var asd = this.Controls.OfType<GroupBox>();
  270.                 // properties of the current shape to a list
  271.                 for (int i = 0; i < prop.Count; i++)
  272.                 {
  273.                     if (prop[i].Name.StartsWith("Measured"))
  274.                     {
  275.                         cols++;
  276.  
  277.                         var measuredObj = prop[i].GetValue(pic);
  278.                         var pi2 = measuredObj.GetType().GetProperties();
  279.  
  280.                         for (int propIdx = 0; propIdx < pi2.Length; propIdx++)
  281.                         {
  282.                             if (row == 2)
  283.                             {
  284.                                 if (proplist.Contains(pi2[propIdx].Name))
  285.                                 {
  286.                                     string name = pic.Name + "." + pi2[propIdx].Name;
  287.                                     worksheet.Cells[1, cols].Value = name;
  288.                                     ixlist.Add(propIdx);
  289.                                     //  if (pi2[propIdx].CanRead)
  290.                                     //  {
  291.                                 }
  292.                             }
  293.  
  294.                             //                      if (ixlist.Contains(propIdx))
  295.                             if (proplist.Contains(pi2[propIdx].Name))
  296.                             {
  297.                                 var measuredVal = pi2[propIdx].GetValue(measuredObj);
  298.                                 worksheet.Cells[row, cols].Value = measuredVal;
  299.                                 cols++;
  300.                             }
  301.                                   //  }
  302.                                
  303.                            
  304.                             //worksheet.Cells[o, cols].Value =
  305.  
  306.                            /* if (pi2[propIdx].CanRead)
  307.                             {
  308.                                 var measuredVal = pi2[propIdx].GetValue(measuredObj);
  309.                                 worksheet.Cells[row, cols].Value = measuredVal;
  310.                             }*/
  311.                            
  312.  
  313.                             // PropertyInfo value = pi2[propIdx].GetValue();
  314.                             // worksheet.Cells[1, cols].Value = value;
  315.                         }
  316.  
  317.  
  318.                         // and if its checkbox is checked
  319.  
  320.                         //PropertyInfo pi2 = actType.getpt
  321.  
  322.                         //                        worksheet.Cells[2, j + 1].Value =    
  323.                     }
  324.  
  325.  
  326.  
  327.                     /*                          PropertyInfo pi = typeof(EShape).GetProperty(propertyNames[i]);
  328.                                           object obj = pi.GetValue(pic);
  329.                                           if (obj != null)
  330.                                           {
  331.                                                   worksheet.Cells[actIdx, i + 1].Value = pi.GetValue(pic);
  332.                                           }*/
  333.                 }
  334.                 //                 actIdx++;
  335.             }
  336.             //o += 2;
  337.             //p += 3;
  338.  
  339.         }
  340.  
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement