目的
以下を満たす Visual Studio / dotnet new 用テンプレートをゼロから作成します。
- 対象CAD:AutoCAD 2025 / 2026 / 2027 対応
- 元となるテンプレート:SDK-style Class Library
- 構成:Debug_ACAD2025 / Release_ACAD2025 構成
- プラットフォーム:x64固定
- 構成切替で TargetFramework / NuGet / DLL名切替
- WPF / WinForms 切替
- CommunityToolkit.Mvvm 導入
- myCommands.cs / PluginExtension.cs を初期配置
- Visual Studio から「新規プロジェクト」で利用可能
これらの設定は、AutoCAD.NETのWizardでも作成できますが、構成を変更することで複数のバージョンに一つのプロジェクトで対応することを目的にしています。
作成手順
以下手順を、Visual Studioだけではできない手順がありますのでPowerShellで実施します。
フォルダー構成は、
- C:\Source\Repos\ProjectTemplates
に作成する手順として記します。
1. 作業フォルダー作成
PowerShell を開く。
mkdir C:\Source\Repos\ProjectTemplates
cd C:\Source\Repos\ProjectTemplates
作業フォルダーがない場合に作業フォルダーを作成します
2. 元になるクラスライブラリ作成
- クラスライブラリを使って、元のテンプレートとします
dotnet new classlib -n MyCompany.AutoCAD.Plugin
cd MyCompany.AutoCAD.Plugin
3. 不要ファイル削除
Class1.csは今後の操作で利用しないので、一旦削除します。
del Class1.cs
4. csproj を全面書き換え
ファイル
- 以下はVS Codeを使っての作業ですので、codeコマンドで起動します
code MyCompany.AutoCAD.Plugin.csproj
内容
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition="'$(Configuration)' == ''">Debug_ACAD2025</Configuration>
<Platform Condition="'$(Platform)' == ''">x64</Platform>
<TargetFramework>net8.0-windows</TargetFramework>
<AutoCADNetVersion>25.0.1</AutoCADNetVersion>
<Configurations>
Debug_ACAD2025;
Release_ACAD2025;
Debug_ACAD2026;
Release_ACAD2026;
Debug_ACAD2027;
Release_ACAD2027
</Configurations>
<Platforms>x64</Platforms>
<PlatformTarget>x64</PlatformTarget>
<Prefer32Bit>false</Prefer32Bit>
<UseWPF>true</UseWPF>
<UseWindowsForms>false</UseWindowsForms>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug_ACAD2025' Or '$(Configuration)' == 'Release_ACAD2025'">
<TargetFramework>net8.0-windows</TargetFramework>
<AutoCADNetVersion>25.0.1</AutoCADNetVersion>
<AssemblyName>$(MSBuildProjectName)_2025</AssemblyName>
<DefineConstants>$(DefineConstants);ACAD2025</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug_ACAD2026' Or '$(Configuration)' == 'Release_ACAD2026'">
<TargetFramework>net8.0-windows</TargetFramework>
<AutoCADNetVersion>25.1.0</AutoCADNetVersion>
<AssemblyName>$(MSBuildProjectName)_2026</AssemblyName>
<DefineConstants>$(DefineConstants);ACAD2026</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug_ACAD2027' Or '$(Configuration)' == 'Release_ACAD2027'">
<TargetFramework>net10.0-windows</TargetFramework>
<AutoCADNetVersion>26.0.0</AutoCADNetVersion>
<AssemblyName>$(MSBuildProjectName)_2027</AssemblyName>
<DefineConstants>$(DefineConstants);ACAD2027</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.StartsWith('Debug'))">
<OutputPath>bin\Debug\</OutputPath>
<Optimize>false</Optimize>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.StartsWith('Release'))">
<OutputPath>bin\Release\</OutputPath>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoCAD.NET" Version="$(AutoCADNetVersion)" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
</ItemGroup>
</Project>
5. myCommands.cs 作成
ここでは、CADの実行コマンドでHello Worldを作ります
code myCommands.cs
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(MyCompany.AutoCAD.Plugin.MyCommands))]
[assembly: ExtensionApplication(typeof(MyCompany.AutoCAD.Plugin.PluginExtension))]
namespace MyCompany.AutoCAD.Plugin;
public class MyCommands
{
[CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
public void MyCommand()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
if (doc != null)
{
Editor ed = doc.Editor;
ed.WriteMessage("\nHello AutoCAD.");
}
}
}
6. PluginExtension.cs 作成
code PluginExtension.cs
using Autodesk.AutoCAD.Runtime;
namespace MyCompany.AutoCAD.Plugin;
public class PluginExtension : IExtensionApplication
{
public void Initialize()
{
}
public void Terminate()
{
}
}
7. Restore
一度、プロジェクトをリストアします
dotnet restore
8. Build
初回起動です。
動作に問題ないことを確認するために、ビルドします。
dotnet build -c Debug_ACAD2025
9. .template.config 作成
ここからは、テンプレートプロジェクトとしての設定を行います。
mkdir .template.config
10. template.json 作成
code .\.template.config\template.json
{
"$schema": "http://json.schemastore.org/template",
"author": "MyCompany",
"identity": "MyCompany.AutoCAD.Plugin.Template",
"name": "AutoCAD Plugin Template",
"shortName": "acad-plugin",
"sourceName": "MyCompany.AutoCAD.Plugin",
"preferNameDirectory": true,
"tags": {
"language": "C#",
"type": "project"
}
}
コード内のNamespaceなどの文字列は、ここで置き換える定義を作ります。
11. テンプレート登録
dotnet new install .
12. テンプレートから新規作成
cd ..
dotnet new acad-plugin -n SamplePlugin
13. Visual Studio で開く
この手順で、VSCodeなど、Visual Studioのエディターが開かれるケースもありますので、その場合は、Visual Studio 2026などで開いてください
start SamplePlugin.sln
14. AutoCAD で確認
NETLOAD で:
bin\Debug\SamplePlugin_2025.dll
を読み込み。
MyCommand
を実行する。
15. sourceName の意味
前述したが、テンプレート内に:
MyCompany.AutoCAD.Plugin
と書いておくと、
SamplePlugin
へ自動置換される。
つまり:
namespace MyCompany.AutoCAD.Plugin;
↓
namespace SamplePlugin;
へ変換される。
16. バッチビルドについて
バッチビルドで、AutoCAD2027とそれ以外を一緒に実行することができません。
これは、Frameworkの設定の関係で、.NET8が優先してコンパイルされていまうため、.NET10.0であるAutoCAD2027を一緒に実行できないためです。