175 lines
5.4 KiB
C#
175 lines
5.4 KiB
C#
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.IO;
|
|
using System.Drawing;
|
|
using Aurora.Api.DataType;
|
|
using System.Collections.Generic;
|
|
|
|
namespace MilPattern {
|
|
public class Caliper : PatternBase, IPatternFinder, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".mrk";
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
private MIL_ID _contextId = MIL.M_NULL;
|
|
|
|
public Caliper(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"Caliper_{patternInfo.GetPatternIndex()}");
|
|
InitContext();
|
|
}
|
|
|
|
private void InitContext() {
|
|
if (E1.OK != Load()) {
|
|
Create();
|
|
}
|
|
}
|
|
|
|
public string GetPatternFilePath(string patternName = PatternConstants.FILE_NAME_PATTERN) {
|
|
return Path.Combine(new string[] {
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
patternName + FILE_EXT_PATTERN
|
|
});
|
|
}
|
|
|
|
public int Load() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
|
|
if (!File.Exists(patternFilePath)) {
|
|
_logger.I($"Pattern File does NOT exist : {patternFilePath}");
|
|
return E2.NOT_FOUND;
|
|
}
|
|
|
|
FreeContexts();
|
|
_contextId = MilMeasureCommon.LoadContext(_patternInfo.MilSystemId, patternFilePath);
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Save() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (!Directory.Exists(patternDirPath)) {
|
|
Directory.CreateDirectory(patternDirPath);
|
|
}
|
|
|
|
return MilMeasureCommon.SaveContext(_contextId, patternFilePath);
|
|
}
|
|
|
|
public int Create() {
|
|
FreeContexts();
|
|
_contextId = MilMeasureWidth.CreateContext(_patternInfo.MilSystemId);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Remove() {
|
|
FreeContexts();
|
|
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (Directory.Exists(patternDirPath)) {
|
|
Directory.Delete(patternDirPath, recursive: true);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public ROI GetFindROI() {
|
|
return MilMeasureCommon.GetFindROI(_contextId);
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
return MilMeasureCommon.SetFindROI(_contextId, findRoi);
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
return MilMeasureWidth.GetRunParam(_contextId);
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
return MilMeasureWidth.SetRunParam(_contextId, runParam);
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
return MilMeasureWidth.Find(_contextId, (MIL_ID)image);
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
return MilMeasureWidth.Find(_contextId, (MIL_ID)image, findRoi);
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return MilMeasureWidth.GetNotFoundPatternResult();
|
|
}
|
|
|
|
public object GetTool() {
|
|
return _contextId;
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
MilMeasureWidth.Draw(_contextId, patternResult, cameraDisplay as IMilCameraDisplay);
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
MIL_ID thumnailImage = MIL.M_NULL;
|
|
try {
|
|
var imageSize = ThumbnailUtil.GetImageSize();
|
|
thumnailImage = MilEnvironment.GetCompatibleImageBuffer(
|
|
imageSize, _patternInfo.MilSystemId);
|
|
MilEnvironment.ClearImage(thumnailImage);
|
|
|
|
var findRoi = GetFindROI();
|
|
var center = MilEnvironment.GetImageCenter(thumnailImage);
|
|
|
|
var offsets = new List<ImagePoint>() {
|
|
new ImagePoint(5, 0),
|
|
new ImagePoint(-5, 0)
|
|
};
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
MIL.MgraRectAngle(
|
|
MIL.M_DEFAULT,
|
|
thumnailImage,
|
|
center.X + offsets[i].X,
|
|
center.Y + offsets[i].Y,
|
|
1,
|
|
imageSize.Height * 1.2,
|
|
0,
|
|
MIL.M_CENTER_AND_DIMENSION + MIL.M_FILLED);
|
|
}
|
|
|
|
MIL.MimRotate(thumnailImage, thumnailImage, findRoi.Angle.Degree,
|
|
MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NEAREST_NEIGHBOR);
|
|
|
|
return MilEnvironment.ConvertToBitmap(thumnailImage);
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
finally {
|
|
MilEnvironment.FreeImage(thumnailImage);
|
|
}
|
|
|
|
return ThumbnailUtil.GetDefaultImage();
|
|
}
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
FreeContexts();
|
|
}
|
|
|
|
private void FreeContexts() {
|
|
MilMeasureCommon.FreeContext(_contextId);
|
|
|
|
_contextId = MIL.M_NULL;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|