Quantcast
Channel: RevitNetAddinWizard & NavisworksNetAddinWizard
Viewing all articles
Browse latest Browse all 872

.NET Extension Property/Method and Revit .NET FormatOptions Properties

$
0
0

The title may sound a bit confusing since .NET does not really support Extension Property yet. Even if it does, why does it have something to do with the Revit .NET FormatOptions properties?

Here is the reason. Revit .NET API changed method and property names of some classes from time to time, from version to version. The FormatOptions class was just one among those. In version 2012 and 2013, it had Rounding and Units properties, but since after, they were renamed to Accuracy and DisplayUnits. Not sure why the changes had to be made there, as both old names and new names all sound very similar and besides names nothing else changed, like types.

It's not a big deal if only one Revit version is supposed to be maintained and supported by an addin. However, it is not so in the real world. A lot of conditional compiler statements like #if/#else/#endif have to be used to maintain the same code stream. It not only causes the looking of the code very bad but also makes the maintenance and improvement work very hard.

We finally got a workaround, defining Extension Property if possible, otherwise using Extension Methods as follows:

public static double GetAccuracy(this FormatOptions opt)
{
#if REVIT2012 || REVIT2013
return opt.Rounding;
#else
return opt.Accuracy;
#endif
}

public static DisplayUnitType GetDisplayUnits(this FormatOptions opt)
{
#if REVIT2012 || REVIT2013
return opt.Units;
#else
return opt.DisplayUnits;
#endif
}

Simply put them into a library that every Revit project can access to and use them instead of all those Rounding/Accuracy, Units/DisplayUnits, and similar stuffs. In case in the future Revit .NET API changes their names again such as in version 2018, the only place needs to be updated in your project is the single method/property. Enjoy! 

Revit .NET Addin Wizard 2015

Revit Shared Parameter File Organizer (Editor and Viewer)

Revit Parameter Organizer

Revit Family Organizer

More


Viewing all articles
Browse latest Browse all 872

Trending Articles