3

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

6
  • 1
    Visual Studio Code isn't Visual Studio, it's completely different. You can't use tutorials made for one to learn how to use the other. There are a lot of tutorials about Visual Studio Code. If you google for visual studio code debugging the very first result should be Debugging in Visual Studio Code Commented Mar 16, 2020 at 20:59
  • Do you mean you want to create a Test Bench? Commented Mar 16, 2020 at 21:46
  • Let's reverse the question and ask "Why not?" What have You done, and what is the problem? as I know and maybe I'm wrong it should be possible. Commented Mar 18, 2020 at 4:20
  • 1
    @PanagiotisKanavos, thanks for your anser. I can see that these two are completly different softwares so I cannot use the tutorials from one to work with another. That is actually the reason I asked my question here. Unfortunately my basing on my current knowledge I was unable to find too much useful information for this excercise. Commented Mar 22, 2020 at 16:00
  • @jdweng thanks for the answer, I believe I was not clear enough. I want to be able to build/compile the project to get the .dll file that I can load to the Autocad and use the functions defined in a code Commented Mar 22, 2020 at 16:03

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.