Advertisement

Friday, August 25, 2017

Using the API to Create a Dictionary that Maps Revit Display Units to Unit Symbols

From the The Building Coder website:

Unit Abbreviations

Håvard Dagsvik of Symetri implemented code to automatically generate a dictionary mapping the Revit DisplayUnitType to corresponding UnitSymbolType display strings.

[...]


In Håvard's own words:

I've been looking at extracting a complete list of unit abbreviations for each ParameterType and their DisplayUnitTypes. In total, roughly 210, I think.

I looked at what was around on your blog and elsewhere. Your manually generated list of unit abbreviations could be expanded to include all UnitSymbolTypes, not just the first 22. Anyhow, I decided to try something different.

What I do is this:
  • Create a shared parameter file containing every parameter type.
  • Attach these parameters to the ProjectInformation element.
  • For every double parameter, set a value of 1.
  • Then, for each parameter value, read the value string and isolate the unit abbreviation it contains.

In the end, the code produces a C# source file including the required using namespace statements and the actual Dictionary. That file can be imported directly back into Visual Studio to be used as an API. Here are both the complete Visual Studio Revit add-in project RVT_UnitTypes.zip and the C# module UnitAbbrevHandler.cs it generates.

It may be somewhat overkill; I'm not sure yet :-) If the list of abbrevs is not complete – though I think it is – it should be easy to tweak it more to get there.

This is related to an Excel export/import function. I export Revit parameters values to Excel, edit them there, and import the changed values back to the Revit elements.

In Excel, it's nice to have the unit abbreviations visible so you know what you are editing, e.g., meters or millimetres, Joules, gram Celsius or BTU for energy and specific heat, etc. That provides the use case for this dictionary. On export, we get the abbreviation from this predefined but auto-generated dictionary.

Originally, I thought it included all possible abbreviations, but it doesn't. The code could easily do that, but I only need one UnitSymbolType per DisplayUnitType. After all, lb/m3 and lbm/m3 represents the same thing.

The only situation I see in which this would matter is on parameter type Currency. The UnitSymbolType $ is obviously not the same as the UnitSymbolType £. But Revit is not a currency converter. In our case we only care about valid DUT's that can be fed into the UnitUtils.ConvertToInternalUnits function.

Tweaking this code, I guess you could create any other relations you might need, e.g., a complete Dictionary mapping "unit abbreviation to DisplayUnitType.

I hope this is useful to others as well.



There's more information available on the The Building Coder website.

No comments: