TellmePdmsPluging/Core/MainThreadInvoker.cs
sladro 8f5bcc0c98 添加模型缩减和项目打开功能的API支持
- 在HttpServer中实现了新的API端点:/api/project/open和/api/model/shrinkwrap
- 添加了ShrinkwrapModel和OpenProject命令的处理逻辑
- 在PdmsManager中实现了ShrinkwrapModel和OpenProject方法,支持相应请求的处理
- 更新了项目文件以包含新的命令和模型请求类

此更新增强了插件的功能,允许用户通过API进行模型缩减和项目打开操作。
2026-02-05 08:22:42 +08:00

109 lines
2.8 KiB
C#

using System;
using System.Threading;
namespace TellmePdmsPluging.Core
{
public static class MainThreadInvoker
{
public static InvokeResult Invoke(ICommand command, int timeoutMs)
{
if (command == null)
{
return new InvokeResult(false, null, new ArgumentNullException("command"), "command为空");
}
var wrapper = new ResultCommandWrapper(command);
SafeQueue.Enqueue(wrapper);
bool signaled = wrapper.Wait(timeoutMs);
if (!signaled)
{
return new InvokeResult(false, null, null, "等待PDMS主线程执行超时");
}
if (wrapper.Error != null)
{
return new InvokeResult(false, null, wrapper.Error, wrapper.Error.Message);
}
return new InvokeResult(true, wrapper.Result, null, null);
}
private class ResultCommandWrapper : IResultCommand
{
private readonly ICommand _inner;
private readonly ManualResetEvent _done;
public ResultCommandWrapper(ICommand inner)
{
_inner = inner;
_done = new ManualResetEvent(false);
CommandId = inner.CommandId;
}
public string CommandId { get; }
public string CommandType
{
get { return _inner.CommandType; }
}
public bool CanCancel
{
get { return _inner.CanCancel; }
}
public object Result { get; private set; }
public Exception Error { get; private set; }
public bool IsCompleted { get; private set; }
public object Execute()
{
try
{
Result = _inner.Execute();
return Result;
}
catch (Exception ex)
{
Error = ex;
return null;
}
finally
{
IsCompleted = true;
_done.Set();
}
}
public void Cancel()
{
_inner.Cancel();
}
public bool Wait(int timeoutMs)
{
return _done.WaitOne(timeoutMs);
}
}
}
public class InvokeResult
{
public InvokeResult(bool success, object result, Exception error, string message)
{
Success = success;
Result = result;
Error = error;
Message = message;
}
public bool Success { get; }
public object Result { get; }
public Exception Error { get; }
public string Message { get; }
}
}