.NET

.NET

Reply
Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 1 of 26 (2,059 Views)

NET-RELOAD Utility for Visual Studio - Download Here

2059 Views, 25 Replies
10-09-2011 06:30 AM

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::smileyfrustrated:TARTUP (append S::smileyfrustrated:TARTUP (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).

Valued Contributor
aksaks
Posts: 93
Registered: ‎03-23-2009
Message 2 of 26 (2,035 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-09-2011 09:18 AM in reply to: Artvegas

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.

 

Thank you!

 

aks

Valued Contributor
aksaks
Posts: 93
Registered: ‎03-23-2009
Message 3 of 26 (1,959 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-10-2011 08:12 PM in reply to: Artvegas

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. 

Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 4 of 26 (1,946 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-10-2011 11:25 PM in reply to: aksaks

Hi aks,

 

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.

 

Cheers,
Art

Moderator
Alexander.Rivilis
Posts: 1,451
Registered: ‎04-09-2008
Message 5 of 26 (1,909 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-12-2011 02:07 AM in reply to: Artvegas

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


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 6 of 26 (1,876 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-12-2011 07:57 AM in reply to: Artvegas

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

Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 7 of 26 (1,868 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-12-2011 08:37 AM in reply to: Alexander.Rivilis

Hi Alexander,

 

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.

 

Art

Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 8 of 26 (1,855 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-12-2011 09:14 AM in reply to: fieldguy

Hi fieldguy,

 

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.

 

Cheers,

Art

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 9 of 26 (1,849 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-12-2011 09:26 AM in reply to: Artvegas

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. 

Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 10 of 26 (1,841 Views)

Re: NET-RELOAD Utility for Visual Studio - Download Here

10-12-2011 09:39 AM in reply to: fieldguy

Hi fieldguy,

 

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 :smileyhappy:

 

Art

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.