namespace Measure
{
public delegate void DelegateTrigger();
public class HMeasureSYS : System.IDisposable
{
public static string sConfigPath = @"MeasureSys.cfg"; /// <summary>
/// U000单元 全局变量
/// </summary>
public const int U000 = ;
/// <summary>
/// USys单元 系统变量
/// </summary>
public const int USys = -; /// <summary>
/// 当前项目
/// </summary>
public static CProject Cur_Project = null; //当前项目 /// <summary>
/// 工程列表
/// </summary>
public static List<CProject> g_ProjectList = new List<CProject>(); /// <summary>
/// 系统变量列表和常量
/// </summary>
public static List<F_DATA_CELL> g_VariableList = new List<F_DATA_CELL>(); /// <summary>
/// 采集设备列表
/// </summary>
public static List<AcqDeviceBase> g_AcqDeviceList = new List<AcqDeviceBase>();
/// <summary>
/// tcp服务器
/// </summary>
public static TCPServer2 g_TcpServer = new TCPServer2(); /// <summary>
/// 系统运行状态
/// </summary>
public static Sys_Status g_SysStatus = new Sys_Status(); /// <summary>
/// 相机触发源,定义为EVENT是为了满足一个信号触发多个相机
/// </summary>
public static event DelegateTrigger g_TR1 = null;
public static event DelegateTrigger g_TR2 = null;
public static event DelegateTrigger g_TR3 = null;
public static event DelegateTrigger g_TR4 = null; /// <summary>
/// 镭射终止采集触发源,定义为EVENT是为了满足一个信号触发多个相机
/// </summary>
public static event DelegateTrigger g_EndTR1 = null;
public static event DelegateTrigger g_EndTR2 = null;
public static event DelegateTrigger g_EndTR3 = null;
public static event DelegateTrigger g_EndTR4 = null; /// <summary>
/// 初始化视觉工程项目
/// </summary>
/// <param name="filepath">初始化文件所在路径</param>
/// <returns></returns>
public static bool InitialVisionProgram(string filepath = @"MeasureSys.cfg")
{
try
{
string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
if (filepath.Contains(@":\") == false)
{
filepath = System.IO.Path.Combine(ThePath, filepath);
} if (filepath.Trim() == "" || System.IO.File.Exists(filepath) == false)
{
System.Windows.Forms.MessageBox.Show("输入文件名错误!");
throw new Exception("视觉测量模块报错:" + filepath + "不存在!");
}
else
{
HMeasureSYS.sConfigPath = filepath;
}
//设备也要同步先关闭 yoga 2018-8-30 15:34:29
HMeasureSYS.DisposeDev();
//由于可能之前已经打开了 此处要先将tcp服务器关闭
HMeasureSYS.g_TcpServer.tcp.Stop(); HMeasureSYS.ReadConfig(HMeasureSYS.sConfigPath);
//HMeasureSYS.ReadSpaceInfo(out HMeasureSYS.g_ProjectList, out HMeasureSYS.g_VariableList, out HMeasureSYS.g_AcqDeviceList, out HMeasureSYS.g_TcpServer, Application.StartupPath + sProject_Path); HMeasureSYS.g_TcpServer.tcp.Start();
//没次读取新的配置文件时重新检测VB代码状态和设备连接状态
HMeasureSYS.InitDevStatus();
return true;
}
catch (Exception ex)
{
MessageBox.Show("Measure.HMeasureSYS.InitialVisionProgram:" + ex.ToString());
return false;
}
} public static void DisposeVisionProgram()
{
Sys_Stop();
HMeasureSYS.g_TcpServer.Stop();
HMeasureSYS.DisposeDev();
} /// <summary>
///系统检测启动
/// </summary>
public static void Sys_Start()
{
for (int i = ; i < HMeasureSYS.g_ProjectList.Count; i++)
{
Sys_Start(i);
}
} /// <summary>
///系统检测启动
/// </summary>
public static void Sys_Start(int index)
{
HMeasureSYS.g_SysStatus.m_RunMode = RunMode.循环运行;
Sys_ThreadStart(index);
} public static void Sys_ThreadStart(int index)
{
if (index > HMeasureSYS.g_ProjectList.Count - )
{
return;
}
CMeasureCell cell = HMeasureSYS.g_ProjectList[index].m_CellList.First(c => c.m_CellCatagory == CellCatagory.线阵图像单元 || c.m_CellCatagory == CellCatagory.面阵图像单元);
if (cell.m_CellCatagory == CellCatagory.面阵图像单元)
{
((CImageReg_Area)cell).m_AcqDevice.eventWait.Reset();
((CImageReg_Area)cell).m_AcqDevice.SignalWait.Reset();
}
else if (cell.m_CellCatagory == CellCatagory.线阵图像单元)
{
((CImageReg_Line)cell).m_AcqDevice.eventWait.Reset();
} HMeasureSYS.g_ProjectList[index].Thread_Start();
}
public static void Sys_StartTest(int index)
{
HMeasureSYS.g_SysStatus.m_RunMode = RunMode.执行一次;
Sys_ThreadStart(index);
} /// <summary>
///系统检测停止
/// </summary>
public static void Sys_Stop(int index)
{
try
{
if (index > HMeasureSYS.g_ProjectList.Count - )
{
return;
}
HMeasureSYS.g_ProjectList[index].Thread_Stop();
int cellIndex = HMeasureSYS.g_ProjectList[index].m_CellList.FindIndex(c => c.m_CellID == HMeasureSYS.g_ProjectList[index].CurCellID);
if (cellIndex >= )
{
CMeasureCell cell = HMeasureSYS.g_ProjectList[index].m_CellList[cellIndex];
if (cell.m_CellCatagory == CellCatagory.面阵图像单元)
{
((CImageReg_Area)cell).m_AcqDevice.SignalWait.Set();
((CImageReg_Area)cell).m_AcqDevice.eventWait.Set();
}
else if (cell.m_CellCatagory == CellCatagory.线阵图像单元)
{
((CImageReg_Line)cell).m_AcqDevice.eventWait.Set();
}
else if (cell.m_CellCatagory== CellCatagory.延时单元)
{
((CDelay)cell).Stop();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Measure.HMeasureSYS.Sys_Stop:" + ex.ToString());
}
} /// <summary>
///系统检测停止
/// </summary>
public static void Sys_Stop()
{
try
{
for (int i = ; i < HMeasureSYS.g_ProjectList.Count; i++)
{
Sys_Stop(i);
}
}
catch (System.Exception ex)
{
MessageBox.Show("Measure.HMeasureSYS.Sys_Stop:" + ex.ToString());
Helper.LogHandler.Instance.VTLogWarning(ex.ToString());
}
} /// <summary>
/// 相机触发事件/镭射开始终止采集触发事件
/// </summary>
/// <param name="str">字符串</param>
public static void setTrigger(string str)
{
switch (str)
{
case "TR1":
if (HMeasureSYS.g_TR1 != null)
{
HMeasureSYS.g_TR1();
}
break;
case "TR2":
if (HMeasureSYS.g_TR2 != null)
{
HMeasureSYS.g_TR2();
}
break;
case "TR3":
if (HMeasureSYS.g_TR3 != null)
{
HMeasureSYS.g_TR3();
}
break;
case "TR4":
if (HMeasureSYS.g_TR4 != null)
{
HMeasureSYS.g_TR4();
}
break;
case "EndTR1":
if (HMeasureSYS.g_EndTR1 != null)
{
HMeasureSYS.g_EndTR1();
}
break;
case "EndTR2":
if (HMeasureSYS.g_EndTR2 != null)
{
HMeasureSYS.g_EndTR2();
}
break;
case "EndTR3":
if (HMeasureSYS.g_EndTR3 != null)
{
HMeasureSYS.g_EndTR3();
}
break;
case "EndTR4":
if (HMeasureSYS.g_EndTR4 != null)
{
HMeasureSYS.g_EndTR4();
}
break;
}
} #region 配置文件
public static void ReadConfig(string filePath)
{
try
{// Deserialize.
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
fs.Seek(, SeekOrigin.Begin);
BinaryFormatter binaryFmt = new BinaryFormatter();
HMeasureSYS.g_VariableList = (List<F_DATA_CELL>)binaryFmt.Deserialize(fs);
HMeasureSYS.g_AcqDeviceList = (List<AcqDeviceBase>)binaryFmt.Deserialize(fs);
AcqDeviceBase.m_LastDeviceID = (int)binaryFmt.Deserialize(fs);
HMeasureSYS.g_TcpServer = (TCPServer2)binaryFmt.Deserialize(fs);
HMeasureSYS.g_ProjectList = (List<CProject>)binaryFmt.Deserialize(fs);
CProject.m_LastProjectID = (int)binaryFmt.Deserialize(fs);
CProject.m_IsMangerGTCard=(bool)binaryFmt.Deserialize(fs);
//fs.Close();
//GT_MotionControl.Helper.InitMotionControl();
}
}
catch (System.Exception ex)
{
MessageBox.Show("Measure.HMeasureSYS.ReadConfig:" + ex.ToString());
LogHandler.Instance.VTLogError(ex.ToString());
}
} public static void SaveConfig(string filePath)
{
//添加相对路径判断 如果是相对路径 必须指定到exe目录 否则可能因为打开文件等对话框影响存储路径 yoga 2018-8-30 11:06:48
string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
string tempFile = "tempMeasureSys.cfg";
tempFile=System.IO.Path.Combine(ThePath, tempFile);
try
{
System.GC.Collect();
using (FileStream fs = new FileStream(tempFile, FileMode.Create))
{
BinaryFormatter binaryFmt = new BinaryFormatter();
fs.Seek(, SeekOrigin.Begin);
binaryFmt.Serialize(fs, HMeasureSYS.g_VariableList);
binaryFmt.Serialize(fs, HMeasureSYS.g_AcqDeviceList);
binaryFmt.Serialize(fs, AcqDeviceBase.m_LastDeviceID);
binaryFmt.Serialize(fs, HMeasureSYS.g_TcpServer);
binaryFmt.Serialize(fs, HMeasureSYS.g_ProjectList);
binaryFmt.Serialize(fs, CProject.m_LastProjectID);
binaryFmt.Serialize(fs, CProject.m_IsMangerGTCard);
//fs.Close();
}
string outPath = filePath;
if (filePath.Contains(@":\") == false)
{
outPath = System.IO.Path.Combine(ThePath, filePath);
}
Helper.FileHelper.FileCoppy(tempFile, outPath);
}
catch (System.Exception ex)
{
MessageBox.Show("保存配置文件失败" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogHandler.Instance.VTLogError(ex.ToString());
}
}
#endregion
/// <summary>
/// 检查所有相机是否在线
/// </summary>
/// <returns>全部在线为true</returns>
public static bool CheckDevStatus()
{
foreach (AcqDeviceBase dev in HMeasureSYS.g_AcqDeviceList)
{
if (dev.m_bConnected==false)
{
return false;
}
}
return true;
}
/// <summary>
/// 初始化设备连接状态
/// </summary>
public static void InitDevStatus()
{
foreach (AcqDeviceBase dev in HMeasureSYS.g_AcqDeviceList)
{
if (dev.m_bConnected)
{
dev.m_bConnected = false;
dev.ConnectDev();
dev.setSetting();
}
else
{
dev.DisConnectDev();
}
}
} /// <summary>
/// 释放设备
/// </summary>
public static void DisposeDev()
{
foreach (AcqDeviceBase dev in HMeasureSYS.g_AcqDeviceList)
{
if (dev.m_bConnected)
{
dev.DisConnectDev();
}
}
} public void Dispose()
{
} #region "图像离线保存"
/// <summary>
/// 保存图片
/// </summary>
/// <param name="filePath">文件存储路径</param>
/// <param name="projectId">项目ID</param>
/// <param name="imgName">保存图片的变量名称</param>
public static void SaveImage(string filePath, int projectId, string imgName)
{
if (HMeasureSYS.g_ProjectList.Count > projectId)
{
RegisterIMG_Info tempImg = new RegisterIMG_Info();
F_DATA_CELL datacell = HMeasureSYS.g_ProjectList[projectId].m_VariableList.FirstOrDefault(c => c.m_Data_CellID == HMeasureSYS.U000 && c.m_Data_Name == imgName);
tempImg.m_Image = ((List<HImageExt>)datacell.m_Data_Value)[].Clone();
tempImg.m_ImageID = filePath;
ThreadPool.QueueUserWorkItem(o =>
{
try
{
RegisterIMG_Info imgInfo = (RegisterIMG_Info)tempImg;
string type = imgInfo.m_Image.GetImageType().ToString();
if (type.Contains("real"))
{
imgInfo.m_Image.WriteImage("tiff", , imgInfo.m_ImageID);
}
else if (type.Contains("byte"))
{
imgInfo.m_Image.WriteImage("tiff lzw alpha", , imgInfo.m_ImageID);
}
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
});
}
} /// <summary>
/// 保存图像
/// </summary>
/// <param name="filepath">图像保存路径,不包括文件后缀如 .png</param>
/// <param name="scaleSize">放缩比例,建议为1.0,不能小于0</param>
/// <param name="isSaveSrcImage">是否保存原图,true表示保存原图,false表示保存效果图</param>
/// <param name="imageName">图像名称 为空保存图像序列中的全部</param>
public static void SaveImage(string filepath, double scaleSize, bool isSaveSrcImage, string imageName)
{
if (filepath == "")
{
System.Windows.Forms.MessageBox.Show("SaveBoardImage:输出路径为空!");
return;
} if (scaleSize <= )
{
System.Windows.Forms.MessageBox.Show("SaveBoardImage:scaleSize必须大于0!");
return;
} List<F_DATA_CELL> datalist = HMeasureSYS.Cur_Project.m_VariableList.FindAll(c => c.m_Data_CellID == HMeasureSYS.U000
&& c.m_Data_Type == DataType.图像);
foreach (F_DATA_CELL item in datalist)
{
if (imageName == "")
{
SaveImage(filepath + "_" + HMeasureSYS.Cur_Project.m_ProjectName + "_" + item.m_Data_Name,
scaleSize,
isSaveSrcImage,
((List<HImageExt>)item.m_Data_Value)[].Clone());
}
else
{
if (item.m_Data_Name == imageName)
{
SaveImage(filepath + "_" + HMeasureSYS.Cur_Project.m_ProjectName + "_" + item.m_Data_Name,
scaleSize,
isSaveSrcImage,
((List<HImageExt>)item.m_Data_Value)[].Clone());
return;
}
}
}
} /// <summary>
/// 保存单一图像
/// </summary>
/// <param name="filepath">图像保存路径,不包括文件后缀如 .png</param>
/// <param name="scaleSize">放缩比例,建议为1.0,不能小于0</param>
/// <param name="isSaveSrcImage">是否保存原图,true表示保存原图,false表示保存效果图</param>
/// <param name="srcImage">需要保存的图像</param>
public static void SaveImage(string filepath, double scaleSize, bool isSaveSrcImage, HImageExt srcImage)
{
if (filepath == "")
{
System.Windows.Forms.MessageBox.Show("SaveBoardImage:输出路径为空!");
return;
} if (scaleSize <= )
{
System.Windows.Forms.MessageBox.Show("SaveBoardImage:scaleSize必须大于0!");
return;
} if (srcImage.IsInitialized() == false && srcImage != null)
{
System.Windows.Forms.MessageBox.Show("SaveBoardImage:图像未初始化!");
return;
} if (isSaveSrcImage)
{
ThreadPool.QueueUserWorkItem(o =>
{
try
{
HImageExt image = srcImage.Clone();
string type = image.GetImageType().ToString();
if (type.Contains("real"))
{
image.WriteImage("tiff", , filepath);
}
else
{
image.WriteImage("tiff lzw alpha", , filepath);
} image.Dispose();
GC.Collect();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
});
}
else
{
ThreadPool.QueueUserWorkItem(o =>
{
try
{
HImageExt image = srcImage.Clone();
int width, height;
image.GetImageSize(out width, out height);
ChoiceTech.Halcon.Control.HWindow_HE tempHWindow_HE = new ChoiceTech.Halcon.Control.HWindow_HE();
tempHWindow_HE.Size = new Size((int)(width * scaleSize), (int)(height * scaleSize));
tempHWindow_HE.showHE(image);
HImage img = tempHWindow_HE.hWindowControl.HalconWindow.DumpWindowImage();
img.WriteImage("tiff lzw alpha", , filepath);
tempHWindow_HE.Dispose();
image.Dispose();
GC.Collect();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
});
}
}
#endregion
}
}

namespace Measure的更多相关文章

  1. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  2. Microsoft.Office.Workflow.Actions Namespace

    Microsoft.Office.Workflow.Actions Namespace SharePoint 2010   Contains the workflow activities that ...

  3. 理解Docker(3):Docker 使用 Linux namespace 隔离容器的运行环境

    本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  4. C++ namespace

    namespace, 命名空间, 用于解决命名冲突的问题. Python中的package/module, Javascript中的object, Java中的package都具有这样的功能. 如何使 ...

  5. C++ 之namespace常见用法

    一.背景 需要使用Visual studio的C++,此篇对namespace的常用用法做个记录. 二.正文 namespace通常用来给类或者函数做个区间定义,以使编译器能准确定位到适合的类或者函数 ...

  6. using namespace std 和 using std::cin

    相较using std::cin使用using namespace std不会使得程序的效率变低,或者稳定性降低,只是这样作会将很多的名字引入程序,使得程序员使用的名字集合变小,容易引起命名冲突. 在 ...

  7. Why Namespace? - 每天5分钟玩转 OpenStack(102)

    上一节我们讨论了 Neutron 将虚拟 router 放置到 namespace 中实现了不同 subnet 之间的路由.今天探讨为什么要用 namespace 封装 router? 回顾一下前面的 ...

  8. struts2中错误There is no Action mapped for namespace [/] and action name [] associated with context path

    1 There is no Action mapped for namespace [/] and action name [] associated with context path [/Stru ...

  9. PHP 命名空间(namespace)

    PHP 命名空间(namespace) PHP 命名空间(namespace)是在PHP 5.3中加入的,如果你学过C#和Java,那命名空间就不算什么新事物. 不过在PHP当中还是有着相当重要的意义 ...

随机推荐

  1. LGOJP2051 [AHOI2009]中国象棋

    比较明显的计数dp.不知道为什么被打了状压的tag... 不难发现无论炮放在哪里其实是等价的,需要知道的只有这一列放了一个炮还是两个炮还是还没放,那么可以设\(f[i,j,k]\)表示第\(i\)行, ...

  2. HBase数据结构

    1 RowKey 与nosql数据库们一样,RowKey是用来检索记录的主键.访问HBASE table中的行,只有三种方式: 1.通过单个RowKey访问 2.通过RowKey的range(正则) ...

  3. 应用安全测试技术DAST、SAST、IAST对比分析【转】

    转自:https://blog.csdn.net/qq_29277155/article/details/92411079 一.全球面临软件安全危机 2010年,大型社交网站rockyou.com被曝 ...

  4. shell脚本 基础应用

    变量分为普通变量可只读变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ...

  5. install_config

    #! /bin/bash REPO='10.10.238.114:4507' zabbix='10.10.238.110' osmaster=`cat /etc/redhat-release |awk ...

  6. [HAOI 2018]染色

    传送门 Description 一个长度为\(N\)的序列, 每个位置都可以被染成 \(M\)种颜色中的某一种. 出现次数恰好为 \(S\)的颜色种数有\(i\)种, 会产生\(w_i\)的愉悦度. ...

  7. C语言函数内局部变量释放的坑

    首先把代码贴上来: #include <stdio.h> #include<windows.h> int f(int **iptr){ ; *iptr = &a; ; ...

  8. Edusoho之Basic Authentication

    通过如下代码,可以正常请求并获取对应的数据: curl -X POST -H "Accept:application/vnd.edusoho.v2+json" -H "A ...

  9. 2015-2016-2《Java程序设计》团队博客3

    项目进展 这周就是对上周所列出的类进行具体实现.但是到目前为止还没有遇到一些实质性的问题.虽然感觉没有问题就是最大的问题,但是还是希望能够尽早发现bug并及时改掉. 目前已经完成前几个文件之间的架构, ...

  10. how does SELECT TOP works when no order by is specified?

    how does SELECT TOP works when no order by is specified? There is no guarantee which two rows you ge ...