I’m sure many of you are aware that there is no "NETUNLOAD" command for unloading a .NET assembly from AutoCAD. Apparently this would require a significant change to AutoCAD's .NET API and is not likely to change anytime soon. Refer here for further details: http://through-the-interface.typepad.com/through_t
As an alternative, I have created an AutoCAD utility that allows you to "re-load" an assembly after making modifications to your code - the best bit being you can do this WITHOUT having to re-start AutoCAD each time.
I’d be really interested in hearing if you find this utility useful for AutoCAD .NET development purposes.
To run the utility:
1. download the attached NetReload.dll.
2. run AutoCAD, but do NOT start it from Visual Studio.
3. NETLOAD the NetReload.dll into AutoCAD.
4. open Visual Studio and make sure a file from the particular project you wish to re-load is active.
5. go back to AutoCAD and run the NRL command.
6. in the command-line, select the Visual Studio instance you wish to re-load into AutoCAD.
If you subsequently go back to Visual Studio and change your code, and then re-run the NRL command in AutoCAD again, the code changes will automatically be loaded into AutoCAD and ready to use.
Note that you won’t be able to debug your code in Visual Studio with this tool (i.e. place breakpoints and step through code). You’ll have to re-start AutoCAD from within Visual Studio to do this.
1. Supports multiple opened instances of Visual Studio.
2. Has been checked in AutoCAD 2011 and 2012 (should also work in AutoCAD 2010).
3. Has been checked with Visual Studio 2010 (should also work for previous versions).
4. The NRL command will default to the last chosen solution for the current AutoCAD session.
5. The NRL command will advise if:
- no Visual Studio instances are currently open.
- no solutions are open in any of the currently opened Visual Studio instances.
- no document is active for the chosen solution.
How It Works
Technically the NRL command does not actually re-load the assembly for the chosen Visual Studio solution. Instead it does the following:
1. accesses the chosen Visual Studio instance using Automation (i.e. COM).
2. based on the active Document for the Visual Studio instance, gets the project to compile.
3. sets the project’s "AssemblyName" property to a random name.
4. compiles the project to an assembly in the following project folder: "\bin\Debug\ReNetload"
5. sets the project’s " AssemblyName" property back to its orignal name.
6. NETLOADs the assembly from "\bin\Debug\ReNetload" into AutoCAD.
The reason this works is because when you NETLOAD the randomly named assembly into AutoCAD, all of the previously defined commands of the same name are overriden by those defined in the last loaded assembly.
A quick way to get this assembly to automatically load when AutoCAD starts is to add the lisp code below to the acad2010.lsp / acad2011.lsp / acad2012.lsp file. Obviously you will need to change this depending on where you place NetReload.dll.
(setq S:TARTUP (append S:TARTUP (list (command "netload" "C:\\MyAutoCAD\\ NetReload.dll") (princ))))
I have attached the C# source code file so you can build this yourself.
Note that you will need to add the following project references so that it compiles:
1. envdte.dll (Visual Studio Automation - i.e. COM).
2. System.Windows.Forms.dll (WaitCursor helper class).
3. acdbmgd.dll and acmgd.dll (AutoCAD).
Of course. Are you kidding? This is going to a great help for me. I cannot wait to try it out on Monday. I am just starting out to rewrite my acad VBA applications to .NET and I have come to the conclusion that I will need to develop as much of the applications as I can independent of AutoCAD before adding in the AutoCAD aspects that will require netloading to continue development. I have considered doing manually what NRL does but as a VS greenhorn I am already overwhelmed performing even the simplest task. I hope you will share this and future applications with us.
I built the project with no errors reported but Microsoft stepped in at Netload time telling me I did not have permissions to do so. I am using VS 2010 express. I can build and netload vb creations. Can you help with this? Thanks.
Thanks for highlighting your issue. Unfortunately it appears that Visual Studio Automation (i.e. COM) is not supported for express editions. I assume this is the problem you are experiencing.
I do have another technique for automating this process using the pre-build event command line setting in Visual Studio. I'll send you a private message on here in a short while.
I would not recommend this method for debugging:
1. If the application is subscribed with the events of AutoCAD - will be conflict.
2. If your application uses shared resources (file, database) - will be conflict.
3. If the application uses the palette - will be conflict.
Only in the simplest cases, you can use this algorithm.
I cannot "build" my app after it has been netloaded in acad. VS 2008 gives me this message:
Error 1 Unable to copy file "obj\Release\ViewPorts.dll" to "bin\Debug\ViewPorts.dll". The process cannot access the file 'bin\Debug\ViewPorts.dll' because it is being used by another process. ViewPorts
1. Start acad
2. Load app
3. Make changes to code
4. Build generates error
Yes these should definately be kept in mind. This utility will not necessarily suit some of the situations you listed, and I wouldn't say it's the way to go when debugging large fully fledged applications.
But if you're just looking to experiment with different parts of the API it is very cumbersome to:
1. Close the drawing you are working on.
2. Close AutoCAD.
3. Change your code in Visual Studio.
4. Build your dll in Visual Studio.
5. Re-start AutoCAD.
6. Re-open the drawing you are working on.
7. NETLOAD your revised dll.
Especially for every change you make to your code - no matter how small. With my utility you can skip steps 1, 2, 4, 5, 6 and 7 above. All you have to do is change your code in Visual Studio and then run my NRL command in AutoCAD.
I like to think this is for the pragmatist. It's saved me loads of time.
Thank you for your feedback.
Yes that is the error you will receive in Visual Studio when you try to build (F6) to a dll that has already been loaded into AutoCAD (i.e. using NETLOAD). Makes sense to me.
It's important to understand with this utility that you don't actually do anything from Visual Studio (other than change your code).
In order to use the utility, please try the following:
1. Download the attached zip file from my original post and extract the NetReload.dll file.
2. Open your solution in Visual Studio and make sure that at least one of your project's code files are current.
3. In AutoCAD, load the NetReload.dll (i.e. using NETLOAD).
4. Run the NRL command in AutoCAD. This command will do all of the work for you. You will be prompted from within AutoCAD to select a running instance of Visual Studio. The command will then automatically build the solution and load it into AutoCAD. You won't have to switch back to Visual Studio.
Please don't hesitate to let me know how this goes for you, or if you have any further questions on this.
I understand now. Thank you very much - this is an awesome tool! Nice code - that opens some new doors. Thanks for sharing.
The user has to make sure to build from VS at the end of the development process to make sure the dll with the desired name matches the "tempassemblyname" dll.
Yes that's right. The utility creates randomly named dll files in the "bin\Debug\NetReload\" folder. Once you're finished you should build (F6) your solution in Visual Studio to update the dll in the "bin\Debug\" folder.
Glad you like it
Access a broad range of knowledge to help get the most out of your products and services.