
【主 题】:在输出的json 最外层包一个questAss字段 【描 述】: [原因]:控制飞机 改成cmd/+的主题方式 要用questAss 来标记 [过程]: [影响]: 【结 束】 # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动
223 lines
7.9 KiB
C#
223 lines
7.9 KiB
C#
using Microsoft.Win32;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Navigation;
|
||
using System.Windows.Shapes;
|
||
using static MPTxtToJson.JsonModel;
|
||
using static MPTxtToJson.JsonModel.QuestAss;
|
||
|
||
namespace MPTxtToJson
|
||
{
|
||
/// <summary>
|
||
/// MainWindow.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class MainWindow : Window
|
||
{
|
||
public MainWindow()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
private string[] strArr;
|
||
private string jsonStr;
|
||
/// <summary>
|
||
/// 导入mp航点文件 按钮事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void HandleImportTxt(object sender, RoutedEventArgs e)
|
||
{
|
||
bool isSelect;//判断有没有获取到文件名
|
||
string fileName = this.OpExplorer("航点文件导入",out isSelect);//获取航点文件路径和文件名
|
||
if (isSelect) {
|
||
strArr = this.ReadFile(fileName);
|
||
this.JsonButton.IsEnabled = true;//激活导出json按钮
|
||
this.jsonStr=JsonBase.ModelTojson(this.MakeJsonModel());//得到json字符串
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// 保存转换好的json到文件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void HandleSaveJsonFile(object sender, RoutedEventArgs e)
|
||
{
|
||
SaveFileDialog sfd = new SaveFileDialog();
|
||
sfd.Filter = "航点文件|*.json;";//资源管理器限制扩展名
|
||
sfd.Title = "保存json航点文件";
|
||
sfd.AddExtension = true;//设置自动在文件名中添加扩展名
|
||
if (sfd.ShowDialog() == true)
|
||
{
|
||
this.SaveStrToFile(sfd.FileName, this.jsonStr);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 生成json模型
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private JsonModel MakeJsonModel()
|
||
{
|
||
JsonModel jsonModel = new JsonModel();
|
||
QuestAss questAss = new QuestAss();
|
||
questAss.ID = "FliCube";
|
||
questAss.ver = "1.0";
|
||
questAss.taskcount = this.strArr.Length-1;
|
||
List<Tasks> tasks = new List<Tasks>();
|
||
int i = 0;
|
||
foreach (var item in strArr)
|
||
{
|
||
if (i > 0) {
|
||
//把每一行的航点字符串 用tab分割出每个参数
|
||
char[] separator = {'\t'};
|
||
string[] sArr = item.Split(separator);
|
||
Tasks t = new Tasks();
|
||
t.seq = Convert.ToInt32(sArr[0]);
|
||
t.frame = Convert.ToInt32(sArr[2]);
|
||
t.command = Convert.ToInt32(sArr[3]);
|
||
t.current = 0;
|
||
t.autocontinue = 1;
|
||
t.param1 = Convert.ToDouble(sArr[4]);
|
||
t.param2 = Convert.ToDouble(sArr[5]);
|
||
t.param3 = Convert.ToDouble(sArr[6]);
|
||
t.param4 = Convert.ToDouble(sArr[7]);
|
||
t.x = Convert.ToDouble(sArr[8]);
|
||
t.y = Convert.ToDouble(sArr[9]);
|
||
t.z = Convert.ToDouble(sArr[10]);
|
||
tasks.Add(t);
|
||
}
|
||
i++;
|
||
}
|
||
questAss.tasks = tasks;
|
||
jsonModel.questAss = questAss;
|
||
return jsonModel;
|
||
}
|
||
/// <summary>
|
||
/// 资源管理器 获取文件路径及文件名
|
||
/// </summary>
|
||
/// <param name="titName">窗口名称</param>
|
||
/// <param name="isSelect">返回 有没有获取到文件</param>
|
||
/// <returns>返回路径及文件名</returns>
|
||
private string OpExplorer(string titName, out bool isSelect)
|
||
{
|
||
Microsoft.Win32.OpenFileDialog ofdl = new Microsoft.Win32.OpenFileDialog();//打开资源管理器
|
||
ofdl.Filter = "航点文件|*.txt;";//资源管理器限制扩展名
|
||
ofdl.Title = titName;
|
||
if (ofdl.ShowDialog() == true)//执行打开资源管理器 并判断有没有获取到文件名
|
||
{
|
||
isSelect = true;
|
||
}
|
||
else
|
||
{
|
||
isSelect = false;
|
||
}
|
||
return ofdl.FileName;
|
||
}
|
||
/// <summary>
|
||
/// 逐行读取 文档
|
||
/// </summary>
|
||
/// <param name="path">文件路径</param>
|
||
/// <returns>返回每行的字符串 集合</returns>
|
||
private string[] ReadFile(string path)
|
||
{
|
||
StreamReader sr = new StreamReader(path, Encoding.Default);
|
||
String line;
|
||
ArrayList arrlist = new ArrayList();
|
||
while ((line = sr.ReadLine()) != null)
|
||
{
|
||
arrlist.Add(line);
|
||
}
|
||
if (arrlist.Count == 1)//判断 svg的文件内容 是否写在一行里面
|
||
{
|
||
string temp = (string)arrlist[0];
|
||
string[] strArray = Regex.Split(temp, "/>");
|
||
return strArray;
|
||
}
|
||
string[] arr = new string[arrlist.Count];
|
||
int key = 0;
|
||
foreach (var item in arrlist)
|
||
{
|
||
arr[key] = (string)item;
|
||
key++;
|
||
}
|
||
return arr;
|
||
}
|
||
/// <summary>
|
||
/// 字符串 保存到指定文件
|
||
/// </summary>
|
||
/// <param name="filePath">指定文件和路径</param>
|
||
/// <param name="str">字符串</param>
|
||
private void SaveStrToFile(string filePath, string str)
|
||
{
|
||
str = str.TrimEnd((char[])"\n\r".ToCharArray());//去除最后的回车符
|
||
string stream = null;
|
||
if (File.Exists(filePath))
|
||
{
|
||
StreamReader reader = new StreamReader(filePath);
|
||
stream = reader.ReadToEnd();
|
||
reader.Close();
|
||
}
|
||
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
|
||
StreamWriter sw = new StreamWriter(fs);
|
||
sw.Write(str);
|
||
sw.Close();
|
||
fs.Close();
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// json文件的数据结构
|
||
/// </summary>
|
||
public class JsonModel
|
||
{
|
||
public QuestAss questAss { get; set; }
|
||
public class QuestAss
|
||
{
|
||
public string ID { get; set; }
|
||
public string ver { get; set; }
|
||
public int taskcount { get; set; }
|
||
public List<Tasks> tasks { get; set; }
|
||
public class Tasks
|
||
{
|
||
public int seq { get; set; }//航点序号
|
||
public int frame { get; set; }
|
||
public int command { get; set; }
|
||
public int current { get; set; }
|
||
public int autocontinue { get; set; }
|
||
public double param1 { get; set; }
|
||
public double param2 { get; set; }
|
||
public double param3 { get; set; }
|
||
public double param4 { get; set; }
|
||
public double x { get; set; }
|
||
public double y { get; set; }
|
||
public double z { get; set; }
|
||
}
|
||
}
|
||
}
|
||
public static class JsonBase
|
||
{
|
||
/// <summary>
|
||
/// FcgmJsonModel对象 序列化 成 json
|
||
/// </summary>
|
||
/// <param name="fcgmJsonModel"></param>
|
||
/// <returns></returns>
|
||
public static string ModelTojson(JsonModel jsonModel)
|
||
{
|
||
return JsonConvert.SerializeObject(jsonModel); //转为字符串
|
||
}
|
||
}
|
||
}
|