186 lines
6.4 KiB
C#
186 lines
6.4 KiB
C#
using System;
|
||
using System.IO;
|
||
using Autodesk.Revit.DB;
|
||
using Autodesk.Revit.UI;
|
||
using RevitHttpControl.Models;
|
||
|
||
namespace RevitHttpControl.Services
|
||
{
|
||
/// <summary>
|
||
/// 文档操作服务
|
||
/// </summary>
|
||
public static class DocumentService
|
||
{
|
||
/// <summary>
|
||
/// 打开文档文件
|
||
/// </summary>
|
||
/// <param name="request">打开文件请求</param>
|
||
/// <returns>打开文件响应</returns>
|
||
public static OpenFileResponse OpenDocument(OpenFileRequest request)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(request?.FilePath))
|
||
throw new ArgumentException("文件路径不能为空");
|
||
|
||
if (!File.Exists(request.FilePath))
|
||
throw new FileNotFoundException($"文件不存在: {request.FilePath}");
|
||
|
||
try
|
||
{
|
||
var fileName = Path.GetFileName(request.FilePath);
|
||
var completed = false;
|
||
Exception capturedException = null;
|
||
|
||
App.Instance.EnqueueCommand(uiApp =>
|
||
{
|
||
try
|
||
{
|
||
// 将字符串路径转换为ModelPath
|
||
var modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(request.FilePath);
|
||
|
||
// 配置打开选项
|
||
var openOptions = new OpenOptions
|
||
{
|
||
DetachFromCentralOption = request.Detached
|
||
? DetachFromCentralOption.DetachAndPreserveWorksets
|
||
: DetachFromCentralOption.DetachAndDiscardWorksets
|
||
};
|
||
|
||
// 打开并激活文档
|
||
uiApp.OpenAndActivateDocument(modelPath, openOptions, false);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
capturedException = ex;
|
||
}
|
||
finally
|
||
{
|
||
completed = true;
|
||
}
|
||
});
|
||
|
||
// 等待命令执行完成(最多等待10秒)
|
||
var timeout = DateTime.Now.AddSeconds(10);
|
||
while (!completed && DateTime.Now < timeout)
|
||
{
|
||
System.Threading.Thread.Sleep(100);
|
||
}
|
||
|
||
if (!completed)
|
||
throw new TimeoutException("打开文件操作超时");
|
||
|
||
if (capturedException != null)
|
||
throw capturedException;
|
||
|
||
return new OpenFileResponse
|
||
{
|
||
Result = "文件打开成功",
|
||
FileName = fileName,
|
||
FilePath = request.FilePath
|
||
};
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new InvalidOperationException($"打开文件失败: {ex.Message}", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步打开文档文件
|
||
/// </summary>
|
||
/// <param name="request">打开文件请求</param>
|
||
/// <param name="taskId">任务ID</param>
|
||
/// <param name="taskManager">任务管理器</param>
|
||
public static void OpenDocumentAsync(OpenFileRequest request, Guid taskId, TaskManager taskManager)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(request?.FilePath))
|
||
{
|
||
taskManager.FailTask(taskId, "文件路径不能为空");
|
||
return;
|
||
}
|
||
|
||
if (!File.Exists(request.FilePath))
|
||
{
|
||
taskManager.FailTask(taskId, $"文件不存在: {request.FilePath}");
|
||
return;
|
||
}
|
||
|
||
var fileName = Path.GetFileName(request.FilePath);
|
||
|
||
App.Instance.EnqueueCommand(uiApp =>
|
||
{
|
||
try
|
||
{
|
||
// 将字符串路径转换为ModelPath
|
||
var modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(request.FilePath);
|
||
|
||
// 配置打开选项
|
||
var openOptions = new OpenOptions
|
||
{
|
||
DetachFromCentralOption = request.Detached
|
||
? DetachFromCentralOption.DetachAndPreserveWorksets
|
||
: DetachFromCentralOption.DetachAndDiscardWorksets
|
||
};
|
||
|
||
// 打开并激活文档
|
||
uiApp.OpenAndActivateDocument(modelPath, openOptions, false);
|
||
|
||
// 成功完成任务
|
||
var response = new OpenFileResponse
|
||
{
|
||
Result = "文件打开成功",
|
||
FileName = fileName,
|
||
FilePath = request.FilePath
|
||
};
|
||
|
||
taskManager.CompleteTask(taskId, response);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
taskManager.FailTask(taskId, $"打开文件失败: {ex.Message}");
|
||
}
|
||
});
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
taskManager.FailTask(taskId, $"打开文件失败: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证文件路径
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>是否有效</returns>
|
||
public static bool ValidateFilePath(string filePath)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(filePath))
|
||
return false;
|
||
|
||
try
|
||
{
|
||
var extension = Path.GetExtension(filePath).ToLowerInvariant();
|
||
return extension == ".rvt" && File.Exists(filePath);
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件信息
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>文件信息</returns>
|
||
public static FileInfo GetFileInfo(string filePath)
|
||
{
|
||
if (!ValidateFilePath(filePath))
|
||
throw new ArgumentException($"无效的文件路径: {filePath}");
|
||
|
||
return new FileInfo(filePath);
|
||
}
|
||
}
|
||
}
|