Setting up a Visual Studio project for multiple Civil 3D versions

In the past, I would write my code for the version of Autodesk software I was using. Then when I updated my software, I would save my code project to a new folder, update the references and move on. In effect, the previous version of the code would go stagnant and all new development was only focused on the current version. This is okay, if you’re in a small environment with only one active version of software running, but once you grow into a bigger environment or you want to support multiple versions for whatever the reason (App Store/private sales/future proofing), then you’ll need to migrate (or start) your projects with that in mind.

This post is going to cover how to setup a brand new VS project to target Civil 3D versions 2019 thru 2021. When the project is done, it can easily be expanded for future versions with only a few steps. I will create this project in a local folder, but you could set it up in GitHub or another cloud service. That’s not going to be in the scope of this outline, and you should be able to find that elsewhere online if you would like to do that.

To start out, open up Visual Studio. I’m using version 2019 but you should be able to use as far back as 2015. Select the Create a new project option at the bottom right of the startup dialog.

Create a new project by selecting the appropriate button on the startup dialog

On the Create new project dialog, select the code base you prefer to use. I am using C#, but you can use Visual Basic too. In the template list on the right side of the window, select Shared Project. You can use the search bar at the top or scroll down until you find it in the list.

Select Shared Project for the code base you prefer

On the Configure new project page, I would recommend naming the project “Core”, as this is where the majority of your code will reside. The location is a local folder in my example but, again, this can be anywhere you’d like to start from. The solution name will be the overall code project name. When VS creates your project folders, it will name the root folder to match the solution and creates a sub-folder for the Core project.

Configure the new shared project
The resulting solution and project as shown in the VS Solution Explorer
The resulting folder structure created by VS

You will notice that the Core project doesn’t have any of the standard data that a new project usually has, such as Properties, References or config files. This is because the Shared Project is where you will write only your code that is then referenced by each version specific project in the overall solution.

Shared Projects let you write common code that is referenced by a number of different application projects. The code is compiled as part of each referencing project and can include compiler directives to help incorporate platform-specific functionality into the shared code base.

Microsoft

Now that we have the Core shared project created, let’s create the first version-specific project. From either the File menu or right-clicking the solution name in the solution explorer, select the New Project option. In the Create New Project dialog, select the Class Library (.NET Framework) option for your code language and press Next.

Create a new Class Library for your specific software version

In the Configure new project dialog, name the project. I use something to make it clear what software version the project should be designed for. Change the Solution option to Add to solution and set your .NET Framework. I left this at the default, which is the most current installed. Press the Next button.

Configure the new project as part of the existing solution
The VS Solution explorer after the project is created
The resulting folder structure

Your solution should now have two projects as shown in the images above. The most recent project, C3D2019, will be set as the current project (note the bold text for the project name), which means it will be the project run when you start debugging. With the new project created, you need to make some edits to the Properties before moving forward. Double click the Properties item under the project name in the Solution explorer. In the Properties tab, select the Build option and add a Conditional compilation symbol. I chose C3D2019 to correlate the symbol more easily in the code. This symbol will be used in your Core code to tell the compiler to only compile a section of code when compiling the C3D 2019 project. We’ll see how this works more clearly once we start writing code later on.

Add a conditional compilation symbol

Once you’ve added your conditional symbol, choose the Debug option on the left and set the Start Action to Start external program. Make sure you reference the version of software your project is intended for. Set your command line arguments. In the screenshot below, I used the default arguments provided for loading Civil 3D out of the box. You can change this to your specific settings if you are writing for a company environment. You want to make sure your testing in the environment that you intend your app to work in. Save and close the properties tab.

Set your debug options

Now you need to add your references. Right-click on References in the Solution explorer and select Add Reference. Under the Shared Projects, select your Core project. Under the Browse section, select the AcDbMgd.dll, AcMgd.dll, and AcCoreMgd.dll files for AutoCAD and the AecBaseMgd.dll and AeccDbMgd.dll files for Civil 3D. Make sure you select the ones for the version your project is targeting. Click OK and change the Copy Local setting for the .dll files to False. You can do this for all five at once by selecting them all and in the Properties panel (usually below the Solution Explorer), change the Copy Local value.

Reference your Shared Project
The minimum references needed for Civil 3D

At this point, I typically run the project to test that the settings are correct. If you press F5, or click the Start button, it should build the C3D2019 project and launch Civil 3D 2019. Once Civil 3D loads, you can use the Netload command to load in the newly created C3D2019.dll in your debug folder. At this point, there are no commands to run, so you can shut down the program. Either close Civil 3D or stop debugging by pressing the stop button or Shift+F5.

To add another Civil 3D version, follow the same steps of adding a new Class Library for each version you plan to target.

The Solution Explorer once all projects are created

From this point onward, you can start writing your code. When you create your new class files, you will do so in the Core shared project. This way, when you run or compile any of the other projects, it will automatically have that core data included. There will be times that you’ll have version specific lines of code. If you are working with some of the COM interfaces provided by Autodesk, many of them are explicitly versioned. For example, the Sheet Set Manager components. Each software release results in a version of the Interop library being iterated. In the Sheet Set example, the reference that is used for the 2019 release is the AcSmComponents22Lib. The 22 is incremented up in each version, so that 2020 is AcSmComponents23Lib and 2021 is AcSmComponents24Lib.

You can see how this can be a problem when trying to reuse the same code base but target specific versions. When writing your code, you can’t just add a ‘using’ statement for all the different versions, the compiler would throw a fit! You need a way to tell the compiler which one to use. That’s where the Conditional compiler statement comes in to play. You can use the preprocessor if statement (#if) to wrap your using statements and isolate the code to your specific version. Using the same Sheet Set example, here’s a look at what that code would look like:

#if C3D2019
using ACSMCOMPONENTS22Lib;
#elif C3D2020
using ACSMCOMPONENTS23Lib;
#elif C3D2021
using ACSMCOMPONENTS24Lib;
#endif

This would be placed with the using statements at the top of the class file, which is part of your Core shared project. To describe what is happening, when the compiler is readying the file, it will examine your Conditional compilation statement and if it matches the #if statement, it will compile the contents. If you need to update your code for another release of the software, all you’ll need to do is add in another #elif statement to wrap the new AcSmComponent library for that release.

You don’t have to use this compilation statement at all, but it is a great way to isolate version specific code. Most of the code you can write for an Autodesk product should be version-agnostic. There will be some minor changes to the APIs over time and you’ll have to make accommodations for that in those instances, but for the majority of your code, it’s just a ‘nice to have’ item that helps future proof your code.

With this project setup for multiple CAD versions, you can bring any existing code into your Core or start on your next big project. Hopefully this will help you manage your projects and reduce the copies you make for each version of software.

Published by Adam Reilly

An Autodesk Civil 3D Certified Professional with almost 15 years of experience in Civil 3D and over 20 years in the civil engineering industry. I love working with, learning about and teaching Civil 3D.

4 thoughts on “Setting up a Visual Studio project for multiple Civil 3D versions

    1. Thanks for asking!
      When I wrote this, I hadn’t done much WPF/XAML. But, in the past year, I’ve had some projects where I was forced to use it then deliberately chose to use it. What I’ve found so far, it needs to be it’s own project that can then be called by one of the versioned projects. It would be great if it would work in the shared project, but it doesn’t. I used a XAML application project and built my latest app data in that. I then added that project as a reference into the main project and called the controls or window as needed.
      WPF/XAML just adds an extra layer of complexity to this and I need to write up my work flow, once I finish a proejct fully to get a good handle on it.

      Like

Leave a comment

Design a site like this with WordPress.com
Get started