Files
wg_cpso/CaeKnowledge/ToolPositionFileHelper.cs
2026-03-25 18:20:24 +08:00

117 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CaeKnowledge.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace CaeKnowledge
{
public static class ToolPositionFileHelper
{
// 保存ToolPosition列表到CSV文本文件
public static void SaveToolPositionsToFile(List<ToolPosition> positions, string filePath)
{
try
{
// 1. 构建CSV表头与类的属性一一对应
var header = "X,Y,Z,I,J,K,SegmentType,Cx,Cy,Cz,FeedRate,SpindleSpeed,RadialDepth,AxialDepth,CurvatureRadius,AverageFx,AverageFy,AverageFz,MaxFx,MaxFy,MaxFz";
// 2. 构建数据行
var dataLines = new List<string> { header };
foreach (var pos in positions)
{
var line = $"{pos.X},{pos.Y},{pos.Z},{pos.I},{pos.J},{pos.K},{(int)pos.SegmentType}," +
$"{pos.Cx},{pos.Cy},{pos.Cz},{pos.FeedRate},{pos.SpindleSpeed},{pos.RadialDepth}," +
$"{pos.AxialDepth},{pos.CurvatureRadius},{pos.AverageFx},{pos.AverageFy},{pos.AverageFz}," +
$"{pos.MaxFx},{pos.MaxFy},{pos.MaxFz}";
dataLines.Add(line);
}
// 3. 写入文件UTF-8编码避免中文/特殊字符乱码)
File.WriteAllLines(filePath, dataLines, System.Text.Encoding.UTF8);
Console.WriteLine($"成功保存 {positions.Count} 条数据到文件:{filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"保存文件失败:{ex.Message}");
throw; // 抛出异常让调用方处理
}
}
// 从CSV文本文件读取ToolPosition列表
public static List<ToolPosition> LoadToolPositionsFromFile(string filePath)
{
var positions = new List<ToolPosition>();
try
{
// 1. 检查文件是否存在
if (!File.Exists(filePath))
{
Console.WriteLine($"文件不存在:{filePath}");
return positions;
}
// 2. 读取所有行并跳过表头
var lines = File.ReadAllLines(filePath, System.Text.Encoding.UTF8).Skip(1).ToList();
// 3. 解析每一行数据
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line)) continue; // 跳过空行
var parts = line.Split(',');
// 校验列数是否匹配(防止文件格式错误)
if (parts.Length != 21)
{
Console.WriteLine($"跳过格式错误的行:{line}");
continue;
}
// 解析每一列数据(带异常处理,防止数据格式错误)
var pos = new ToolPosition();
try
{
pos.X = double.Parse(parts[0]);
pos.Y = double.Parse(parts[1]);
pos.Z = double.Parse(parts[2]);
pos.I = double.Parse(parts[3]);
pos.J = double.Parse(parts[4]);
pos.K = double.Parse(parts[5]);
pos.SegmentType = (EnumSegmentType)int.Parse(parts[6]);
pos.Cx = double.Parse(parts[7]);
pos.Cy = double.Parse(parts[8]);
pos.Cz = double.Parse(parts[9]);
pos.FeedRate = double.Parse(parts[10]);
pos.SpindleSpeed = double.Parse(parts[11]);
pos.RadialDepth = double.Parse(parts[12]);
pos.AxialDepth = double.Parse(parts[13]);
pos.CurvatureRadius = double.Parse(parts[14]);
pos.AverageFx = double.Parse(parts[15]);
pos.AverageFy = double.Parse(parts[16]);
pos.AverageFz = double.Parse(parts[17]);
pos.MaxFx = double.Parse(parts[18]);
pos.MaxFy = double.Parse(parts[19]);
pos.MaxFz = double.Parse(parts[20]);
positions.Add(pos);
}
catch (FormatException ex)
{
Console.WriteLine($"解析行数据失败:{line},错误:{ex.Message}");
}
}
Console.WriteLine($"成功读取 {positions.Count} 条数据从文件:{filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"读取文件失败:{ex.Message}");
throw;
}
return positions;
}
}
}