ComputerGraphics/20210129/zzx-20210129作业/json/LineConverter.cs
2021-02-25 15:53:31 +08:00

83 lines
3.6 KiB
C#

/************************************************************************
* ClassName : LineConverter
* Description :
* Author : Einsam
* CreateTime : 2019/4/1 17:34:22
************************************************************************
* █████▒█ ██ ▄████▄ ██ ▄█▀ ██████╗ ██╗ ██╗ ██████╗
* ▓██ ▒ ██ ▓██▒▒██▀ ▀█ ██▄█▒ ██╔══██╗██║ ██║██╔════╝
* ▒████ ░▓██ ▒██░▒▓█ ▄ ▓███▄░ ██████╔╝██║ ██║██║ ███╗
* ░▓█▒ ░▓▓█ ░██░▒▓▓▄ ▄██▒▓██ █▄ ██╔══██╗██║ ██║██║ ██║
* ░▒█░ ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄ ██████╔╝╚██████╔╝╚██████╔╝
* ▒ ░ ░▒▓▒ ▒ ▒ ░ ░▒ ▒ ░▒ ▒▒ ▓▒ ╚═════╝ ╚═════╝ ╚═════╝
* ░ ░░▒░ ░ ░ ░ ▒ ░ ░▒ ▒░
* ░ ░ ░░░ ░ ░ ░ ░ ░░ ░
* ░ ░ ░ ░ ░
* ░
************************************************************************
* Copyright @ u-BIM Dev. 2018 . All rights reserved.
************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace GraphicsStudy.Json
{
public class LineConverter : Newtonsoft.Json.JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
}
else
{
JObject jObject = new JObject();
jObject.Add("StartX", (value as Line).GetEndPoint(0).X);
jObject.Add("StartY", (value as Line).GetEndPoint(0).Y);
jObject.Add("StartZ", (value as Line).GetEndPoint(0).Z);
jObject.Add("EndX", (value as Line).GetEndPoint(1).X);
jObject.Add("EndY", (value as Line).GetEndPoint(1).Y);
jObject.Add("EndZ", (value as Line).GetEndPoint(1).Z);
writer.WriteValue(jObject.ToString());
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jObject = JObject.Parse(reader.Value as string);
double startX = jObject.Value<double>("StartX");
double startY = jObject.Value<double>("StartY");
double startZ = jObject.Value<double>("StartZ");
XYZ startPoint = new XYZ(startX, startY, startZ);
double endX = jObject.Value<double>("EndX");
double endY = jObject.Value<double>("EndY");
double endZ = jObject.Value<double>("EndZ");
XYZ endPoint = new XYZ(endX, endY, endZ);
return Line.CreateBound(startPoint, endPoint);
}
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Line))
{
return true;
}
return false;
}
}
}