基本信息
源码名称:gerber解析
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2020-11-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 6 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace GMDO
{
    
    public partial class Form1 : Form
    {
        bool update = false; // Initialiation pictureBox for original xy 
        float x1;
        float y1;
        
        public float originalX = 50;
        public float originalY = 500;
        public int sacleR = 100000;
        public static string selectPath;
        public static List<string> filesSelect = new List<string>();
        
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            pictureBox1.Refresh();


        }



        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            Pen p = new Pen(Color.White, 2);
            g.DrawEllipse(p, 50, 50, 100, 100);

            

        }

      private void ReadGerber()
      {

      }

      public class coordinateVentor
      {
          public float x;
          public float y;
          public string dType = "";
          public bool IsClosedPolygon = false;
      }
      public class pointNum
      {
          public float x;
          public float y;
      }

      public List<coordinateVentor> coordinateVentorList = new List<coordinateVentor>();
      public List<PointF> pointNumList = new List<PointF>();
      public List<bool> isClosedPolygonList = new List<bool>();

      public class gerberFormat
      {
          public int rNum = 2;
          public int fNum = 6;
          public string unit = "IN";
          public string layerName = "1";
      }

     
      public void ReadFile(string path)
      {
          StreamReader sr = new StreamReader(path, Encoding.Default);
          gerberFormat format = new gerberFormat(); 
          string line;
          string linePrevious="";
          string lineTmp = "";
          string text = "";
          bool closedPolygon = false;
          while ((line = sr.ReadLine()) != null )
          {
              Console.WriteLine(line.ToString());
              text = text   line.ToString()   "\r\n";
             
              if (line.Contains("FSLA"))
              {
                  format.rNum = Convert.ToInt16(line.Substring(line.IndexOf("X")   1, 1));
                  format.fNum = Convert.ToInt16(line.Substring(line.IndexOf("X")   2, 1));
              }
              if (line.Contains("MO"))
              {
                  format.unit = line.Substring(line.IndexOf("MO")   1, 2);
              }

              if (line.Contains("G36")) closedPolygon = true;
              if (line.Contains("G37")) closedPolygon = false;
              

              if (line.Contains("D0") && (line.Contains("X") || line.Contains("Y")) && !line.Contains("G0"))
              {
                  coordinateVentor coord = new coordinateVentor();
                  coord.dType = line.Substring((line.IndexOf("D0")), 3);
                  coord.IsClosedPolygon = closedPolygon;
                  //int xIndex = line.IndexOf("X");
                  int xIndex = returnCharIndex(line, "X");
                  int yIndex = returnCharIndex(line, "Y");
                  int dIndex = returnCharIndex(line, "D0");
                  int xIndexP = returnCharIndex(linePrevious, "X");
                  int yIndexP = returnCharIndex(linePrevious, "Y");
                  int dIndexP = returnCharIndex(linePrevious, "D0");
                  if (line.Contains("X") && line.Contains("Y"))
                  {                    
                      coord.x = Convert.ToSingle(line.Substring(xIndex 1, yIndex-xIndex-1)) / sacleR;
                      coord.y = Convert.ToSingle(line.Substring(yIndex   1, dIndex - yIndex - 1)) / sacleR;
                      linePrevious = line; 
                  }
                  else if (line.Contains("X") && !line.Contains("Y"))
                  {
                      coord.x = Convert.ToSingle(line.Substring(xIndex   1, dIndex - 1)) / sacleR;
                      coord.y = Convert.ToSingle(linePrevious.Substring(yIndexP   1, (dIndexP - 1) - yIndexP)) / sacleR;
                      lineTmp = line.Substring(0, dIndex - 0)   linePrevious.Substring(yIndexP, dIndexP - 0 - yIndexP)   line.Substring((dIndex), 3);
                      linePrevious = lineTmp;
                  }
                  else if (line.Contains("Y") && !line.Contains("X"))
                  {
                      coord.x = Convert.ToSingle(linePrevious.Substring(xIndexP   1, yIndexP - 1 - xIndexP)) / sacleR;
                      coord.y = Convert.ToSingle(line.Substring(yIndex   1, dIndex - 1)) / sacleR;
                      lineTmp = linePrevious.Substring(xIndexP, yIndexP - 0 - xIndexP)   line.Substring(0, dIndex - 0)   line.Substring((dIndex), 3);
                      linePrevious = lineTmp;
                  }
                  else
                  {
                      
                  }
                  coordinateVentorList.Add(coord);
              }

              
          }
          textBox1.Text = text;
      }

      private int returnCharIndex(string str, string p)
      {
          return str.IndexOf(p);
      }

 

        private void ReadExcell()
      {

      }

        private void ReadODB()
        {

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = folderBrowserDialog1.SelectedPath.ToString();
            }

            selectPath = txtPath.Text; 
            FileList fileListForm = new FileList();
            fileListForm.Show();
            //filesSelect = FileList.files;
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            update = true;

             //filesSelect = FileList.filesResult ;
            ReadFile(selectPath);
            pictureBox1.Refresh();
        }

        private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //Initation original x,y 
            Graphics g = e.Graphics;
            Pen p0 = new Pen(Color.White, 1);
            g.DrawEllipse(p0, originalX-10, originalY-10, 20, 20);
            g.DrawLine(p0, originalX, originalY, originalX, originalY - 30);
            g.DrawLine(p0, originalX, originalY, originalX   30, originalY);
            SolidBrush br = new SolidBrush(Color.Red);
            //Draw gerber 
            if (update== false) return;
            int lineWidth = 3; 
            Pen p = new Pen(Color.Red, lineWidth);
            pointNum pointPolyline = new pointNum();
            
            for (int i = 0; i < coordinateVentorList.Count(); i  )
            {
                if (coordinateVentorList[i].dType == "D02" )
                {
                    var temp = pointNumList.ToArray();
                    if (temp.Count() > 1)
                    {
                        if (isClosedPolygonList[0])
                        {
                            g.FillPolygon(br, temp);
                        }
                        else
                        {                             
                           g.DrawLines(p, temp);                                                        
                        }
                        pointNumList.Clear();
                        isClosedPolygonList.Clear();
                    }
                    
                        pointNumList.Add(new PointF(originalX   coordinateVentorList[i].x, originalY - coordinateVentorList[i].y));
                        isClosedPolygonList.Add(coordinateVentorList[i].IsClosedPolygon);
                                                           
                } else if (coordinateVentorList[i].dType == "D01")
                {
                    pointNumList.Add(new PointF(originalX   coordinateVentorList[i].x, originalY - coordinateVentorList[i].y));
                    isClosedPolygonList.Add(coordinateVentorList[i].IsClosedPolygon);
                }                             
                else if (coordinateVentorList[i].dType == "D03")
                {
                    x1 = coordinateVentorList[i].x;
                    y1 = coordinateVentorList[i].y;
                    g.FillEllipse(br, x1   originalX, originalY - y1, 5, 5);
                }
                
               //g.FillPolygon(br, curvePoints);
                //g.DrawPolygon(p, curvePoints);
            }

            var temp2 = pointNumList.ToArray();
            if (temp2.Count() > 0)
            {
                //g.DrawPolygon(p, temp);
                if (isClosedPolygonList[0])
                {
                    g.FillPolygon(br, temp2);
                }
                else
                {
                    g.DrawLines(p, temp2);
                }
                pointNumList.Clear();
            }
            
        }






        //List<Orag> list = new List<Orag>();
        //list = new List<Orag> { new Orag(1, 0, "上海公司"), new Orag(5, 0, "北京公司"), new Orag(2, 1, "上海公司1"), new Orag(3, 1, "上海公司2"), new Orag(7, 3, "上海公司2-1"), new Orag(4, 1, "上海公司3"), new Orag(6, 5, "北京公司1") };
        //public void Addnode()
        //{
        //    for (int i = 0; i < list.Count; i  )
        //    {
        //        if (list[i].pid == 0)
        //        {
        //            TreeNode pnode = new TreeNode();
        //            pnode.Text = list[i].name;
        //            pnode.Tag = list[i].id;
        //            treeView1.Nodes.Add(pnode);
        //            AddChildnode(list[i].id, pnode);
        //        }
        //    }
        //}

        //public void AddChildnode(int pid, TreeNode pnode)
        //{
        //    for (int i = 0; i < list.Count; i  )
        //    {
        //        if (list[i].pid == pid)
        //        {
        //            TreeNode cnode = new TreeNode();
        //            cnode.Text = list[i].name;
        //            cnode.Tag = list[i].id;
        //            pnode.Nodes.Add(cnode);
        //            AddChildnode(list[i].id, cnode);
        //        }
        //    }
        //}


        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

        }
        
    }
}