-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
166 lines (151 loc) · 5.83 KB
/
Utils.cs
File metadata and controls
166 lines (151 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasingPathPositionCalculator;
public class Utils
{
public static T ParseInput<T>(string? input,
string typeName = "",
Func<T, bool>? condition = null,
string conditionMsg = "")
where T : IParsable<T>
{
typeName = string.IsNullOrEmpty(typeName) ? typeof(T).Name : typeName;
condition ??= t => true;
while (true)
{
input ??= "";
input = input.Trim();
if (input.Length == 0)
{
Console.WriteLine($"{typeName}不能为空!请重新输入..");
input = Console.ReadLine();
continue;
}
if (!T.TryParse(input, CultureInfo.InvariantCulture, out T val))
{
Console.WriteLine($"不正确的格式!请输入一个有效的{typeName}!");
input = Console.ReadLine();
continue;
}
if (!condition(val))
Console.WriteLine(string.IsNullOrEmpty(conditionMsg) ? "不满足约束条件!" : conditionMsg);
return val;
}
}
public static (T, T) ParseCoordinate<T>(string? input, string typeName = "") where T : IParsable<T>
{
typeName = string.IsNullOrEmpty(typeName) ? typeof(T).Name : typeName;
while (true)
{
// 筛选有效字符,分割
string sanitizedInput = new string(input
.Where(c => char.IsDigit(c) || c == ',' || c == '.' || c == '-' || c == ',' || c == '。' || c == '/')
.ToArray());
string[] parts = sanitizedInput.Split(',', StringSplitOptions.RemoveEmptyEntries);
// 验证分割结果
if (parts.Length != 2)
{
Console.WriteLine($"格式错误!请输入两个用英文逗号分隔的{typeName}( 示例: 30,-200 ):");
input = Console.ReadLine();
continue;
}
// 单独验证X Y
T Xval = ParseInput<T>(parts[0], $"X: {typeName}");
T Yval = ParseInput<T>(parts[1], $"Y: {typeName}");
return (Xval, Yval);
}
}
public static void OpenInNotepad(Group group)
{
try
{
// 获取程序目录下的tmp文件夹路径
string programDirectory = AppDomain.CurrentDomain.BaseDirectory;
string tmpDirectory = Path.Combine(programDirectory, "tmp");
// 确保tmp目录存在
if (!Directory.Exists(tmpDirectory))
{
Directory.CreateDirectory(tmpDirectory);
}
// 创建临时文件(使用Guid确保文件名唯一)
string tempFile = Path.Combine(tmpDirectory, $"{Guid.NewGuid():N}.txt");
// 写入数据到文件
using (StreamWriter writer = new StreamWriter(tempFile))
{
writer.WriteLine($"路径组: {group.GroupName} (UID: {group.GroupUID})");
writer.WriteLine($"起始X: {group.StartX}, 结束X: {group.EndX}");
writer.WriteLine($"持续时间: {group.Duration}, 密度: {group.Density}");
writer.WriteLine($"缓动类型: {group.Easing}");
writer.WriteLine("采样点: (注意!:点前时间标尺均为相对于起点拍)");
writer.WriteLine("====================");
foreach (var point in group.Points)
{
writer.WriteLine(point.ToString());
}
writer.WriteLine("====================");
writer.WriteLine($"总共 {group.Count} 个采样点");
}
// 使用传统记事本(notepad.exe 的完整路径)
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = Path.Combine(Environment.SystemDirectory, "notepad.exe"),
Arguments = $"\"{tempFile}\"",
UseShellExecute = false // 不使用Shell执行
};
Process.Start(startInfo);
Console.WriteLine($"已在记事本中打开采样点数据: {tempFile}");
}
catch (Exception ex)
{
Console.WriteLine($"打开记事本时出错: {ex.Message}");
// 备用方案:直接显示在控制台
Console.WriteLine("无法打开记事本,在控制台中显示内容:");
Console.WriteLine("====================");
foreach (var point in group.Points)
{
Console.WriteLine(point.ToString());
}
Console.WriteLine("====================");
}
}
public static void OpenTextFile(string filePath)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = filePath,
UseShellExecute = true
};
Process.Start(startInfo);
}
catch (Exception ex)
{
Console.WriteLine($"无法打开文件: {ex.Message}");
Log.Warning($"打开文件失败: {filePath} - {ex.Message}");
}
}
public static void OpenExplorer(string directoryPath)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = $"\"{directoryPath}\"",
UseShellExecute = true
};
Process.Start(startInfo);
}
catch (Exception ex)
{
Console.WriteLine($"无法打开资源管理器: {ex.Message}");
Log.Warning($"打开资源管理器失败: {ex.Message}");
}
}
}