How can I improve the speed of .NET Plug-in Develo

2019-01-28 21:34发布

I'm developing an AutoCAD add-in, which uses a .NET 4.6 assembly. I am finding the development process very frustrating; the API is very large and the documentation beyond getting started is all over the place and very hard to find. The only way to get anything done seems to be just prototyping functions in Visual Studio and seeing if it works. It then takes several minutes to load up AutoCAD and attach a debugger, which wrecks my prototyping workflow.

It would be very handy if I could have something like Linqpad to prototype my applications so I can find the data that I'm looking for. I'm not married to the idea of using Linqpad; however, if there's another technique I'm missing I'd love to hear about it.

I'm not sure if this is something I might achieve with the pro version but as there is no trial I can't find out.

2条回答
乱世女痞
2楼-- · 2019-01-28 21:50

It is true, the API is indeed large, and some functionality is somewhat poorly documented. I found it difficult to get started, but once I became familiar with the core concept and functionality, I started to very much enjoy working with it as the API very powerful and easy to work with once you "get it". I am not sure what sources you have checked, but I started with this Developer Documentation. Scroll down to the training labs. I would recommend that you at least read through both ObjectARX and .NET Training labs. Spend a few days working through the .NET labs as it will save you countless hours down the line.

As for debugging, the set-up below should give you the most effective and efficient approach (here I use VS 2015 and AutoCad 2015). In the project properties, under the "Debug" tab, your settings should be as follows:

  1. Check "Start external program:" and enter the path of the AutoCad executable. This applies to other versions of AutoCad.
  2. In the "Command line arguments:" field, enter /nologo. This will significantly speed up the start-up of AutoCad.

enter image description here

To load your dll into AutoCad automatically, you can either do it through the registry (my preferred way) or through acad20xxdoc.lsp or acad.lsp (AutoCad will run these scripts by default for each document you open or when AutoCad starts respectively). You can find details in this post.

Now Pressing F5 will start AutoCad and load the debugger. You can now step through lines of code as for any other piece of code. Place a break point at the start of your code, then execute your command in AutoCad. Your break point should get hit.

Aside: /nologo can also be added to AutoCad's Desktop shortcut to speed up AutoCad for users. It can be added to the "Target:" field in the shortcut properties. i.e. "C:\Program Files\Autodesk\AutoCAD 2015\acad.exe" /nologo

Note: A possible Gotcha! Make sure that any AutoCad dlls that your project references have their properties set to:

  1. Embed Interop Types -> False
  2. Copy Local -> False. Also make sure that there are no AutoCAD dlls (i.e. acmgd.dll or any dlls with names starting with "ac") in your Debug/Release directory. Delete any that you find. Not doing so will cause errors and crashes that will be very hard to trace down.
查看更多
Lonely孤独者°
3楼-- · 2019-01-28 22:03

You can run a script at debug to NETLOAD your DLL with the /b commandline switch.

Add something like the following to the end of what you already have in the Command line arguments text box in Visual Studio

/b "C:\Path\To\Script\AutoNetLoadDebug.scr"

The script called AutoNetLoadDebug.scr has contents are something like

netload "C:\Path\To\Dll\Your.dll"

Note: there needs to a line feed at the end of that line so it actually runs the command. Make sure you don't have AutoCAD set up to already load that DLL or a different build of it. If you do, launch AutoCAD with a different profile using the /p command line switch.

You can't run anything from the AutoCAD API outside of AutoCAD, the .NET DLLs are just mappings of the unmanaged code deep in the bowels of AutoCAD

I do lots of logging (Serilog), especially in debug. There's also CADtest on Github as well as https://github.com/wtertinek/AcadTestRunner. I tried once to Mock the AutoCAD API. Once. Hence, CADtest.

Protip: try-catch everywhere and watch for nulls but you have probably already discovered that.

Read the AutoCAD Tag wiki and have a look at the other forums and blogs mentioned there, they are goldmine of AutoCAD API knowledge

查看更多
登录 后发表回答