优化生成器事务调用,项目结构

This commit is contained in:
ShrlAlgo
2025-09-04 15:53:29 +08:00
parent b021bded67
commit 8251f80f0b
5 changed files with 25 additions and 41 deletions

View File

@@ -1,7 +1,12 @@
右键RevitGen项目打包生成nuget可以修改版本。 - 右键RevitGen项目打包生成nuget可以修改版本。
在插件项目去引用nuget包本地路径可能需要修改nuget.config中的相对路径。 - 在插件项目去引用nuget包本地路径需要修改nuget.config中的相对路径。
使用RevitCommand生成命令和面板CommandHandler来定义运行的逻辑方法 - 使用partial修饰你的命令类
如果是路径的图标的话,需要属性窗口中把生成方式改成嵌入式资源 - 使用RevitCommand生成命令和面板
- CommandHandler来定义运行的逻辑方法
- 如果是路径的图标的话,需要属性窗口中把生成方式改成嵌入式资源
- 命令自带UIApplication,UIDocument,Document,ActiveView等属性
- 自带事务处理
- 命令的特性会生成一个RevitGenApplication的类注册命令生成面板。
``` ```
using Autodesk.Revit.DB; using Autodesk.Revit.DB;
using Autodesk.Revit.UI; using Autodesk.Revit.UI;
@@ -13,7 +18,7 @@ using RevitGenTest.Properties;
namespace RevitGenTest namespace RevitGenTest
{ {
[RevitCommand("我的第一个命令", ToolTip = "这是一个自动生成的酷炫命令!", PanelName = "核心功能", Icon = nameof(Resources.CodeList_32px))] [RevitCommand("我的第一个命令", ToolTip = "这是一个自动生成的酷炫命令!", PanelName = "核心功能", Icon = nameof(Resources.CodeList_32px))]
public partial class RevitAddin public partial class RevitAddinOne
{ {
[CommandHandler] [CommandHandler]
private void Run() private void Run()
@@ -34,8 +39,8 @@ namespace RevitGenTest
TaskDialog.Show("成功", $"找到了 {walls.Count} 面墙。"); TaskDialog.Show("成功", $"找到了 {walls.Count} 面墙。");
} }
} }
[RevitCommand("我的第个命令", ToolTip = "这是一个自动生成的酷炫命令!", PanelName = "核心功能", Icon = "Resources/CodeList_32px.png")] [RevitCommand("我的第个命令", ToolTip = "这是自动生成的酷炫命令!", PanelName = "核心功能", Icon = "Resources/CodeList_32px.png")]
public partial class RevitAddinX public partial class RevitAddinTwo
{ {
[CommandHandler] [CommandHandler]
private void Run() private void Run()

View File

@@ -1,8 +1,4 @@
// RevitGen.Common/Attributes/RevitCommandAttribute.cs using System;
using System;
using Autodesk.Revit.Attributes;
namespace RevitGen.Attributes namespace RevitGen.Attributes
{ {
@@ -35,11 +31,6 @@ namespace RevitGen.Attributes
/// </summary> /// </summary>
public string ToolTip { get; set; } = ""; public string ToolTip { get; set; } = "";
/// <summary>
/// 命令的事务模式。生成器将根据此模式自动处理事务。
/// </summary>
public TransactionMode TransactionMode { get; set; } = TransactionMode.Manual;
/// <summary> /// <summary>
/// 构造函数 /// 构造函数
/// </summary> /// </summary>
@@ -52,5 +43,7 @@ namespace RevitGen.Attributes
} }
Text = text; Text = text;
} }
public bool UsingTransaction { get; set; }
} }
} }

View File

