214 lines
8.5 KiB
C#
214 lines
8.5 KiB
C#
// ============================================================
|
|
// CadBridge:WinForms 宿主与网页 CAD(CAD-Viewer)之间的互操作桥
|
|
// · 下行(C#→网页):PostMessage 发 startPick/zoomToHandle/deleteHandles/clearHighlight
|
|
// · 上行(网页→C#):WebMessageReceived 收 paPicked/entityErased/paExited
|
|
// · 扒图结果与实体删除事件以 C# 事件形式提供给 Form3(替代 AutoCAD 版
|
|
// 的 PaTextSelector.BatchTextsPicked 与 Database.ObjectErased)
|
|
// ============================================================
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Web.Script.Serialization;
|
|
using Microsoft.Web.WebView2.Core;
|
|
using Microsoft.Web.WebView2.WinForms;
|
|
|
|
namespace cadbt
|
|
{
|
|
public static class CadBridge
|
|
{
|
|
private static WebView2 _wv;
|
|
private static readonly JavaScriptSerializer _json = new JavaScriptSerializer();
|
|
|
|
/// <summary>网页 CAD 是否已挂载(WebView2 已附加)。</summary>
|
|
public static bool IsViewerReady
|
|
{
|
|
get { return _wv != null && _wv.CoreWebView2 != null; }
|
|
}
|
|
|
|
/// <summary>网页 CAD 中实体被删除(句柄十六进制字符串)。</summary>
|
|
public static event Action<string> EntityErased;
|
|
|
|
/// <summary>网页 CAD 扒图完成一批(图号/箱号/元件)。</summary>
|
|
public static event Action<MyApp.PaMode, List<MyApp.PaTextResult>> PaPicked;
|
|
|
|
/// <summary>用户按 ESC 退出网页扒图连续模式。</summary>
|
|
public static event Action PaExited;
|
|
|
|
/// <summary>用户按 F1(焦点在网页或宿主时都触发)——元件两两合并。</summary>
|
|
public static event Action F1Requested;
|
|
|
|
/// <summary>用户按 F2——元件三个一组合并。</summary>
|
|
public static event Action F2Requested;
|
|
|
|
/// <summary>用户按 F3——元件四个一组合并。</summary>
|
|
public static event Action F3Requested;
|
|
|
|
/// <summary>触发 F1 请求(消息过滤器等宿主侧来源调用)。</summary>
|
|
public static void RaiseF1()
|
|
{
|
|
if (F1Requested != null) F1Requested();
|
|
}
|
|
|
|
/// <summary>触发 F2 请求(消息过滤器等宿主侧来源调用)。</summary>
|
|
public static void RaiseF2()
|
|
{
|
|
if (F2Requested != null) F2Requested();
|
|
}
|
|
|
|
/// <summary>触发 F3 请求(消息过滤器等宿主侧来源调用)。</summary>
|
|
public static void RaiseF3()
|
|
{
|
|
if (F3Requested != null) F3Requested();
|
|
}
|
|
|
|
/// <summary>把 WebView2 挂到桥上(在控件初始化完成后调用一次)。</summary>
|
|
public static void Attach(WebView2 wv)
|
|
{
|
|
if (_wv == wv) return;
|
|
if (_wv != null) _wv.WebMessageReceived -= OnMessage;
|
|
_wv = wv;
|
|
if (_wv != null) _wv.WebMessageReceived += OnMessage;
|
|
}
|
|
|
|
private static void OnMessage(object sender, CoreWebView2WebMessageReceivedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (e.TryGetWebMessageAsString() != null) return; // 只处理 JSON 消息
|
|
}
|
|
catch { }
|
|
try
|
|
{
|
|
var msg = _json.Deserialize<Dictionary<string, object>>(e.WebMessageAsJson);
|
|
if (msg == null || !msg.ContainsKey("type")) return;
|
|
string type = msg["type"] as string;
|
|
|
|
if (type == "paPicked")
|
|
{
|
|
int mode = Convert.ToInt32(msg["mode"]);
|
|
var results = new List<MyApp.PaTextResult>();
|
|
var arr = msg["results"] as System.Collections.ArrayList;
|
|
if (arr != null)
|
|
{
|
|
foreach (Dictionary<string, object> item in arr)
|
|
{
|
|
results.Add(new MyApp.PaTextResult
|
|
{
|
|
Text = Convert.ToString(item["text"]) ?? "",
|
|
FrameHandle = Convert.ToString(item["frameHandle"]) ?? "",
|
|
TextHandle = Convert.ToString(item["textHandle"]) ?? ""
|
|
});
|
|
}
|
|
}
|
|
// WebMessageReceived 在 UI 线程触发,可直接调用 Form3
|
|
if (PaPicked != null)
|
|
PaPicked((MyApp.PaMode)mode, results);
|
|
}
|
|
else if (type == "entityErased")
|
|
{
|
|
string handle = Convert.ToString(msg["handle"]);
|
|
if (!string.IsNullOrWhiteSpace(handle) && EntityErased != null)
|
|
EntityErased(handle);
|
|
}
|
|
else if (type == "paError")
|
|
{
|
|
string errMsg = Convert.ToString(msg["message"]);
|
|
System.Windows.Forms.MessageBox.Show(
|
|
"网页 CAD 返回错误:\n" + errMsg,
|
|
"扒数据", System.Windows.Forms.MessageBoxButtons.OK,
|
|
System.Windows.Forms.MessageBoxIcon.Warning);
|
|
}
|
|
else if (type == "f1")
|
|
{
|
|
if (F1Requested != null) F1Requested();
|
|
}
|
|
else if (type == "f2")
|
|
{
|
|
if (F2Requested != null) F2Requested();
|
|
}
|
|
else if (type == "f3")
|
|
{
|
|
if (F3Requested != null) F3Requested();
|
|
}
|
|
else if (type == "paExited")
|
|
{
|
|
if (PaExited != null) PaExited();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// 消息格式异常直接忽略,不影响宿主
|
|
}
|
|
}
|
|
|
|
private static void Post(object msg)
|
|
{
|
|
if (!IsViewerReady) return;
|
|
try
|
|
{
|
|
_wv.CoreWebView2.PostWebMessageAsJson(_json.Serialize(msg));
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送扒图命令。接受原 CAD 命令名(CADPATU/CADPAXH/CADPAYJ/CADPACL)
|
|
/// 或网页命令名(patu/paxh/payj)。
|
|
/// </summary>
|
|
public static void SendPaCommand(string cmd)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(cmd)) return;
|
|
string c = cmd.Trim().ToUpperInvariant();
|
|
int mode = 0;
|
|
if (c == "CADPATU" || c == "PATU") mode = 0;
|
|
else if (c == "CADPAXH" || c == "PAXH") mode = 1;
|
|
else if (c == "CADPAYJ" || c == "PAYJ") mode = 2;
|
|
else if (c == "CADPACL" || c == "PACL")
|
|
{
|
|
ClearHighlight();
|
|
return;
|
|
}
|
|
else return;
|
|
Post(new { type = "startPick", mode = mode });
|
|
}
|
|
|
|
/// <summary>缩放居中到指定 Handle 的实体并高亮(点击表格行定位框)。</summary>
|
|
public static void ZoomToFrameHandle(string handle)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(handle)) return;
|
|
Post(new { type = "zoomToHandle", handle = handle });
|
|
}
|
|
|
|
/// <summary>批量删除指定 Handle 的实体(删表格行联动删框)。</summary>
|
|
public static void DeleteFramesByHandles(IEnumerable<string> handles)
|
|
{
|
|
if (handles == null) return;
|
|
var list = new List<string>();
|
|
foreach (string h in handles)
|
|
if (!string.IsNullOrWhiteSpace(h)) list.Add(h);
|
|
if (list.Count == 0) return;
|
|
Post(new { type = "deleteHandles", handles = list });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 合并蒙版:保留 keep 句柄的蒙版并扩大到所有 removes 蒙版的并集,
|
|
/// 被合并行的蒙版从网页 CAD 移除(元件合并时蒙版同步合并)。
|
|
/// </summary>
|
|
public static void MergeMasks(string keep, System.Collections.Generic.IEnumerable<string> removes)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(keep)) return;
|
|
var list = new List<string>();
|
|
if (removes != null)
|
|
foreach (string h in removes)
|
|
if (!string.IsNullOrWhiteSpace(h) && h != keep) list.Add(h);
|
|
if (list.Count == 0) return;
|
|
Post(new { type = "mergeMasks", keep = keep, removes = list });
|
|
}
|
|
|
|
/// <summary>清除网页 CAD 中的所有高亮。</summary>
|
|
public static void ClearHighlight()
|
|
{
|
|
Post(new { type = "clearHighlight" });
|
|
}
|
|
}
|
|
}
|