1

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

majkel
  • 11
  • 2
  • 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](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwiHke638Z_oAhXO0KQKHeNQDdgQFjAAegQIARAB&url=https%3A%2F%2Fcode.visualstudio.com%2Fdocs%2Feditor%2Fdebugging&usg=AOvVaw0L79mzgvXAGPG76wBUIUIG) – Panagiotis Kanavos Mar 16 '20 at 20:59
  • Do you mean you want to create a Test Bench? – jdweng Mar 16 '20 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. – CAD Developer Mar 18 '20 at 04:20
  • @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. – majkel Mar 22 '20 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 – majkel Mar 22 '20 at 16:03
  • @CADDeveloper, thanks for your reply. I am a begginer in coding and to be honest I started doing that to improve functionality of AutoCAD (Civil 3D) on the projects I am working on. I was following the tutorials and basing on the API developers reference guide was able to introduce some of my ideas using VS Community. With VSCode I am able to start the VB.NET Project(I dont know how to set application type to WPF Class Library - this is what I've done in VS Comminity),add the references but I do not know how to modify the json files to run the project. I was hoping someone already did it – majkel Mar 22 '20 at 16:14

1 Answers1

1

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