Console app in C# fails to execute as soon as data

2019-08-16 14:26发布

I'm developing some AutoCAD add-ons in C# and I was hoping to work/debug my classes in a console application first until I'm ready to implement the functionality inside AutoCAD. /dot net libraries cannot be unloaded and one needs to restart/reload acad each time the code is modified/ Interestingly I'm finding that as soon as I declare a variable which uses an autocad data type my console app refuses to run and Im presented with a "the application is in break mode" screen in visual studio. For example this code does not run:

  using System;
  using Autodesk.AutoCAD.Geometry;
  namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hi");
            Point2d p;            
            Console.ReadKey();
        }
    }
}

Trying this referencing Autodesk C3D 2016 dll libraries. Does anyone have an explanation of what is going on here and/or any workarounds? thnks

2条回答
Evening l夕情丶
2楼-- · 2019-08-16 14:36

The AutoCAD .NET API is designed to run in-process only. AutoCAD .NET libraries can only be used to build plug-ins (DLL) which have to be loaded in AutoCAD for execution.

查看更多
Luminary・发光体
3楼-- · 2019-08-16 14:36

To debug your AutoCAD Add-On code, you need to create a C# Class Library Project referencing the Autodesk CAD 2016 Sdk Libraries, and encapsulate your code on public method that declared with CommandMethodAttribute. With this method declared with CommandMethod it's the be your trigger between the AutoCAD prompt command and your Add-On code.

public class AcadCommands
{
    [CommandMethod("TriggerCmd")]
    public void TriggerCommand
    {
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        ed.WriteMessage("Hi");
        Point2d p;
    }
}

Note: at your Visual Studio Project Properties, you need to configure to start debug pointed to acad.exe and when the AutoCAD applications starts, open some DWG and prompt the "NETLOAD" command to include your debbugable compiled DLL from your VS Project to AutoCAD Application context. enter image description here

查看更多
登录 后发表回答