删除JSON导出功能;去掉ExportSelectedItemsAsync中残留的Task.Run

- SectionBoxExporter: 删除 ExportToJson/BuildExportData/GetFullPathName
- LayerManagementViewModel: 保存对话框去掉JSON格式选项,去掉JSON/NWD分支的Task.Run
This commit is contained in:
tian 2026-05-16 15:36:23 +08:00
parent 1e8916b828
commit 36a5944548
2 changed files with 8 additions and 196 deletions

View File

@ -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);
}
/// <summary>
/// 构建导出数据
/// </summary>
private JObject BuildExportData(BoundingBox3D sectionBox, List<ModelItem> 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;
}
/// <summary>
/// 获取对象的全路径名称
/// </summary>
private string GetFullPathName(ModelItem item)
{
if (item == null) return "";
var pathParts = new List<string>();
// 从当前节点向上遍历到根节点
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);
}
/// <summary>
/// 导出为NWD文件委托给 NwdExportHelper
/// 适用于通用场景(如保存选择项)
@ -446,39 +316,5 @@ namespace NavisworksTransport.Core
"剖面盒导出");
}
/// <summary>
/// 导出为JSON文件公开方法供外部调用
/// </summary>
public string ExportToJson(Document document, List<ModelItem> 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;
}
}
}
}

View File

@ -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;
// 如果用户选择了JSONFilterIndex为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)