适用版本:SolidEdge所有版本
SolidEdge是Siemens PLM Software公司旗下的三维CAD软件,采用Siemens PLM Software公司自己拥有专利的Parasolid作为软件核心,将普及型CAD系统与世界上最具领先地位的实体造型引擎结合在一起,是基于Windows平台、功能强大且易用的三维CAD软件。
SolidEdge二次开发有COM和Addin两种方式,其中COM(组件对象模型)具有不依赖平台和编程语言等优点,是最常用的一种Solid二次开发方式。当通过外部方式进行访问SolidEdge应用程序时,有可能会报以下异常:
应用程序正在运行时或未响应(RPC_E_CALL_REJECTED 0x80010001)
拒绝被调用(RPC_E_SERVERCALL_RETRYLATER 0x8001010A)
发生这些错误是因为二次开发程序和SolidEdge之间的线程争用问题,可以通过实现IOleMessageFilter接口处理这些异常。
说 明
首先创建OleMessageFilter,实现IOleMessageFilter接口,代码如下:
namespace UGITCApp
{
class OleMessageFilter : IOleMessageFilter
{
public static void Register()
{
IOleMessageFilter newFilter = new OleMessageFilter();
IOleMessageFilter oldFilter = null;
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
{
CoRegisterMessageFilter(newFilter, out oldFilter);
}
else
{
throw new COMException(“Unable to register message filter because the current thread apartment state is not STA.”);
}
}
public static void Revoke()
{
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(null, out oldFilter);
}
int IOleMessageFilter.HandleInComingCall(
int dwCallType,
System.IntPtr hTaskCaller,
int dwTickCount,
System.IntPtr lpInterfaceInfo)
{
return (int)SERVERCALL.SERVERCALL_ISHANDLED;
}
int IOleMessageFilter.RetryRejectedCall(
System.IntPtr hTaskCallee,
int dwTickCount,
int dwRejectType)
{
if (dwRejectType == (int)SERVERCALL.SERVERCALL_RETRYLATER)
{
return 99;
}
return -1;
}
int IOleMessageFilter.MessagePending(
System.IntPtr hTaskCallee,
int dwTickCount,
int dwPendingType)
{
return (int)PENDINGMSG.PENDINGMSG_WAITDEFPROCESS;
}
[DllImport(“Ole32.dll”)]
private static extern int CoRegisterMessageFilter(
IOleMessageFilter newFilter,
out IOleMessageFilter oldFilter);
}
[ComImport(), Guid(“00000016-0000-0000-C000-000000000046”),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(
int dwCallType,
IntPtr hTaskCaller,
int dwTickCount,
IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(
IntPtr hTaskCallee,
int dwTickCount,
int dwRejectType);
[PreserveSig]
int MessagePending(
IntPtr hTaskCallee,
int dwTickCount,
int dwPendingType);
}
}
使用方法如下:
static void Main(string[] args)
{
OleMessageFilter.Register();
// Make COM calls.
OleMessageFilter.Revoke();
}