I was trying to figure out if it is possible to set and debug a vb.net/C#.net AutoCAD project using VS Code? Sorry if this is a silly question but I do not have extensive experience in this area. So far I've been able to follow tutorials, set and develop a project using VS Community but couldn't find any guidance on how to do it with VS Code. Thanks in advance
1 Answer
Got it working with vscode and Acad2021. The example works with the following setup:
To get debugging working with full .net framework I followed this guide.
create a new project with: dotnet new classlib
change your csproj file to (Note: for different autocad versions different versions of the nuget packages will be needed):
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <TargetFramework>net48</TargetFramework> <RootNamespace>ProjectName</RootNamespace> <PlatformTarget>x64</PlatformTarget> <DebugType>portable</DebugType> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> <ItemGroup> <PackageReference Include="AutoCAD.NET" Version="24.0.0"></PackageReference> <PackageReference Include="AutoCADCommands" Version="2020.0.0"></PackageReference> </ItemGroup> </Project>set your launch.json to:
{ "version": "0.2.0", "configurations": [ { "name": ".NET Core Attach", "type": "clr", "request": "attach", "processId": "${command:pickProcess}" }, ] }change the Class1.cs file to:
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; namespace AU.KO_WT_TestPlugin { public class Initialization : IExtensionApplication { [Autodesk.AutoCAD.Runtime.CommandMethod("MyFirstCommand")] public void cmdMyFirst() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\nI have created my first command."); } void IExtensionApplication.Initialize() { } void IExtensionApplication.Terminate() { } } }run the command: dotnet build
open autocad
in autocad run the netload command and load your newly generated dll under PathToProject/bin/debug/net48/ProjectName.dll
back in vscode press F5 to start debugging
select the acad.exe process
set breakpoint in your c# code
in autocad call the command MyFirstCommand
debug away
Notes:
- as .net assemblies can't be unloaded in autocad after changing your code and recompiling it with dotnet build, it is necessary to restart autocad and netload the new assembly
- It should be possible to create a task that launches autocad and netloads the assembly through a script. I haven't found time to explore that possibility
visual studio code debuggingthe very first result should be Debugging in Visual Studio Code