majkel
majkel

Reputation: 31

AutoCad .net project using Visual Studio Code

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

Upvotes: 3

Views: 3733

Answers (1)

hansWurst-creator
hansWurst-creator

Reputation: 21

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.

  1. create a new project with: dotnet new classlib

  2. 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>
    
  3. set your launch.json to:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": ".NET Core Attach",
                "type": "clr",
                "request": "attach",
                "processId": "${command:pickProcess}"
            },
        ]
    }
    
  4. 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()
            {
            }
        }
    }
    
  5. run the command: dotnet build

  6. open autocad

  7. in autocad run the netload command and load your newly generated dll under PathToProject/bin/debug/net48/ProjectName.dll

  8. back in vscode press F5 to start debugging

  9. select the acad.exe process

  10. set breakpoint in your c# code

  11. in autocad call the command MyFirstCommand

  12. 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

Upvotes: 2

Related Questions