@@ -1,10 +1,10 @@
using Microsoft.CodeAnalysis; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
namespace RevitGen.Generator namespace RevitGen.Generator
{ {
/// <summary> /// <summary>
@@ -34,7 +34,7 @@ namespace RevitGen.Generator
} }
var attributeData = classSymbol.GetAttributes().First(ad => ad.AttributeClass?.ToDisplayString() == RevitCommandAttributeFullName); var attributeData = classSymbol.GetAttributes().First(ad => ad.AttributeClass?.ToDisplayString() == RevitCommandAttributeFullName);
var transactionMode = GetAttributeProperty(attributeData, "TransactionMode", 1); var usingTransaction = GetAttributeProperty(attributeData, "UsingTransaction", true);
var source = new StringBuilder(); var source = new StringBuilder();
source.AppendLine("// <auto-generated/>"); source.AppendLine("// <auto-generated/>");
@@ -46,7 +46,7 @@ namespace RevitGen.Generator
source.AppendLine(" using System.ComponentModel;"); source.AppendLine(" using System.ComponentModel;");
source.AppendLine(); source.AppendLine();
source.AppendLine($" [Transaction((TransactionMode){transactionMode})]"); source.AppendLine($" [Transaction(TransactionMode.Manual)]");
source.AppendLine($" public partial class {className} : IExternalCommand"); source.AppendLine($" public partial class {className} : IExternalCommand");
source.AppendLine(" {"); source.AppendLine(" {");
source.AppendLine(" private ExternalCommandData _commandData;"); source.AppendLine(" private ExternalCommandData _commandData;");
@@ -66,7 +66,7 @@ namespace RevitGen.Generator
source.AppendLine(); source.AppendLine();
source.AppendLine(" try"); source.AppendLine(" try");
source.AppendLine(" {"); source.AppendLine(" {");
if (transactionMode == 1) if (usingTransaction)
{ {
source.AppendLine($" using (var trans = new Transaction(this.Document, \"{className}\"))"); source.AppendLine($" using (var trans = new Transaction(this.Document, \"{className}\"))");
source.AppendLine(" {"); source.AppendLine(" {");
@@ -170,7 +170,8 @@ namespace RevitGen.Generator
source.AppendLine(); source.AppendLine();
var commandsWithData = commandClasses.Select(c => new { var commandsWithData = commandClasses.Select(c => new
{
Symbol = c, Symbol = c,
Attribute = c.GetAttributes().First(ad => ad.AttributeClass?.ToDisplayString() == RevitCommandAttributeFullName) Attribute = c.GetAttributes().First(ad => ad.AttributeClass?.ToDisplayString() == RevitCommandAttributeFullName)
}).ToList(); }).ToList();

View File

@@ -4,7 +4,7 @@
<!-- 包信息 --> <!-- 包信息 -->
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<PackageId>RevitGen</PackageId> <PackageId>RevitGen</PackageId>
<Version>1.0.1</Version> <Version>1.0.2</Version>
<Authors>ShrlAlgo</Authors> <Authors>ShrlAlgo</Authors>
<PackageProjectUrl>https://www.shrlalgo.cn/</PackageProjectUrl> <PackageProjectUrl>https://www.shrlalgo.cn/</PackageProjectUrl>
<RepositoryUrl>https://github.com/ShrlAlgo/RevitGen</RepositoryUrl> <RepositoryUrl>https://github.com/ShrlAlgo/RevitGen</RepositoryUrl>
@@ -23,12 +23,11 @@
<ItemGroup> <ItemGroup>
<!-- 1. 将 Common.dll 打包到 lib/ 目录 --> <!-- 1. 将 Common.dll 打包到 lib/ 目录 -->
<None Include="..\RevitGen.Common\bin\$(Configuration)\netstandard2.0\RevitGen.Common.dll" Pack="true" PackagePath="lib\netstandard2.0" /> <None Include="..\RevitGen.Common\bin\$(Configuration)\netstandard2.0\RevitGen.Common.dll" Pack="true" PackagePath="ref\netstandard2.0" />
<!-- 2. 将 Generator.dll 打包到 analyzers/ 目录 --> <!-- 2. 将 Generator.dll 打包到 analyzers/ 目录 -->
<None Include="..\RevitGen.Generator\bin\$(Configuration)\netstandard2.0\RevitGen.Generator.dll" Pack="true" PackagePath="analyzers\dotnet\cs" /> <None Include="..\RevitGen.Generator\bin\$(Configuration)\netstandard2.0\RevitGen.Generator.dll" Pack="true" PackagePath="analyzers\dotnet\cs" />
<None Include="build\RevitGen.targets" Pack="true" PackagePath="build" />
<!-- 3. 将新创建的 .targets 文件打包到 build/ 目录 --> <!-- 3. 将新创建的 .targets 文件打包到 build/ 目录 -->
<None Include="..\README.md" Pack="true" PackagePath="\" /> <None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\LICENSE" Pack="true" PackagePath="\" /> <None Include="..\LICENSE" Pack="true" PackagePath="\" />
@@ -43,7 +42,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Properties\" /> <Folder Include="Properties\" />
<Folder Include="build\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,13 +0,0 @@
<Project>
<ItemGroup>
<!--
这会告诉使用此包的项目,需要引用一个额外的程序集。
$(MSBuildThisFileDirectory) 是这个 .targets 文件所在的目录 (即 build/)。
我们用它来定位到我们打包进来的 Common.dll。
-->
<Reference Include="RevitGen.Common">
<HintPath>$(MSBuildThisFileDirectory)..\lib\netstandard2.0\RevitGen.Common.dll</HintPath>
<PrivateAssets>all</PrivateAssets>
</Reference>
</ItemGroup>
</Project>