You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
229 lines
7.4 KiB
229 lines
7.4 KiB
using System.Text.Json.Nodes; |
|
using Newtonsoft.Json; |
|
using SharpDX; |
|
using BrilliantSightClient.Model.Attributes; |
|
using BrilliantSightClient.Model.Entity.ApiEntity; |
|
using BrilliantSightClient.Model.Extension; |
|
using BrilliantSightClient.Model.Helper; |
|
using BrilliantSightClient.Views.UserControl.ViewportData.Entity; |
|
using BrilliantSightClient.Views.UserControl.ViewportData.Enum; |
|
using BrilliantSightClient.Views.UserControl.ViewportData.Helper; |
|
using SparkDotNetCore.DiamondScanner.Entity; |
|
|
|
namespace BrilliantSightClient.Views.UserControl.ViewportData; |
|
|
|
public class ViewportData |
|
{ |
|
/// <summary> |
|
/// 钻石编码 |
|
/// </summary> |
|
public String DiamondCode { get; set; } |
|
|
|
/// <summary> |
|
/// 钻石数据 |
|
/// </summary> |
|
public String DiamondData { get; set; } |
|
|
|
public ColorConfigEntity ColorConfig { get; set; } = new ColorConfigEntity(); |
|
|
|
/// <summary> |
|
/// 初始化构造 |
|
/// </summary> |
|
/// <param name="diamondCode">钻石编码</param> |
|
/// <param name="diamondData">钻石数据</param> |
|
public ViewportData(string diamondCode, string diamondData) |
|
{ |
|
DiamondCode = diamondCode; |
|
DiamondData = diamondData; |
|
} |
|
/// <summary> |
|
/// 初始化构造 |
|
/// </summary> |
|
/// <param name="diamondCode">钻石编码</param> |
|
/// <param name="diamondData">钻石数据</param> |
|
public ViewportData(string diamondCode, string diamondData, ColorConfigEntity colorConfig) |
|
{ |
|
DiamondCode = diamondCode; |
|
DiamondData = diamondData; |
|
ColorConfig = colorConfig; |
|
} |
|
/// <summary> |
|
/// 空白构造 |
|
/// </summary> |
|
public ViewportData() |
|
{ |
|
} |
|
[Log] |
|
public void LoadData() |
|
{ |
|
var settings = new JsonSerializerSettings |
|
{ |
|
ContractResolver = new DefaultValueContractResolver(), |
|
NullValueHandling = NullValueHandling.Ignore |
|
}; |
|
DiaResult? diamondData = null; |
|
try |
|
{ |
|
diamondData = JsonConvert.DeserializeObject<AlgorithmResultEntity>(DiamondData.ToSafeString(),settings); |
|
} |
|
catch |
|
{ |
|
throw new Exception("DiamondData is invalid"); |
|
} |
|
|
|
if(diamondData == null) throw new Exception("DiamondData is invalid"); |
|
|
|
try |
|
{ |
|
List<Viewport3DTriangleEntity> facets = new List<Viewport3DTriangleEntity>(); |
|
if (diamondData.Facets != null) |
|
{ |
|
foreach (var item in diamondData.Facets) |
|
{ |
|
var value = CommonHelper.CreateByJsonStr(JsonConvert.SerializeObject(item, settings)); |
|
facets.Add(value); |
|
} |
|
} |
|
else |
|
{ |
|
throw new Exception("facets is invalid"); |
|
} |
|
|
|
// if (json.AsObject()["positive_direction"] != null) |
|
// { |
|
// JsonObject positive = json.AsObject()["positive_direction"].AsObject(); |
|
// ViewportManager.PositiveDirection.X = float.Parse(positive["x"]?.ToString() ?? "1.0"); |
|
// ViewportManager.PositiveDirection.Y = float.Parse(positive["z"]?.ToString() ?? "0"); |
|
// ViewportManager.PositiveDirection.Z = float.Parse(positive["y"]?.ToString() ?? "0"); |
|
// } |
|
// else |
|
// { |
|
// ViewportManager.PositiveDirection.X = 1.0f; |
|
// ViewportManager.PositiveDirection.Y = 0f; |
|
// ViewportManager.PositiveDirection.Z = 0; |
|
// } |
|
ViewportManager.PositiveDirection.X = 1.0f; |
|
ViewportManager.PositiveDirection.Y = 0f; |
|
ViewportManager.PositiveDirection.Z = 0; |
|
|
|
ViewportManager.DiamondCode = DiamondCode; |
|
ViewportManager.DiamondType = diamondData.ErrorMsg.ToSafeString(); |
|
|
|
ViewportManager.DiamondData = JsonObject.Parse(JsonConvert.SerializeObject(diamondData.Measurements)).AsObject(); |
|
var midZ = facets.SelectMany(e => new[] { e.Point1.X, e.Point2.X, e.Point3.X }).OrderBy(z => Math.Abs(z)) |
|
.First(); |
|
List<Viewport3DTriangleEntity> facetsFinal = new List<Viewport3DTriangleEntity>(); |
|
foreach (var item in facets) |
|
{ |
|
var data = ViewportHelperPro.VectorClockwiseSort(new List<Vector3> |
|
{ item.Point1, item.Point2, item.Point3 }); |
|
|
|
if (item.PlaneType == PlaneType.StarFacet || item.PlaneType == PlaneType.UpperGirdleFacet || |
|
item.PlaneType == PlaneType.UpperMainFacet || item.PlaneType == PlaneType.TableFacet) |
|
{ |
|
item.Point1 = data[0]; |
|
item.Point2 = data[1]; |
|
item.Point3 = data[2]; |
|
} |
|
else if (item.PlaneType == PlaneType.Girdle) |
|
{ |
|
var center = ViewportHelperPro.GetCentroid(data); |
|
data.Sort((v1, v2) => |
|
{ |
|
double angle1 = Math.Atan2(v1.Y - center.Y, v1.X - center.X); |
|
double angle2 = Math.Atan2(v2.Y - center.Y, v2.X - center.X); |
|
return angle1.CompareTo(angle2); |
|
}); |
|
if (center.Z >= midZ) |
|
{ |
|
|
|
item.Point1 = data[0]; |
|
item.Point2 = data[1]; |
|
item.Point3 = data[2]; |
|
} |
|
else |
|
{ |
|
item.Point1 = data[2]; |
|
item.Point2 = data[1]; |
|
item.Point3 = data[0]; |
|
|
|
} |
|
} |
|
else |
|
{ |
|
item.Point1 = data[2]; |
|
item.Point2 = data[1]; |
|
item.Point3 = data[0]; |
|
} |
|
|
|
item.TriangleCode = CommonHelper.GenerateTriangleCode(item.Point1, item.Point2, item.Point3); |
|
facetsFinal.Add(item); |
|
} |
|
|
|
if (diamondData.Status.Equals("P021")) |
|
{ |
|
ViewportManager.isUglyDiamond = true; |
|
} |
|
else |
|
{ |
|
ViewportManager.isUglyDiamond = false; |
|
} |
|
|
|
ViewportManager.ViewportTriangle.Clear(); |
|
ViewportManager.ViewportTriangle.AddRange(facetsFinal); |
|
ViewportManager.ColorConfig = ColorConfig; |
|
// ViewportManager.LoadModelByEntities(facetsFinal); |
|
} |
|
catch (Exception ex) |
|
{ |
|
throw new Exception(ex.Message); |
|
} |
|
} |
|
|
|
[Log] |
|
public async Task SaveAsToStlFile(string filename) |
|
{ |
|
try |
|
{ |
|
await ViewportHelperPro.ExportModelsToStlASync(filename); |
|
return ; |
|
} |
|
catch |
|
{ |
|
return ; |
|
} |
|
} |
|
[Log] |
|
public async Task SaveAsToVedioFile(string filename) |
|
{ |
|
try |
|
{ |
|
await ViewportHelperPro.ExportModelsToVideo(null, filename); |
|
|
|
} |
|
catch |
|
{ |
|
return ; |
|
} |
|
} |
|
[Log] |
|
public bool ConvertMp4ToDat(string mp4FilePath, string datFilePath) |
|
{ |
|
|
|
try |
|
{ |
|
VideoHelper.ConvertMp4ToDat(datFilePath, mp4FilePath); |
|
return true; |
|
} |
|
catch |
|
{ |
|
return false; |
|
} |
|
} |
|
|
|
public void mockSel(string planKey) |
|
{ |
|
ViewportManager.MockSelectFacet(planKey); |
|
} |
|
|
|
} |