Many thanks to Jonathan for the helpful answer.
Yup, you basically have two choices: DIY or use the official Revit API support.
Personally, I am a fan of DIY and retaining total control (and responsibility). Not always the most efficient way to go, though.
Personally, again, I mostly just have to deal with very simple units, such as the length issue you describe. You can look at The Building Coder samples to see how I handle it there for various situations:
https://github.com/jeremytammik/the_building_coder_samples
Specifically, the Util class includes a region for Unit handling:
https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/Util.cs#L1206-L...
The main part of interest to you is at the beginning of that region:
private const double _inchToMm = 25.4;
private const double _footToMm = 12 * _inchToMm;
private const double _footToMeter = _footToMm * 0.001;
private const double _sqfToSqm = _footToMeter * _footToMeter;
private const double _cubicFootToCubicMeter = _footToMeter * _sqfToSqm;
/// <summary>
/// Convert a given length in feet to millimetres.
/// </summary>
public static double FootToMm(double length)
{
return length * _footToMm;
}
/// <summary>
/// Convert a given length in feet to millimetres,
/// rounded to the closest millimetre.
/// </summary>
public static int FootToMmInt(double length)
{
//return (int) ( _feet_to_mm * d + 0.5 );
return (int) Math.Round(_footToMm * length,
MidpointRounding.AwayFromZero);
}
/// <summary>
/// Convert a given length in feet to metres.
/// </summary>
public static double FootToMetre(double length)
{
return length * _footToMeter;
}
/// <summary>
/// Convert a given length in millimetres to feet.
/// </summary>
public static double MmToFoot(double length)
{
return length / _footToMm;
}
/// <summary>
/// Convert a given point or vector from millimetres to feet.
/// </summary>
public static XYZ MmToFoot(XYZ v)
{
return v.Divide(_footToMm);
}
/// <summary>
/// Convert a given volume in feet to cubic meters.
/// </summary>
public static double CubicFootToCubicMeter(double volume)
{
return volume * _cubicFootToCubicMeter;
}