using System; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Shell; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Microsoft.Web.WebView2.Core; namespace WebUITest { public partial class WebUIWindow : Window { private UIDocument _uiDoc; public WebUIWindow(UIDocument uiDoc) { InitializeComponent(); _uiDoc = uiDoc; InitializeWebView(); } private async void InitializeWebView() { string userDataFolder = Path.Combine(Path.GetTempPath(), "WebUI_Revit_UserData"); var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder); await WebView.EnsureCoreWebView2Async(env); WebView.CoreWebView2.WebMessageReceived += WebMessageReceived; // 获取嵌入的 HTML 内容 string htmlContent = GetEmbeddedResource("WebUI_WallBatchTag.Html.index.html"); if (string.IsNullOrEmpty(htmlContent)) { MessageBox.Show("HTML 内容加载失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } // 加载嵌入的 CSS 和 JS 文件 //var cssContent = GetEmbeddedResource("WebUI_WallBatchTag.Resources.antd.min.css"); //var jsContent = GetEmbeddedResource("WebUI_WallBatchTag.Resources.antd.min.js"); //// 将 HTML 写入临时文件并加载 //string tempPath = Path.Combine(Path.GetTempPath(), "index.html"); //File.WriteAllText(tempPath, htmlContent); //WebView.Source = new Uri(tempPath); // 在 HTML 内容中注入 CSS 和 JS //htmlContent = InjectCssAndJs(htmlContent, cssContent, jsContent); // 尝试加载 HTML try { WebView.NavigateToString(htmlContent); } catch (Exception ex) { MessageBox.Show($"HTML 加载失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } //private string InjectCssAndJs(string htmlContent, string cssContent, string jsContent) //{ // // 在 HTML 内容中找到
标签,插入 CSS // var cssInjected = $""; // var jsInjected = $""; // // 确保注入 CSS // int headEndIndex = htmlContent.IndexOf(""); // if (headEndIndex >= 0) // { // htmlContent = htmlContent.Insert(headEndIndex, cssInjected); // } // // 确保注入 JS,通常放在 或 var jsTag = $""; htmlContent = htmlContent.Replace("", $"{jsTag}前 // int bodyEndIndex = htmlContent.IndexOf(""); // if (bodyEndIndex >= 0) // { // htmlContent = htmlContent.Insert(bodyEndIndex, jsInjected); // } // return htmlContent; //} private string InjectCssAndJs(string htmlContent, string cssContent, string jsContent) { // 注入 CSS 到
var cssTag = $""; htmlContent = htmlContent.Replace("", $"{cssTag}"); // 注入 JS 到
");
return htmlContent;
}
private string GetEmbeddedResource(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
private void WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
string json = e.WebMessageAsJson;
dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
string newTag = data.newTag;
ApplyTagToWalls(newTag);
}
private void ApplyTagToWalls(string tagValue)
{
// 执行墙标记批量修改操作
Document doc = _uiDoc.Document;
var walls = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Walls)
.WhereElementIsNotElementType()
.Cast