Supporting multiple AutoCAD versions - conditional compilation in C#

Someone asked by email how to get the block import code first shown last week and further discussed in yesterday's post working with AutoCAD 2006. I've been using AutoCAD 2007 for this, but to get it working there are only a few changes to be made.

Firstly you'll need to make sure you have the correct versions of acmgd.dll and acdbmgd.dll referenced into the project (you should be able to tell from their path which version of AutoCAD they were installed with).

Secondly you'll need to modify your code slightly, as WblockCloneObjects() has changed its signature from 2006 to 2007. The below code segment shows how to use pre-processor directives to implement conditional compilation in C#. You will need to set either AC2006 or AC2007 as a "conditional compilation symbol" in your project settings for this to work:

#if AC2006

        mapping = sourceDb.WblockCloneObjects(blockIds,

                                              destDb.BlockTableId,

                                              DuplicateRecordCloning.Replace,

                                              false);

#elif AC2007

        sourceDb.WblockCloneObjects(blockIds,

                                    destDb.BlockTableId,

                                    mapping,

                                    DuplicateRecordCloning.Replace,

                                    false);

#endif

You would possibly use #else rather than #elif above, simply because that would make your code more future-proof: it would automatically support new versions without needing to specifically add code to check for them.

On a side note, you can actually specify that entire methods should only be compiled into a certain version. Firstly, you'll need to import the System.Diagnostics namespace:

using System.Diagnostics;

Then you simply use the Conditional attribute to declare a particular method (in this case a command) for a specific version of AutoCAD (you might also specify a debug-only command by being conditional on the DEBUG pre-processor symbol, should you wish):

namespace BlockImport

{

  public class BlockImportClass

  {

    [Conditional("AC2007"),CommandMethod("IB")]

    public void ImportBlock()

    {

...

11 responses to “Supporting multiple AutoCAD versions - conditional compilation in C#”

  1. Why doesn't Autodesk provide such a define in its own headers??

  2. There are no headers for .NET, and therefore nothing that could be picked up by the pre-processor.

    You could probably use reflection to query the type information at runtime (or the version of the product/assemblies used), but it gets messy. It would have the advantage of not needing different apps for different versions, however.

    As for ObjectARX, there is a wishlist in the system for a version indicator in the headers that can be queried by the pre-processor.

    Regards,

    Kean

  3. How would this code run in AutoCAD 2004 and 2005?

    Our problem with adopting .NET for our ACAD apps is the need to support a few versions back. Users simply are not upgrading fast enough!

  4. The code would not work with AutoCAD 2004 and *might* work with AutoCAD 2005 (I haven't tried it).

    This is a classic problem: a new generation of development technology gets introduced but you need to support legacy product versions for which it is not available. The same happened for VBA and ObjectARX, too. One option would be to implement new features only for newer versions using the new technology, gradually phasing it into existing code. Otherwise you would unfortunately either have to use old technology (a waste of time & energy) or wait for the new technology to be available across all supported versions of the product.

    Regards,

    Kean

  5. Hi Kean,
    As a beginner with C# and AutoCAD, what are the essentials I need to start a project? (dlls etc)

    Have you anything in your archives that I could use as a "getting started" guide?

    Thanks in advance

    Ben

  6. Kean,

    Re-activating the theme, I didn't understand how I will reference the "acmgd" and "acdbmgd", for the both two (o "N") versions that I want my application to be compatible?
    If possible, will it make possible to run the same binary in diferent versions of AutoCAD?

    Thanks again,
    Felipe
    Sp/Brasil

  7. Kean Walmsley Avatar

    Felipe,

    You should use assemblies that match the oldest AutoCAD version you want to support. We do try to maintain forward compatibility, where possible, but you may find some changes in behaviour as bugs get fixed (etc.).

    I've just been building an AutoCAD 2010 application with the 2007 libraries, for instance. I just can't depend on newer API capabilities, of course.

    Regards,

    Kean

  8. Hello This is my code but i am getting unknown error, when Autocad is open by C# run mode.
    1. AutoCAD is open
    2. Netload
    3. when we have to select my dell. then shown " unknown error"

    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Text;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using System.Diagnostics;

    namespace ClassLibrary6
    {
    public class Class1
    {
    [Conditional("AC2007"),CommandMethod("Test")]
    public void test()
    {
    MessageBox.Show("Autocad Welcome");
    }

    }
    }

  9. Hello GIS,

    Have you set the conditional compilation symbol in the project's properties?

    Regards,

    Kean

  10. Hi Kean,
    Is there any possibilities to separate the acdbmgd.dll,acmgd.dll,accoremgd.dll files and write into one acad.dll by refecting all the class,method,properties in the 3 COMS?
    And then we only need to using 'acad.dll'
    so the program will adopt for all the acad versions
    haha
    Regards
    swaywood

  11. Hi swaywood,

    Anything is possible, but there is a good reason for them being separate. Some applications only want to use acdbmgd.dll, some want to use acdbmgd.dll and accoremgd.dll and some want to use all three.

    Building applications against these compartmentalised allows host applications for RealDWG and our AcCore implementation to load the first two categories of application.

    It may be slightly more cumbersome for people just developing for AutoCAD, but architecturally speaking it makes much more sense.

    Regards,

    Kean

Leave a Reply to Rene Cancel reply

Your email address will not be published. Required fields are marked *