191 lines
8.5 KiB
C#
191 lines
8.5 KiB
C#
using Microsoft.Office.Interop.Excel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using VisionCommon;
|
|
using static GUI.MainPage;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
|
namespace DB
|
|
{
|
|
public class RecipeFieldScan
|
|
{
|
|
private static readonly RecipeFieldScan instance = new RecipeFieldScan();
|
|
|
|
static RecipeFieldScan() { }
|
|
private RecipeFieldScan() { }
|
|
public static RecipeFieldScan Instance { get { return instance; } }
|
|
|
|
private string defaultScoutName = "D:\\ILSOSConfig\\ILSOS_PrismaticScanner\\_vision\\FieldScanImage\\Default\\DefaultFieldScanImage.Jpeg";
|
|
|
|
#region GUI에서 활용하는 함수 (영역 업데이트, 필드 로드, 필드 저장, 필드 영역 전달 등등)
|
|
public void UpdateScoutField(List<ChildBufRowCol> fieldScan, int selectedIndex)
|
|
{
|
|
if (fieldScanDatas.Count() - 1 < selectedIndex) {
|
|
for (int i = fieldScanDatas.Count() - 1; i < selectedIndex; i++) {
|
|
if (fieldScanDatas.Count() == 0) AddFieldScanData(GetAutoPageName(selectedIndex), defaultScoutName, fieldScan);
|
|
else {
|
|
string str = GetPageImageName(GetAutoPageName(fieldScanDatas.Count - 1), ref defaultScoutName);
|
|
AddFieldScanData(GetAutoPageName(selectedIndex), str, fieldScan);
|
|
}
|
|
}
|
|
} else {
|
|
ChangeFieldScan(GetAutoPageName(selectedIndex), fieldScan);
|
|
}
|
|
}
|
|
public void LoadFieldHelper(string fileName, int pageCount, string defaultFileStr = null, List<ChildBufRowCol> defaultChildBufRowCols=null)
|
|
{
|
|
try {
|
|
XmlDocument doc = new XmlDocument();
|
|
var extenstion = Path.GetExtension(fileName);
|
|
|
|
if (!File.Exists(fileName)) File.WriteAllText(fileName, "");
|
|
|
|
if (extenstion == ".json") {
|
|
var jsonString = File.ReadAllText(fileName);
|
|
} else {
|
|
FileInfo fi = new FileInfo(fileName);
|
|
StreamReader sr = new StreamReader(fileName);
|
|
doc.Load(sr);
|
|
sr.Dispose();
|
|
}
|
|
|
|
XmlElement root = doc.DocumentElement;
|
|
|
|
|
|
// JSON 파일 읽기, JSON을 List로 변환
|
|
string json = File.ReadAllText(fileName);
|
|
List<FieldScanData> jsonData = new List<FieldScanData>();
|
|
|
|
if (json != null && json != "") jsonData = System.Text.Json.JsonSerializer.Deserialize<List<FieldScanData>>(json);
|
|
else ;
|
|
|
|
fieldScanDatas.Clear();
|
|
|
|
// default 값 써주기
|
|
FieldScanData defaultFieldScan = new FieldScanData("none", defaultFileStr, defaultChildBufRowCols);
|
|
|
|
// page 개수보다 입력된 개수가 작을 경우(비정상)와 정상인 경우에 따라 다르게 저장.
|
|
if (jsonData.Count != pageCount) {
|
|
for (int i = 0; i < pageCount; i++) {
|
|
// PageName이 동일하다면 그대로 써줄 것. 아니라면, 초기 값을 써줄 것.
|
|
var modifyFieldScan = jsonData.FirstOrDefault(f => f.PageName.Equals(GetAutoPageName(i), StringComparison.OrdinalIgnoreCase));
|
|
if (modifyFieldScan != null && modifyFieldScan.FieldScans != null && modifyFieldScan.FileName != null) AddFieldScanData(modifyFieldScan.PageName, modifyFieldScan.FileName, modifyFieldScan.FieldScans);
|
|
else AddFieldScanData(GetAutoPageName(i), defaultFieldScan.FileName, defaultFieldScan.FieldScans);
|
|
}
|
|
} else {
|
|
foreach (FieldScanData js in jsonData) {
|
|
if (js != null && js.FieldScans != null && js.FileName != null) AddFieldScanData(js.PageName, js.FileName, js.FieldScans);
|
|
else AddFieldScanData(js.PageName, defaultFieldScan.FileName, defaultFieldScan.FieldScans);
|
|
}
|
|
}
|
|
} catch (Exception ex) {
|
|
throw ex;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Scout 후, Field Size 변경 후, Recipe 저장 시 FieldScan 저장되어야 함.
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="defaultFileStr"></param>
|
|
public void SaveFieldHelper(string fileName, string defaultFileStr = null)
|
|
{
|
|
try {
|
|
using (MemoryStream stream = new MemoryStream()) {
|
|
XmlTextWriter wr = new XmlTextWriter(stream, Encoding.UTF8);
|
|
wr.Formatting = System.Xml.Formatting.Indented;
|
|
wr.Flush();
|
|
|
|
foreach(FieldScanData fieldScan in fieldScanDatas) {
|
|
fieldScan.FieldScans
|
|
.OrderBy(row => row.RowIdx)
|
|
.ThenBy(col => col.ColIdx)
|
|
.Select(param => param.Clone() as ChildBufRowCol)
|
|
.ToList();
|
|
}
|
|
|
|
var json = System.Text.Json.JsonSerializer.Serialize(fieldScanDatas, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(fileName, json);
|
|
}
|
|
} catch {
|
|
|
|
}
|
|
}
|
|
public List<ChildBufRowCol> GetPageFieldScan(string pageName)
|
|
{
|
|
var modifyFieldScan = fieldScanDatas.FirstOrDefault(f => f.PageName.Equals(pageName, StringComparison.OrdinalIgnoreCase));
|
|
if (modifyFieldScan != null) {
|
|
return modifyFieldScan.FieldScans
|
|
.OrderBy(row => row.RowIdx)
|
|
.ThenBy(col => col.ColIdx)
|
|
.Select(param => param.Clone() as ChildBufRowCol)
|
|
.ToList();
|
|
} else return null;
|
|
}
|
|
public string GetAutoPageName(int pageNo) { return string.Format("selectedIndex{0}", pageNo); }
|
|
#endregion
|
|
|
|
#region Dictionary 관리를 위한 함수
|
|
void AddFieldScanData(string pageName, string fileName, List<ChildBufRowCol> childBuf)
|
|
{
|
|
fieldScanDatas.Add(new FieldScanData(pageName,fileName,childBuf));
|
|
for (int i = 0; i < fieldScanDatas.Count(); i++) {
|
|
fieldScanDatas[i].PageName = GetAutoPageName(i);
|
|
}
|
|
}
|
|
public void DeleteFieldScanData(string pageName)
|
|
{
|
|
fieldScanDatas = fieldScanDatas.Where(fieldScan => fieldScan.PageName != pageName).ToList();
|
|
for (int i = 0; i < fieldScanDatas.Count(); i++) {
|
|
fieldScanDatas[i].PageName = GetAutoPageName(i);
|
|
}
|
|
}
|
|
bool ChangeFieldScan(string pageName, List<ChildBufRowCol> childBuf)
|
|
{
|
|
var modifyFieldScan = fieldScanDatas.FirstOrDefault(f => f.PageName.Equals(pageName, StringComparison.OrdinalIgnoreCase));
|
|
if (modifyFieldScan != null) {
|
|
modifyFieldScan.FieldScans = childBuf;
|
|
return true;
|
|
} else return false;
|
|
}
|
|
public bool ChangeFieldString(string pageName, string fileName)
|
|
{
|
|
var modifyFieldScan = fieldScanDatas.FirstOrDefault(f => f.PageName.Equals(pageName, StringComparison.OrdinalIgnoreCase));
|
|
if (modifyFieldScan != null) {
|
|
modifyFieldScan.FileName = fileName;
|
|
return true;
|
|
} else return false;
|
|
}
|
|
public string GetPageImageName(string pageName, ref string defaultStr)
|
|
{
|
|
var modifyFieldScan = fieldScanDatas.FirstOrDefault(f => f.PageName.Equals(pageName, StringComparison.OrdinalIgnoreCase));
|
|
if (modifyFieldScan != null) {
|
|
return modifyFieldScan.FileName;
|
|
} else return defaultStr;
|
|
}
|
|
#endregion
|
|
|
|
#region 데이터를 위한 Class
|
|
private List<FieldScanData> fieldScanDatas = new List<FieldScanData>();
|
|
public class FieldScanData
|
|
{
|
|
public string PageName { get; set; }
|
|
public string FileName { get; set; }
|
|
public List<ChildBufRowCol> FieldScans { get; set; }
|
|
|
|
public FieldScanData(string pageName, string fileName, List<ChildBufRowCol> fieldScans)
|
|
{
|
|
this.PageName = pageName;
|
|
this.FileName = fileName;
|
|
this.FieldScans = fieldScans;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|