From 1813c9831d09fdf21d30e209ac5a3af0a834ddd2 Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Fri, 16 Jan 2026 21:06:20 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=BD=91=E6=A0=BC=E5=8F=AF?= =?UTF-8?q?=E8=A7=86=E5=8C=96=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E7=9A=84=E7=BB=93=E6=9E=84=E5=92=8C=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NavisworksTransportPlugin.csproj | 1 + src/Core/GridVisualization.cs | 111 ++++++++++++++++++++++ src/Core/PathPlanningManager.cs | 114 +++++++--------------- src/Core/PathPointRenderPlugin.cs | 153 ++++++++++++++++++++++++++++++ 4 files changed, 301 insertions(+), 78 deletions(-) create mode 100644 src/Core/GridVisualization.cs diff --git a/NavisworksTransportPlugin.csproj b/NavisworksTransportPlugin.csproj index ab979e2..1098c34 100644 --- a/NavisworksTransportPlugin.csproj +++ b/NavisworksTransportPlugin.csproj @@ -113,6 +113,7 @@ + diff --git a/src/Core/GridVisualization.cs b/src/Core/GridVisualization.cs new file mode 100644 index 0000000..dd167a0 --- /dev/null +++ b/src/Core/GridVisualization.cs @@ -0,0 +1,111 @@ +using System.Collections.Generic; +using Autodesk.Navisworks.Api; + +namespace NavisworksTransport +{ + /// + /// 网格可视化类型(仅用于网格可视化,不用于数据模型) + /// + public enum GridVisualizationType + { + /// + /// 可通行通道(绿色) + /// + Walkable, + + /// + /// 障碍物(灰色) + /// + Obstacle, + + /// + /// 未知区域(红色) + /// + Unknown, + + /// + /// 门(半透明绿色) + /// + Door + } + + /// + /// 网格可视化标记点 + /// + public class GridMarker + { + /// + /// 3D 位置 + /// + public Point3D Position { get; set; } + + /// + /// 网格可视化类型(可通行性) + /// + public GridVisualizationType CellType { get; set; } + + /// + /// 网格坐标 X + /// + public int GridX { get; set; } + + /// + /// 网格坐标 Y + /// + public int GridY { get; set; } + + /// + /// 高度层 Z 坐标 + /// + public double LayerZ { get; set; } + + public GridMarker() { } + + public GridMarker(Point3D position, GridVisualizationType cellType, int gridX, int gridY, double layerZ) + { + Position = position; + CellType = cellType; + GridX = gridX; + GridY = gridY; + LayerZ = layerZ; + } + } + + /// + /// 网格可视化集合 + /// + public class GridVisualization + { + /// + /// 可视化 ID + /// + public string Id { get; set; } + + /// + /// 可通行的网格标记(绿色) + /// + public List Walkable { get; } = new List(); + + /// + /// 障碍物网格标记(灰色) + /// + public List Obstacle { get; } = new List(); + + /// + /// 未知区域网格标记(红色) + /// + public List Unknown { get; } = new List(); + + /// + /// 门网格标记(半透明绿色) + /// + public List Door { get; } = new List(); + + public GridVisualization() { } + + public GridVisualization(string id) + { + Id = id; + } + } +} diff --git a/src/Core/PathPlanningManager.cs b/src/Core/PathPlanningManager.cs index c36db7f..fe238fd 100644 --- a/src/Core/PathPlanningManager.cs +++ b/src/Core/PathPlanningManager.cs @@ -3678,26 +3678,8 @@ namespace NavisworksTransport // 清除之前的网格可视化 ClearGridVisualization(); - // 创建四种独立的网格可视化路径 - var channelRoute = new PathRoute("GridVisualization_Channel") - { - Id = "grid_visualization_channel" - }; - - var unknownRoute = new PathRoute("GridVisualization_Unknown") - { - Id = "grid_visualization_unknown" - }; - - var obstacleRoute = new PathRoute("GridVisualization_Obstacle") - { - Id = "grid_visualization_obstacle" - }; - - var doorRoute = new PathRoute("GridVisualization_Door") - { - Id = "grid_visualization_door" - }; + // 创建网格可视化集合 + var gridVis = new GridVisualization("grid_visualization"); // 统计变量 int channelCells = 0; @@ -3729,8 +3711,7 @@ namespace NavisworksTransport { double layerZ = layer.Z; - PathRoute targetRoute = null; - string gridTypeName = ""; + GridVisualizationType? cellType = null; // 仅根据 layer.IsWalkable 决定渲染 if (layer.IsWalkable) @@ -3740,8 +3721,7 @@ namespace NavisworksTransport { if (_showDoorGrid) { - targetRoute = doorRoute; - gridTypeName = "门"; + cellType = GridVisualizationType.Door; doorCells++; } } @@ -3750,8 +3730,7 @@ namespace NavisworksTransport // 其他可通行类型统一为通道样式(绿色) if (_showWalkableGrid) { - targetRoute = channelRoute; - gridTypeName = "通道"; + cellType = GridVisualizationType.Walkable; channelCells++; } } @@ -3761,27 +3740,33 @@ namespace NavisworksTransport // 不可通行层 - 渲染为障碍物(灰色) if (_showObstacleGrid) { - targetRoute = obstacleRoute; - gridTypeName = "障碍"; + cellType = GridVisualizationType.Obstacle; obstacleCells++; } } - // 创建该层的可视化点 - if (targetRoute != null) + // 创建该层的可视化标记 + if (cellType.HasValue) { var gridCenter = new Point3D(centerX, centerY, layerZ); + var marker = new GridMarker(gridCenter, cellType.Value, x, y, layerZ); - var gridPoint = new PathPoint + // 根据类型添加到对应列表 + switch (cellType.Value) { - Position = gridCenter, - Name = $"网格_{gridTypeName}({x},{y})_Z{layerZ:F2}", - Type = PathPointType.WayPoint, - Index = totalVisualized, - Notes = $"GridType:{cell.CellType}, LayerZ={layerZ:F2}, IsWalkable={layer.IsWalkable}" - }; - - targetRoute.Points.Add(gridPoint); + case GridVisualizationType.Walkable: + gridVis.Walkable.Add(marker); + break; + case GridVisualizationType.Obstacle: + gridVis.Obstacle.Add(marker); + break; + case GridVisualizationType.Unknown: + gridVis.Unknown.Add(marker); + break; + case GridVisualizationType.Door: + gridVis.Door.Add(marker); + break; + } totalVisualized++; } } @@ -3792,15 +3777,8 @@ namespace NavisworksTransport if (_showUnknownGrid) { var gridCenter = new Point3D(centerX, centerY, gridCorner.Z); - var gridPoint = new PathPoint - { - Position = gridCenter, - Name = $"网格_空洞({x},{y})", - Type = PathPointType.WayPoint, - Index = totalVisualized, - Notes = $"GridType:Unknown, NoLayers" - }; - unknownRoute.Points.Add(gridPoint); + var marker = new GridMarker(gridCenter, GridVisualizationType.Unknown, x, y, gridCorner.Z); + gridVis.Unknown.Add(marker); unknownCells++; totalVisualized++; } @@ -3808,30 +3786,8 @@ namespace NavisworksTransport } } - // 分别渲染四种类型的网格点 - if (channelRoute.Points.Count > 0) - { - renderPlugin.RenderPointOnly(channelRoute); - LogManager.Info($"[网格可视化] 渲染可通行网格层: {channelRoute.Points.Count} 个(绿色)"); - } - - if (doorRoute.Points.Count > 0) - { - renderPlugin.RenderPointOnly(doorRoute); - LogManager.Info($"[网格可视化] 渲染门网格层: {doorRoute.Points.Count} 个(50%透明绿色)"); - } - - if (unknownRoute.Points.Count > 0) - { - renderPlugin.RenderPointOnly(unknownRoute); - LogManager.Info($"[网格可视化] 渲染Unknown网格: {unknownRoute.Points.Count} 个(红色,调试用)"); - } - - if (obstacleRoute.Points.Count > 0) - { - renderPlugin.RenderPointOnly(obstacleRoute); - LogManager.Info($"[网格可视化] 渲染障碍物网格层: {obstacleRoute.Points.Count} 个(灰色)"); - } + // 使用新的专门渲染方法(类型安全) + renderPlugin.RenderGridVisualization(gridVis); // 输出统计信息 LogManager.Info($"[网格可视化] 可视化完成:"); @@ -3864,12 +3820,14 @@ namespace NavisworksTransport var renderPlugin = PathPointRenderPlugin.Instance; if (renderPlugin != null) { - // 清除所有网格可视化路径(包括旧的和新的) - renderPlugin.RemovePath("grid_visualization_all"); // 旧的单一路径 - renderPlugin.RemovePath("grid_visualization_channel"); // 新的通道路径 - renderPlugin.RemovePath("grid_visualization_unknown"); // 新的Unknown路径 - renderPlugin.RemovePath("grid_visualization_obstacle"); // 新的障碍物路径 - renderPlugin.RemovePath("grid_visualization_door"); // 新的门路径 + // 清除所有网格可视化(使用新的 ID 命名) + renderPlugin.RemovePath("grid_visualization_walkable"); // 可通行的网格 + renderPlugin.RemovePath("grid_visualization_obstacle"); // 障碍物网格 + renderPlugin.RemovePath("grid_visualization_unknown"); // 未知区域网格 + renderPlugin.RemovePath("grid_visualization_door"); // 门网格 + // 清除旧的网格可视化 ID(向后兼容) + renderPlugin.RemovePath("grid_visualization_all"); + renderPlugin.RemovePath("grid_visualization_channel"); LogManager.Info("[网格可视化] 网格可视化已清除"); } else diff --git a/src/Core/PathPointRenderPlugin.cs b/src/Core/PathPointRenderPlugin.cs index f6cbdc5..53d0547 100644 --- a/src/Core/PathPointRenderPlugin.cs +++ b/src/Core/PathPointRenderPlugin.cs @@ -699,6 +699,159 @@ namespace NavisworksTransport } } + /// + /// 清除网格可视化 + /// + public void ClearGridVisualization() + { + try + { + lock (_lockObject) + { + // 清除所有网格可视化 + RemovePath("grid_visualization_walkable"); + RemovePath("grid_visualization_obstacle"); + RemovePath("grid_visualization_unknown"); + RemovePath("grid_visualization_door"); + // 清除旧的网格可视化 ID(向后兼容) + RemovePath("grid_visualization_all"); + RemovePath("grid_visualization_channel"); + } + + RequestViewRefresh(); + } + catch (Exception ex) + { + LogManager.Error($"[网格可视化] 清除网格可视化失败: {ex.Message}", ex); + } + } + + /// + /// 渲染网格可视化(使用专门的 GridMarker 类型) + /// + /// 网格可视化对象 + public void RenderGridVisualization(GridVisualization gridVis) + { + if (gridVis == null) + { + LogManager.Warning("[网格可视化] GridVisualization 对象为 null,无法渲染"); + return; + } + + try + { + // 清除之前的网格可视化 + ClearGridVisualization(); + + // 渲染可通行的网格点(绿色) + if (gridVis.Walkable.Count > 0) + { + RenderGridMarkers("grid_visualization_walkable", gridVis.Walkable, GridVisualizationType.Walkable); + LogManager.Info($"[网格可视化] 渲染可通行网格标记: {gridVis.Walkable.Count} 个(绿色)"); + } + + // 渲染障碍物网格点(灰色) + if (gridVis.Obstacle.Count > 0) + { + RenderGridMarkers("grid_visualization_obstacle", gridVis.Obstacle, GridVisualizationType.Obstacle); + LogManager.Info($"[网格可视化] 渲染障碍物网格标记: {gridVis.Obstacle.Count} 个(灰色)"); + } + + // 渲染未知区域网格点(红色) + if (gridVis.Unknown.Count > 0) + { + RenderGridMarkers("grid_visualization_unknown", gridVis.Unknown, GridVisualizationType.Unknown); + LogManager.Info($"[网格可视化] 渲染未知网格标记: {gridVis.Unknown.Count} 个(红色)"); + } + + // 渲染门网格点(半透明绿色) + if (gridVis.Door.Count > 0) + { + RenderGridMarkers("grid_visualization_door", gridVis.Door, GridVisualizationType.Door); + LogManager.Info($"[网格可视化] 渲染门网格标记: {gridVis.Door.Count} 个(半透明绿色)"); + } + + RequestViewRefresh(); + } + catch (Exception ex) + { + LogManager.Error($"[网格可视化] 渲染失败: {ex.Message}", ex); + } + } + + /// + /// 渲染一组网格标记 + /// + /// 路径 ID + /// 网格标记列表 + /// 网格可视化类型 + private void RenderGridMarkers(string pathId, List markers, GridVisualizationType cellType) + { + if (markers == null || markers.Count == 0) + { + return; + } + + var visualization = new PathVisualization + { + PathId = pathId, + PathRoute = null // 网格可视化不使用 PathRoute + }; + + // 创建点标记 + foreach (var marker in markers) + { + var pointMarker = CreateGridMarker(marker); + visualization.PointMarkers.Add(pointMarker); + } + + lock (_lockObject) + { + _pathVisualizations[pathId] = visualization; + } + } + + /// + /// 创建网格标记(类型安全,无需字符串匹配) + /// + /// 网格标记 + /// 圆点标记对象 + private CircleMarker CreateGridMarker(GridMarker marker) + { + // 根据网格可视化类型确定颜色和透明度 + RenderStyle style = new RenderStyle(Color.White, 1.0); + switch (marker.CellType) + { + case GridVisualizationType.Walkable: + style = GetRenderStyle(RenderStyleName.GridChannel); + break; + case GridVisualizationType.Obstacle: + style = GetRenderStyle(RenderStyleName.GridObstacle); + break; + case GridVisualizationType.Unknown: + style = GetRenderStyle(RenderStyleName.GridUnknown); + break; + case GridVisualizationType.Door: + // 门使用50%透明度 + style = new RenderStyle(GetRenderStyle(RenderStyleName.GridChannel).Color, 0.5); + break; + } + + return new CircleMarker + { + Center = marker.Position, + Normal = new Vector3D(0, 0, 1), + Radius = GetRadiusForGridVisualization(), + Color = style.Color, + Alpha = style.Alpha, + Filled = true, + PointType = PathPointType.WayPoint, + GridPointType = _gridPointType, + SequenceNumber = 0, + CreatedTime = DateTime.Now + }; + } + /// /// 只构建点标记,不构建连线 ///