359 lines
12 KiB
C#
359 lines
12 KiB
C#
using CaeGlobals;
|
|
using CaeKnowledge.Data;
|
|
using CaeKnowledge.View;
|
|
using LiteDB;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace CaeKnowledge
|
|
{
|
|
public class KnowledgeTools
|
|
{
|
|
public static Cutter FindCutter(string cid)
|
|
{
|
|
var fullPath = CaeGlobals.Tools.GetLiteDatabase();
|
|
|
|
using(var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<Cutter>("cutters");
|
|
return col.FindOne(x => x.Cid == cid);
|
|
}
|
|
}
|
|
|
|
public static CuttingParameter FindCuttingParameter(string pid)
|
|
{
|
|
var fullPath = CaeGlobals.Tools.GetLiteDatabase();
|
|
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<CuttingParameter>("cutting_parameters");
|
|
return col.FindOne(x => x.Pid == pid);
|
|
}
|
|
}
|
|
|
|
public static List<ViewCutter> LoadViewCutters(string fullPath)
|
|
{
|
|
List<ViewCutter> cutters = new List<ViewCutter>();
|
|
|
|
try
|
|
{
|
|
DataTable dataTable = LoadExcelFile(fullPath, out var isSuccess, out var resultMsg);
|
|
|
|
if (dataTable.Columns.Count != 11)
|
|
{
|
|
throw new CaeException("导入Excel文件的列数大于或者小于11个!");
|
|
}
|
|
|
|
if (isSuccess)
|
|
{
|
|
foreach (DataRow row in dataTable.Rows)
|
|
{
|
|
Cutter cutter = new Cutter(ObjectId.NewObjectId())
|
|
{
|
|
Cid = row[0].ToString(),
|
|
Cname = row[1].ToString(),
|
|
Description = row[2].ToString(),
|
|
Code = row[3].ToString(),
|
|
Material = row[4].ToString(),
|
|
Number = IntParse(row[5].ToString()),
|
|
Diameter = DoubleParse(row[6].ToString()),
|
|
ToolLength = DoubleParse(row[7].ToString()),
|
|
FluteLength = DoubleParse(row[8].ToString()),
|
|
Flute = IntParse(row[9].ToString()),
|
|
HelixAngle = DoubleParse(row[10].ToString())
|
|
};
|
|
|
|
cutters.Add(new ViewCutter(cutter));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ExceptionTools.Show(resultMsg);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionTools.Show(ex);
|
|
}
|
|
|
|
return cutters;
|
|
}
|
|
|
|
public static List<ViewCuttingParameter> LoadCuttingParameters(string fullPath)
|
|
{
|
|
List<ViewCuttingParameter> cuttingParameters = new List<ViewCuttingParameter>();
|
|
|
|
try
|
|
{
|
|
DataTable dataTable = LoadExcelFile(fullPath, out var isSuccess, out var resultMsg);
|
|
|
|
if (dataTable.Columns.Count != 9)
|
|
{
|
|
throw new CaeException("导入Excel文件的列数大于或者小于9个!");
|
|
}
|
|
|
|
if (isSuccess)
|
|
{
|
|
foreach (DataRow row in dataTable.Rows)
|
|
{
|
|
CuttingParameter cp = new CuttingParameter(ObjectId.NewObjectId())
|
|
{
|
|
Pid = row[0].ToString(),
|
|
Cid = row[1].ToString(),
|
|
Material = row[2].ToString(),
|
|
AxialForceCoefficient = DoubleParse(row[3].ToString()),
|
|
RadialForceCoefficient = DoubleParse(row[4].ToString()),
|
|
TangentialForceCoefficient=DoubleParse(row[5].ToString()),
|
|
AxialForceConstant = DoubleParse(row[6].ToString()),
|
|
RadialForceConstant = DoubleParse(row[7].ToString()),
|
|
TangentialForceConstant = DoubleParse(row[8].ToString())
|
|
};
|
|
|
|
cuttingParameters.Add(new ViewCuttingParameter(cp));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ExceptionTools.Show(resultMsg);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionTools.Show(ex);
|
|
}
|
|
|
|
return cuttingParameters;
|
|
}
|
|
|
|
private static DataTable LoadExcelFile(string fullPath, out bool isSuccess, out string resultMsg)
|
|
{
|
|
isSuccess = false;
|
|
resultMsg = "读取Excel文件错误";
|
|
if (!File.Exists(fullPath)) return new DataTable();
|
|
|
|
using (var stream = new FileStream(fullPath, FileMode.Open))
|
|
{
|
|
var fileType = Path.GetExtension(fullPath);
|
|
|
|
return NpoiExcelImportHelper.Self.ExcelToDataTable(stream, fileType,
|
|
out isSuccess, out resultMsg);
|
|
}
|
|
}
|
|
|
|
public static void SaveDatabase(string fullPath, List<Cutter> cutters)
|
|
{
|
|
try
|
|
{
|
|
// Open (or create) the LiteDB database at the specified path
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
// Get (or create) a collection named "view_cutters"
|
|
var col = db.GetCollection<Cutter>("cutters");
|
|
|
|
// Optionally, clear the collection before inserting new data
|
|
col.DeleteAll();
|
|
|
|
// Insert all cutters
|
|
col.InsertBulk(cutters);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Handle exceptions (e.g., log or display an error)
|
|
ExceptionTools.Show(ex);
|
|
}
|
|
}
|
|
|
|
public static void SaveDatabase(string fullPath, List<CuttingParameter> cuttingParameters)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<CuttingParameter>("cutting_parameters");
|
|
col.DeleteAll();
|
|
col.InsertBulk(cuttingParameters);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionTools.Show(ex);
|
|
}
|
|
}
|
|
|
|
public static void SaveDatabase(string fullPath, List<ViewElasticWithDensity> elasticWithDensities)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<ViewElasticWithDensity>("elastic_with_densities");
|
|
col.DeleteAll();
|
|
col.InsertBulk(elasticWithDensities);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionTools.Show(ex);
|
|
}
|
|
}
|
|
|
|
public static List<Cutter> LoadCuttersFromDatabase(string fullPath)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<Cutter>("cutters");
|
|
// Fetch all documents in the collection and return as a list
|
|
return col.FindAll().ToList();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Handle exceptions (e.g., log or display an error)
|
|
// Console.Error.WriteLine($"Error loading data from LiteDB: {ex.Message}");
|
|
// Return an empty list on error
|
|
return new List<Cutter>();
|
|
}
|
|
}
|
|
|
|
public static List<CuttingParameter> LoadCuttingParametersFromDatabase(string fullPath)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<CuttingParameter>("cutting_parameters");
|
|
return col.FindAll().ToList();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new List<CuttingParameter>();
|
|
}
|
|
}
|
|
|
|
public static List<ViewElasticWithDensity> LoadElasticWithDensitiesFromDatabase(string fullPath)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<ViewElasticWithDensity>("elastic_with_densities");
|
|
return col.FindAll().ToList();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new List<ViewElasticWithDensity>();
|
|
}
|
|
}
|
|
|
|
public static void FromCutter(ViewCutter cutter, ref DataRow row)
|
|
{
|
|
row[nameof(ViewCutter.Id)] = cutter.Id;
|
|
|
|
row[nameof(ViewCutter.Cid)] = cutter.Cid;
|
|
row[nameof(ViewCutter.Cname)] = cutter.Cname;
|
|
row[nameof(ViewCutter.Description)] = cutter.Description;
|
|
row[nameof(ViewCutter.Code)] = cutter.Code;
|
|
row[nameof(ViewCutter.Material)] = cutter.Material;
|
|
|
|
row[nameof(ViewCutter.Number)] = cutter.Number;
|
|
row[nameof(ViewCutter.Diameter)] = cutter.Diameter;
|
|
row[nameof(ViewCutter.ToolLength)] = cutter.ToolLength;
|
|
row[nameof(ViewCutter.FluteLength)] = cutter.FluteLength;
|
|
row[nameof(ViewCutter.Flute)] = cutter.Flute;
|
|
row[nameof(ViewCutter.HelixAngle)] = cutter.HelixAngle;
|
|
}
|
|
|
|
public static ViewCutter ToCutter(DataRow row)
|
|
{
|
|
Cutter cutter = new Cutter(row[nameof(ViewCutter.Id)] as ObjectId)
|
|
{
|
|
Cid = (string)row[nameof(ViewCutter.Cid)],
|
|
Cname = (string)row[nameof(ViewCutter.Cname)],
|
|
Description = (string)row[nameof(ViewCutter.Description)],
|
|
Code = (string)row[nameof(ViewCutter.Code)],
|
|
Material = (string)row[nameof(ViewCutter.Material)],
|
|
Number = (int)row[nameof(ViewCutter.Number)],
|
|
Diameter = (double)row[nameof(ViewCutter.Diameter)],
|
|
ToolLength = (double)row[nameof(ViewCutter.ToolLength)],
|
|
FluteLength = (double)row[nameof(ViewCutter.FluteLength)],
|
|
Flute = (int)row[nameof(ViewCutter.Flute)],
|
|
HelixAngle = (double)row[nameof(ViewCutter.HelixAngle)]
|
|
};
|
|
|
|
return new ViewCutter(cutter);
|
|
}
|
|
|
|
private static int IntParse(string s)
|
|
{
|
|
int value = 0;
|
|
|
|
if (int.TryParse(s, out var v))
|
|
{
|
|
value = v;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
private static double DoubleParse(string s)
|
|
{
|
|
double value = 0.0;
|
|
|
|
if (double.TryParse(s, out var v))
|
|
{
|
|
value = v;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static Type ObjectIdType => typeof(ObjectId);
|
|
|
|
// Luke 2026-3-25
|
|
public static void SaveDatabase(string fullPath, List<ProcessingJob> jobs)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<ProcessingJob>("jobs");
|
|
col.DeleteAll();
|
|
col.InsertBulk(jobs);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionTools.Show(ex);
|
|
}
|
|
}
|
|
|
|
public static List<ProcessingJob> LoadDatabase(string fullPath)
|
|
{
|
|
try
|
|
{
|
|
using (var db = new LiteDatabase(fullPath))
|
|
{
|
|
var col = db.GetCollection<ProcessingJob>("jobs");
|
|
// Fetch all documents in the collection and return as a list
|
|
return col.FindAll().ToList();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Handle exceptions (e.g., log or display an error)
|
|
// Console.Error.WriteLine($"Error loading data from LiteDB: {ex.Message}");
|
|
// Return an empty list on error
|
|
return new List<ProcessingJob>();
|
|
}
|
|
}
|
|
}
|
|
} |