【类 型】:

【主	题】:初始化
【描	述】:
	[原因]:
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
szdot 2024-05-30 20:17:38 +08:00
commit 74d971ea4b
6 changed files with 387 additions and 0 deletions

9
App.xaml Normal file
View File

@ -0,0 +1,9 @@
<Application x:Class="MPTxtToJson.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MPTxtToJson"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

17
App.xaml.cs Normal file
View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace MPTxtToJson
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
}
}

108
MPTxtToJson.csproj Normal file
View File

@ -0,0 +1,108 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0E66BA13-D60B-4F55-8FDA-1129C8909DFF}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>MPTxtToJson</RootNamespace>
<AssemblyName>MPTxtToJson</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>logo.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Resource Include="logo.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

25
MPTxtToJson.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32602.291
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MPTxtToJson", "MPTxtToJson.csproj", "{0E66BA13-D60B-4F55-8FDA-1129C8909DFF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0E66BA13-D60B-4F55-8FDA-1129C8909DFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E66BA13-D60B-4F55-8FDA-1129C8909DFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E66BA13-D60B-4F55-8FDA-1129C8909DFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E66BA13-D60B-4F55-8FDA-1129C8909DFF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DD317A0-E8BA-4F20-8B4B-2D5A4E5EF9A1}
EndGlobalSection
EndGlobal

13
MainWindow.xaml Normal file
View File

@ -0,0 +1,13 @@
<Window x:Class="MPTxtToJson.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MPTxtToJson"
mc:Ignorable="d"
Title="MPTxtToJson" Height="150" Width="200">
<Grid Margin="0,0,0.6,-0.2">
<Button Content="导入MP航点" HorizontalAlignment="Left" Margin="60,0,0,83" VerticalAlignment="Bottom" Width="75" Click="HandleImportTxt"/>
<Button x:Name="JsonButton" Content="导出Josn" HorizontalAlignment="Left" Margin="60,0,0,36" VerticalAlignment="Bottom" Width="75" IsEnabled="False" Click="HandleSaveJsonFile"/>
</Grid>
</Window>

215
MainWindow.xaml.cs Normal file
View File

@ -0,0 +1,215 @@
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;
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();
jsonModel.ID = "FliCube";
jsonModel.ver = "1.0";
jsonModel.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++;
}
jsonModel.tasks = tasks;
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 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);    //转为字符串
}
}
}