From 5005419912a435c73b8f5ef68b4bec4e0ac31c19 Mon Sep 17 00:00:00 2001 From: Tongg Date: Mon, 10 Feb 2025 16:51:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=203D=E6=A8=A1=E5=9E=8B=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SparkClient.csproj | 8 + SparkClient.sln.DotSettings.user | 6 + Views/UserControl/Viewport3D.xaml.cs | 2 +- .../ViewportData/Helper/UnityHelper.cs | 16 +- .../ViewportData/Helper/ViewportHelperPro.cs | 179 ++++++++++++++---- .../ViewportData/Helper/ViewportManager.cs | 5 +- 6 files changed, 173 insertions(+), 43 deletions(-) diff --git a/SparkClient.csproj b/SparkClient.csproj index c60e2e5..8d59447 100644 --- a/SparkClient.csproj +++ b/SparkClient.csproj @@ -7,6 +7,13 @@ enable true logo.ico + SparkClient - 星辉 + SparkClient Team + SparkClient钻石检测工具 + Copyright ©2025 北京跃洋新视科技有限公司 + 北京跃洋新视科技有限公司 + 1.0.0 + 1.0.0 @@ -19,6 +26,7 @@ + diff --git a/SparkClient.sln.DotSettings.user b/SparkClient.sln.DotSettings.user index 117353a..9b4669a 100644 --- a/SparkClient.sln.DotSettings.user +++ b/SparkClient.sln.DotSettings.user @@ -32,8 +32,10 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -55,11 +57,14 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -78,6 +83,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded diff --git a/Views/UserControl/Viewport3D.xaml.cs b/Views/UserControl/Viewport3D.xaml.cs index e1effc6..c1c3c6b 100644 --- a/Views/UserControl/Viewport3D.xaml.cs +++ b/Views/UserControl/Viewport3D.xaml.cs @@ -220,7 +220,7 @@ public partial class Viewport3D break; case "BtnShow3DView": ObjExporter.ExportToObj2(ViewportManager.ViewportTriangle, @"D:\id03.obj"); - //UnityHelper.GenerateRender(ViewportManager.ViewportTriangle.First().TriangleCode, "123"); + UnityHelper.GenerateRender(ViewportManager.ViewportTriangle.First().TriangleCode, "123"); break; } diff --git a/Views/UserControl/ViewportData/Helper/UnityHelper.cs b/Views/UserControl/ViewportData/Helper/UnityHelper.cs index 4c9c28b..54f75e0 100644 --- a/Views/UserControl/ViewportData/Helper/UnityHelper.cs +++ b/Views/UserControl/ViewportData/Helper/UnityHelper.cs @@ -28,7 +28,7 @@ public class UnityHelper */ public static void GenerateRender(string modelInfo, string diamondCode) { - string fileName = GenerateSignature(modelInfo, diamondCode) + ".stl"; + string fileName = GenerateSignature(modelInfo, diamondCode); string fullStlPath = Path.Combine(_stl_path, fileName); // 1. 检查STL文件是否存在 @@ -46,8 +46,8 @@ public class UnityHelper CleanDirectory(_stl_path); // 3. 导出模型为STL - ExportModelsToStl(fullStlPath); - + ExportModelsToStl(fullStlPath + ".stl"); + ObjExporter.ExportToObj2(ViewportManager.ViewportTriangle, fullStlPath + ".obj"); // 4. 执行Unity渲染命令两次 ExecuteUnityRendering(fileName, 0); // 渲染心图 ExecuteUnityRendering(fileName, 1); // 渲染箭图 @@ -127,13 +127,15 @@ public class UnityHelper { Text = "心箭图模拟渲染", Width = 800, - Height = 600 + Height = 600, + MaximizeBox = false, + MinimizeBox = false, }; PictureBox heartBox = new PictureBox { ImageLocation = Path.Combine(_image_path, "0.png"), - SizeMode = PictureBoxSizeMode.Zoom, + SizeMode = PictureBoxSizeMode.CenterImage, Dock = DockStyle.Left, Width = 400 }; @@ -141,7 +143,7 @@ public class UnityHelper PictureBox arrowBox = new PictureBox { ImageLocation = Path.Combine(_image_path, "1.png"), - SizeMode = PictureBoxSizeMode.Zoom, + SizeMode = PictureBoxSizeMode.CenterImage, Dock = DockStyle.Right, Width = 400 }; @@ -151,7 +153,7 @@ public class UnityHelper Label label = new Label { - Text = "心箭图为模拟渲染,实际请以切工镜观测为准。", + Text = "[预览版]心箭图为模拟渲染,实际请以切工镜观测为准。", Dock = DockStyle.Bottom, Height = 30, TextAlign = System.Drawing.ContentAlignment.MiddleCenter diff --git a/Views/UserControl/ViewportData/Helper/ViewportHelperPro.cs b/Views/UserControl/ViewportData/Helper/ViewportHelperPro.cs index f512564..6b72f65 100644 --- a/Views/UserControl/ViewportData/Helper/ViewportHelperPro.cs +++ b/Views/UserControl/ViewportData/Helper/ViewportHelperPro.cs @@ -328,34 +328,94 @@ public class ViewportHelperPro { List result = new List(); //按面分组,腰面特殊单独生成 - List waistList = entities - .Where(entity => entity.PlaneType == PlaneType.Girdle) - .ToList(); - List selFaceVector = new List(); + // List waistList = entities + // .Where(entity => entity.PlaneType == PlaneType.Girdle) + // .ToList(); + // List selFaceVector = new List(); List> lines = new List>(); - if (waistList.Count > 0) + // if (waistList.Count > 0) + // { + // foreach (var entity in waistList) + // { + // selFaceVector.Add(entity.Point1); + // selFaceVector.Add(entity.Point2); + // selFaceVector.Add(entity.Point3); + // } + // HashSet uniqueVectors = new HashSet(selFaceVector); + // var facetPoints = VectorClockwiseSort(uniqueVectors.ToList()); + // for (int i = 0; i < facetPoints.ToList().Count - 1; i++) + // { + // var nowItem = facetPoints.ToList()[i]; + // var nextItem = facetPoints.ToList()[i + 1]; + // var line = new Tuple(nowItem, nextItem); + // if (IsLineSegmentParallelToYAxis(line)) + // { + // lines.Add(line); + // result.Add(DisplayLineModel3D(new List() { nowItem, nextItem }, color ?? ViewportManager.ColorConfig.MainBorderColor, thickness)); + // } + // } + // } + + Dictionary> feactList = entities + .Where(entity => entity.PlaneType == PlaneType.Girdle) + .GroupBy(entity => entity.PlaneCode) + .ToDictionary(group => group.Key, group => group.ToList()); + + foreach (var yaoFace in feactList) { - foreach (var entity in waistList) + List selFaceVector = new List(); + foreach (var entity in yaoFace.Value) { selFaceVector.Add(entity.Point1); selFaceVector.Add(entity.Point2); selFaceVector.Add(entity.Point3); } HashSet uniqueVectors = new HashSet(selFaceVector); - - for (int i = 0; i < selFaceVector.ToList().Count - 1; i++) + + var facetPoints = VectorClockwiseSort(uniqueVectors.ToList()); + Vector3 center = GetCentroid(uniqueVectors.ToList()); + Console.WriteLine($"{yaoFace.Key} \t {center.X} \t {center.Y} \t {center.Z}"); + List leftLine = new List(); + List rightLine = new List(); + + foreach (var point in facetPoints) { - var nowItem = selFaceVector.ToList()[i]; - var nextItem = selFaceVector.ToList()[i + 1]; - var line = new Tuple(nowItem, nextItem); - if (IsLineSegmentParallelToYAxis(line)) + var direction = point - center; + + var cross = Vector3.Cross(new Vector3(0, 0, 1), direction); + + if (cross.Y >= 0) + { + rightLine.Add(point); + } + else { - lines.Add(line); - result.Add(DisplayLineModel3D(new List() { nowItem, nextItem }, color ?? ViewportManager.ColorConfig.MainBorderColor, thickness)); + leftLine.Add(point); } } - } + if (rightLine.Count == 2 && leftLine.Count == 2) + { + var leftLineTuple = new Tuple(leftLine.First(), leftLine.Last()); + var rightLineTuple = new Tuple(rightLine.First(), rightLine.Last()); + lines.Add(leftLineTuple); + lines.Add(rightLineTuple); + result.Add(DisplayLineModel3D(new List() { leftLine.First(), leftLine.Last() }, color ?? ViewportManager.ColorConfig.MainBorderColor, thickness)); + result.Add(DisplayLineModel3D(new List() { rightLine.First(), rightLine.Last() }, color ?? ViewportManager.ColorConfig.MainBorderColor, thickness)); + } + // for (int i = 0; i < facetPoints.ToList().Count - 1; i++) + // { + // var nowItem = facetPoints.ToList()[i]; + // var nextItem = facetPoints.ToList()[i + 1]; + // var line = new Tuple(nowItem, nextItem); + // if (IsLineSegmentParallelToYAxis(line)) + // { + // lines.Add(line); + // result.Add(DisplayLineModel3D(new List() { nowItem, nextItem }, color ?? ViewportManager.ColorConfig.MainBorderColor, thickness)); + // } + // } + } + CalculateLineSegmentStats(lines, out ViewportManager.MainModelGirdleMaxLines, out ViewportManager.MainModelGirdleMinLines, out ViewportManager.MainModelGirdleAvgLines); ViewportManager.MainModelGirdleLines = lines; return result; @@ -490,20 +550,27 @@ public class ViewportHelperPro return result; } int linePointType = facetIndex % 4; - + Console.WriteLine(facetIndex); switch (linePointType) { case 0: - case 3: - var longestLine1 = GetLongestOrShortestLineSegment(facetPoints, returnLongest: true); + case 2: + var longestLine1 = GetLeftOrRightLineSegment(facetPoints, facetIndex >= 24 && facetIndex <= 55 ? true : false); result.Add(DisplayLineModel3D(new List>() { longestLine1 }, new Color4(1f, 0, 0, 1f), 2f)); break; case 1: - case 2: - var longestLine2 = GetLongestOrShortestLineSegment(facetPoints, returnLongest: false); + case 3: + var longestLine2 = GetLeftOrRightLineSegment(facetPoints, facetIndex >= 24 && facetIndex <= 55 ? false : true); result.Add(DisplayLineModel3D(new List>() { longestLine2 }, new Color4(1f, 0, 0, 1f), 2f)); break; } + + if (result.Exists(e => e == null)) + { + Logger.Info("目标线段为空"); + return result; + } + //开始找值 string param = string.Empty; var valueIndex = 0; @@ -1129,20 +1196,28 @@ public class ViewportHelperPro public static LineGeometryModel3D DisplayLineModel3D(List> points, Color4 lineColor, double thickness = 1.0) { - var lineBuilder = new LineBuilder(); - foreach (var line in points) + try { - lineBuilder.AddLine(line.Item1, line.Item2); + var lineBuilder = new LineBuilder(); + foreach (var line in points) + { + lineBuilder.AddLine(line.Item1, line.Item2); + } + var edgeLinesModel = new LineGeometryModel3D + { + Geometry = lineBuilder.ToLineGeometry3D(), + Color = lineColor.ToColor(), + Thickness = thickness, + }; + return edgeLinesModel; } - - - var edgeLinesModel = new LineGeometryModel3D + catch (Exception e) { - Geometry = lineBuilder.ToLineGeometry3D(), - Color = lineColor.ToColor(), - Thickness = thickness, - }; - return edgeLinesModel; + Logger.Error(e.Message); + Logger.Error(e.StackTrace); + + return null; + } } /// @@ -1865,12 +1940,12 @@ public class ViewportHelperPro var v = p2 - p0; return Vector3.Cross(u, v); } - private static bool IsLineSegmentParallelToYAxis(Tuple lineSegment,float epsilon = 1e-6f) + private static bool IsLineSegmentParallelToYAxis(Tuple lineSegment,float epsilon = 0.001f) { Vector3 nowItem = lineSegment.Item1; Vector3 nextItem = lineSegment.Item2; - - if (Math.Abs(nowItem.X - nextItem.X) < epsilon && Math.Abs(nowItem.Z - nextItem.Z) < epsilon) + //&& Math.Abs(nowItem.Z - nextItem.Z) < epsilon + if (Math.Abs(nowItem.X - nextItem.X) < epsilon ) { return true; // 平行于 Y 轴 } @@ -1888,6 +1963,8 @@ public class ViewportHelperPro Tuple resultSegment = null; float resultLength = returnLongest ? float.MinValue : float.MaxValue; // 初始化为最小或最大长度 + facetPoints = VectorClockwiseSort(new System.Collections.Generic.HashSet(facetPoints).ToList()); + // 遍历所有相邻的点,构成线段 for (int i = 0; i < facetPoints.Count; i++) { @@ -1911,6 +1988,40 @@ public class ViewportHelperPro return resultSegment; // 返回符合条件的线段 } + + private static Tuple GetLeftOrRightLineSegment(List facetPoints, + bool isLeft = true) + { + Tuple resultSegment = null; + //去重 + facetPoints = VectorClockwiseSort(new System.Collections.Generic.HashSet(facetPoints).ToList()); + //计算中心点 + Vector3 center = GetCentroid(facetPoints); + + List leftList = new List(); + List rightList = new List(); + + foreach (var point in facetPoints) + { + Vector3 direction = point - center; + Vector3 cross = Vector3.Cross(new Vector3(0,0,1), direction); + if (cross.Y >= 0) + rightList.Add(point); + else + leftList.Add(point); + } + + + if (isLeft) + resultSegment = new Tuple(leftList.First(), leftList.Last()); + else + resultSegment = new Tuple(rightList.First(), rightList.Last()); + + + return resultSegment; // 返回符合条件的线段 + } + + private static void CalculateLineSegmentStats(List> lines, out Tuple maxLine, out Tuple minLine, diff --git a/Views/UserControl/ViewportData/Helper/ViewportManager.cs b/Views/UserControl/ViewportData/Helper/ViewportManager.cs index f33c2dc..47b60a2 100644 --- a/Views/UserControl/ViewportData/Helper/ViewportManager.cs +++ b/Views/UserControl/ViewportData/Helper/ViewportManager.cs @@ -166,7 +166,10 @@ public class ViewportManager // if (DoubleClickSelectShowBorderAngle) models.Add(SelShowType.BorderAngle); if (DoubleClickSelectShowPlaneType) models.Add(SelShowType.IsTypePanel); ChooseAddModels.AddRange(ViewportHelperPro.GentrateChosenView(ChooseTriangleCode, valType, models.ToArray())); - ChooseAddModels.ForEach(e => _viewport.Items.Add(e)); + ChooseAddModels.ForEach(e => + { + if (e != null) _viewport.Items.Add(e); + }); } /// /// 清除所有的选中效果