diff --git a/resources/default_config.toml b/resources/default_config.toml index e5d091c..589eb86 100644 --- a/resources/default_config.toml +++ b/resources/default_config.toml @@ -89,6 +89,15 @@ width_limit_meters = 3.0 # 别名树完整展示的最大层级深度(0 = 仅根节点) # 默认值 2,即展示根节点 + 两层子节点 max_full_display_depth = 2 + +[batch] + # 是否启用多进程并行批处理(默认开启) + # 关闭时批处理串行执行(并发数视为 1) + use_multi_process = true + + # 并发副 Navisworks 实例数(范围 1-20,默认 3) + # 每个实例独立端口(18778 起)与日志,用于剖面盒自动检测等任务加速 + max_parallel_instances = 3 # 自定义物流类别 # 用于扩展内置类别,只需填写类别名称 diff --git a/src/Core/BatchQueueManager.cs b/src/Core/BatchQueueManager.cs index a41c87a..91fedf7 100644 --- a/src/Core/BatchQueueManager.cs +++ b/src/Core/BatchQueueManager.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Autodesk.Navisworks.Api; using NavisworksTransport.Core.Animation; using NavisworksTransport.Core.Collision; +using NavisworksTransport.Core.Config; using NavisworksTransport.Core.Models; using NavisworksTransport.Core.Services; @@ -154,11 +155,32 @@ namespace NavisworksTransport.Core } /// - /// 解析并行副实例数:环境变量 TRANSPORTPLUGIN_MAX_PARALLEL 可调(1-20),默认 3。 + /// 解析并行副实例数:完全由 config.toml 的 [batch] 配置驱动(use_multi_process + max_parallel_instances)。 + /// 配置缺失/无效时明确报错(禁止 fallback 默认值);环境变量 TRANSPORTPLUGIN_MAX_PARALLEL 可临时覆盖(运维/测试)。 /// private static int ResolveMaxParallelInstances() { - int maxParallel = 3; + var batchConfig = ConfigManager.Instance.Current?.Batch; + if (batchConfig == null) + { + throw new InvalidOperationException("缺少批处理配置(config.toml 的 [batch] 段未加载)"); + } + + if (!batchConfig.UseMultiProcess) + { + LogManager.Info("[批处理队列] 多进程已关闭(config),串行执行"); + return 1; + } + + if (batchConfig.MaxParallelInstances < 1 || batchConfig.MaxParallelInstances > 20) + { + throw new InvalidOperationException( + $"无效的并发实例数: {batchConfig.MaxParallelInstances}(config.toml [batch] max_parallel_instances,范围 1-20)"); + } + + int maxParallel = batchConfig.MaxParallelInstances; + + // 环境变量临时覆盖(运维/测试场景,显式设置才生效) string raw = Environment.GetEnvironmentVariable("TRANSPORTPLUGIN_MAX_PARALLEL"); if (!string.IsNullOrWhiteSpace(raw) && int.TryParse(raw, out int parsed) && parsed >= 1 && parsed <= 20) { diff --git a/src/Core/Config/ConfigManager.cs b/src/Core/Config/ConfigManager.cs index 31c7fe1..f92df3e 100644 --- a/src/Core/Config/ConfigManager.cs +++ b/src/Core/Config/ConfigManager.cs @@ -502,6 +502,17 @@ namespace NavisworksTransport.Core.Config } } + // 批处理配置(可选) + if (model.ContainsKey("batch")) + { + var batch = model["batch"] as TomlTable; + if (batch != null) + { + config.Batch.UseMultiProcess = GetBoolValueWithDefault(batch, "use_multi_process", true, missingItems); + config.Batch.MaxParallelInstances = GetIntValueWithDefault(batch, "max_parallel_instances", 3, missingItems); + } + } + // 加载自定义类别配置(可选) if (model.ContainsKey("custom_category")) { @@ -551,6 +562,7 @@ namespace NavisworksTransport.Core.Config Logistics = new LogisticsConfig(), CoordinateSystem = new CoordinateSystemConfig(), AliasTree = new AliasTreeConfig(), + Batch = new BatchConfig(), CustomCategories = new List() }; } @@ -734,6 +746,17 @@ namespace NavisworksTransport.Core.Config } } + // 批处理配置回写 + if (model.ContainsKey("batch")) + { + var batch = model["batch"] as TomlTable; + if (batch != null && config.Batch != null) + { + batch["use_multi_process"] = config.Batch.UseMultiProcess; + batch["max_parallel_instances"] = config.Batch.MaxParallelInstances; + } + } + // 更新自定义类别配置 // 注意:由于Tomlyn对数组的处理限制,自定义类别通过专门的API管理 // 这里不直接操作TOML模型,而是在保存前通过模板合并 diff --git a/src/Core/Config/SystemConfig.cs b/src/Core/Config/SystemConfig.cs index 7e04533..8a360ff 100644 --- a/src/Core/Config/SystemConfig.cs +++ b/src/Core/Config/SystemConfig.cs @@ -48,6 +48,11 @@ namespace NavisworksTransport.Core.Config /// public AliasTreeConfig AliasTree { get; set; } + /// + /// 批处理配置 + /// + public BatchConfig Batch { get; set; } = new BatchConfig(); + /// /// 自定义类别列表 /// @@ -326,4 +331,20 @@ namespace NavisworksTransport.Core.Config /// public int MaxFullDisplayDepth { get; set; } = 2; } + + /// + /// 批处理配置 + /// + public class BatchConfig + { + /// + /// 是否启用多进程并行批处理(默认开启;关闭时并发数视为 1,串行执行) + /// + public bool UseMultiProcess { get; set; } = true; + + /// + /// 并发副 Navisworks 实例数(默认 3,范围 1-20) + /// + public int MaxParallelInstances { get; set; } = 3; + } } diff --git a/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs b/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs index 6426bf7..3683d2e 100644 --- a/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs +++ b/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs @@ -26,6 +26,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels private BatchQueueStatus _statusFilter = BatchQueueStatus.All; private string _progressText; private bool _isExecuting; + private bool _useMultiProcess = true; // 默认开启,构造函数从配置加载 #endregion @@ -73,6 +74,27 @@ namespace NavisworksTransport.UI.WPF.ViewModels set => SetProperty(ref _progressText, value); } + /// + /// 是否启用多进程并行批处理(绑定 config.toml [batch] use_multi_process,默认开启) + /// + public bool UseMultiProcess + { + get => _useMultiProcess; + set + { + if (SetProperty(ref _useMultiProcess, value)) + { + // 同步并保存到配置 + var config = NavisworksTransport.Core.Config.ConfigManager.Instance.Current; + if (config?.Batch != null) + { + config.Batch.UseMultiProcess = value; + NavisworksTransport.Core.Config.ConfigManager.Instance.SaveConfig(config, notifyChange: false); + } + } + } + } + /// /// 是否正在执行 /// @@ -134,6 +156,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels _queueItems = new ObservableCollection(); + // 从配置加载多进程开关(默认开启) + var batchConfig = NavisworksTransport.Core.Config.ConfigManager.Instance.Current?.Batch; + if (batchConfig != null) + { + _useMultiProcess = batchConfig.UseMultiProcess; + } + // 初始化命令 ExecuteQueueCommand = new RelayCommand(async () => await ExecuteQueueAsync(), () => CanExecuteQueue); StopExecutionCommand = new RelayCommand(StopExecution, () => true); diff --git a/src/UI/WPF/Views/BatchTaskManagementView.xaml b/src/UI/WPF/Views/BatchTaskManagementView.xaml index 6b7fbc8..550c4fb 100644 --- a/src/UI/WPF/Views/BatchTaskManagementView.xaml +++ b/src/UI/WPF/Views/BatchTaskManagementView.xaml @@ -67,6 +67,18 @@ NavisworksTransport 批处理队列管理页签视图 - 采用与其他页签一 Foreground="{StaticResource NavisworksTextBrush}" Margin="0,5,0,0" TextWrapping="Wrap"/> + + + + + +