更新整理
This commit is contained in:
28
WebUITest/Command.cs
Normal file
28
WebUITest/Command.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Autodesk.Revit.UI;
|
||||
|
||||
using System;
|
||||
|
||||
namespace WebUITest
|
||||
{
|
||||
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
|
||||
public class Command : IExternalCommand
|
||||
{
|
||||
public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
|
||||
{
|
||||
try
|
||||
{
|
||||
var uiApp = commandData.Application;
|
||||
var uiDoc = uiApp.ActiveUIDocument;
|
||||
var wnd = new WebUIWindow(uiDoc);
|
||||
wnd.Owner = System.Windows.Application.Current?.MainWindow;
|
||||
wnd.ShowDialog();
|
||||
return Result.Succeeded;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = ex.Message;
|
||||
return Result.Failed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
44
WebUITest/Html/index.html
Normal file
44
WebUITest/Html/index.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>AntD + React</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/antd@4.21.0/dist/antd.min.css" />
|
||||
<!--<script src="https://cdn.jsdelivr.net/npm/react@17/umd/react.development.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.development.js"></script>-->
|
||||
<script src="https://cdn.jsdelivr.net/npm/antd@4.21.0/dist/antd.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
<script>
|
||||
const { Button, Input } = antd;
|
||||
|
||||
const App = () => {
|
||||
const [value, setValue] = React.useState("");
|
||||
return React.createElement("div", { style: { padding: 20 } }, [
|
||||
React.createElement(Input, {
|
||||
key: "input",
|
||||
value,
|
||||
placeholder: "输入墙标记",
|
||||
onChange: e => setValue(e.target.value),
|
||||
style: { marginBottom: 10 }
|
||||
}),
|
||||
React.createElement(Button, {
|
||||
key: "btn",
|
||||
type: "primary",
|
||||
onClick: () => {
|
||||
if (value) {
|
||||
window.chrome?.webview?.postMessage({ newTag: value });
|
||||
} else {
|
||||
alert("请输入内容");
|
||||
}
|
||||
}
|
||||
}, "提交")
|
||||
]);
|
||||
};
|
||||
|
||||
ReactDOM.render(React.createElement(App), document.getElementById("root"));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
33
WebUITest/Properties/AssemblyInfo.cs
Normal file
33
WebUITest/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("WebUITest")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("WebUITest")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2025")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("be44de16-ea42-424a-b16f-07c46e157618")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
24
WebUITest/WebUITest.csproj
Normal file
24
WebUITest/WebUITest.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net472</TargetFramework>
|
||||
<OutputType>Library</OutputType>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<UseWPF>True</UseWPF>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Html\index.html" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
|
||||
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3179.45" />
|
||||
<PackageReference Include="Nice3point.Revit.Api.RevitAPI" Version="2020.*" PrivateAssets="All" />
|
||||
<PackageReference Include="Nice3point.Revit.Api.RevitAPIUI" Version="2020.*" PrivateAssets="All" />
|
||||
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.3" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Update="WebUIWindow.xaml.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
9
WebUITest/WebUIWindow.xaml
Normal file
9
WebUITest/WebUIWindow.xaml
Normal file
@@ -0,0 +1,9 @@
|
||||
<Window x:Class="WebUITest.WebUIWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
|
||||
Title="批量墙标记修改" Height="600" Width="800">
|
||||
<Grid>
|
||||
<wv2:WebView2 x:Name="WebView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
|
||||
</Grid>
|
||||
</Window>
|
||||
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