实例介绍
【实例简介】
【实例截图】
【核心代码】
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)
{
}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)