308 lines
11 KiB
C#
308 lines
11 KiB
C#
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
|
|
namespace MilPattern {
|
|
public class LineWidthRunParam : IRunParam, ICloneable {
|
|
public int PatternId1 { get; set; } = -1;
|
|
public int PatternId2 { get; set; } = -1;
|
|
|
|
public object Clone() {
|
|
return new LineWidthRunParam() {
|
|
PatternId1 = PatternId1,
|
|
PatternId2 = PatternId2
|
|
};
|
|
}
|
|
}
|
|
|
|
public class LineWidth : PatternBase, IPatternFinder, IPatternBlock, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".json";
|
|
private PatternBlockParamExt _paramExt = null;
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
|
|
public LineWidth(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"LineWidth_{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() {
|
|
_paramExt = LoadParamExt();
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Save() {
|
|
return SaveParamExt(_paramExt);
|
|
}
|
|
|
|
public int Create() {
|
|
_paramExt = LoadParamExt();
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Remove() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (Directory.Exists(patternDirPath)) {
|
|
Directory.Delete(patternDirPath, recursive: true);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public ROI GetFindROI() {
|
|
// 수정 필요
|
|
return new ROI();
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
return new LineWidthRunParam() {
|
|
PatternId1 = _paramExt.PatternId1,
|
|
PatternId2 = _paramExt.PatternId2
|
|
};
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
var param = runParam as LineWidthRunParam;
|
|
if (param is null) {
|
|
return E1.GENERIC;
|
|
}
|
|
_paramExt.PatternId1 = param.PatternId1;
|
|
_paramExt.PatternId2 = param.PatternId2;
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
Results = new List<CaliperResult>()
|
|
};
|
|
}
|
|
|
|
public object GetTool() {
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
var milCameraDisplay = cameraDisplay as IMilCameraDisplay;
|
|
|
|
// Line
|
|
foreach (var result in patternResult.Results) {
|
|
var caliperResult = result as CaliperResult;
|
|
DrawLinePatternResult(caliperResult.PatternResultFirst, milCameraDisplay);
|
|
DrawLinePatternResult(caliperResult.PatternResultSecond, milCameraDisplay);
|
|
}
|
|
|
|
// Cross
|
|
if (patternResult.Status == eFoundStatus.FoundOK) {
|
|
var crosses = new List<Cross>();
|
|
|
|
foreach (BaseResult baseResult in patternResult.Results) {
|
|
var result = baseResult as CaliperResult;
|
|
var color = GraphicColors.PatternFoundOK;
|
|
var cross = new Cross(result.ImagePosition.Clone() as ImagePoint, 5);
|
|
crosses.Add(cross);
|
|
|
|
milCameraDisplay.SetPatternTracking(cross);
|
|
milCameraDisplay.AddCross(cross, color);
|
|
|
|
// Width
|
|
DrawLineWidth(result, milCameraDisplay);
|
|
|
|
// Text
|
|
var msgs = new List<string>();
|
|
msgs.Add(string.Format("I({0:0.0}, {1:0.0})", result.ImagePosition.X, result.ImagePosition.Y));
|
|
msgs.Add(result.ConvertMaterialPosition());
|
|
msgs.Add(string.Format("Width[Px] : {0:0.000}", result.WidthPx));
|
|
msgs.Add(string.Format("Width : {0:0.000}", CoordUnitValueConverter.GetCoordUnitValue(result.WidthMm)));
|
|
msgs.Add(string.Format("TactTime : {0:0.000}", patternResult.TactTimeInMilliSec));
|
|
msgs.Add(string.Format("ID : {0}_{1}", patternResult.PatternIndex.PatternId, patternResult.PatternIndex.PatternSubId));
|
|
|
|
milCameraDisplay.AddTextList(
|
|
GraphicColors.Orange, new ImagePoint(result.ImagePosition.X + 15, result.ImagePosition.Y + 15), msgs);
|
|
}
|
|
milCameraDisplay.DrawPatternTrackings(crosses);
|
|
}
|
|
}
|
|
|
|
private void DrawLinePatternResult(PatternResult linePatternResult, IMilCameraDisplay milCameraDisplay) {
|
|
if (linePatternResult.Results != null) {
|
|
foreach (var pr in linePatternResult.Results) {
|
|
var lineFinderResult = pr as LineFinderResult;
|
|
if (lineFinderResult != null) {
|
|
MilMeasureLine.Draw((MIL_ID)lineFinderResult.ContextId, linePatternResult, milCameraDisplay);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DrawLineWidth(CaliperResult caliperResult, IMilCameraDisplay milCameraDisplay) {
|
|
var line = new Line<ImagePoint>(caliperResult.ImagePosition, caliperResult.ImagePositionSecond_VI);
|
|
milCameraDisplay.AddLine(GraphicColors.Orange, line, false);
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
#region IPatternBlock
|
|
public List<int> GetPatternBlockIds() {
|
|
return new List<int>() { _paramExt.PatternId1, _paramExt.PatternId2 };
|
|
}
|
|
|
|
public PatternResult Find(List<PatternResult> patternResults) {
|
|
if (patternResults == null || patternResults.Count != 2) {
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
TactTimeInMilliSec = 0
|
|
};
|
|
}
|
|
|
|
var caliperResults = new List<CaliperResult>();
|
|
var line1Results = patternResults[0].Results as List<LineFinderResult>;
|
|
var line2Results = patternResults[1].Results as List<LineFinderResult>;
|
|
|
|
if (line1Results == null ||
|
|
line2Results == null ||
|
|
line1Results.Count != 1 ||
|
|
line2Results.Count != 1) {
|
|
|
|
caliperResults.Add(
|
|
new CaliperResult(
|
|
new ImagePoint(),
|
|
new ImagePoint(),
|
|
new ImagePoint(),
|
|
new LineEquation(1, 1, 1),
|
|
new LineEquation(1, 1, 1),
|
|
patternResults[0],
|
|
patternResults[1],
|
|
0));
|
|
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
Results = caliperResults,
|
|
TactTimeInMilliSec = 0
|
|
};
|
|
}
|
|
|
|
var line1Result = line1Results[0];
|
|
var line2Result = line2Results[0];
|
|
|
|
caliperResults.Add(new CaliperResult(
|
|
line1Result.ImagePosition,
|
|
line2Result.ImagePosition,
|
|
NativePattern.GetVerticalIntersectionPoint(line1Result.ImagePosition, line1Result.LineEquation, line2Result.LineEquation),
|
|
line1Result.LineEquation,
|
|
line2Result.LineEquation,
|
|
patternResults[0],
|
|
patternResults[1],
|
|
NativePattern.GetDistancePointToLine(line1Result.ImagePosition, line2Result.LineEquation)));
|
|
|
|
return new PatternResult() {
|
|
Status = (patternResults.Count > 0) ? eFoundStatus.FoundOK : eFoundStatus.NotFound,
|
|
Results = caliperResults,
|
|
TactTimeInMilliSec = 0
|
|
};
|
|
}
|
|
|
|
public Bitmap GetThumbnail(List<ROI> findRois) {
|
|
if (findRois == null || findRois.Count != 2) {
|
|
return ThumbnailUtil.GetDefaultImage();
|
|
}
|
|
|
|
MIL_ID thumnailImage = MIL.M_NULL;
|
|
try {
|
|
var offsets = new List<ImagePoint>() {
|
|
new ImagePoint(5, 0),
|
|
new ImagePoint(-5, 0)
|
|
};
|
|
var imageSize = ThumbnailUtil.GetImageSize();
|
|
thumnailImage = MilEnvironment.GetCompatibleImageBuffer(
|
|
imageSize, _patternInfo.MilSystemId);
|
|
MilEnvironment.ClearImage(thumnailImage);
|
|
var center = MilEnvironment.GetImageCenter(thumnailImage);
|
|
|
|
for (int i = 0; i < findRois.Count; i++) {
|
|
var findRoi = findRois[i];
|
|
MIL.MgraRectAngle(
|
|
MIL.M_DEFAULT, thumnailImage,
|
|
center.X + offsets[i].X,
|
|
center.Y + offsets[i].Y,
|
|
1,
|
|
imageSize.Height,
|
|
findRoi.Angle.Degree,
|
|
MIL.M_CENTER_AND_DIMENSION + MIL.M_FILLED);
|
|
}
|
|
|
|
return MilEnvironment.ConvertToBitmap(thumnailImage);
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
finally {
|
|
MilEnvironment.FreeImage(thumnailImage);
|
|
}
|
|
|
|
return ThumbnailUtil.GetDefaultImage();
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
}
|
|
#endregion
|
|
|
|
#region Line Width Specific
|
|
public bool IsParamExtExist() {
|
|
return PatternBlockParamExt.Exists(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
public PatternBlockParamExt LoadParamExt() {
|
|
return PatternBlockParamExt.Load(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
public int SaveParamExt(PatternBlockParamExt patternBlockParam) {
|
|
return PatternBlockParamExt.Save(
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
patternBlockParam);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|