NET-RELOAD Utility for Visual Studio - Download Here

NET-RELOAD Utility for Visual Studio - Download Here

Anonymous
Not applicable
16,471 Views
46 Replies
Message 1 of 47

NET-RELOAD Utility for Visual Studio - Download Here

Anonymous
Not applicable

NET-RELOAD Utility

 

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_the_interface/2008/09/tired-of-not-be.html

 

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.

 

Cheers,

Art

 

 

Instructions

 

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.

 

 

Notes

 

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.

 

 

Tips

 

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::STARTUP (append S::STARTUP (list (command "netload" "C:\\MyAutoCAD\\ NetReload.dll") (princ))))

 

 

Source Code

 

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).

16,472 Views
46 Replies
Replies (46)
Message 41 of 47

chris.harling
Explorer
Explorer

Hey guys, 

 

I made a Visual Studio 2019 solution for NETRELOAD to hopefully make compiling a bit easier.

 

Sadly Autodesk doesn't let me upload a single *.dll file, so if you don't want to compile:

1) Unzip the archive and navigate to NETRELOAD\bin\Debug

2) In that folder is NETRELOAD.dll

3) In AutoCAD, you can use NETLOAD to load in that file.

 

Tested with Visual Studio 2019 and AutoCAD Electrical 2021

 

hope it helps.

Message 42 of 47

Chuong.Ho
Advocate
Advocate

It is a open allow you can see detail command and lispfunction to execute do not need restart autocad.

https://github.com/chuongmep/CadAddinManager

Chuong Ho

EESignature

Message 43 of 47

desdinova
Advocate
Advocate

Hi Chuong.Ho,

Your app is working so good but

I can't load my some apps. Can you look my addin in attachment.

What can be the issue?

Regards.

https://www.sendgb.com/upload/?utm_source=uwR7eqsEd0p 

0 Likes
Message 44 of 47

Chuong.Ho
Advocate
Advocate

I think you need check assembly ref, it working in my computer.

Regard !

ChuongHo_1-1654491220988.png

 

 

 

Chuong Ho

EESignature

Message 45 of 47

desdinova
Advocate
Advocate

Thanh you for your answer.

I will check the references.

0 Likes
Message 46 of 47

MGO-Norsyn
Advocate
Advocate

Great tool, thank you Chuong.Ho!

0 Likes
Message 47 of 47

MGO-Norsyn
Advocate
Advocate

Hi all

I would like to contribute with a solution for a problem with NetReload utility.

 

Act I

I have been using this to great effect in my simple codes, but as the time goes, more dependencies appear and some of them do not play nice with NetReload. For example System.Text.Json. I wanted to use this library instead of Newtonsoft, but, as it needs to be installed as a nuget package, it doesn't get loaded with NetReload for some reason.

I have searched for a solution for many years, but couldn't find one (I am a coding noob), so I was restrained from using external nugets if I wanted to use NetReload. I had to literally copy the source code of the desired libraries or use the normal debug mode with restarting AutoCad (hot reload helps, but cannot handle all situations).

Then I finally found a solution, I don't remember how, but it is almost a miracle, thank God, because it has been interfering with my work for many years.

The solution is to create a custom eventhandler for ResolveEvent:

 

 

private static Assembly Debug_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string assemblyFolder = pathToAssembly;
    var name = args.Name.Split(',')[0];
    name += ".dll";
    string filePath = Path.Combine(assemblyFolder, name);
    if (File.Exists(filePath)) return Assembly.LoadFrom(filePath);
    else return null;
}

 

 

Then in your IExtensionApplication.Initialize() method subscribe to the ResolveEvent with the custom eventhandler:

 

 

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Debug_AssemblyResolve);

 

 

When compiling, all dependencies' dlls are copied to the assembly folder and so the resolver can find them there. If it can't, it falls back on the general resolve routine.

This was help from God sent to me, now I could use external libraries from nugets with netreload.

End of Act I

 

Act II

In my new happy life, I was coding my simple codes happily, but then a strange affliction struck. Sometimes, when using Netreload, the assemblys' IExtensionApplication.Initialize() wouldn't run. I would type 'NRL' and see the message that the reloading has taken place, but my own message from Initialize() wouldn't show and no custom commands would be available.

So my assembly wasn't loaded after all. And I simply couldn't find what was wrong and I didn't know where to look. What was more confusing, it seems to be working on my work machine but not on my home machine. I've tried to solve this problem a number of times, but always failed, as I never got an error message. So I would send a silent prayer thanking God each time a reload succeeded, because it was pure magic for me.

I was suffering for some time, but then God took mercy on me, and after some failed attempts, I finally looked at the fuslogvw. It showed some binding failures, but didn't tell me where they occured. As my Inititialize() method never ran, this must happen before loading into AutoCAD.

In a pure bout of miracle inspiration, I started NetReload in debug mode and somehow came to realize, that NetReload also failed to load some assemblies sometimes when running 'NRL' command. Using my previous experience from Act I, I created a custom AssemblyResolver for NetReload too. This code must be added at the top of 'NRL' command:

 

 

string netReloadDir = null;

ResolveEventHandler assemblyResolveHandler = (sender, args) =>
{
    if (netReloadDir == null) return null;
    ed.WriteMessage($"Resolving assembly: {args.Name}\n");

    string parentDir = Directory.GetParent(netReloadDir).FullName;
    string assemblyPath = Path.Combine(parentDir, new AssemblyName(args.Name).Name + ".dll");

    if (File.Exists(assemblyPath))
    {
        ed.WriteMessage($"Found and loading assembly!\n");
        return Assembly.LoadFrom(assemblyPath);
    }

    ed.WriteMessage($"Assembly not found in parent path!\n");
    return null; // Let the default resolution process handle it
};

 

 

 netReloadDir is set further down in the method, when we know the location. Thank God, this cured my strange affliction.

End of Act II

 

If you want to see the final code, it is in my fork of NetReload:

https://github.com/shtirlitsDva/Autocad-Civil3d-Tools/tree/master/Acad-C3D-Tools/Net-Reload

 

I've also added some simple improvements, as such as not compiling if the configuration is not set to Debug. Also I added a custom keyword selector when selecting a solution to compile, as the builtin ed.GetKeywords' PromptKeywordOptions is very strange (I still cannot find a comprehensive guide on how to use it with keywords) and fails if your solution have a mix of cases and hyphens and it does not handle it very well.

 

Thank you for reading this post and I hope this helps other lonely souls in their journey in coding simple codes for AutoCAD.