From 12a6316ffa897b4ce4205f545b88359195b386d6 Mon Sep 17 00:00:00 2001
From: duheng <2784771470@qq.com>
Date: 星期三, 25 九月 2024 17:34:24 +0800
Subject: [PATCH] Merge branch 'master' of http://47.103.154.90:83/r/HStation/XHS.V1.0

---
 WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/04-link/Valve.cs |  129 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 129 insertions(+), 0 deletions(-)

diff --git a/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/04-link/Valve.cs b/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/04-link/Valve.cs
index 1c64370..89022cd 100644
--- a/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/04-link/Valve.cs
+++ b/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/01-network/04-link/Valve.cs
@@ -66,5 +66,134 @@
         public float? SelectedHeight { get; set; }
 
 
+        //鍖呭洿鐩�
+        private BoundingBox3d _boundingBox = null;
+
+        //鑾峰彇鍖呭洿鐩�
+        private BoundingBox3d GetBoundingBox()
+        {
+            var lineWidth = this.LineWidth.HasValue ? this.LineWidth.Value : CacheHelper.HydroL3d.Pipe.Line.Width;
+            return new BoundingBox3d()
+            {
+                Min = new Point3d()
+                {
+                    X = Math.Min(this.StartPosition.X, this.EndPosition.X) - lineWidth / 2f,
+                    Y = Math.Min(this.StartPosition.Y, this.EndPosition.Y) - lineWidth / 2f,
+                    Z = Math.Min(this.StartPosition.Z, this.EndPosition.Z) - lineWidth / 2f
+                },
+                Max = new Point3d()
+                {
+                    X = Math.Max(this.StartPosition.X, this.EndPosition.X) + lineWidth / 2f,
+                    Y = Math.Max(this.StartPosition.Y, this.EndPosition.Y) + lineWidth / 2f,
+                    Z = Math.Max(this.StartPosition.Z, this.EndPosition.Z) + lineWidth / 2f
+                }
+            };
+        }
+
+        /// <summary>
+        /// 缁樺埗
+        /// </summary>
+        public override void Draw(SharpGL.OpenGL gl)
+        {
+            _boundingBox = GetBoundingBox();
+            if (this.Hovered)
+            {
+                var lineWidth = this.HoveredLineWidth.HasValue ? this.HoveredLineWidth.Value : CacheHelper.HydroL3d.Pipe.HoveredLine.Width;
+                var lineColor = this.HoveredLineColor.HasValue ? this.HoveredLineColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Pipe.HoveredLine.Color);
+                DrawPipe(gl, lineWidth / 2f, lineColor);
+            }
+            else if (this.Selected)
+            {
+                var lineWidth = this.SelectedLineWidth.HasValue ? this.SelectedLineWidth.Value : CacheHelper.HydroL3d.Pipe.SelectedLine.Width;
+                var lineColor = this.SelectedLineColor.HasValue ? this.SelectedLineColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Pipe.SelectedLine.Color);
+                DrawPipe(gl, lineWidth / 2f, lineColor);
+            }
+            else
+            {
+                var lineWidth = this.LineWidth.HasValue ? this.LineWidth.Value : CacheHelper.HydroL3d.Pipe.Line.Width;
+                var lineColor = this.LineColor.HasValue ? this.LineColor.Value : ColorTranslator.FromHtml(CacheHelper.HydroL3d.Pipe.Line.Color);
+                DrawPipe(gl, lineWidth / 2f, lineColor);
+            }
+        }
+
+        //缁樺埗绠¢亾
+        private void DrawPipe(SharpGL.OpenGL gl, float radius, Color fillColor)
+        {
+            gl.Color(fillColor.R / 255.0f, fillColor.G / 255.0f, fillColor.B / 255.0f);
+
+            int slices = 32;  // 绠¢亾鍦嗗懆鐨勫垎娈垫暟锛屽彲鏍规嵁闇�瑕佷慨鏀�
+
+            Point3d direction = new Point3d(this.EndPosition - this.StartPosition);
+            float length = direction.Length();
+            direction.Normalize();
+
+            // 璁$畻鏃嬭浆杞�
+            Point3d axis = Point3d.CrossProduct(new Point3d(0, 0, 1), direction);
+            if (axis.LengthSquared() < 0.0001f)
+            {
+                axis = new Point3d(1, 0, 0);
+            }
+            axis.Normalize();
+
+            float angle = (float)Math.Acos(Point3d.DotProduct(new Point3d(0, 0, 1), direction));
+
+            gl.PushMatrix();
+
+            // 骞崇Щ鍒拌捣鐐�
+            gl.Translate(this.StartPosition.X, this.StartPosition.Y, this.StartPosition.Z);
+
+            // 缁曡酱鏃嬭浆
+            gl.Rotate(angle * 180.0f / (float)Math.PI, axis.X, axis.Y, axis.Z);
+
+            // 缁樺埗绠¢亾渚ч潰
+            gl.Begin(OpenGL.GL_QUAD_STRIP);
+            for (int i = 0; i <= slices; i++)
+            {
+                float theta = (float)i / slices * 2.0f * (float)Math.PI;
+                float x = radius * (float)Math.Cos(theta);
+                float y = radius * (float)Math.Sin(theta);
+
+                gl.Vertex(x, y, 0.0f);
+                gl.Vertex(x, y, length);
+            }
+            gl.End();
+
+            // 缁樺埗绠¢亾涓ょ鐨勫渾闈�
+            gl.Begin(OpenGL.GL_POLYGON);
+            for (int i = 0; i <= slices; i++)
+            {
+                float theta = (float)i / slices * 2.0f * (float)Math.PI;
+                float x = radius * (float)Math.Cos(theta);
+                float y = radius * (float)Math.Sin(theta);
+
+                gl.Vertex(x, y, 0.0f);
+            }
+            gl.End();
+
+            gl.Begin(OpenGL.GL_POLYGON);
+            for (int i = 0; i <= slices; i++)
+            {
+                float theta = (float)i / slices * 2.0f * (float)Math.PI;
+                float x = radius * (float)Math.Cos(theta);
+                float y = radius * (float)Math.Sin(theta);
+
+                gl.Vertex(x, y, length);
+            }
+            gl.End();
+
+            gl.PopMatrix();
+        }
+
+        /// <summary>
+        /// 鍖呭惈
+        /// </summary>
+        public override bool Contains(Point3d pt)
+        {
+            if (_boundingBox == null)
+            {
+                return false;
+            }
+            return _boundingBox.Contains(pt);
+        }
     }
 }

--
Gitblit v1.9.3