Hello,
I'm new to Revit API and facing an issue while trying to convert values from internal units to meters or millimeters in the Revit API. Specifically, when I attempt to modify a parameter by entering a value like 15, I expect it to represent 15 meters. However, the code seems to convert this value to 15 feet, which is equivalent to approximately 4.572 meters.
I would greatly appreciate any guidance or suggestions on how to correctly convert values from internal units (feet) to meters or millimeters in the Revit API with a class for units. I want to ensure that my parameter modifications are accurately reflected in the desired unit system.
Thank you in advance for your assistance!
attaching a picture explaining what is going on .
Solved! Go to Solution.
Solved by jeremy_tammik. Go to Solution.
Hi ,
You need to use the UnitUtils class https://www.revitapidocs.com/2023/128dd879-fea8-5d7b-1eb2-d64f87753990.htm .
This explains the change that happened in r2022 well https://archi-lab.net/handling-the-revit-2022-unit-changes/
This also - https://thebuildingcoder.typepad.com/blog/2021/04/pdf-export-forgetypeid-and-multi-target-add-in.htm...
Best , Jonathan
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:
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;
}
Can't find what you're looking for? Ask the community or share your knowledge.