189 lines
5.9 KiB
C#
189 lines
5.9 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;
|
|
|
|
namespace MilPattern {
|
|
public class LineFinder : PatternBase, IPatternFinder, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".mrk";
|
|
private MrkParamExt _paramExt = null;
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
private MIL_ID _contextId = MIL.M_NULL;
|
|
|
|
public LineFinder(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"LineFinder_{patternInfo.GetPatternIndex()}");
|
|
InitContext();
|
|
}
|
|
|
|
private void InitContext() {
|
|
if (E1.OK != Load()) {
|
|
Create();
|
|
}
|
|
}
|
|
|
|
public string GetPatternFilePath() {
|
|
return Path.Combine(new string[] {
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
PatternConstants.FILE_NAME_PATTERN + 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;
|
|
}
|
|
|
|
_contextId = MilMeasureCommon.LoadContext(_patternInfo.MilSystemId, patternFilePath);
|
|
_paramExt = LoadParamExt();
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Save() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (!Directory.Exists(patternDirPath)) {
|
|
Directory.CreateDirectory(patternDirPath);
|
|
}
|
|
|
|
SaveParamExt(_paramExt);
|
|
return MilMeasureCommon.SaveContext(_contextId, patternFilePath);
|
|
}
|
|
|
|
public int Create() {
|
|
FreeContexts();
|
|
_contextId = MilMeasureLine.CreateContext(_patternInfo.MilSystemId);
|
|
_paramExt = LoadParamExt();
|
|
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() {
|
|
var runParam = MilMeasureLine.GetRunParam(_contextId) as LineFinderRunParam;
|
|
runParam.MrkParamExt = _paramExt.Clone() as MrkParamExt;
|
|
return runParam;
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
_paramExt = ((LineFinderRunParam)runParam).MrkParamExt.Clone() as MrkParamExt;
|
|
return MilMeasureLine.SetRunParam(_contextId, runParam);
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
return MilMeasureLine.Find(_contextId, (MIL_ID)image);
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
return MilMeasureLine.Find(_contextId, (MIL_ID)image, findRoi);
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return MilMeasureLine.GetNotFoundPatternResult();
|
|
}
|
|
|
|
public object GetTool() {
|
|
return _contextId;
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
MilMeasureLine.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);
|
|
|
|
MIL.MgraRectAngle(
|
|
MIL.M_DEFAULT, thumnailImage,
|
|
center.X,
|
|
center.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
|
|
|
|
|
|
#region LineFinder Specific
|
|
public bool IsParamExtExist() {
|
|
return MrkParamExt.Exists(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal MrkParamExt LoadParamExt() {
|
|
return MrkParamExt.Load(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal int SaveParamExt(MrkParamExt mrkParamExt) {
|
|
return MrkParamExt.Save(
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
mrkParamExt);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|