2871 lines
95 KiB
C#
2871 lines
95 KiB
C#
using Community;
|
||
using DAL;
|
||
using HalconDotNet;
|
||
using MahApps.Metro.Controls.Dialogs;
|
||
using Microsoft.Win32;
|
||
using Model;
|
||
using Model.Vision;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Windows;
|
||
using System.Windows.Threading;
|
||
using ViewModel.Robots;
|
||
using Views.Vision;
|
||
|
||
namespace ViewModel.Vision
|
||
{
|
||
/// <summary>
|
||
/// Halcon视觉类
|
||
/// </summary>
|
||
public partial class HalconVisual : NotificationObject
|
||
{
|
||
public VisualDAL Vdal = new VisualDAL();
|
||
|
||
/// <summary>
|
||
/// 上报异常的委托
|
||
/// </summary>
|
||
/// <param name="hv"></param>
|
||
public delegate void delegeateExcep(string hv);
|
||
/// <summary>
|
||
/// 上报异常的委托事件
|
||
/// </summary>
|
||
public event delegeateExcep eventExcepReport;
|
||
|
||
#region 视觉窗口对象
|
||
private AOI myWindow;
|
||
/// <summary>
|
||
/// 视觉窗口对象
|
||
/// </summary>
|
||
public AOI MyWindow
|
||
{
|
||
get { return myWindow; }
|
||
set
|
||
{
|
||
myWindow = value;
|
||
this.RaisePropertyChanged("MyWindow");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 显示权限
|
||
private Visibility displayLevel;
|
||
/// <summary>
|
||
/// 显示权限
|
||
/// </summary>
|
||
public Visibility DisplayLevel
|
||
{
|
||
get { return displayLevel; }
|
||
set
|
||
{
|
||
displayLevel = value;
|
||
if (value == Visibility.Collapsed)
|
||
{
|
||
ControlShow = "0";
|
||
|
||
}
|
||
else
|
||
{
|
||
ControlShow = "30*";
|
||
}
|
||
this.RaisePropertyChanged("DisplayLevel");
|
||
}
|
||
}
|
||
|
||
#region 超级权限
|
||
private Visibility superPrivilege;
|
||
/// <summary>
|
||
/// 超级权限
|
||
/// </summary>
|
||
public Visibility SuperPrivilege
|
||
{
|
||
get { return superPrivilege; }
|
||
set
|
||
{
|
||
superPrivilege = value;
|
||
this.RaisePropertyChanged("SuperPrivilege");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 视觉控件显示的状态
|
||
|
||
private string controlShow;
|
||
/// <summary>
|
||
/// 相机设置控件是否显示
|
||
/// </summary>
|
||
public string ControlShow
|
||
{
|
||
get { return controlShow; }
|
||
set
|
||
{
|
||
controlShow = value;
|
||
RaisePropertyChanged("ControlShow");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
#endregion
|
||
|
||
#region 机器人对象
|
||
private FeiYuRobotVM robot;
|
||
/// <summary>
|
||
/// 机器人对象
|
||
/// </summary>
|
||
public FeiYuRobotVM Robot
|
||
{
|
||
get { return robot; }
|
||
set
|
||
{
|
||
robot = value;
|
||
RaisePropertyChanged("Robot");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 视觉工程项目集合
|
||
private ObservableCollection<VisualEngineering> visualEngineeringList;
|
||
/// <summary>
|
||
/// 视觉工程项目集合
|
||
/// </summary>
|
||
public ObservableCollection<VisualEngineering> VisualEngineeringList
|
||
{
|
||
get { return visualEngineeringList; }
|
||
set
|
||
{
|
||
visualEngineeringList = value;
|
||
this.RaisePropertyChanged("VisualEngineeringList");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 当前使用的相机类型
|
||
/// <summary>
|
||
/// 当前使用的相机类型,即第几个相关
|
||
/// </summary>
|
||
public int CameraType { get; set; }
|
||
#endregion
|
||
|
||
#region 视觉对应的生产机种
|
||
private MachineModel machineObj;
|
||
/// <summary>
|
||
/// 视觉对应的生产机种
|
||
/// </summary>
|
||
public MachineModel MachineObj
|
||
{
|
||
get { return machineObj; }
|
||
set
|
||
{
|
||
machineObj = value;
|
||
if (value.ID > 0)
|
||
{
|
||
//Camera = Vdal.GetCamera(value.ID, CameraType);//获取相机对象
|
||
//Camera.eventProcessImage += ProcessHImage; // 注册halcon显示回调函数
|
||
|
||
//Camera.resolvingPowerSet += SetResolvingPower; //注册分辨率设置事件
|
||
|
||
VisualEngineeringList = Vdal.GetVisualEngineeringList(MachineObj.ID, Camera.ID); //获取相机对应的视觉工程项目集合
|
||
|
||
}
|
||
this.RaisePropertyChanged("MachineObj");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否要执行显示图像的委托
|
||
/// </summary>
|
||
public bool Buzhixing { get; set; }
|
||
/// <summary>
|
||
/// 图像委托实现
|
||
/// </summary>
|
||
/// <param name="image"></param>
|
||
public void ProcessHImage(HObject image)
|
||
{
|
||
if (Buzhixing)
|
||
{
|
||
if (image != null)
|
||
{
|
||
Ho_Image = image;
|
||
ClearObjData();
|
||
ShowObj(Ho_Image);
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Halcon图像显示控件
|
||
/// <summary>
|
||
/// Halcon图像显示控件
|
||
/// </summary>
|
||
public HWindowControlWPF HWindowWPF { get; set; }
|
||
|
||
|
||
private HSmartWindowControlWPF hWindowWPF2;
|
||
/// <summary>
|
||
/// Halcon图像预览控件(只能用于预览)
|
||
/// </summary>
|
||
public HSmartWindowControlWPF HWindowWPF2
|
||
{
|
||
get { return hWindowWPF2; }
|
||
set
|
||
{
|
||
hWindowWPF2 = value;
|
||
RaisePropertyChanged("HWindowWPF2");
|
||
}
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 图像对象
|
||
private HObject ho_Image;
|
||
/// <summary>
|
||
/// 相机图像
|
||
/// </summary>
|
||
public HObject Ho_Image
|
||
{
|
||
get { return ho_Image; }
|
||
set
|
||
{
|
||
ho_Image = value;
|
||
}
|
||
}
|
||
|
||
private HObject ho_ImageLocal;
|
||
/// <summary>
|
||
/// 本地图像
|
||
/// </summary>
|
||
public HObject Ho_ImageLocal
|
||
{
|
||
get { return ho_ImageLocal; }
|
||
set
|
||
{
|
||
ho_ImageLocal = value;
|
||
this.RaisePropertyChanged("Ho_ImageLocal");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 当前选中的产品区域模型
|
||
private V_RectangleModel selectModel;
|
||
/// <summary>
|
||
/// 当前选中的区域模型
|
||
/// </summary>
|
||
public V_RectangleModel SelectModel
|
||
{
|
||
get { return selectModel; }
|
||
set
|
||
{
|
||
selectModel = value;
|
||
|
||
PassagewayBrightness1 = value.PassagewayBrightness1;
|
||
PassagewayBrightness2 = value.PassagewayBrightness2;
|
||
PassagewayBrightness3 = value.PassagewayBrightness3;
|
||
PassagewayBrightness4 = value.PassagewayBrightness4;
|
||
SelectCodeTypeID = value.MyROI.CodeTypeID;
|
||
SelectCodeStr = value.MyROI.CodeStr;
|
||
this.RaisePropertyChanged("SelectModel");
|
||
|
||
}
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 各种方法
|
||
/// <summary>
|
||
/// 相机检查
|
||
/// </summary>
|
||
public DelegateCommand CamCheckCommand { get; set; }
|
||
/// <summary>
|
||
/// 相机重置
|
||
/// </summary>
|
||
public DelegateCommand CamResetCommand { get; set; }
|
||
/// <summary>
|
||
/// 停止连续采集
|
||
/// </summary>
|
||
public DelegateCommand StopCalibrationCommand { get; set; }
|
||
/// <summary>
|
||
/// 打开相机
|
||
/// </summary>
|
||
public DelegateCommand OpenCameraCommand { get; set; }
|
||
/// <summary>
|
||
/// 图像采集
|
||
/// </summary>
|
||
public DelegateCommand GetImageCommand { get; set; }
|
||
/// <summary>
|
||
/// 加载本地图像
|
||
/// </summary>
|
||
public DelegateCommand LoadingImageCommand { get; set; }
|
||
|
||
/// <summary>
|
||
/// 相机校正
|
||
/// </summary>
|
||
public DelegateCommand CameraCorrectionCommand { get; set; }
|
||
/// <summary>
|
||
|
||
/// <summary>
|
||
/// 保存相机参数
|
||
/// </summary>
|
||
public DelegateCommand SaveCamPerCommand { get; set; }
|
||
|
||
/// <summary>
|
||
/// 定位模型建立设置
|
||
/// </summary>
|
||
public DelegateCommand LocationSettingCommand { get; set; }
|
||
/// <summary>
|
||
/// 定位测试
|
||
/// </summary>
|
||
public DelegateCommand LocationCheckCommand { get; set; }
|
||
/// <summary>
|
||
/// 批量定位测试
|
||
/// </summary>
|
||
public DelegateCommand BatchLocationCheckCommand { get; set; }
|
||
|
||
/// <summary>
|
||
/// Roi形状选择
|
||
/// </summary>
|
||
public DelegateCommand RoiShapeSelectCommand { get; set; }
|
||
|
||
#endregion
|
||
|
||
#region 相机对象
|
||
private FeiYuCamere camera;
|
||
/// <summary>
|
||
/// 相机对象
|
||
/// </summary>
|
||
public FeiYuCamere Camera
|
||
{
|
||
get { return camera; }
|
||
set
|
||
{
|
||
camera = value;
|
||
this.RaisePropertyChanged("Camera");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 是否正在创建或编辑区域模型或ROI
|
||
/// <summary>
|
||
/// 是否正在创建或编辑区域模型或ROI
|
||
/// </summary>
|
||
public bool CreateModeling { get; set; }
|
||
#endregion
|
||
|
||
#region 光源通道集合
|
||
private List<string> passagewayLightList;
|
||
/// <summary>
|
||
/// 光源通道集合
|
||
/// </summary>
|
||
public List<string> PassagewayLightList
|
||
{
|
||
get { return passagewayLightList; }
|
||
set
|
||
{
|
||
passagewayLightList = value;
|
||
this.RaisePropertyChanged("PassagewayLightList");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 光源通道
|
||
private string passagewayLight;
|
||
/// <summary>
|
||
/// 光源通道
|
||
/// </summary>
|
||
public string PassagewayLight
|
||
{
|
||
get { return passagewayLight; }
|
||
set
|
||
{
|
||
passagewayLight = value;
|
||
if (!string.IsNullOrEmpty(value))
|
||
{
|
||
switch (value)
|
||
{
|
||
case "SA":
|
||
Brightness = PassagewayBrightness1;
|
||
break;
|
||
case "SB":
|
||
Brightness = PassagewayBrightness2;
|
||
break;
|
||
case "SC":
|
||
Brightness = PassagewayBrightness3;
|
||
break;
|
||
case "SD":
|
||
Brightness = PassagewayBrightness4;
|
||
break;
|
||
}
|
||
|
||
}
|
||
this.RaisePropertyChanged("PassagewayLight");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 光源各通道亮度
|
||
/// <summary>
|
||
/// 光源通道1亮度
|
||
/// </summary>
|
||
public long PassagewayBrightness1 { get; set; }
|
||
/// <summary>
|
||
/// 光源通道2亮度
|
||
/// </summary>
|
||
public long PassagewayBrightness2 { get; set; }
|
||
/// <summary>
|
||
/// 光源通道3亮度
|
||
/// </summary>
|
||
public long PassagewayBrightness3 { get; set; }
|
||
/// <summary>
|
||
/// 光源通道4亮度
|
||
/// </summary>
|
||
public long PassagewayBrightness4 { get; set; }
|
||
#endregion
|
||
|
||
#region 光源亮度
|
||
private long brightness;
|
||
/// <summary>
|
||
/// 光源亮度
|
||
/// </summary>
|
||
public long Brightness
|
||
{
|
||
get { return brightness; }
|
||
set
|
||
{
|
||
brightness = value;
|
||
|
||
try
|
||
{
|
||
if (value >= 0 && !string.IsNullOrEmpty(PassagewayLight))
|
||
{
|
||
// LightCOM.Send("SA" + string.Format("{0:d3}", value) + "#");
|
||
LightCOM.Send(PassagewayLight + string.Format("{0:d3}", value) + "#");
|
||
switch (PassagewayLight)
|
||
{
|
||
case "SA":
|
||
PassagewayBrightness1 = value;
|
||
break;
|
||
case "SB":
|
||
PassagewayBrightness2 = value;
|
||
break;
|
||
case "SC":
|
||
PassagewayBrightness3 = value;
|
||
break;
|
||
case "SD":
|
||
PassagewayBrightness4 = value;
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
|
||
this.RaisePropertyChanged("Brightness");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 光源串口对象
|
||
private SerialCom lightCOM;
|
||
/// <summary>
|
||
/// 光源串口对象
|
||
/// </summary>
|
||
public SerialCom LightCOM
|
||
{
|
||
get { return lightCOM; }
|
||
set
|
||
{
|
||
lightCOM = value;
|
||
this.RaisePropertyChanged("LightCOM");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 图片保留天数集合
|
||
private List<int> retentionDaysImageList;
|
||
/// <summary>
|
||
/// 图片保留天数集合
|
||
/// </summary>
|
||
public List<int> RetentionDaysImageList
|
||
{
|
||
get { return retentionDaysImageList; }
|
||
set
|
||
{
|
||
retentionDaysImageList = value;
|
||
this.RaisePropertyChanged("RetentionDaysImageList");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 特征识别系数集合
|
||
private List<double> coeList;
|
||
/// <summary>
|
||
/// 特征识别系数集合
|
||
/// </summary>
|
||
public List<double> CoeList
|
||
{
|
||
get { return coeList; }
|
||
set
|
||
{
|
||
coeList = value;
|
||
this.RaisePropertyChanged("CoeList");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 构造函数
|
||
public HalconVisual(MachineModel machine, SerialCom sc, FeiYuCamere camere)
|
||
{
|
||
MyWindow = new AOI();
|
||
MyWindow.DataContext = this;
|
||
HWindowWPF2 = new HSmartWindowControlWPF();
|
||
SuperPrivilege = Visibility.Collapsed;
|
||
ControlShow = "0";
|
||
|
||
PassagewayLightList = new List<string>() { "SA", "SB", "SC", "SD" };
|
||
LightCOM = sc;
|
||
|
||
RetentionDaysImageList = new List<int>() { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 };
|
||
CoeList = new List<double>() { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 };
|
||
|
||
|
||
HWindowWPF = MyWindow.hsm;
|
||
|
||
|
||
//HWindowWPF.HMouseWheel += HWindowWPF_HMouseWheel;
|
||
//HWindowWPF.HMouseDown += HWindowWPF_HMouseDown;
|
||
//HWindowWPF.HMouseUp += HWindowWPF_HMouseUp;
|
||
|
||
|
||
|
||
|
||
Camera = camere;//获取相机对象
|
||
Camera.eventProcessImage += ProcessHImage; // 注册halcon显示回调函数
|
||
Camera.resolvingPowerSet += SetResolvingPower; //注册分辨率设置事件
|
||
|
||
MachineObj = machine;
|
||
|
||
Ho_Image = new HObject();
|
||
HobjectList = new List<HObject>();
|
||
|
||
SweepCodeInitialize();
|
||
CalibrationInitialize();
|
||
InitializationFunctionEditing();
|
||
V_OcrVMInitialization();
|
||
MeasureInitialize();
|
||
InspectionFeaturesInitialization();
|
||
|
||
this.CamCheckCommand = new DelegateCommand();
|
||
this.CamCheckCommand.ExecuteAction = new Action<object>(CamCheck);
|
||
|
||
this.CamResetCommand = new DelegateCommand();
|
||
this.CamResetCommand.ExecuteAction = new Action<object>(CamReset);
|
||
|
||
this.StopCalibrationCommand = new DelegateCommand();
|
||
this.StopCalibrationCommand.ExecuteAction = new Action<object>(StopCalibration);
|
||
|
||
this.OpenCameraCommand = new DelegateCommand();
|
||
this.OpenCameraCommand.ExecuteAction = new Action<object>(OpenVideo);
|
||
|
||
this.GetImageCommand = new DelegateCommand();
|
||
this.GetImageCommand.ExecuteAction = new Action<object>(GetImage);
|
||
|
||
this.SaveCamPerCommand = new DelegateCommand();
|
||
this.SaveCamPerCommand.ExecuteAction = new Action<object>(SaveCamPer);
|
||
|
||
this.LoadingImageCommand = new DelegateCommand();
|
||
this.LoadingImageCommand.ExecuteAction = new Action<object>(LoadingImage);
|
||
|
||
//相机校正
|
||
this.CameraCorrectionCommand = new DelegateCommand();
|
||
this.CameraCorrectionCommand.ExecuteAction = new Action<object>(CameraCorrection);
|
||
|
||
this.LocationSettingCommand = new DelegateCommand();
|
||
this.LocationSettingCommand.ExecuteAction = new Action<object>(LocationSetting);
|
||
|
||
this.LocationCheckCommand = new DelegateCommand();
|
||
this.LocationCheckCommand.ExecuteAction = new Action<object>(LocationCheck);
|
||
this.BatchLocationCheckCommand = new DelegateCommand();
|
||
this.BatchLocationCheckCommand.ExecuteAction = new Action<object>(BatchLocationCheck);
|
||
|
||
this.RoiShapeSelectCommand = new DelegateCommand();
|
||
this.RoiShapeSelectCommand.ExecuteAction = new Action<object>(RoiShapeSelect);
|
||
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// ROI形状选择
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
private void RoiShapeSelect(object obj)
|
||
{
|
||
string str = obj.ToString();
|
||
DrawRoiShape = (DrawRoiEmn)Enum.Parse(typeof(DrawRoiEmn), str);
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="WPFconttrol">Halcon显示控件</param>
|
||
/// <param name="machine">机种</param>
|
||
/// <param name="CameType">相机类型 0:第一个相机1:第二个相机</param>
|
||
/// <param name="sc">光源串口对象</param>
|
||
public HalconVisual(HWindowControlWPF WPFconttrol, MachineModel machine, int camType, SerialCom sc)
|
||
{
|
||
MyWindow = new AOI();
|
||
HWindowWPF = MyWindow.hsm;
|
||
MyWindow.DataContext = this;
|
||
ControlShow = "0";
|
||
|
||
PassagewayLightList = new List<string>() { "SA", "SB", "SC", "SD" };
|
||
LightCOM = sc;
|
||
|
||
RetentionDaysImageList = new List<int>() { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 };
|
||
CoeList = new List<double>() { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 };
|
||
|
||
|
||
HWindowWPF = WPFconttrol;
|
||
//HWindowWPF.HMouseWheel += HWindowWPF_HMouseWheel;
|
||
//HWindowWPF.HMouseDown += HWindowWPF_HMouseDown;
|
||
//HWindowWPF.HMouseUp += HWindowWPF_HMouseUp;
|
||
this.CameraType = camType;
|
||
MachineObj = machine;
|
||
|
||
Ho_Image = new HObject();
|
||
HobjectList = new List<HObject>();
|
||
|
||
SweepCodeInitialize();
|
||
CalibrationInitialize();
|
||
InitializationFunctionEditing();
|
||
V_OcrVMInitialization();
|
||
MeasureInitialize();
|
||
InspectionFeaturesInitialization();
|
||
|
||
this.CamCheckCommand = new DelegateCommand();
|
||
this.CamCheckCommand.ExecuteAction = new Action<object>(CamCheck);
|
||
|
||
this.CamResetCommand = new DelegateCommand();
|
||
this.CamResetCommand.ExecuteAction = new Action<object>(CamReset);
|
||
|
||
this.StopCalibrationCommand = new DelegateCommand();
|
||
this.StopCalibrationCommand.ExecuteAction = new Action<object>(StopCalibration);
|
||
|
||
this.OpenCameraCommand = new DelegateCommand();
|
||
this.OpenCameraCommand.ExecuteAction = new Action<object>(OpenVideo);
|
||
|
||
this.GetImageCommand = new DelegateCommand();
|
||
this.GetImageCommand.ExecuteAction = new Action<object>(GetImage);
|
||
|
||
this.SaveCamPerCommand = new DelegateCommand();
|
||
this.SaveCamPerCommand.ExecuteAction = new Action<object>(SaveCamPer);
|
||
|
||
this.LoadingImageCommand = new DelegateCommand();
|
||
this.LoadingImageCommand.ExecuteAction = new Action<object>(LoadingImage);
|
||
|
||
//相机校正
|
||
this.CameraCorrectionCommand = new DelegateCommand();
|
||
this.CameraCorrectionCommand.ExecuteAction = new Action<object>(CameraCorrection);
|
||
|
||
this.LocationSettingCommand = new DelegateCommand();
|
||
this.LocationSettingCommand.ExecuteAction = new Action<object>(LocationSetting);
|
||
|
||
this.LocationCheckCommand = new DelegateCommand();
|
||
this.LocationCheckCommand.ExecuteAction = new Action<object>(LocationCheck);
|
||
|
||
this.BatchLocationCheckCommand = new DelegateCommand();
|
||
this.BatchLocationCheckCommand.ExecuteAction = new Action<object>(BatchLocationCheck);
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
#region 定位设置
|
||
/// <summary>
|
||
/// 定位设置
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
private void LocationSetting(object obj)
|
||
{
|
||
|
||
if (SelectModel.ID <= 0)
|
||
{
|
||
if (SaveAll(SelectModel))
|
||
{
|
||
SelectModel = Vdal.GetRectangleModel(SelectModel.VeiID, SelectModel.AreaType);
|
||
if (SelectModel.ID > 0)
|
||
{
|
||
GenerateFeaturesModel(SelectModel);
|
||
}
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
GenerateFeaturesModel(SelectModel);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 定位测试
|
||
/// <summary>
|
||
/// 定位测试
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
private void LocationCheck(object obj)
|
||
{
|
||
if (SelectModel.ID > 0)
|
||
{
|
||
FeatureMatchingIng(SelectModel);
|
||
}
|
||
else
|
||
{
|
||
MyMessageBox.Show("没有任何特征数据");
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// 批量定位测试
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
private void BatchLocationCheck(object obj)
|
||
{
|
||
BathCheck();
|
||
}
|
||
#endregion
|
||
|
||
#region 鼠标按下
|
||
|
||
HTuple m_hRowB, m_hColB, m_hButton, m_hRowE, m_hColE;
|
||
/// <summary>
|
||
/// 鼠标按下
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
public void HWindowWPF_HMouseDown(object sender, HMouseEventArgsWPF e)
|
||
{
|
||
|
||
if (CreateModeling || HobjectList.Count <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
HOperatorSet.SetCheck("~give_error");
|
||
HOperatorSet.GetMposition(HWindowWPF.HalconWindow, out m_hRowB, out m_hColB, out m_hButton);
|
||
|
||
}
|
||
catch
|
||
{
|
||
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 鼠标松开
|
||
/// <summary>
|
||
/// 鼠标松开
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
public void HWindowWPF_HMouseUp(object sender, HMouseEventArgsWPF e)
|
||
{
|
||
if (CreateModeling || HobjectList.Count <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
HOperatorSet.SetCheck("~give_error");
|
||
HOperatorSet.GetMposition(HWindowWPF.HalconWindow, out m_hRowE, out m_hColE, out m_hButton);
|
||
|
||
HTuple row1, col1, row2, col2;
|
||
double dbRowMove, dbColMove;
|
||
double dbRowB, dbColB, dbRowE, dbColE;
|
||
dbRowB = m_hRowB;
|
||
dbRowE = m_hRowE;
|
||
dbColB = m_hColB;
|
||
dbColE = m_hColE;
|
||
dbRowMove = -dbRowE + dbRowB;
|
||
dbColMove = -dbColE + dbColB;
|
||
|
||
HOperatorSet.GetPart(HWindowWPF.HalconWindow, out row1, out col1, out row2, out col2);
|
||
HOperatorSet.SetPart(HWindowWPF.HalconWindow, row1 + dbRowMove, col1 + dbColMove, row2 + dbRowMove, col2 + dbColMove);
|
||
|
||
|
||
RefeshWindow();//刷新显示
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 显示控件滚动图片放缩小
|
||
/// <summary>
|
||
/// 显示控件滚动图片放缩小
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
public void HWindowWPF_HMouseWheel(object sender, HMouseEventArgsWPF e)
|
||
{
|
||
if (CreateModeling || HobjectList.Count <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
//double x = e.X;
|
||
//double y = e.Y;
|
||
double Zoom = 1.0;
|
||
double ZoomTrans = 1.0, ZoomOrg = 1.0, RowShif, ColShif;
|
||
HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2;
|
||
HTuple Row, Col, Button;
|
||
|
||
HOperatorSet.SetCheck("~give_error");
|
||
HOperatorSet.GetMposition(HWindowWPF.HalconWindow, out Row, out Col, out Button);
|
||
HOperatorSet.GetPart(HWindowWPF.HalconWindow, out Row0, out Column0, out Row00, out Column00);
|
||
|
||
if (e.Delta >= 0)
|
||
{
|
||
if (0 != Zoom && Zoom >= 32.0)
|
||
{
|
||
Zoom = 32.0;
|
||
}
|
||
else
|
||
{
|
||
Zoom = 1.0 + Zoom;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (0 != Zoom && Zoom <= (1.0 / 16))
|
||
{
|
||
Zoom = 1.0 / 16;
|
||
}
|
||
else
|
||
{
|
||
Zoom = Zoom / 2;
|
||
}
|
||
}
|
||
ZoomTrans = Zoom / ZoomOrg;
|
||
ZoomOrg = Zoom;
|
||
RowShif = 0;
|
||
ColShif = 0;
|
||
Ht = Row00 - Row0;
|
||
Wt = Column00 - Column0;
|
||
r1 = (Row0 + ((1 - (1.0 / ZoomTrans)) * (Row - Row0))) - (RowShif / ZoomTrans);
|
||
c1 = (Column0 + ((1 - (1.0 / ZoomTrans)) * (Col - Column0))) - (ColShif / ZoomTrans);
|
||
r2 = r1 + (Ht / ZoomTrans);
|
||
c2 = c1 + (Wt / ZoomTrans);
|
||
|
||
HOperatorSet.SetPart(HWindowWPF.HalconWindow, r1, c1, r2, c2);
|
||
|
||
RefeshWindow();//刷新显示
|
||
|
||
|
||
}
|
||
catch
|
||
{
|
||
RefeshWindow();//刷新显示
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#endregion
|
||
|
||
#region 设置控件分辨率
|
||
|
||
/// <summary>
|
||
/// 十字架准星
|
||
/// </summary>
|
||
HObject ho_Cross;
|
||
/// <summary>
|
||
/// 设置控件分辨率
|
||
/// </summary>
|
||
/// <param name="re"></param>
|
||
public void SetResolvingPower(Rect re)
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0) //检查窗口是否存在
|
||
{
|
||
//直接填充
|
||
// HWindowWPF.ImagePart = re;
|
||
|
||
#region 等比例显示
|
||
//等比例显示
|
||
int imgWidth, imgHeight, winRow, winCol, winWidth, winHeight, partWidth, partHeight;
|
||
try
|
||
{
|
||
imgWidth = (int)re.Width;
|
||
imgHeight = (int)re.Height;//image.GetImageSize(out imgWidth, out imgHeight);
|
||
HWindowWPF.HalconWindow.GetWindowExtents(out winRow, out winCol, out winWidth, out winHeight);
|
||
|
||
double x = 0, y = 0;
|
||
if (winWidth < winHeight)
|
||
{
|
||
partHeight = imgWidth * winHeight / winWidth;
|
||
if (imgWidth < winWidth && imgWidth < imgHeight)
|
||
{
|
||
partWidth = imgHeight * winWidth / winHeight;
|
||
}
|
||
else
|
||
{
|
||
partWidth = imgWidth;
|
||
}
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
|
||
partWidth = imgHeight * winWidth / winHeight;
|
||
|
||
if (imgHeight < winHeight && imgHeight < imgWidth)
|
||
{
|
||
partHeight = imgWidth * winHeight / winWidth;
|
||
}
|
||
else
|
||
{
|
||
partHeight = imgHeight;
|
||
}
|
||
}
|
||
|
||
x = (imgWidth - partWidth) / 2;
|
||
x = x > 0 ? 0 : x;
|
||
y = (imgHeight - partHeight) / 2;
|
||
y = y > 0 ? 0 : y;
|
||
|
||
|
||
re.Width = partWidth;
|
||
re.Height = partHeight;
|
||
re.X = x;
|
||
re.Y = y;
|
||
HWindowWPF.ImagePart = re;
|
||
//HWindowWPF.HalconWindow.SetPart(100, 0, partHeight - 1, partWidth - 1);
|
||
// window.DispImage(image);
|
||
|
||
//刷新十字准星
|
||
// HOperatorSet.GenCrossContourXld(out ho_Cross, re.Height / 2, re.Width / 2, 500, 0);
|
||
HOperatorSet.GenCrossContourXld(out ho_Cross, imgHeight / 2, imgWidth / 2, 500, 0);
|
||
}
|
||
catch (HalconException hEx)
|
||
{
|
||
ShowText(hEx.Message, "red");
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 相机检测
|
||
/// <summary>
|
||
/// 相机检测
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public void CamCheck(object obj)
|
||
{
|
||
Camera.GetCamNameList();
|
||
}
|
||
#endregion
|
||
|
||
#region 相机重置
|
||
/// <summary>
|
||
/// 相机重置
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public void CamReset(object obj)
|
||
{
|
||
Camera.CloseCam();
|
||
try
|
||
{
|
||
HWindowWPF.HalconWindow.ClearWindow();
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 保存相机参数
|
||
/// <summary>
|
||
/// 保存相机参数
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public void SaveCamPer(object obj)
|
||
{
|
||
if (Camera.ID <= 0)
|
||
{
|
||
Camera.ErrorMessage = Vdal.AddCamera(Camera) > 0 ? "相机参数添加成功" : "相机参数添加失败";
|
||
}
|
||
else
|
||
{
|
||
Camera.ErrorMessage = Vdal.UpdateCamera(Camera) > 0 ? "相机参数更新成功" : "相机参数更新失败";
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 开启视屏
|
||
/// <summary>
|
||
/// 是否是连续取图模式
|
||
/// </summary>
|
||
bool ContinuousAcquisition { get; set; }
|
||
/// <summary>
|
||
/// 开启视屏
|
||
/// </summary>
|
||
public void OpenVideo(object obj)
|
||
{
|
||
if (Camera == null)
|
||
{
|
||
return;
|
||
}
|
||
Buzhixing = true;
|
||
ContinuousAcquisition = Camera.StartGrabbing();
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
#region 停止连续采集
|
||
/// <summary>
|
||
/// 停止连续采集
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public void StopCalibration(object obj)
|
||
{
|
||
Camera.StopGrabbing();
|
||
ContinuousAcquisition = false;
|
||
}
|
||
#endregion
|
||
|
||
#region 加载本地图像
|
||
|
||
private string imgPath;
|
||
/// <summary>
|
||
/// 本地图片地址
|
||
/// </summary>
|
||
public string ImgPath
|
||
{
|
||
get { return imgPath; }
|
||
set
|
||
{
|
||
imgPath = value;
|
||
this.RaisePropertyChanged("ImgPath");
|
||
}
|
||
}
|
||
|
||
HObject ho_bendiImage = new HObject();
|
||
|
||
/// <summary>
|
||
/// 加载本地图像
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public void LoadingImage(object obj)
|
||
{
|
||
HTuple hv_Width, hv_Height;
|
||
OpenFileDialog openFile = new OpenFileDialog();
|
||
openFile.Filter = "图像文件(*.tif*.tiff*.bmp*.jpg*.jpeg*.jp2*.png*.pcx*.pgm*.ppm*.pbm*.xwd*.ima*.hobj)|*.tif;*.tiff;*.bmp;*.jpg;*.jpeg;*.jp2;*.png;*.pcx;*.pgm;*.ppm;*.pbm;*.xwd;*.ima;*.hobj|所有文件(*.*)|*.*";
|
||
try
|
||
{
|
||
if (openFile.ShowDialog() == true)
|
||
{
|
||
ImgPath = openFile.FileName;
|
||
ho_bendiImage.Dispose();
|
||
HOperatorSet.ReadImage(out ho_bendiImage, ImgPath);
|
||
HOperatorSet.GetImageSize(ho_bendiImage, out hv_Width, out hv_Height); //获取分辨率
|
||
SetResolvingPower(new System.Windows.Rect(0, 0, hv_Width, hv_Height));//设置图像像是控件分辨率
|
||
|
||
ClearWindowAndData();
|
||
ShowObj(ho_bendiImage);
|
||
ShowText("Image Size:" + hv_Height.ToString() + "*" + hv_Width.ToString(), "green");
|
||
Ho_ImageLocal = ho_bendiImage;
|
||
|
||
EnableLocalImage = true;
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.Show("本地图片打开失败");
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 是否启用本地图片
|
||
private bool enableLocalImage;
|
||
/// <summary>
|
||
/// 是否启用本地图片
|
||
/// </summary>
|
||
public bool EnableLocalImage
|
||
{
|
||
get { return enableLocalImage; }
|
||
set
|
||
{
|
||
enableLocalImage = value;
|
||
if (Ho_ImageLocal != null && value)
|
||
{
|
||
Ho_Image = Ho_ImageLocal;
|
||
}
|
||
this.RaisePropertyChanged("EnableLocalImage");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 获取图片
|
||
/// <summary>
|
||
/// 获取单张图片
|
||
/// </summary>
|
||
public void GetImage(object obj)
|
||
{
|
||
|
||
ContinuousAcquisition = false;
|
||
EnableLocalImage = false;
|
||
if (!GetImage(null))
|
||
{
|
||
return;
|
||
}
|
||
|
||
HTuple hv_Width, hv_Height;
|
||
HOperatorSet.GetImageSize(Ho_Image, out hv_Width, out hv_Height); //获取分辨率
|
||
ShowText("Image Size:" + hv_Height.ToString() + "*" + hv_Width.ToString(), "green");
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取图像
|
||
/// </summary>
|
||
/// <param name="RM">RM对象用于设置光源</param>
|
||
/// <returns></returns>
|
||
public bool GetImage(V_RectangleModel RM)
|
||
{
|
||
//1,获取图像
|
||
if (EnableLocalImage)
|
||
{
|
||
Ho_Image = Ho_ImageLocal;
|
||
}
|
||
else
|
||
{
|
||
//if (RM != null)
|
||
//{
|
||
// SetLight(RM);//设置光源亮度
|
||
//}
|
||
Buzhixing = true;
|
||
Ho_Image = Camera.GrabOne();
|
||
}
|
||
|
||
if (!HobjectIsNull(Ho_Image))
|
||
{
|
||
ShowObj(Ho_Image);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
ShowText("获取图像失败", "red");
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 获取图像
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool GetImage()
|
||
{
|
||
//1,获取图像
|
||
if (EnableLocalImage)
|
||
{
|
||
Ho_Image = Ho_ImageLocal;
|
||
}
|
||
else
|
||
{
|
||
Buzhixing = true;
|
||
Ho_Image = Camera.GrabOne();
|
||
}
|
||
|
||
if (!HobjectIsNull(Ho_Image))
|
||
{
|
||
ShowObj(Ho_Image);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
ShowText("获取图像失败", "red");
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 判断Hobject是否为空
|
||
/// <summary>
|
||
/// 判断Hobject是否为空
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
/// <returns></returns>
|
||
public bool HobjectIsNull(HObject obj)
|
||
{
|
||
bool bl = false;
|
||
try
|
||
{
|
||
if (obj != null)
|
||
{
|
||
HObject ho_temp;
|
||
HTuple hv_temp;
|
||
|
||
//IsInitialized只能用来判断图像有无初始化,不能判断图像空不空或有没有
|
||
if (!obj.IsInitialized())
|
||
{
|
||
bl = true;
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.GenEmptyObj(out ho_temp);
|
||
HOperatorSet.TestEqualObj(obj, ho_temp, out hv_temp);
|
||
if (hv_temp == 1)
|
||
{
|
||
bl = true;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
bl = true;
|
||
}
|
||
return bl;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return bl;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 绘制区域
|
||
/// <summary>
|
||
/// 绘制区域
|
||
/// </summary>
|
||
public V_RectangleModel DrawArea(HObject imageTemp, V_RectangleModel rm)
|
||
{
|
||
try
|
||
{
|
||
HObject ho_Rectangle;
|
||
HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null, hv_Column2 = null;
|
||
|
||
HOperatorSet.GenEmptyObj(out ho_Rectangle);
|
||
|
||
#region 1、图像准备
|
||
|
||
//显示图片
|
||
ShowObj(imageTemp);
|
||
|
||
ShowText("图像准备完毕,请绘制Mark范围区域", "green");
|
||
#endregion
|
||
|
||
#region 2、绘制模型区域
|
||
SetColored("blue");
|
||
//画笔设为边缘(边框)
|
||
SetDraw("margin");
|
||
//设置边框的粗细为3
|
||
HOperatorSet.SetLineWidth(HWindowWPF.HalconWindow, 3);
|
||
|
||
//画一个矩形 获取起点坐标和终点坐标
|
||
HOperatorSet.DrawRectangle1(HWindowWPF.HalconWindow, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2);
|
||
|
||
//获取矩形区域模型
|
||
ho_Rectangle.Dispose();
|
||
HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
|
||
|
||
//显示图片
|
||
ShowObj(imageTemp);
|
||
//显示矩形区域
|
||
ShowObj(ho_Rectangle, "green");
|
||
|
||
#endregion
|
||
|
||
#region 3、获取模型信息
|
||
rm.Point_Row1 = hv_Row1;
|
||
rm.Point_Column1 = hv_Column1;
|
||
rm.Point_Row2 = hv_Row2;
|
||
rm.Point_Column2 = hv_Column2;
|
||
#endregion
|
||
|
||
ho_Rectangle.Dispose();
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
return rm;
|
||
}
|
||
#endregion
|
||
|
||
#region 绘制ROI
|
||
private DrawRoiEmn drawRoiShape;
|
||
/// <summary>
|
||
/// 绘制的ROI形状
|
||
/// </summary>
|
||
public DrawRoiEmn DrawRoiShape
|
||
{
|
||
get { return drawRoiShape; }
|
||
set
|
||
{
|
||
drawRoiShape = value;
|
||
this.RaisePropertyChanged("DrawRoiShape");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绘制ROI
|
||
/// </summary>
|
||
/// <param name="roi"></param>
|
||
/// <returns></returns>
|
||
public V_ROI DrawROI(V_ROI roi)
|
||
{
|
||
HObject ho_Rectangle;
|
||
HTuple hv_Row = null, hv_Column = null, hv_Radius = null, hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
|
||
HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null, hv_Column2 = null;
|
||
|
||
|
||
ShowText("图像准备完毕,请绘制ROI区域", "green");
|
||
|
||
#region 2、绘制模型区域
|
||
//SetColored("blue");
|
||
HOperatorSet.SetColor(HWindowWPF.HalconWindow, "blue");
|
||
//画笔设为边缘(边框)
|
||
SetDraw("margin");
|
||
// HOperatorSet.SetDraw(HWindowWPF.HalconWindow, "margin");
|
||
//设置边框的粗细为3
|
||
HOperatorSet.SetLineWidth(HWindowWPF.HalconWindow, 1);
|
||
HOperatorSet.GenEmptyObj(out ho_Rectangle);
|
||
switch (DrawRoiShape)
|
||
{
|
||
case DrawRoiEmn.C:
|
||
//画圆形
|
||
HOperatorSet.DrawCircle(HWindowWPF.HalconID, out hv_Row, out hv_Column, out hv_Radius);
|
||
HOperatorSet.GenCircle(out ho_Rectangle, hv_Row, hv_Column, hv_Radius);
|
||
|
||
roi.hv_Row = hv_Row;
|
||
roi.hv_Column = hv_Column;
|
||
roi.hv_Radius = hv_Radius;
|
||
roi.ho_OutlineROI = ho_Rectangle;
|
||
break;
|
||
case DrawRoiEmn.R1:
|
||
//画一个矩形 获取起点坐标和终点坐标
|
||
HOperatorSet.DrawRectangle1(HWindowWPF.HalconWindow, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2);
|
||
//获取矩形区域模型
|
||
//ho_Rectangle.Dispose();
|
||
HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
|
||
|
||
roi.hv_Row = hv_Row1;
|
||
roi.hv_Column = hv_Column1;
|
||
roi.hv_Row1 = hv_Row2;
|
||
roi.hv_Column1 = hv_Column2;
|
||
roi.ho_OutlineROI = ho_Rectangle;
|
||
break;
|
||
case DrawRoiEmn.R2:
|
||
HOperatorSet.DrawRectangle2(HWindowWPF.HalconWindow, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2);
|
||
HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2);
|
||
|
||
roi.hv_Row = hv_Row;
|
||
roi.hv_Column = hv_Column;
|
||
roi.hv_Phi = hv_Phi;
|
||
roi.hv_length1 = hv_Length1;
|
||
roi.hv_length2 = hv_Length2;
|
||
roi.ho_OutlineROI = ho_Rectangle;
|
||
break;
|
||
}
|
||
|
||
roi.RoiShape = DrawRoiShape;
|
||
//显示ROI区域
|
||
ShowObj(ho_Rectangle, "green");
|
||
|
||
#endregion
|
||
|
||
return roi;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绘制ROI 选取特征专用
|
||
/// </summary>
|
||
/// <param name="roi"></param>
|
||
/// <returns></returns>
|
||
public bool DrawROI(V_RectangleModel RM)
|
||
{
|
||
bool bl = false;
|
||
try
|
||
{
|
||
|
||
HObject ho_Rectangle, ho_region_removeds;
|
||
HTuple hv_Row = null, hv_Column = null, hv_Radius = null, hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
|
||
HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null, hv_Column2 = null;
|
||
HTuple hv_Button = null;
|
||
|
||
ShowText("请绘制ROI!单个ROI绘制完成单击右键后,继续绘制单击左键,结束单击右键", "green");
|
||
|
||
#region 2、绘制模型区域
|
||
|
||
SetColored("blue");
|
||
SetDraw("margin");
|
||
|
||
//设置边框的粗细为3
|
||
HOperatorSet.SetLineWidth(HWindowWPF.HalconWindow, 1);
|
||
HOperatorSet.GenEmptyObj(out ho_Rectangle);
|
||
HOperatorSet.GenEmptyObj(out ho_region_removeds);
|
||
hv_Button = 1;
|
||
while (hv_Button == 1)
|
||
{
|
||
SetColored("blue");
|
||
switch (DrawRoiShape)
|
||
{
|
||
case DrawRoiEmn.C:
|
||
//画圆形
|
||
HOperatorSet.DrawCircle(HWindowWPF.HalconID, out hv_Row, out hv_Column, out hv_Radius);
|
||
HOperatorSet.GenCircle(out ho_Rectangle, hv_Row, hv_Column, hv_Radius);
|
||
RM.MyROI.hv_Row = hv_Row;
|
||
RM.MyROI.hv_Column = hv_Column;
|
||
RM.MyROI.hv_Radius = hv_Radius;
|
||
RM.MyROI.ho_OutlineROI = ho_Rectangle;
|
||
|
||
break;
|
||
case DrawRoiEmn.R1:
|
||
//画一个矩形 获取起点坐标和终点坐标
|
||
HOperatorSet.DrawRectangle1(HWindowWPF.HalconWindow, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2);
|
||
//获取矩形区域模型
|
||
//ho_Rectangle.Dispose();
|
||
HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
|
||
|
||
RM.MyROI.hv_Row = hv_Row1;
|
||
RM.MyROI.hv_Column = hv_Column1;
|
||
RM.MyROI.hv_Row1 = hv_Row2;
|
||
RM.MyROI.hv_Column1 = hv_Column2;
|
||
RM.MyROI.ho_OutlineROI = ho_Rectangle;
|
||
|
||
break;
|
||
case DrawRoiEmn.R2:
|
||
HOperatorSet.DrawRectangle2(HWindowWPF.HalconWindow, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2);
|
||
HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2);
|
||
RM.MyROI.hv_Row = hv_Row;
|
||
RM.MyROI.hv_Column = hv_Column;
|
||
RM.MyROI.hv_Phi = hv_Phi;
|
||
RM.MyROI.hv_length1 = hv_Length1;
|
||
RM.MyROI.hv_length2 = hv_Length2;
|
||
RM.MyROI.ho_OutlineROI = ho_Rectangle;
|
||
break;
|
||
}
|
||
|
||
RM.MyROI.RoiShape = DrawRoiShape;
|
||
HOperatorSet.Union2(ho_region_removeds, ho_Rectangle, out ho_region_removeds);
|
||
//显示ROI区域
|
||
ShowObj(ho_Rectangle, "green");
|
||
//等待鼠标按下 1:左键 4:右键
|
||
HOperatorSet.GetMbutton(HWindowWPF.HalconWindow, out hv_Row, out hv_Column, out hv_Button);
|
||
|
||
|
||
}
|
||
RM.MyROI.ho_OutlineROI = ho_region_removeds;
|
||
// HOperatorSet.Difference(RM.ho_RectangleModel, ho_region_removeds, out RM.MyROI.ho_OutlineROI);
|
||
ShowObj(Ho_Image);
|
||
ShowObj(RM.ho_RectangleModel, "blue");
|
||
//显示ROI区域
|
||
ShowObj(RM.MyROI.ho_OutlineROI, "green");
|
||
#endregion
|
||
|
||
bl = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ShowObj(Ho_Image);
|
||
ShowText("ROI绘制错误" + ex, "red", 30);
|
||
}
|
||
|
||
return bl;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 相机校正
|
||
/// <summary>
|
||
/// 相机校正中
|
||
/// </summary>
|
||
public bool CameraCorrectioning { get; set; }
|
||
|
||
/// <summary>
|
||
/// 相机校正
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public void CameraCorrection(object obj)
|
||
{
|
||
CameraCorrectioning = true;
|
||
}
|
||
#endregion
|
||
|
||
#region 建立特征模板
|
||
/// <summary>
|
||
/// 建立特征模板
|
||
/// </summary>
|
||
/// <param name="CP"></param>
|
||
public void GenerateFeaturesModel(V_RectangleModel RM)
|
||
{
|
||
|
||
if (CreateModeling)
|
||
{
|
||
MyMessageBox.Show("当前已处于编辑状态,请完成之前的所有操作");
|
||
return;
|
||
}
|
||
|
||
if (MyMessageBox.Show("你确定要编辑模型吗?", "提示", MessageDialogStyle.AffirmativeAndNegative) == MessageDialogResult.Negative)
|
||
{
|
||
return;
|
||
}
|
||
|
||
|
||
HTuple hv_Row = new HTuple();
|
||
HTuple hv_Column = new HTuple(), hv_Angle = new HTuple();
|
||
HTuple hv_Score = new HTuple(), hv_I = new HTuple(), hv_RectificationHomMat2D = new HTuple();
|
||
|
||
|
||
HObject ho_ModelRegion;
|
||
HObject ho_ModelContours, ho_TransContours;
|
||
|
||
HOperatorSet.GenEmptyObj(out ho_ModelRegion);
|
||
|
||
HOperatorSet.GenEmptyObj(out ho_ModelContours);
|
||
HOperatorSet.GenEmptyObj(out ho_TransContours);
|
||
|
||
//1,获取图像
|
||
if (!GetImage())
|
||
{
|
||
return;
|
||
}
|
||
//去畸变
|
||
//DeDistortion();
|
||
|
||
CreateModeling = true;
|
||
//1,绘制区域模型
|
||
V_RectangleModel rm = new V_RectangleModel();
|
||
rm = DrawArea(Ho_Image, rm);
|
||
rm.Instantiation();
|
||
|
||
//2,绘制ROI
|
||
rm.MyROI = new V_ROI();
|
||
DrawROI(rm);
|
||
//3、寻找ROI特征
|
||
SetDraw("fill");
|
||
// HOperatorSet.SetRgba(HWindowWPF2.HalconWindow, 255, 0, 0, 100); //设置后mask将显示为透明的红色,第四个参数控制透明度,范围:0~255
|
||
ho_TransContours.Dispose();
|
||
ho_TransContours = ModelPerReduction(rm);
|
||
ShowObj(ho_TransContours);
|
||
|
||
if (ho_TransContours == null)
|
||
{
|
||
MyMessageBox.Show("未能找到明显特征区域");
|
||
CreateModeling = false;
|
||
return;
|
||
}
|
||
|
||
if (MessageDialogResult.Affirmative == MyMessageBox.Show("模板设置完成是否保存?", "提示", MessageDialogStyle.AffirmativeAndNegative))
|
||
{
|
||
RM.Point_Row1 = rm.Point_Row1;
|
||
RM.Point_Row2 = rm.Point_Row2;
|
||
RM.Point_Column1 = rm.Point_Column1;
|
||
RM.Point_Column2 = rm.Point_Column2;
|
||
|
||
if (!EnableLocalImage)
|
||
{
|
||
RM.PassagewayBrightness1 = this.PassagewayBrightness1;
|
||
RM.PassagewayBrightness2 = this.PassagewayBrightness2;
|
||
RM.PassagewayBrightness3 = this.PassagewayBrightness3;
|
||
RM.PassagewayBrightness4 = this.PassagewayBrightness4;
|
||
}
|
||
|
||
|
||
RM.MyROI.ModelFeaturePiont.hv_RefRow = rm.MyROI.ModelFeaturePiont.hv_RefRow;
|
||
RM.MyROI.ModelFeaturePiont.hv_RefColumn = rm.MyROI.ModelFeaturePiont.hv_RefColumn;
|
||
|
||
//矩形参数
|
||
RM.MyROI.hv_Row = rm.MyROI.hv_Row;
|
||
RM.MyROI.hv_Row1 = rm.MyROI.hv_Row1;
|
||
RM.MyROI.hv_Column = rm.MyROI.hv_Column;
|
||
RM.MyROI.hv_Column1 = rm.MyROI.hv_Column1;
|
||
|
||
//圆形参数
|
||
RM.MyROI.RoiShape = rm.MyROI.RoiShape;
|
||
RM.MyROI.hv_Radius = rm.MyROI.hv_Radius;
|
||
|
||
//可变矩形参数
|
||
RM.MyROI.hv_Phi = rm.MyROI.hv_Phi;
|
||
RM.MyROI.hv_length1 = rm.MyROI.hv_length1;
|
||
RM.MyROI.hv_length2 = rm.MyROI.hv_length2;
|
||
|
||
|
||
RM.Instantiation();
|
||
RM.MyROI.Initialize();
|
||
RM.MyROI.hv_ModelID = rm.MyROI.hv_ModelID;
|
||
RM.MyROI.ho_OutlineROI = rm.MyROI.ho_OutlineROI;
|
||
|
||
//if (SaveAll(RM) && RM.MyROI.SaveFeatureModel() && RM.MyROI.Save_roi_Region())
|
||
if (SaveAll(RM) && RM.MyROI.SaveFeatureModel())
|
||
{
|
||
|
||
string filePath = @"Image/" + RM.MyROI.HelpFileName + ".bmp"; //模板图片路径
|
||
HOperatorSet.WriteImage(Ho_Image, "bmp", 0, filePath); //保存模板图片
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
MyMessageBox.Show("保存失败");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
CreateModeling = false;
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 创建模型参数
|
||
/// <summary>
|
||
/// 创建模型参数
|
||
/// </summary>
|
||
/// <param name="RM"></param>
|
||
public HObject ModelPerReduction(V_RectangleModel RM)
|
||
{
|
||
HTuple hv_ModelID = null, hv_ModelRegionArea = null;
|
||
HTuple hv_RefRow = null, hv_RefColumn = null, hv_HomMat2D = null;
|
||
HTuple hv_Row = new HTuple();
|
||
HTuple hv_Column = new HTuple(), hv_Angle = new HTuple();
|
||
HTuple hv_Score = new HTuple(), hv_I = new HTuple(), hv_RectificationHomMat2D = new HTuple();
|
||
|
||
|
||
HObject ho_ModelRegion;
|
||
HObject ho_ModelContours, ho_TransContours;
|
||
|
||
HOperatorSet.GenEmptyObj(out ho_ModelRegion);
|
||
HOperatorSet.GenEmptyObj(out ho_ModelContours);
|
||
HOperatorSet.GenEmptyObj(out ho_TransContours);
|
||
|
||
try
|
||
{
|
||
//2,绘制区域模型
|
||
RM.Instantiation();
|
||
//截取矩形选框中的图像
|
||
HObject ho_ImageRM;
|
||
HOperatorSet.GenEmptyObj(out ho_ImageRM);
|
||
HOperatorSet.ReduceDomain(Ho_Image, RM.ho_RectangleModel, out ho_ImageRM);
|
||
|
||
//3,绘制ROI
|
||
// RM.MyROI.Initialize();
|
||
//截取矩形选框中的图像
|
||
HObject ho_ImageROI;
|
||
HOperatorSet.GenEmptyObj(out ho_ImageROI);
|
||
HOperatorSet.ReduceDomain(ho_ImageRM, RM.MyROI.ho_OutlineROI, out ho_ImageROI);
|
||
|
||
HOperatorSet.CreateShapeModel(ho_ImageROI, "auto", (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto", out hv_ModelID);
|
||
// HOperatorSet.CreateShapeModel(ho_ImageROI, "auto", (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), "auto", (new HTuple("point_reduction_medium")).TupleConcat("no_pregeneration"), "use_polarity", "auto", "auto", out hv_ModelID);
|
||
//模型将它转换成图像的轮廓
|
||
ho_ModelContours.Dispose();
|
||
HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);
|
||
//
|
||
//获取参考位置
|
||
HOperatorSet.AreaCenter(RM.MyROI.ho_OutlineROI, out hv_ModelRegionArea, out hv_RefRow, out hv_RefColumn);
|
||
HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out hv_HomMat2D);
|
||
ho_TransContours.Dispose();
|
||
HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D);
|
||
|
||
RM.MyROI.hv_ModelID = hv_ModelID;
|
||
RM.MyROI.ModelFeaturePiont.hv_RefRow = hv_RefRow;
|
||
RM.MyROI.ModelFeaturePiont.hv_RefColumn = hv_RefColumn;
|
||
|
||
return ho_TransContours;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 特征匹配
|
||
/// <summary>
|
||
/// 特征匹配
|
||
/// </summary>
|
||
/// <param name="RM"></param>
|
||
public bool FeatureMatchingIng(V_RectangleModel RM)
|
||
{
|
||
bool pipeiOK = false;
|
||
|
||
if (RM == null || RM.ID <= 0)
|
||
{
|
||
MessageBox.Show("请选择测试项目");
|
||
return pipeiOK;
|
||
}
|
||
|
||
HObject ho_ImageReduced, ho_ModelContours, ho_TransContours, ho_ImageROI;
|
||
HTuple hv_HomMat2D = null;
|
||
HTuple hv_Row = new HTuple();
|
||
HTuple hv_Column = new HTuple(), hv_Angle = new HTuple();
|
||
HTuple hv_Score = new HTuple(), hv_I = new HTuple(), hv_RectificationHomMat2D = new HTuple();
|
||
|
||
HOperatorSet.GenEmptyObj(out ho_ImageReduced);
|
||
HOperatorSet.GenEmptyObj(out ho_ModelContours);
|
||
HOperatorSet.GenEmptyObj(out ho_TransContours);
|
||
HOperatorSet.GenEmptyObj(out ho_ImageROI);
|
||
|
||
//1,获取图像
|
||
if (!GetImage(RM))
|
||
{
|
||
return pipeiOK;
|
||
}
|
||
|
||
|
||
//去畸变
|
||
// DeDistortion();
|
||
try
|
||
{
|
||
//读取模型文件
|
||
if (!RM.MyROI.ReadFeatureModel())
|
||
{
|
||
ShowTextPer("读取特征模型文件失败", "red");
|
||
return pipeiOK;
|
||
}
|
||
|
||
RM.Instantiation();
|
||
//截取矩形选框中的图像
|
||
HOperatorSet.ReduceDomain(Ho_Image, RM.ho_RectangleModel, out ho_ImageReduced);
|
||
//识别图像特征
|
||
HOperatorSet.FindShapeModel(ho_ImageReduced, RM.MyROI.hv_ModelID, (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), RM.MyROI.RecognitionCoefficient, 1, 0.5, (new HTuple("least_squares")).TupleConcat("max_deformation 5"), 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
|
||
|
||
//HOperatorSet.FindShapeModel(ho_ImageReduced, RM.MyROI.hv_ModelID, (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), RM.MyROI.RecognitionCoefficient, 1, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
|
||
|
||
if (hv_Score == null || hv_Score.TupleLength() <= 0)
|
||
{
|
||
ShowTextPer("没有找到匹配的特征", "red");
|
||
return pipeiOK;
|
||
}
|
||
else
|
||
{
|
||
|
||
//模型将它转换成图像的轮廓
|
||
|
||
HOperatorSet.GetShapeModelContours(out ho_ModelContours, RM.MyROI.hv_ModelID, 1);
|
||
for (int i = 0; i <= hv_Score.TupleLength() - 1; i++)
|
||
{
|
||
HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);
|
||
HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_Angle.TupleSelect(i), 0, 0, out hv_HomMat2D);
|
||
HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_Row.TupleSelect(i), hv_Column.TupleSelect(i), out hv_HomMat2D);
|
||
|
||
HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D);
|
||
|
||
SetDraw("margin");
|
||
//显示矩形区域
|
||
ShowObj(RM.ho_RectangleModel, "blue");
|
||
|
||
//显示特征
|
||
ShowObj(ho_TransContours, "green");
|
||
|
||
}
|
||
RM.MyROI.ModelFeaturePiont.PixelRow = hv_Row.TupleSelect(0);
|
||
RM.MyROI.ModelFeaturePiont.PixelColumn = hv_Column.TupleSelect(0);
|
||
|
||
|
||
|
||
//弧度转角度
|
||
RM.MyROI.ModelFeaturePiont.AngleU = Convert.ToDecimal((double)hv_Angle.TupleSelect(0).TupleDeg());
|
||
|
||
|
||
|
||
#region 标准标定文件读取
|
||
//读取外部标定文件
|
||
//string ParametersPath = @"Calibration/" + MachineObj.MachineName + SelectModel.ID + MachineObj.SoftID + "_CameraParameters.dat"; //相机内参保存路径
|
||
//string PosePath= @"Calibration/" + MachineObj.MachineName + SelectModel.ID + MachineObj.SoftID + "_CameraPose.dat"; //姿态参数
|
||
|
||
// HTuple hv_X_World = null, hv_Y_World = null;
|
||
//if (File.Exists(ParametersPath)&&File.Exists(PosePath))
|
||
//{
|
||
// if (WhetherReadFile)
|
||
// {
|
||
// HOperatorSet.ReadCamPar(ParametersPath, out hv_ReadCameraParameters); //读取相机内参
|
||
// HOperatorSet.ReadPose(PosePath, out hv_ReadPose); //读取相机外参
|
||
//WhetherReadFile = false;
|
||
// }
|
||
|
||
// //像素坐标转换成世界坐标
|
||
// HOperatorSet.ImagePointsToWorldPlane(hv_ReadCameraParameters, hv_ReadPose, RM.CharacteristicsROI.ModelFeaturePiont.DeviationX, RM.CharacteristicsROI.ModelFeaturePiont.DeviationY, 0.001, out hv_X_World, out hv_Y_World);
|
||
|
||
// RM.CharacteristicsROI.ModelFeaturePiont.DeviationX = hv_X_World;
|
||
// RM.CharacteristicsROI.ModelFeaturePiont.DeviationY = hv_Y_World;
|
||
|
||
// HOperatorSet.WriteString(HWindowWPF.HalconWindow, "世界坐标" + "X:" + RM.CharacteristicsROI.ModelFeaturePiont.DeviationX + ",Y:" + RM.CharacteristicsROI.ModelFeaturePiont.DeviationY + ",U:" + RM.CharacteristicsROI.ModelFeaturePiont.AngleU);
|
||
//}
|
||
//else
|
||
//{
|
||
// HOperatorSet.WriteString(HWindowWPF.HalconWindow,"像素坐标"+ "X:" + RM.CharacteristicsROI.ModelFeaturePiont.DeviationX + ",Y:" + RM.CharacteristicsROI.ModelFeaturePiont.DeviationY + ",U:" + RM.CharacteristicsROI.ModelFeaturePiont.AngleU);
|
||
//}
|
||
#endregion
|
||
|
||
|
||
//读取已标定文件
|
||
if (RM.ReadCalibrationPar())
|
||
{
|
||
HTuple PixelRow = null, PixelColumn = null, hv_RefRow = null, hv_RefColumn = null;
|
||
//像素坐标转换成世界坐标
|
||
HOperatorSet.AffineTransPoint2d(RM.hv_JiuDianCalibrationPar, (HTuple)RM.MyROI.ModelFeaturePiont.PixelRow, (HTuple)RM.MyROI.ModelFeaturePiont.PixelColumn, out PixelRow, out PixelColumn);
|
||
HOperatorSet.AffineTransPoint2d(RM.hv_JiuDianCalibrationPar, (HTuple)RM.MyROI.ModelFeaturePiont.hv_RefRow, (HTuple)RM.MyROI.ModelFeaturePiont.hv_RefColumn, out hv_RefRow, out hv_RefColumn);
|
||
|
||
RM.MyROI.ModelFeaturePiont.DeviationX = Convert.ToDecimal((double)(PixelColumn - hv_RefColumn));
|
||
RM.MyROI.ModelFeaturePiont.DeviationY = Convert.ToDecimal((double)(PixelRow - hv_RefRow));
|
||
|
||
|
||
|
||
ShowText("世界坐标" + "X:" + Math.Round(RM.MyROI.ModelFeaturePiont.DeviationX, 4) + ",Y:" + Math.Round(RM.MyROI.ModelFeaturePiont.DeviationY, 4) + ",U:" + Math.Round(RM.MyROI.ModelFeaturePiont.AngleU, 4), "green");
|
||
|
||
}
|
||
else
|
||
{
|
||
decimal x = Convert.ToDecimal(RM.MyROI.ModelFeaturePiont.hv_RefRow - RM.MyROI.ModelFeaturePiont.PixelRow);
|
||
decimal y = Convert.ToDecimal(RM.MyROI.ModelFeaturePiont.hv_RefColumn - RM.MyROI.ModelFeaturePiont.PixelColumn);
|
||
|
||
ShowText("像素坐标" + "X:" + Math.Round(x, 4) + ",Y:" + Math.Round(y, 4) + ",U:" + Math.Round(RM.MyROI.ModelFeaturePiont.AngleU, 4), "green");
|
||
|
||
}
|
||
|
||
pipeiOK = true;
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
ShowTextPer("没有找到匹配的特征", "red");
|
||
}
|
||
|
||
|
||
return pipeiOK;
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 有无检查
|
||
/// <summary>
|
||
/// 有无检查
|
||
/// </summary>
|
||
/// <param name="RM"></param>
|
||
/// <returns></returns>
|
||
public bool YouwuCheck(V_RectangleModel RM)
|
||
{
|
||
bool pipeiOK = false;
|
||
|
||
if (RM == null || RM.ID <= 0)
|
||
{
|
||
|
||
return pipeiOK;
|
||
}
|
||
|
||
HObject ho_ImageReduced, ho_ModelContours, ho_TransContours, ho_ImageROI;
|
||
HTuple hv_HomMat2D = null;
|
||
HTuple hv_Row = new HTuple();
|
||
HTuple hv_Column = new HTuple(), hv_Angle = new HTuple();
|
||
HTuple hv_Score = new HTuple(), hv_I = new HTuple(), hv_RectificationHomMat2D = new HTuple();
|
||
|
||
HOperatorSet.GenEmptyObj(out ho_ImageReduced);
|
||
HOperatorSet.GenEmptyObj(out ho_ModelContours);
|
||
HOperatorSet.GenEmptyObj(out ho_TransContours);
|
||
HOperatorSet.GenEmptyObj(out ho_ImageROI);
|
||
|
||
//SetLight(RM);
|
||
////1,获取图像
|
||
//if (!GetImage(RM))
|
||
//{
|
||
// return pipeiOK;
|
||
//}
|
||
|
||
//CloseSetLight(RM);
|
||
|
||
//去畸变
|
||
// DeDistortion();
|
||
try
|
||
{
|
||
//读取模型文件
|
||
if (!RM.MyROI.ReadFeatureModel())
|
||
{
|
||
// ShowTextPer("读取特征模型文件失败", "red");
|
||
//显示矩形区域
|
||
ShowObj(RM.ho_RectangleModel, "red");
|
||
return pipeiOK;
|
||
}
|
||
|
||
RM.Instantiation();
|
||
//截取矩形选框中的图像
|
||
HOperatorSet.ReduceDomain(Ho_Image, RM.ho_RectangleModel, out ho_ImageReduced);
|
||
//识别图像特征
|
||
HOperatorSet.FindShapeModel(ho_ImageReduced, RM.MyROI.hv_ModelID, (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), RM.MyROI.RecognitionCoefficient, 1, 0.5, (new HTuple("least_squares")).TupleConcat("max_deformation 5"), 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
|
||
//HOperatorSet.FindShapeModel(ho_ImageReduced, RM.MyROI.hv_ModelID, (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), RM.MyROI.RecognitionCoefficient, 1, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
|
||
|
||
if (hv_Score == null || hv_Score.TupleLength() <= 0)
|
||
{
|
||
//ShowTextPer("没有找到匹配的特征", "red");
|
||
//显示矩形区域
|
||
ShowObj(RM.ho_RectangleModel, "red");
|
||
return pipeiOK;
|
||
}
|
||
else
|
||
{
|
||
|
||
//模型将它转换成图像的轮廓
|
||
|
||
HOperatorSet.GetShapeModelContours(out ho_ModelContours, RM.MyROI.hv_ModelID, 1);
|
||
for (int i = 0; i <= hv_Score.TupleLength() - 1; i++)
|
||
{
|
||
HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);
|
||
HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_Angle.TupleSelect(i), 0, 0, out hv_HomMat2D);
|
||
HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_Row.TupleSelect(i), hv_Column.TupleSelect(i), out hv_HomMat2D);
|
||
|
||
HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D);
|
||
|
||
SetDraw("margin");
|
||
//显示矩形区域
|
||
ShowObj(RM.ho_RectangleModel, "blue");
|
||
|
||
//显示特征
|
||
ShowObj(ho_TransContours, "green");
|
||
|
||
}
|
||
pipeiOK = true;
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
ShowTextPer("没有找到匹配的特征", "red");
|
||
}
|
||
|
||
|
||
return pipeiOK;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量有无检查
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool BathCheck()
|
||
{
|
||
bool bl = true;
|
||
|
||
|
||
DateTime qututime = new DateTime();
|
||
bool qutubl = true;
|
||
int qutuNumber = 0;
|
||
while (qutubl)
|
||
{
|
||
if (qututime.AddMilliseconds(1000) < DateTime.Now)
|
||
{
|
||
//1,获取图像
|
||
if (!GetImage())
|
||
{
|
||
if (qutuNumber >= 3)
|
||
{
|
||
return bl;
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
qutubl = false;
|
||
break;
|
||
}
|
||
}
|
||
|
||
qutuNumber++;
|
||
qututime = DateTime.Now;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
foreach (var item in VisualEngineeringList[0].VisualEngineeringItemsList)
|
||
{
|
||
|
||
bl = YouwuCheck(item.MyRectangleModel) == false ? false : bl;
|
||
|
||
}
|
||
|
||
if (bl)
|
||
{
|
||
ShowText("OK", "green");
|
||
}
|
||
else
|
||
{
|
||
ShowText("NG", "red");
|
||
}
|
||
|
||
|
||
|
||
return bl;
|
||
}
|
||
#endregion
|
||
|
||
#region 保存参数
|
||
|
||
/// <summary>
|
||
/// 更新ROI
|
||
/// </summary>
|
||
/// <param name="ROI"></param>
|
||
/// <returns></returns>
|
||
public bool SaveROI(V_ROI ROI)
|
||
{
|
||
bool bl = false;
|
||
if (Vdal.UpdateROI(ROI) > 0)
|
||
{
|
||
bl = true;
|
||
}
|
||
return bl;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存所有参数
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public bool SaveAll(V_RectangleModel RM)
|
||
{
|
||
bool bl = false;
|
||
if (Vdal.UpdateRectangleModel(RM) > 0)
|
||
{
|
||
RM.MyROI.ModelID = RM.ID;
|
||
if (Vdal.UpdateROI(RM.MyROI) > 0)
|
||
{
|
||
bl = true;
|
||
}
|
||
}
|
||
|
||
return bl;
|
||
}
|
||
#endregion
|
||
|
||
#region 图像显示方法
|
||
/// <summary>
|
||
/// 图像显示队列集合
|
||
/// </summary>
|
||
public List<HObject> HobjectList { get; set; }
|
||
/// <summary>
|
||
/// 显示内容
|
||
/// </summary>
|
||
public void ShowObj(HObject Hobj)
|
||
{
|
||
if (HobjectIsNull(Hobj))
|
||
{
|
||
return;
|
||
}
|
||
HOperatorSet.SetSystem("flush_graphic", "false");
|
||
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.DispObj(Hobj, this.HWindowWPF.HalconWindow);
|
||
}
|
||
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.DispObj(Hobj, this.HWindowWPF2.HalconWindow);
|
||
}
|
||
//如果是连续采图 则显示十字准星
|
||
if (ContinuousAcquisition)
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF.HalconWindow, "green");
|
||
HOperatorSet.DispObj(ho_Cross, this.HWindowWPF.HalconWindow);
|
||
}
|
||
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF2.HalconWindow, "green");
|
||
HOperatorSet.DispObj(ho_Cross, this.HWindowWPF2.HalconWindow);
|
||
}
|
||
}
|
||
HOperatorSet.SetSystem("flush_graphic", "true");
|
||
}
|
||
/// <summary>
|
||
/// 显示内容
|
||
/// </summary>
|
||
/// <param name="Hobj"></param>
|
||
/// <param name="hv_Color"></param>
|
||
public void ShowObj(HObject Hobj, string hv_Color)
|
||
{
|
||
if (HobjectIsNull(Hobj))
|
||
{
|
||
return;
|
||
}
|
||
HOperatorSet.SetSystem("flush_graphic", "false");
|
||
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(Hobj, this.HWindowWPF.HalconWindow);
|
||
}
|
||
|
||
if (HWindowWPF2 != null)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(Hobj, this.HWindowWPF2.HalconWindow);
|
||
}
|
||
|
||
//如果是连续采图 则显示十字准星
|
||
if (ContinuousAcquisition)
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(ho_Cross, this.HWindowWPF.HalconWindow);
|
||
}
|
||
|
||
if (HWindowWPF2 != null)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(ho_Cross, this.HWindowWPF2.HalconWindow);
|
||
}
|
||
}
|
||
|
||
HOperatorSet.SetSystem("flush_graphic", "true");
|
||
}
|
||
/// <summary>
|
||
/// 显示内容
|
||
/// </summary>
|
||
/// <param name="Hobj"></param>
|
||
/// <param name="hv_Color"></param>
|
||
public void ShowObj(HObject Hobj, int hv_Color)
|
||
{
|
||
if (HobjectIsNull(Hobj))
|
||
{
|
||
return;
|
||
}
|
||
HOperatorSet.SetSystem("flush_graphic", "false");
|
||
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(Hobj, this.HWindowWPF.HalconWindow);
|
||
}
|
||
|
||
if (HWindowWPF2 != null)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(Hobj, this.HWindowWPF2.HalconWindow);
|
||
}
|
||
|
||
//如果是连续采图 则显示十字准星
|
||
if (ContinuousAcquisition)
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(ho_Cross, this.HWindowWPF.HalconWindow);
|
||
}
|
||
|
||
if (HWindowWPF2 != null)
|
||
{
|
||
SetColored(hv_Color);
|
||
HOperatorSet.DispObj(ho_Cross, this.HWindowWPF2.HalconWindow);
|
||
}
|
||
}
|
||
|
||
HOperatorSet.SetSystem("flush_graphic", "true");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清空显示控件内的所有内容
|
||
/// </summary>
|
||
public void ClearWindowAndData()
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.ClearWindow(HWindowWPF.HalconWindow);
|
||
}
|
||
if (HWindowWPF2 != null)
|
||
{
|
||
HOperatorSet.ClearWindow(HWindowWPF2.HalconWindow);
|
||
}
|
||
HobjectList = new List<HObject>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清除需要显示的队列数据
|
||
/// </summary>
|
||
public void ClearObjData()
|
||
{
|
||
if (HobjectList.Count > 0)
|
||
{
|
||
HobjectList = new List<HObject>();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刷新显示
|
||
/// </summary>
|
||
public void RefeshWindow()
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HWindowWPF.HalconWindow.ClearWindow();
|
||
}
|
||
|
||
foreach (HObject hobj in HobjectList)
|
||
{
|
||
HOperatorSet.DispObj(hobj, this.HWindowWPF.HalconWindow);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 显示文本信息
|
||
/// <summary>
|
||
/// 显示文本信息
|
||
/// </summary>
|
||
/// <param name="hv_String"></param>
|
||
/// <param name="hv_Row"></param>
|
||
/// <param name="hv_Column"></param>
|
||
/// <param name="hv_Color"></param>
|
||
public void ShowText(HTuple hv_String, HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, int fontSize = 20)
|
||
{
|
||
SetFont("楷体", fontSize, "true", "false");
|
||
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF.HalconWindow, hv_Color);
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF.HalconWindow, hv_Row, hv_Column);
|
||
|
||
HOperatorSet.WriteString(HWindowWPF.HalconWindow, str);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF.HalconWindow, hv_Row, hv_Column);
|
||
HOperatorSet.WriteString(HWindowWPF.HalconWindow, hv_String);
|
||
}
|
||
}
|
||
|
||
try
|
||
{
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF2.HalconWindow, hv_Color);
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF2.HalconWindow, hv_Row, hv_Column);
|
||
|
||
HOperatorSet.WriteString(HWindowWPF2.HalconWindow, str);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF2.HalconWindow, hv_Row, hv_Column);
|
||
HOperatorSet.WriteString(HWindowWPF2.HalconWindow, hv_String);
|
||
}
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 显示文本信息
|
||
/// </summary>
|
||
/// <param name="hv_WindowHandle"></param>
|
||
/// <param name="hv_String"></param>
|
||
/// <param name="hv_Color"></param>
|
||
public void ShowText(HTuple hv_String, HTuple hv_Color, int fontSize = 20)
|
||
{
|
||
SetFont("楷体", fontSize, "true", "false");
|
||
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF.HalconWindow, hv_Color);
|
||
HTuple hv_Row = 12, hv_Column = 12;
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF.HalconWindow, hv_Row, hv_Column);
|
||
|
||
HOperatorSet.WriteString(HWindowWPF.HalconWindow, str);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF.HalconWindow, hv_Row, hv_Column);
|
||
HOperatorSet.WriteString(HWindowWPF.HalconWindow, hv_String);
|
||
}
|
||
}
|
||
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF2.HalconWindow, hv_Color);
|
||
HTuple hv_Row = 12, hv_Column = 12;
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF2.HalconWindow, hv_Row, hv_Column);
|
||
|
||
HOperatorSet.WriteString(HWindowWPF2.HalconWindow, str);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.SetTposition(HWindowWPF2.HalconWindow, hv_Row, hv_Column);
|
||
HOperatorSet.WriteString(HWindowWPF2.HalconWindow, hv_String);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 显示文本框文本
|
||
/// </summary>
|
||
public void ShowTextPer(HTuple hv_String, double hv_Row, double hv_Column, HTuple hv_Color)
|
||
{
|
||
SetFont("楷体", 30, "true", "false");
|
||
HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
|
||
hv_Row = hv_Row <= 0 ? 12 : hv_Row;
|
||
hv_Column = hv_Column <= 0 ? 12 : hv_Column;
|
||
|
||
|
||
//不显示边框
|
||
hv_GenParamName = "shadow";
|
||
hv_GenParamValue = "false";
|
||
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF.HalconWindow, str, "window", hv_Row, hv_Column, hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF.HalconWindow, hv_String, "window", hv_Row, hv_Column, hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
}
|
||
}
|
||
|
||
|
||
try
|
||
{
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF2.HalconWindow, str, "window", hv_Row, hv_Column, hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF2.HalconWindow, hv_String, "window", hv_Row, hv_Column, hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
}
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
}
|
||
|
||
|
||
////不显示文本框
|
||
//hv_GenParamName = "box";
|
||
//hv_GenParamValue = "false";
|
||
//HOperatorSet.DispText(HWindowWPF.HalconWindow, "测试", "window", "top", "left", "red", hv_GenParamName, hv_GenParamValue);
|
||
|
||
////背景色 蓝色 不显示边框
|
||
//hv_GenParamName = "box_color";
|
||
//hv_GenParamValue = "blue";
|
||
//hv_GenParamName = hv_GenParamName.TupleConcat("shadow");
|
||
//hv_GenParamValue = hv_GenParamValue.TupleConcat("false");
|
||
|
||
//HOperatorSet.DispText(HWindowWPF.HalconWindow, "测试", "window", 200, 200, "red", hv_GenParamName, hv_GenParamValue);
|
||
|
||
////背景色 蓝色 边框绿色
|
||
//hv_GenParamName = "box_color";
|
||
//hv_GenParamValue = "blue";
|
||
//hv_GenParamName = hv_GenParamName.TupleConcat("shadow_color");
|
||
//hv_GenParamValue = hv_GenParamValue.TupleConcat("green");
|
||
//HOperatorSet.DispText(HWindowWPF.HalconWindow, "测试", "window", 300, 300, "red", hv_GenParamName, hv_GenParamValue);
|
||
}
|
||
/// <summary>
|
||
/// 显示文本框文本
|
||
/// </summary>
|
||
/// <param name="hv_String"></param>
|
||
/// <param name="hv_Color"></param>
|
||
public void ShowTextPer(HTuple hv_String, HTuple hv_Color)
|
||
{
|
||
SetFont("楷体", 30, "true", "false");
|
||
HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
|
||
|
||
//不显示边框
|
||
hv_GenParamName = "shadow";
|
||
hv_GenParamValue = "false";
|
||
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF.HalconWindow, str, "window", "top", "left", hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF.HalconWindow, hv_String, "window", 12, 12, hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
}
|
||
}
|
||
|
||
try
|
||
{
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
if (hv_String.Length > 1)
|
||
{
|
||
foreach (string str in hv_String.ToSArr())
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF2.HalconWindow, str, "window", "top", "left", hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
hv_Row = hv_Row + 80;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HOperatorSet.DispText(HWindowWPF2.HalconWindow, hv_String, "window", 12, 12, hv_Color, hv_GenParamName, hv_GenParamValue);
|
||
}
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
}
|
||
}
|
||
|
||
#region 设置字体
|
||
/// <summary>
|
||
/// 设置字体
|
||
/// </summary>
|
||
/// <param name="hv_Font">字体</param>
|
||
/// <param name="hv_Size">字体大小</param>
|
||
/// <param name="hv_Bold">是否粗体</param>
|
||
/// <param name="hv_Slant">是否倾斜</param>
|
||
public void SetFont(string hv_Font, int hv_Size, string hv_Bold, string hv_Slant)
|
||
{
|
||
try
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HTuple hv_OS = null, hv_Fonts = new HTuple();
|
||
string hv_Style = "";
|
||
string FontStyle = "";
|
||
HTuple hv_FontList = null;
|
||
HOperatorSet.QueryFont(HWindowWPF.HalconWindow, out hv_FontList);//获取系统所有字体
|
||
HOperatorSet.GetSystem("operating_system", out hv_OS); //获取系统字体
|
||
|
||
if (hv_FontList.ToSArr().Contains(hv_Font))
|
||
{
|
||
FontStyle = hv_Font;
|
||
}
|
||
else
|
||
{
|
||
FontStyle = hv_FontList[0];
|
||
}
|
||
|
||
FontStyle = FontStyle + "-";
|
||
//字体是否需要倾斜
|
||
if (hv_Bold == "true")
|
||
{
|
||
hv_Style = hv_Style + "Bold";
|
||
}
|
||
|
||
if (hv_Slant == "true")
|
||
{
|
||
hv_Style = hv_Style + "Italic";
|
||
}
|
||
if (hv_Style == "")
|
||
{
|
||
hv_Style = "Normal";
|
||
}
|
||
FontStyle = FontStyle + hv_Style;
|
||
FontStyle = FontStyle + "-";
|
||
|
||
if (hv_Size <= 0)
|
||
{
|
||
hv_Size = 16;
|
||
}
|
||
if (hv_OS.ToString().Substring(0, 2) == "Win")
|
||
{
|
||
hv_Size = (int)(1.13677 * hv_Size);
|
||
}
|
||
|
||
|
||
FontStyle = FontStyle + hv_Size;
|
||
|
||
HOperatorSet.SetFont(HWindowWPF.HalconWindow, FontStyle);
|
||
}
|
||
|
||
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
HTuple hv_OS = null, hv_Fonts = new HTuple();
|
||
string hv_Style = "";
|
||
string FontStyle = "";
|
||
HTuple hv_FontList = null;
|
||
HOperatorSet.QueryFont(HWindowWPF2.HalconWindow, out hv_FontList);//获取系统所有字体
|
||
HOperatorSet.GetSystem("operating_system", out hv_OS); //获取系统字体
|
||
|
||
if (hv_FontList.ToSArr().Contains(hv_Font))
|
||
{
|
||
FontStyle = hv_Font;
|
||
}
|
||
else
|
||
{
|
||
FontStyle = hv_FontList[0];
|
||
}
|
||
|
||
FontStyle = FontStyle + "-";
|
||
//字体是否需要倾斜
|
||
if (hv_Bold == "true")
|
||
{
|
||
hv_Style = hv_Style + "Bold";
|
||
}
|
||
|
||
if (hv_Slant == "true")
|
||
{
|
||
hv_Style = hv_Style + "Italic";
|
||
}
|
||
if (hv_Style == "")
|
||
{
|
||
hv_Style = "Normal";
|
||
}
|
||
FontStyle = FontStyle + hv_Style;
|
||
FontStyle = FontStyle + "-";
|
||
|
||
if (hv_Size <= 0)
|
||
{
|
||
hv_Size = 16;
|
||
}
|
||
if (hv_OS.ToString().Substring(0, 2) == "Win")
|
||
{
|
||
hv_Size = (int)(1.13677 * hv_Size);
|
||
}
|
||
|
||
|
||
FontStyle = FontStyle + hv_Size;
|
||
|
||
HOperatorSet.SetFont(HWindowWPF2.HalconWindow, FontStyle);
|
||
}
|
||
|
||
|
||
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
throw;
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
|
||
#endregion
|
||
|
||
#region 设置窗体前景色
|
||
/// <summary>
|
||
/// 设置窗体前景色
|
||
/// </summary>
|
||
public void SetColored(string hv_Color)
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF.HalconWindow, hv_Color);
|
||
}
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColor(HWindowWPF2.HalconWindow, hv_Color);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置窗体前景色
|
||
/// </summary>
|
||
/// <param name="hv_Color"></param>
|
||
public void SetColored(int hv_Color)
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
|
||
HOperatorSet.SetColored(HWindowWPF.HalconWindow, hv_Color);
|
||
|
||
}
|
||
if (HWindowWPF2.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetColored(HWindowWPF2.HalconWindow, hv_Color);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 设置画笔样式(填充模式 "fill" 边框模式:"margin")
|
||
/// <summary>
|
||
/// 设置画笔样式(填充模式 "fill" 边框模式:"margin")
|
||
/// </summary>
|
||
/// <param name="DrawStyle"></param>
|
||
public void SetDraw(string DrawStyle)
|
||
{
|
||
if (HWindowWPF.HalconID.ToInt32() > 0)
|
||
{
|
||
HOperatorSet.SetDraw(HWindowWPF.HalconWindow, DrawStyle);
|
||
}
|
||
if (HWindowWPF2 != null)
|
||
{
|
||
HOperatorSet.SetDraw(HWindowWPF2.HalconWindow, DrawStyle);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 设置工状态光源亮度
|
||
DateTime tempTime;
|
||
/// <summary>
|
||
/// 设置工状态光源亮度
|
||
/// </summary>
|
||
/// <param name="RM"></param>
|
||
public void SetLight(V_RectangleModel RM)
|
||
{
|
||
try
|
||
{
|
||
if (LightCOM == null)
|
||
{
|
||
return;
|
||
}
|
||
if (RM.PassagewayBrightness1 > 0)
|
||
{
|
||
LightCOM.Send("SA" + string.Format("{0:d3}", RM.PassagewayBrightness1) + "#");
|
||
}
|
||
if (RM.PassagewayBrightness2 > 0)
|
||
{
|
||
LightCOM.Send("SB" + string.Format("{0:d3}", RM.PassagewayBrightness2) + "#");
|
||
}
|
||
if (RM.PassagewayBrightness3 > 0)
|
||
{
|
||
LightCOM.Send("SC" + string.Format("{0:d3}", RM.PassagewayBrightness3) + "#");
|
||
}
|
||
if (RM.PassagewayBrightness4 > 0)
|
||
{
|
||
LightCOM.Send("SD" + string.Format("{0:d3}", RM.PassagewayBrightness4) + "#");
|
||
}
|
||
tempTime = DateTime.Now;
|
||
|
||
while (true)
|
||
{
|
||
if (DateTime.Now > tempTime.AddMilliseconds(200))
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ShowTextPer("光源设置失败" + ex.Message, "red");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 关闭已设置的光源状态
|
||
/// <summary>
|
||
/// 关闭已设置的光源状态
|
||
/// </summary>
|
||
/// <param name="RM"></param>
|
||
public void CloseSetLight(V_RectangleModel RM)
|
||
{
|
||
try
|
||
{
|
||
if (LightCOM == null)
|
||
{
|
||
return;
|
||
}
|
||
if (RM.PassagewayBrightness1 > 0)
|
||
{
|
||
LightCOM.Send("SA" + string.Format("{0:d3}", 0) + "#");
|
||
}
|
||
if (RM.PassagewayBrightness2 > 0)
|
||
{
|
||
LightCOM.Send("SB" + string.Format("{0:d3}", 0) + "#");
|
||
}
|
||
if (RM.PassagewayBrightness3 > 0)
|
||
{
|
||
LightCOM.Send("SC" + string.Format("{0:d3}", 0) + "#");
|
||
}
|
||
if (RM.PassagewayBrightness4 > 0)
|
||
{
|
||
LightCOM.Send("SD" + string.Format("{0:d3}", 0) + "#");
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ShowTextPer("光源设置失败" + ex.Message, "red");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 记录异常信息
|
||
|
||
byte[] data { get; set; }
|
||
|
||
/// <summary>
|
||
/// 文件夹路径
|
||
/// </summary>
|
||
string path { get; set; }
|
||
/// <summary>
|
||
/// 文件路径
|
||
/// </summary>
|
||
string paths { get; set; }
|
||
/// <summary>
|
||
/// 记录信息对象
|
||
/// </summary>
|
||
RecordInfo info { get; set; }
|
||
/// <summary>
|
||
/// 打开的文件流对象
|
||
/// </summary>
|
||
FileStream FS { get; set; }
|
||
|
||
/// <summary>
|
||
/// 异常信息集合
|
||
/// </summary>
|
||
public ObservableCollection<RecordInfo> ExcepList { get; set; }
|
||
/// <summary>
|
||
/// 记录日志信息到文件
|
||
/// </summary>
|
||
/// <param name="showMag">描述文字</param>
|
||
/// <param name="por">操作建议</param>
|
||
public void RecordLog(string showMag, string por)
|
||
{
|
||
try
|
||
{
|
||
info = new RecordInfo() { Number = ExcepList.Count() + 1, HappenTime = DateTime.Now, Describe = showMag, Proposal = por };
|
||
//异步新增项 这是微软的BUG 只能这么麻烦了
|
||
HWindowWPF.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ParameterizedThreadStart(AddItem), info);
|
||
|
||
path = @"E:\\SystemLog";
|
||
paths = @"E:\\SystemLog\" + DateTime.Now.ToString("D") + ".txt";
|
||
//创建文件夹
|
||
if (!Directory.Exists(path))
|
||
{
|
||
Directory.CreateDirectory(path);
|
||
}
|
||
|
||
//打开或创建文件
|
||
FS = File.Open(paths, FileMode.Append);
|
||
|
||
//获得字节数组
|
||
|
||
data = System.Text.Encoding.Default.GetBytes(info.Number + "-" + info.HappenTime.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "-" + info.Describe + "-" + info.Proposal + "\r\n");
|
||
//开始写入
|
||
FS.Write(data, 0, data.Length);
|
||
//清空缓冲区、关闭流
|
||
FS.Flush();
|
||
FS.Close();
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
}
|
||
|
||
}
|
||
|
||
void AddItem(object info)
|
||
{
|
||
ExcepList.Add((RecordInfo)info);
|
||
}
|
||
#endregion
|
||
|
||
#region 保存生产过程的图片
|
||
/// <summary>
|
||
/// 保存生产过程的图片
|
||
/// </summary>
|
||
/// <param name="OKorNG"></param>
|
||
public void SaveImagePro(bool OKorNG)
|
||
{
|
||
if (Camera.SaveImangeOK && OKorNG)
|
||
{
|
||
HImage imagetemp = HWindowWPF.HalconWindow.DumpWindowImage(); //视觉窗口图像
|
||
//创建文件夹
|
||
if (!Directory.Exists(@"D:/ImageOK/" + Camera.CamName + "/"))
|
||
{
|
||
Directory.CreateDirectory(@"D:/ImageOK/" + Camera.CamName + "/");
|
||
}
|
||
|
||
string filePath = @"D:/ImageOK/" + Camera.CamName + "/" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
|
||
|
||
HOperatorSet.WriteImage(imagetemp, "jpg", 0, filePath);
|
||
|
||
imagetemp.Dispose();
|
||
}
|
||
else if (Camera.SaveImangeNG && !OKorNG)
|
||
{
|
||
//HImage imagetemp =HWindowWPF.HalconWindow.DumpWindowImage(); //视觉窗口图像
|
||
//创建文件夹
|
||
if (!Directory.Exists(@"D:/ImageNG/" + Camera.CamName + "/"))
|
||
{
|
||
Directory.CreateDirectory(@"D:/ImageNG/" + Camera.CamName + "/");
|
||
}
|
||
|
||
string filePath = @"D:/ImageNG/" + Camera.CamName + "/" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
|
||
|
||
HOperatorSet.WriteImage(Ho_Image, "bmp", 0, filePath);
|
||
// imagetemp.Dispose();
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
}
|
||
}
|