185 lines
6.3 KiB
C#
185 lines
6.3 KiB
C#
using Aurora.Api;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using MilCommon;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VisionCommon;
|
|
|
|
namespace MilPattern {
|
|
public class MultiLineFinder : 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 MultiLineFinder(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);
|
|
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 = MilMeasureLine.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 MilMeasureLine.GetRunParam(_contextId);
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
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(
|
|
//[SR] Mil내부에서 병렬에 대한 대비가 있는듯함 아래코드는 충동하지 않음
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
Parallel.ForEach(patternResult.Results, baseResult => {
|
|
var lineFinderResult = baseResult as LineFinderResult;
|
|
if (lineFinderResult.Status == eFoundStatus.FoundOK) {
|
|
MilMeasureLine.Draw((MIL_ID)lineFinderResult.ContextId, cameraDisplay as IMilCameraDisplay);
|
|
MilMeasureLine.DrawTextForMulti(lineFinderResult, cameraDisplay as IMilCameraDisplay);
|
|
}
|
|
});
|
|
//[SR] 병렬로 인한 충돌이 있다면 아래 코드로 원복 해야함. 25/02/20
|
|
//foreach (BaseResult baseResult in patternResult.Results) {
|
|
|
|
// // 안찾아져도 됨 patternResult.Status == eFoundStatus.FoundOK || patternResult.Status == eFoundStatus.ForceOK
|
|
// var lineFinderResult = baseResult as LineFinderResult;
|
|
// if (lineFinderResult.Status == eFoundStatus.FoundOK) {
|
|
// MilMeasureLine.Draw((MIL_ID)lineFinderResult.ContextId, cameraDisplay as IMilCameraDisplay);
|
|
// MilMeasureLine.DrawTextForMulti(lineFinderResult, 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
|
|
}
|
|
|
|
} |