cad.net 跨进程通讯
Acad跨进程通讯
在Acad跨进程通讯用途其实很广泛.
1,COM接口交互
https://www.cnblogs.com/JJBox/p/11381254.html
2,Win32API的SendMessage其实也是,
毕竟ctrl+n新建图纸也是发送QNEW指令,然后消息循环捕获再执行指令.
https://www.cnblogs.com/JJBox/p/13766772.html
3,那么TCP/UDP/C#的Remoting框架/网络通讯等等.
Remoting
正在研究cad的跨进程通讯,
难点在于cad是服务端的话,让客户端直接调用暴露的接口,而不会暴露实现,
因此写了一个Remoting的案例,如下代码就可以进行了.
至于WCF工程,由于它的App.config处理有些地方不是很理解,之后再仔细研究.
动图演示
工程结构
- 服务端工程 --> cad的类库,透过cad调用 --> 引用端口工程
- 客户端工程 --> 控制台工程 --> 引用端口工程
- 端口工程 --> 类库
1.服务端工程
#if !HC2020
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
#else
using GrxCAD.ApplicationServices;
using GrxCAD.DatabaseServices;
using GrxCAD.EditorInput;
using Acap = GrxCAD.ApplicationServices.Application;
#endif
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System;
using JoinBox.IAdapter;
namespace JoinBox.Service
{
public class Remoting
{
/// <summary>
/// 注册服务管道 Remoting,实现占用端口
/// </summary>
[JoinBoxInitialize]
public void RegisterServerChannel()
{
var channel = new TcpServerChannel(AdapterHelper.Port);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(BaseService), //服务端实现(通讯地址接口)的函数
AdapterHelper.TcpPortAdapter, //通讯地址接口
WellKnownObjectMode.Singleton);
}
}
// 服务端实现接口工程的方法,在cad命令栏打印
public class BaseService : MarshalByRefObject, IJoinBoxAdapter
{
public void PostAcedString(string str)
{
//阻塞线程并切换上下文,另见 https://www.cnblogs.com/JJBox/p/14179921.html
AutoGo.SyncManager.SyncContext.Post(() => {
Document doc = Acap.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ed.WriteMessage(str);
});
}
}
}
2.客户端工程 JoinBox.Client
using System;
using System.Threading;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using JoinBox.IAdapter;
namespace JoinBox.Client
{
public class Program
{
public static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
var adapter = (IJoinBoxAdapter)Activator.GetObject(typeof(IJoinBoxAdapter), AdapterHelper.TcpPortFullName);
while (true)
{
var str = DateTime.Now.ToString() + "\n";
Console.WriteLine(str);
adapter.PostAcedString(str);//调用接口工程的方法.
Thread.Sleep(1000);
}
}
}
}
3.接口工程 JoinBox.IAdapter
此工程同时暴露给服务端和客户端利用
namespace JoinBox.IAdapter
{
//公共接口和方法
public interface IJoinBoxAdapter
{
public void PostAcedString(string str);
}
//公共的属性
public class AdapterHelper
{
/// <summary>
/// 端口
/// </summary>
public static int Port = 15341;
/// <summary>
/// 通讯地址(接口)--服务端用
/// </summary>
public static string TcpPortAdapter = "jingjing/" + nameof(IJoinBoxAdapter);
/// <summary>
/// 通讯地址(完整)--客户端用
/// </summary>
public static string TcpPortFullName = $"tcp://localhost:{Port}/" + TcpPortAdapter;
}
}
(完)