From 36a594454809b4d759e032a285a946c4a35616aa Mon Sep 17 00:00:00 2001
From: tian <11429339@qq.com>
Date: Sat, 16 May 2026 15:36:23 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4JSON=E5=AF=BC=E5=87=BA?=
=?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9B=E5=8E=BB=E6=8E=89ExportSelectedItem?=
=?UTF-8?q?sAsync=E4=B8=AD=E6=AE=8B=E7=95=99=E7=9A=84Task.Run?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- SectionBoxExporter: 删除 ExportToJson/BuildExportData/GetFullPathName
- LayerManagementViewModel: 保存对话框去掉JSON格式选项,去掉JSON/NWD分支的Task.Run
---
src/Core/SectionBoxExporter.cs | 164 ------------------
.../ViewModels/LayerManagementViewModel.cs | 40 +----
2 files changed, 8 insertions(+), 196 deletions(-)
diff --git a/src/Core/SectionBoxExporter.cs b/src/Core/SectionBoxExporter.cs
index 3029d24..c09044b 100644
--- a/src/Core/SectionBoxExporter.cs
+++ b/src/Core/SectionBoxExporter.cs
@@ -4,8 +4,6 @@ using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Navisworks.Api;
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
using NavisworksTransport.Utils;
using NavisApplication = Autodesk.Navisworks.Api.Application;
@@ -288,134 +286,6 @@ namespace NavisworksTransport.Core
(a.Min.Z <= b.Max.Z && a.Max.Z >= b.Min.Z);
}
- ///
- /// 构建导出数据
- ///
- private JObject BuildExportData(BoundingBox3D sectionBox, List objects, Document document)
- {
- var root = new JObject();
-
- // 1. 导出剖面盒信息
- var sectionBoxInfo = new JObject
- {
- ["min"] = new JArray { sectionBox.Min.X, sectionBox.Min.Y, sectionBox.Min.Z },
- ["max"] = new JArray { sectionBox.Max.X, sectionBox.Max.Y, sectionBox.Max.Z },
- ["center"] = new JArray
- {
- (sectionBox.Min.X + sectionBox.Max.X) / 2,
- (sectionBox.Min.Y + sectionBox.Max.Y) / 2,
- (sectionBox.Min.Z + sectionBox.Max.Z) / 2
- },
- ["size"] = new JArray
- {
- sectionBox.Max.X - sectionBox.Min.X,
- sectionBox.Max.Y - sectionBox.Min.Y,
- sectionBox.Max.Z - sectionBox.Min.Z
- }
- };
- root["sectionBox"] = sectionBoxInfo;
-
- // 2. 导出文档信息
- var docInfo = new JObject
- {
- ["fileName"] = document.FileName ?? "Unknown",
- ["units"] = document.Units.ToString(),
- ["exportTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
- ["objectCount"] = objects.Count
- };
- root["documentInfo"] = docInfo;
-
- // 3. 导出对象列表
- var objectsArray = new JArray();
- foreach (var item in objects)
- {
- var bbox = item.BoundingBox();
- var objInfo = new JObject
- {
- ["name"] = GetFullPathName(item),
- ["boundingBox"] = new JObject
- {
- ["min"] = new JArray { bbox.Min.X, bbox.Min.Y, bbox.Min.Z },
- ["max"] = new JArray { bbox.Max.X, bbox.Max.Y, bbox.Max.Z },
- ["center"] = new JArray
- {
- (bbox.Min.X + bbox.Max.X) / 2,
- (bbox.Min.Y + bbox.Max.Y) / 2,
- (bbox.Min.Z + bbox.Max.Z) / 2
- },
- ["size"] = new JArray
- {
- bbox.Max.X - bbox.Min.X,
- bbox.Max.Y - bbox.Min.Y,
- bbox.Max.Z - bbox.Min.Z
- }
- }
- };
-
- // 添加Transform信息(仅非单位变换)
- try
- {
- var components = item.Transform.Factor();
- var rotation = components.Rotation.ToAxisAndAngle();
- bool isIdentity = Math.Abs(rotation.Angle) < 0.0001 &&
- Math.Abs(components.Scale.X - 1) < 0.0001 &&
- Math.Abs(components.Scale.Y - 1) < 0.0001 &&
- Math.Abs(components.Scale.Z - 1) < 0.0001 &&
- Math.Abs(components.Translation.X) < 0.0001 &&
- Math.Abs(components.Translation.Y) < 0.0001 &&
- Math.Abs(components.Translation.Z) < 0.0001;
-
- if (!isIdentity)
- {
- objInfo["transform"] = new JObject
- {
- ["translation"] = new JArray { components.Translation.X, components.Translation.Y, components.Translation.Z },
- ["rotation"] = new JObject
- {
- ["axis"] = new JArray { rotation.Axis.X, rotation.Axis.Y, rotation.Axis.Z },
- ["angle"] = rotation.Angle
- },
- ["scale"] = new JArray { components.Scale.X, components.Scale.Y, components.Scale.Z }
- };
- }
- }
- catch (Exception ex)
- {
- LogManager.Debug($"获取Transform信息失败: {ex.Message}");
- }
-
- objectsArray.Add(objInfo);
- }
- root["objects"] = objectsArray;
-
- return root;
- }
-
- ///
- /// 获取对象的全路径名称
- ///
- private string GetFullPathName(ModelItem item)
- {
- if (item == null) return "";
-
- var pathParts = new List();
-
- // 从当前节点向上遍历到根节点
- var current = item;
- while (current != null)
- {
- string name = current.DisplayName;
- if (!string.IsNullOrEmpty(name))
- {
- pathParts.Insert(0, name);
- }
- current = current.Parent;
- }
-
- // 使用 "/" 连接路径
- return string.Join("/", pathParts);
- }
-
///
/// 导出为NWD文件(委托给 NwdExportHelper)
/// 适用于通用场景(如保存选择项)
@@ -446,39 +316,5 @@ namespace NavisworksTransport.Core
"剖面盒导出");
}
- ///
- /// 导出为JSON文件(公开方法供外部调用)
- ///
- public string ExportToJson(Document document, List objects, string filePath)
- {
- try
- {
- // 获取当前剖面盒边界(用于JSON中的元数据)
- var viewpoint = document.CurrentViewpoint.Value;
- var clipPlanes = viewpoint.ClipPlanes;
- BoundingBox3D sectionBox = default;
-
- if (clipPlanes?.Enabled == true && clipPlanes.Mode == ClipPlaneSetMode.Box && clipPlanes.Box != null)
- {
- var box = clipPlanes.Box;
- sectionBox = new BoundingBox3D(
- new Point3D(box.Min.X, box.Min.Y, box.Min.Z),
- new Point3D(box.Max.X, box.Max.Y, box.Max.Z)
- );
- }
-
- var exportData = BuildExportData(sectionBox, objects, document);
- string json = JsonConvert.SerializeObject(exportData, Formatting.Indented);
- File.WriteAllText(filePath, json);
- LogManager.Info($"[SectionBoxExporter] JSON导出完成: {objects.Count} 个对象 -> {filePath}");
- return filePath;
- }
- catch (Exception ex)
- {
- LogManager.Error($"[SectionBoxExporter] JSON导出失败: {ex.Message}");
- return null;
- }
- }
-
}
}
diff --git a/src/UI/WPF/ViewModels/LayerManagementViewModel.cs b/src/UI/WPF/ViewModels/LayerManagementViewModel.cs
index 310e10c..2f977da 100644
--- a/src/UI/WPF/ViewModels/LayerManagementViewModel.cs
+++ b/src/UI/WPF/ViewModels/LayerManagementViewModel.cs
@@ -1836,15 +1836,14 @@ namespace NavisworksTransport.UI.WPF.ViewModels
var document = Autodesk.Navisworks.Api.Application.ActiveDocument;
var originalSelection = restoreSelection ? document.CurrentSelection.SelectedItems.ToList() : null;
- // 获取保存路径和导出格式
+ // 获取保存路径
string saveFilePath = null;
- bool exportAsJson = false;
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
var saveDialog = new Microsoft.Win32.SaveFileDialog
{
Title = dialogTitle,
- Filter = "Navisworks文件 (*.nwd)|*.nwd|JSON文件 (*.json)|*.json",
+ Filter = "Navisworks文件 (*.nwd)|*.nwd",
DefaultExt = "nwd",
FileName = defaultFileName
};
@@ -1852,9 +1851,6 @@ namespace NavisworksTransport.UI.WPF.ViewModels
if (saveDialog.ShowDialog() == true)
{
saveFilePath = saveDialog.FileName;
- // 如果用户选择了JSON(FilterIndex为2或文件扩展名为json)
- exportAsJson = (saveDialog.FilterIndex == 2 ||
- saveFilePath.EndsWith(".json", StringComparison.OrdinalIgnoreCase));
}
});
@@ -1863,28 +1859,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels
return;
}
- bool exportResult = false;
- string errorMessage = "";
-
- if (exportAsJson)
- {
- // JSON导出(使用 SectionBoxExporter)
- var exporter = new SectionBoxExporter();
- await Task.Run(() =>
- {
- var result = exporter.ExportToJson(document, items, saveFilePath);
- exportResult = !string.IsNullOrEmpty(result);
- });
- }
- else
- {
- // NWD导出(使用 NwdExportHelper)
- await Task.Run(() =>
- {
- var result = NwdExportHelper.ExportToNwd(items, saveFilePath);
- exportResult = !string.IsNullOrEmpty(result);
- });
- }
+ // NWD导出
+ var result = NwdExportHelper.ExportToNwd(items, saveFilePath);
+ bool exportResult = !string.IsNullOrEmpty(result);
+ string errorMessage = result == null ? "导出失败" : "";
// 恢复原始选择(如果需要)
if (restoreSelection && originalSelection != null)
@@ -1909,16 +1887,14 @@ namespace NavisworksTransport.UI.WPF.ViewModels
if (File.Exists(saveFilePath))
{
var fileInfo = new FileInfo(saveFilePath);
- string format = exportAsJson ? "JSON" : "NWD";
MessageBox.Show(
- $"{format}导出成功!\n\n文件: {saveFilePath}\n大小: {fileInfo.Length / 1024} KB",
+ $"NWD导出成功!\n\n文件: {saveFilePath}\n大小: {fileInfo.Length / 1024} KB",
"导出结果", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
else
{
- string format = exportAsJson ? "JSON" : "NWD";
- MessageBox.Show($"{format}导出失败: {errorMessage}", "导出错误", MessageBoxButton.OK, MessageBoxImage.Error);
+ MessageBox.Show($"NWD导出失败: {errorMessage}", "导出错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)