using NLog.Fluent;
using OTSCLRINTERFACE;
using OTSModelSharp.ServiceCenter;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace OTSSysMgrApp
{
public partial class StageTest : Form
{
#region 全部变量声明
RecommendedConfiguration recommendedConfiguration;
//连接状态
bool ConnectionState = false;
static string xmlFilePath = System.Configuration.ConfigurationManager.ConnectionStrings["XMLFilePath"].ConnectionString;
static string LogPath = System.Configuration.ConfigurationManager.ConnectionStrings["LogPath"].ConnectionString;
static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
ISemController m_SemHardwareMgr = null;
IEDSController m_EDSHardwareMgr = null;
//图片
Bitmap bitmap = null;
//国际化
Language lan;
//国际化存储信息
Hashtable table;
public enum connectionEnumType
{
EDSOnlyPointXRay = 0,
EDSMultiPointXRay = 1,
EDSAreaXRay = 2,
ScanImage = 3
}
#endregion
public StageTest()
{
InitializeComponent();
m_EDSHardwareMgr = EDSController.GetEDSController(1024, 768, 5000, true, "");
m_SemHardwareMgr = SemController.GetSEMController();
lan = new Language(this);
table = lan.GetNameTable(this.Name);
if (!ConnectionState)
{
//和电镜建立通讯连接
ConnectionState = m_SemHardwareMgr.Connect();
///获取当前电镜的ID号
//SemType = cfun.GetSemType();
if (!ConnectionState)
{
log.Error(table["message3"].ToString());
}
else
{
string str = table["str1"].ToString();
log.Info("The energy spectrum is connected");
}
}
else
{
string str = table["str2"].ToString();
ConnectionState = false;
}
}
private void btnGPositionXYR_Click(object sender, EventArgs e)
{
try
{
//赋值
double PositionX = 0;
double PositionY = 0;
double PositionR = 0;
//获取参数
bool result = m_SemHardwareMgr.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR);
if (result)
{
//赋值 显示
tbPositionX.Text = Convert.ToDouble(PositionX).ToString();
tbPositionY.Text = Convert.ToDouble(PositionY).ToString();
tbPositionR.Text = Convert.ToDouble(PositionR).ToString();
log.Info("X,Y-axis coordinates:(" + tbPositionX.Text + "," + tbPositionY.Text + ")");
}
else
{
//配置结果提示
ShowMessage(6);
log.Error("Failed to obtain the X and Y coordinates");
}
}
catch (Exception ex)
{
//记录日志信息(异常日志)
log.Error(ex.Message.ToString());
}
}
private void btn_setpositionxyr_Click(object sender, EventArgs e)
{
try
{
if (!IsNull(tbSPositionX))
{
this.Focus();
return;
}
if (!IsType(tbSPositionX.Text, 2))
{
this.Focus();
return;
}
if (!IsNull(tbSPositionY))
{
this.Focus();
return;
}
if (!IsType(tbSPositionY.Text, 2))
{
this.Focus();
return;
}
if (!IsNull(tbSPositionR))
{
this.Focus();
return;
}
if (!IsType(tbSPositionR.Text, 2))
{
this.Focus();
return;
}
//赋值
double PositionX = Convert.ToDouble(tbSPositionX.Text);
double PositionY = Convert.ToDouble(tbSPositionY.Text);
double PositionR = Convert.ToDouble(tbSPositionR.Text);
bool result = m_SemHardwareMgr.MoveSEMToPoint(PositionX, PositionY, PositionR);
if (result)
{
ShowMessage(3);
log.Info("Set successfully, current X and Y coordinates should be:(" + PositionX.ToString() + "," + PositionY.ToString() + PositionR.ToString() + ")");
}
else
{
ShowMessage(4);
log.Error("Failed to set X and Y coordinates!");
}
}
catch (Exception ex)
{
//记录日志信息
log.Error(ex.Message.ToString());
}
}
private void btn_Recommendedconfiguration_Click(object sender, EventArgs e)
{
if (recommendedConfiguration == null || recommendedConfiguration.IsDisposed)
{
recommendedConfiguration = new RecommendedConfiguration(Language.ReadDefaultLanguage());
recommendedConfiguration.Show();
}
else
{
recommendedConfiguration.Activate();
}
}
private void btn_continuousshot_Click(object sender, EventArgs e)
{
btn_continuousshot.Enabled = false;
btn_confirm.Enabled = false;
this.Refresh();
Graphics g = panel2.CreateGraphics();
//g.SmoothingMode = SmoothingMode.HighSpeed;
//g.CompositingQuality = CompositingQuality.HighSpeed;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;//高质量低速度呈现
g.SmoothingMode = SmoothingMode.HighQuality;// 指定高质量、低速度呈现。
g.InterpolationMode = InterpolationMode.NearestNeighbor;
ISemController m_Sem = SemController.GetSEMController();
bool flag = m_Sem.Connect();
//电镜设置对象
var cfun = ScanController.GetScanController();
bool IsConnec = cfun.Init();
double Magnification = 0;
m_Sem.GetMagnification(ref Magnification);
//double a_dScanFieldSizeX = 0;
//double a_dScanFieldSizeY = 0;
//m_Sem.GetScanFieldSize(ref a_dScanFieldSizeX,ref a_dScanFieldSizeY);
double scanFieldSize100 = 0;
double.TryParse(tB_scanFieldSize100.Text, out scanFieldSize100);
double scanFieldSize = scanFieldSize100 * 100 / Magnification;
double PositionX = 0;
double PositionY = 0;
double PositionR = 0;
m_Sem.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR);
double PositionXO = PositionX;
double PositionYO = PositionY;
//double PositionR0 = PositionR;
int width = 1024;
int height = 768;
//设置图像分辨率
if (cb_imageresolution.Text.Trim() == "")
{
MessageBox.Show("imageresolution cannot be null!");
return;
}
else
{
width = Convert.ToInt32(cb_imageresolution.Text.Split('*')[0]);
height = Convert.ToInt32(cb_imageresolution.Text.Split('*')[1]);
}
this.Refresh();
double scanFieldSizeH = scanFieldSize * height / width;
m_Sem.SetScanExternal(true);
try
{
for (int i = 0; i < 4; i++)
{
if (i == 0)
{
}
else if (i == 1)
{
switch (cb_Xdirection.SelectedItem.ToString())
{
case "RIGHT_TOWARD": PositionX += scanFieldSize; break;
default: PositionX -= scanFieldSize; break;
}
m_Sem.MoveSEMToPoint(PositionX, PositionY);
}
else if (i == 2)
{
switch (cb_Ydirection.SelectedItem.ToString())
{
case "UP_TOWARD": PositionY -= scanFieldSizeH; break;
default: PositionY += scanFieldSizeH; break;
}
m_Sem.MoveSEMToPoint(PositionX, PositionY);
}
else
{
switch (cb_Xdirection.SelectedItem.ToString())
{
case "RIGHT_TOWARD": PositionX -= scanFieldSize; break;
default: PositionX += scanFieldSize; break;
}
m_Sem.MoveSEMToPoint(PositionX, PositionY);
}
//Thread.Sleep(50);
byte[] ImageByte = new byte[width * height];
bool resultValue = GetScanImage(width, height, "2", ref ImageByte);
Size size = new Size(panel2.Width / 2, panel2.Height / 2);
if (resultValue)
{
if (ImageByte != null)
{
var bitmap = CImageHandler.ToGrayBitmap(ImageByte, width, height);
switch (i)
{
case 0:
Point point0 = new Point(0, 0);
Rectangle rect0 = new Rectangle(point0, size);
g.DrawImage(bitmap, rect0);
break;
case 1:
Point point1 = new Point(panel2.Width / 2, 0);
Rectangle rect1 = new Rectangle(point1, size);
g.DrawImage(bitmap, rect1);
break;
case 2:
Point point2 = new Point(panel2.Width / 2, panel2.Height / 2);
Rectangle rect2 = new Rectangle(point2, size);
g.DrawImage(bitmap, rect2);
break;
case 3:
Point point3 = new Point(0, panel2.Height / 2);
Rectangle rect3 = new Rectangle(point3, size);
g.DrawImage(bitmap, rect3);
break;
}
//this.Refresh();
}
}
}
m_Sem.MoveSEMToPoint(PositionXO, PositionYO);
}
catch (Exception ex)
{
log.Error("continuousshot--错误信息:" + ex.ToString());
}
finally
{
m_Sem.SetScanExternal(false);
btn_continuousshot.Enabled = true;
btn_confirm.Enabled = true;
btn_continuousshot.Refresh();
g.Dispose();
}
}
private bool GetScanImage(int iWidth, int iHeigh, string DwellTime, ref byte[] bImageData)
{
//电镜设置对象
var scan = ScanController.GetScanController();
int GetImgCount = 0;
try
{
//连接电镜
bool IsConnec = scan.Init();
if (!IsConnec)
{
return false;
}
#region 设置图像分辨率
//设置宽度
if (!scan.SetImageSize(iWidth, iHeigh))
{
return false;
}
#endregion
#region 采集时间
//采集时间
DwellTimeLevel nDwellTime = DwellTimeLevel.Low;
switch (DwellTime)
{
case "Low":
nDwellTime = DwellTimeLevel.Low;
break;
case "Medium":
nDwellTime = DwellTimeLevel.Medium;
break;
case "High":
nDwellTime = DwellTimeLevel.High;
break;
}
//设置采集时间
if (!scan.SetDwellTime(nDwellTime))
{
return false;
}
#endregion
int resultCount = iWidth * iHeigh;
var img = scan.AcquireBSEImage();
bImageData = img.GetImageDataPtr();
}
catch (Exception ex)
{
NLog.LogManager.GetCurrentClassLogger().Error(ex.ToString());
return false;
}
return true;
}
private void btn_confirm_Click(object sender, EventArgs e)
{
double dscanFieldSize100 = 0;
if (cb_Xdirection.Text.Trim() != null && cb_imageresolution.Text.Trim() != null && cb_Ydirection.Text.Trim() != null && double.TryParse(tB_scanFieldSize100.Text, out dscanFieldSize100))
{
string xmlpath = Application.StartupPath + @"\Config\SysData\OTSProgMgrParam.pmf";
string xAxisDir = "";
string yAxisDir = "";
switch (cb_Xdirection.SelectedItem.ToString())
{
case "LEFT_TOWARD":
xAxisDir = "0:LEFT_TOWARD";
break;
default:
xAxisDir = "1:RIGHT_TOWARD";
break;
}
switch (cb_Ydirection.Text)
{
case "UP_TOWARD":
yAxisDir = "0:" + cb_Ydirection.Text;
break;
default:
yAxisDir = "1:DOWN_TOWARD";
break;
}
SaveConfigXml(xmlpath, xAxisDir, yAxisDir, tB_scanFieldSize100.Text);
}
else
{
MessageBox.Show("Please check params!");
}
}
bool ReadConfigXml(string xmlpath, ref string xAxisDir, ref string yAxisDir, ref string scanFieldSize)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlpath);
XmlNodeList nodeList = xmlDocument.SelectSingleNode("XMLData").ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("RegName") == "StageData")
{
scanFieldSize = xe.GetAttribute("scanFieldSize");
xAxisDir = xe.GetAttribute("xAxisDir");
yAxisDir = xe.GetAttribute("yAxisDir");
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
return true;
}
bool SaveConfigXml(string xmlpath, string xAxisDir, string yAxisDir, string scanFieldSize)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlpath);
XmlNodeList nodeList = xmlDocument.SelectSingleNode("XMLData").ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("RegName") == "StageData")
{
xe.SetAttribute("scanFieldSize", scanFieldSize);
xe.SetAttribute("xAxisDir", xAxisDir);
xe.SetAttribute("yAxisDir", yAxisDir);
break;
}
}
xmlDocument.Save(xmlpath);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
return true;
}
public bool ConnectionSem(connectionEnumType connectionType)
{
//连接电镜标识
bool DisConnResult = false;
//判断连接状态
if (!ConnectionState)
{
//连接电镜设置
DisConnResult = m_EDSHardwareMgr.Connect();
}
if (DisConnResult)
{
ConnectionState = true;
}
else
{
ConnectionState = false;
}
return DisConnResult;
}
#region 用户信息提示
///
/// 提示
///
///
private void ShowMessage(int MessageType)
{
string message1 = table["message1"].ToString();
string message2 = table["message2"].ToString();
string message3 = table["message3"].ToString();
string message4 = table["message4"].ToString();
string message5 = table["message5"].ToString();
string message6 = table["message6"].ToString();
string message7 = table["message7"].ToString();
string message8 = table["message8"].ToString();
string message9 = table["message9"].ToString();
string message10 = table["message10"].ToString();
string MessageInfo = string.Empty;
switch (MessageType)
{
case 0:
MessageInfo = message1;
break;
case 1:
MessageInfo = message2;
break;
case 2:
MessageInfo = message3;
break;
case 3:
MessageInfo = message4;
break;
case 4:
MessageInfo = message5;
break;
case 5:
MessageInfo = message6;
break;
case 6:
MessageInfo = message7;
break;
case 7:
MessageInfo = message8;
break;
case 8:
MessageInfo = message9;
break;
case 9:
MessageInfo = message10;
break;
}
MessageBox.Show(MessageInfo, "Tip");
}
#endregion
#region 判断控制内容是否为空 与 判断输入的格式是否正确
///
/// 判断控制内容是否为空
///
///
///
public bool IsNull(TextBox tbContent)
{
if (tbContent.Text.Trim().Equals(""))
{
//为空提示
ShowMessage(0);
//获取焦点
tbContent.Focus();
return false;
}
return true;
}
///
///
///
///
///
///
public bool IsType(object ObjValue, int ObjType)
{
try
{
switch (ObjType)
{
case 1:
int intValue = Convert.ToInt32(ObjValue);
break;
case 2:
double douValue = Convert.ToDouble(ObjValue);
break;
case 3:
float floValue = Convert.ToSingle(ObjValue);
break;
}
return true;
}
catch (Exception)
{
//为空提示
ShowMessage(7);
return false;
}
}
#endregion
void InitEvent()
{
string edsType = FileHelper.GetXMLInformations("EDSName");
string SemType = FileHelper.GetXMLInformations("SemControllerName");
if (SemType == "FEI")
{
if (edsType == "FEI")
{
cb_imageresolution.SelectedIndex = 2;
}
else if (edsType == "Oxford")
{
cb_imageresolution.SelectedIndex = 0;
}
else if (edsType == "Bruker")
{
cb_imageresolution.SelectedIndex = 1;
}
else if (edsType == "OffLine")
{
cb_imageresolution.SelectedIndex = 1;
}
cb_Xdirection.SelectedIndex = 1;
cb_Ydirection.SelectedIndex = 0;
}
}
private void StageTest_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
//m_EDSHardwareMgr = EDSController.GetEDSController(width, height, 5000, true, "");
cb_Xdirection.Items.Add("LEFT_TOWARD");
cb_Xdirection.Items.Add("RIGHT_TOWARD");
cb_Ydirection.Items.Add("UP_TOWARD");
cb_Ydirection.Items.Add("DOWN_TOWARD");
tB_scanFieldSize100.Text = "1270";
cb_imageresolution.Items.Add("1024*768");
cb_imageresolution.Items.Add("1024*704");
cb_imageresolution.Items.Add("1536*1024");
string xmlpath = Application.StartupPath + @"\Config\SysData\OTSProgMgrParam.pmf";
string xAxisDir = "";
string yAxisDir = "";
string scanFieldSize = "";
ReadConfigXml(xmlpath, ref xAxisDir, ref yAxisDir, ref scanFieldSize);
if (xAxisDir.Split(':')[1] == "LEFT_TOWARD")
{
cb_Xdirection.SelectedIndex = 0;
}
else
{
cb_Xdirection.SelectedIndex = 1;
}
if (yAxisDir.Split(':')[1] == "UP_TOWARD")
{
cb_Ydirection.SelectedIndex = 0;
}
else
{
cb_Ydirection.SelectedIndex = 1;
}
tB_scanFieldSize100.Text = scanFieldSize;
cb_imageresolution.SelectedIndex = 0;
InitEvent();
}
}
}