更新整理
This commit is contained in:
145
WebUITest/WebUIWindow.xaml.cs
Normal file
145
WebUITest/WebUIWindow.xaml.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
|
||||
|
||||
|
||||
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 内容中找到 <head> 标签,插入 CSS
|
||||
// var cssInjected = $"<style>{cssContent}</style>";
|
||||
// var jsInjected = $"<script>{jsContent}</script>";
|
||||
|
||||
// // 确保注入 CSS
|
||||
// int headEndIndex = htmlContent.IndexOf("</head>");
|
||||
// if (headEndIndex >= 0)
|
||||
// {
|
||||
// htmlContent = htmlContent.Insert(headEndIndex, cssInjected);
|
||||
// }
|
||||
|
||||
// // 确保注入 JS,通常放在 </body> 前
|
||||
// int bodyEndIndex = htmlContent.IndexOf("</body>");
|
||||
// if (bodyEndIndex >= 0)
|
||||
// {
|
||||
// htmlContent = htmlContent.Insert(bodyEndIndex, jsInjected);
|
||||
// }
|
||||
|
||||
// return htmlContent;
|
||||
//}
|
||||
|
||||
private string InjectCssAndJs(string htmlContent, string cssContent, string jsContent)
|
||||
{
|
||||
// 注入 CSS 到 <head>
|
||||
var cssTag = $"<style>{cssContent}</style>";
|
||||
htmlContent = htmlContent.Replace("</head>", $"{cssTag}</head>");
|
||||
|
||||
// 注入 JS 到 <body> 或 <head>
|
||||
var jsTag = $"<script>{jsContent}</script>";
|
||||
htmlContent = htmlContent.Replace("</body>", $"{jsTag}</body>");
|
||||
|
||||
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<Wall>()
|
||||
.ToList();
|
||||
|
||||
using (Transaction tx = new Transaction(doc, "批量修改墙标记"))
|
||||
{
|
||||
tx.Start();
|
||||
foreach (var wall in walls)
|
||||
{
|
||||
Parameter p = wall.get_Parameter(BuiltInParameter.ALL_MODEL_MARK);
|
||||
if (p != null && !p.IsReadOnly)
|
||||
p.Set(tagValue);
|
||||
}
|
||||
tx.Commit();
|
||||
}
|
||||
|
||||
MessageBox.Show($"共修改 {walls.Count} 面墙的标记为 '{tagValue}'。", "完成", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user