One of the top requests whenever we survey programmers on their needs is “more sample code”. And I agree this is important – its what I want too when I’m learning an API. One oft overlooked source of sample code is AutoCAD itself. We don’t obfuscate our .NET code, and so the AutoCAD components written in .NET (mostly the UI components) provide good usage and (hopefully ) also best practice examples of how to use the various APIs we expose. This is one of the advantages of ‘eating your own dog food’ – another advantage is obviously that it provides additional ‘on the job’ testing for the APIs. To access this sample code, you just need a .NET decompiler, like RedGate Reflector. Simply open the DLL you think might be a .NET assembly and let Reflector do its thing. In this example, I’ve opened the AutoCAD 2012 AcLayer.dll assembly:
Expanding the assembly allows you to drill down into the namespaces and classes it contains:
In this case, I’m looking at the AutoCAD Commands defined by the assembly:
I can drill down into the implementation of each command:
I can even view the code in different .NET languages. The above image is C#, now I’ve changed to VB.NET:
The MSIL doesn’t include comments, of course, so you’re on your own working out exactly what the code is doing. As an additional aid, Kean has already demonstrated on his blog how to use Reflector Pro to debug into the AutoCAD .NET API layer.