Compiling and running .NET4.0 Revit commands

by Guy Robinson 31. May 2010 14:04

For those of you excited by .NET4.0’s new features and wanting to test Revit commands compiled with .NET4.0, you’re in luck. Before I show you how, a few caveats:

  • I’ve tested this for a few days. While I haven’t run into any unexplained issues , there are areas of new functionality in .NET4 that can cause a bit of a head scratch.
  • Of course this is unsupported so you can try submitting a bug but don’t be surprised if Autodesk just laugh at you. In other words, “testing and inhouse development only please”.

How to enable .NET4.0

Assuming you have .NET4.0 installed, to enable .NET4.0 support change the runtime lines in the Revit.exe.config to the following. Note if this isn’t the .NET2 version installed then you need to modify the .NET2 version as required.

<startup useLegacyV2RuntimeActivationPolicy="true" >
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727" />
</startup>

The critical line is the useLegacyV2RuntimeActivationPolicy=true. To confirm this is working, create a .NET4 assembly and add the following command.

[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
[DisplayName("Test NET40")]
[Description("Test NET4.0 test command")]
public class Command:IExternalCommand
{
    #region Implementation of IExternalCommand
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        var uiApplication = commandData.Application;
        var application = uiApplication.Application;
        var uiDocument = uiApplication.ActiveUIDocument;
        var document = uiDocument.Document;
        var transaction = new Transaction(document, "test .NET4.0");
        try
        {
            transaction.Start();
            var td = new TaskDialog("Test Version4") { MainContent = "Running CLR " + Environment.Version.ToString() };
            td.Show();
            transaction.Commit();
            return Result.Succeeded;
        }
        catch (Exception)
        {
            if (transaction.HasStarted()) transaction.RollBack();
            return Result.Failed;
        }
    }
 
    #endregion
}

Enjoy!!! Looking forward to seeing some Revit->Windows Phone7 mashups!!

Just to clarify, although you can test .NET4 commands Revit is unusable with .NET4 enabled. It causes Revit to crash as soon as you select a standard command. 

Comments are closed

About the Author

A .NET software Developer providing custom applications and commands for architecture firms exclusively working with Autodesk Revit and integration with any associated applications. All from a little place north of Whitianga, New Zealand.

Page List

Disclaimer

I'm self employed so the opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway☺

© Copyright2008

Creative Commons License
Blog content is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

With the following exception. All code snippets, application and libraries are licensed under a a Apache License Version 2.0

Autodesk Revit®

Autodesk: Revit is a product that is wholly owned by Autodesk. Any reference to Revit,Revit API, Revit Architecture, Revit MEP or Revit Structure on this site is made acknowledging this ownership. Refer to Autodesk's own web site and product pages for specific trademark and copyright information. Autodesk represents a great many products and every attempt will be made to respect their ownership whenever one of these other products is mentioned on this site.