CAD Clinic: Going .Net15 May, 2005 By: Mike Tuersley Cadalyst
Get started with .Net in AutoCAD 2006
With the release of AutoCAD 2006, the AutoCAD programming world is one step closer to a pure Microsoft .Net interface. The heralded managed API is extremely close to completion (just missing custom object support), and now's the time to take the leap and be ahead of the pack!
Now, if you're thinking that you don't have Visual Studio .Net, so why should you read this, the main point of this article is that you don't need Visual Studio .Net to write .Net code for AutoCAD 2006. .Net includes command line compilers for both VB.NET and C#.NET, so anyone can compile their source to an EXE or DLL. This article is intended for anyone who customizes AutoCAD or one of its verticals.
CLR. . . IL. . . JIT
What? Is that some new language? Well, it is sort of -- it's Microsoft-ese (for lack of a better term). These acronyms mean:
CLR: Common Runtime Language
IL or MSI: Managed Code/Intermediate Language
JIT: Just In Time
Before .Net each programming language compiled its source code into native, processor-specific code which typically meant you needed the IDE (Integrated Development Editor) in order to compile it. .Net introduced compiling to IL, which is much like a staging area. Whenever the code is accessed to run, the .Net JIT compiler takes the IL code and converts it to native code.
The beauty is that the JIT compiler doesn't care whether the source was written in VB.NET, C#.NET or C++.NET.
So, what do you need to get started? Just download and install the latest version of the Microsoft .Net Framework. The Framework is an integral Windows component for building and running the next generation of software applications and Web services. ¹
Once you download and install it, you have access to the .Net compilers. Next you need to decide what, or which editor, you're going to write your code in. Three possibilities are:
1. Purchase a version of Microsoft's Visual Studio.Net. I have to include this option because it is the best option. With Studio, you have tools not available in the next three options (figure 1).
Figure 1. A screen capture of Microsoft's Visual Studio .Net program.
2. Download a copy of one of the Visual Express 2005 beta IDEs. While these are still in beta, they work extremely well and are quite stable. Eventually, they won't be free, but the rumored retail price will be $49.
3. Download a free IDE such as SharpDevelop. This great freeware editor lets you generate forms if nothing else (figure 2).
Figure 2. A screen capture of SharpDevelop.
4. Create your own IDE. What? Because the .Net compilers can be accessed from a Command prompt, you can use any editor of your choice such as Programmer's File Editor [PFE32], NotePad++, SourceCodeEditor and the like. From some of these, you can even setup access to the .Net compilers. Granted, this is not the best choice, but it is a valid one, especially if you need to prove to management that an investment in Visual Studio will have returns.
Choose Your Language
Choosing your programming language is probably where an AutoLISP programmer has an advantage over the VBA or VB6er. The reason is there's no baggage to bring over into .Net. Focusing on just VB for the moment, VB.NET is not VB7 and has nothing in common with VB6 other than the name Visual Basic. That's a hard one for VB-ers entering .Net, especially when they see that they can still write the same code they've used for years and it still works for the most part. The reality is that the code can cause more problems than it's worth. For example, the old style of error handling does not use the .Net garbage collector and setting an object to Nothing doesn't eliminate it from memory. These oversights will cause nagging memory leaks in your programs.
My advice is to jump into C# so you know you won't bring anything along for the ride. Sure, you have a learning curve, but you'll be happier in the long run.
Remember that C# has nothing to do with C++. It is a new programming language formatted similar to C++, but it has the rapid development tools found in VB -- especially when it comes to GUIs.
If you choose to use a program other than one of the Visual Studio versions or SharpDevelop, you need to know how to access the command compiler. It's very simple. Assuming you have the .Net framework installed, look in the directory C:\WINDOWS\Microsoft.NET\Framework\<Version_Number> -- where <Version_Number> is the version of the framework you have installed such as v1.1.4322.
In this folder there are two executables:
- CSC.EXE -- the C# compiler
- VBC.EXE -- the VB compiler
csc /t:library /out:MyCodeLibrary.dll myCSharpCode.cs for a DLL
csc /t:exe /out:MyCodeExecutable.exe myCSharpCode.cs for an EXE
For an extensive list of compiler options, visit the Microsoft web site.
Kick It Up A Notch!
For once, this falls to me. Here at the Clinic, I will start showing .Net code done in both VB.NET and C#.NET. No, I won't be abandoning VBA examples or answering questions involving VBA. But I will focus more on the .Net technology since it is the next generation platform for Autodesk customization and not a lot of resources on the Internet target this area.
Next month, I'll introduce both VB.NET and C#.NET programming by starting you off with a simple program and showing it in both languages. Until then, I challenge you to go download the Framework and decide on an editor to use so that you'll be ready to try out the program next month and get yourself started in .Net development.
In the meantime, if you are interested, check out a copy of VB.NET or C# for Dummies at your local library and start digging in!
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!