166 lines
5.0 KiB
C#
166 lines
5.0 KiB
C#
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.IO;
|
|
using System.Drawing;
|
|
using VisionCommonUI;
|
|
|
|
namespace MilPattern {
|
|
public class Blob : PatternBase, IPatternFinder, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".mblob";
|
|
private BlobParamExt _paramExt = null;
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
private MIL_ID _contextId = MIL.M_NULL;
|
|
private MIL_ID _resultContextId = MIL.M_NULL;
|
|
|
|
public Blob(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"Blob_{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 = MilBlob.LoadContext(_patternInfo.MilSystemId, patternFilePath);
|
|
_resultContextId = MilBlob.CreateResultContext(_contextId);
|
|
_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 MilBlob.SaveContext(_contextId, patternFilePath);
|
|
}
|
|
|
|
public int Create() {
|
|
FreeContexts();
|
|
_contextId = MilBlob.CreateContext(_patternInfo.MilSystemId);
|
|
_resultContextId = MilBlob.CreateResultContext(_contextId);
|
|
_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 _paramExt.FindROI.Clone() as ROI;
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
_paramExt.FindROI = findRoi.Clone() as ROI;
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
return new BlobRunParam() {
|
|
BlobParamExt = _paramExt.Clone() as BlobParamExt
|
|
};
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
_paramExt = ((BlobRunParam)runParam).BlobParamExt.Clone() as BlobParamExt;
|
|
return MilBlob.SetRunParam(_contextId, runParam);
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
return MilBlob.Find(_contextId, _resultContextId, (MIL_ID)image, _paramExt.FindROI.Clone() as ROI, _paramExt);
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
return MilBlob.Find(_contextId, _resultContextId, (MIL_ID)image, findRoi, _paramExt);
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return MilBlob.GetNotFoundPatternResult();
|
|
}
|
|
|
|
public object GetTool() {
|
|
return _contextId;
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
MilBlob.Draw(patternResult, cameraDisplay as IMilCameraDisplay);
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
return ThumbnailImage.Blob();
|
|
}
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
FreeContexts();
|
|
}
|
|
|
|
private void FreeContexts() {
|
|
MilBlob.FreeContext(_contextId);
|
|
MilBlob.FreeContext(_resultContextId);
|
|
|
|
_contextId = MIL.M_NULL;
|
|
_resultContextId = MIL.M_NULL;
|
|
}
|
|
#endregion
|
|
|
|
#region Blob Specific
|
|
internal BlobParamExt LoadParamExt() {
|
|
return BlobParamExt.Load(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal int SaveParamExt(BlobParamExt blobParamExt) {
|
|
return BlobParamExt.Save(
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
blobParamExt);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|