I'm trying to convert decimal values inside a Python function to Feet and Inches (fractional), IE: 1.5 -> 1'-6"
For that I tried to use this method from the API:
UnitFormatUtils.TryParse Method (Units, UnitType, String, ValueParsingOptions, Double)
I could not find examples besides the API, where I ran into issues with ValueParsingOptions
Anybody can point out to an example, or a simpler method to convert from decimal to fractional feet+inches using the API?
I did write a Python function for converting from decimal to fractional, but I was looking for a lazy solution this time.
Thank you for your help.
Solved! Go to Solution.
I'm trying to convert decimal values inside a Python function to Feet and Inches (fractional), IE: 1.5 -> 1'-6"
For that I tried to use this method from the API:
UnitFormatUtils.TryParse Method (Units, UnitType, String, ValueParsingOptions, Double)
I could not find examples besides the API, where I ran into issues with ValueParsingOptions
Anybody can point out to an example, or a simpler method to convert from decimal to fractional feet+inches using the API?
I did write a Python function for converting from decimal to fractional, but I was looking for a lazy solution this time.
Thank you for your help.
Solved! Go to Solution.
Solved by jeremytammik. Go to Solution.
As a sample, you can look to the Units Sample in Revit SDK Samples.
But, I didn't find it very helpful. I searched about Revit Unit Conversion many times but there is no single solution as I see. In Building Code Jeremmy Tammik had explained this a few times. He said only imperial unit in Revit is length but I found much strange values in Revit API. I suggest you keep going to create your classes. But maybe, I couldn't figure it out. You can try the SDK sample. I hope you find it.
I hope this helps,
Recep.
As a sample, you can look to the Units Sample in Revit SDK Samples.
But, I didn't find it very helpful. I searched about Revit Unit Conversion many times but there is no single solution as I see. In Building Code Jeremmy Tammik had explained this a few times. He said only imperial unit in Revit is length but I found much strange values in Revit API. I suggest you keep going to create your classes. But maybe, I couldn't figure it out. You can try the SDK sample. I hope you find it.
I hope this helps,
Recep.
Dear Adrian,
Thank you for your query, and many thanks to Recep for his valuable help.
The Revit API provides quite a lot of quite powerful unit conversion utility functions.
However, I mostly prefer to roll my own anyway. Question of taste.
TryParse is not intended to be used for the task you describe:
https://apidocs.co/apps/revit/2019/3e6dcee8-6a58-efe5-67f8-3422af74545c.htm
It parses a formatted string into a number with units if possible.
However, you have a number to start with, not a string.
What you need is the Format method taking (Units, UnitType, Double, Boolean, Boolean):
https://apidocs.co/apps/revit/2019/4131eb4b-3d50-9f2b-f876-654b9b54975e.htm
It formats a number with units into a string, which is just what you are asking for.
Example code from the Revit SDK Samples FabricationPartLayout/CS/PartInfo.cs:
private string GetStringFromNumber(Document doc, double number, UnitType unitType) { return UnitFormatUtils.Format(doc.GetUnits(), unitType, number, true, false); }
To convert decimal value of 1.5 to feet and inches (fractional), e.g.: 1.5 -> 1'-6":
GetStringFromNumber( doc, 1.5, UnitType.UT_Length )
I hope this helps.
Best regards,
Jeremy
Dear Adrian,
Thank you for your query, and many thanks to Recep for his valuable help.
The Revit API provides quite a lot of quite powerful unit conversion utility functions.
However, I mostly prefer to roll my own anyway. Question of taste.
TryParse is not intended to be used for the task you describe:
https://apidocs.co/apps/revit/2019/3e6dcee8-6a58-efe5-67f8-3422af74545c.htm
It parses a formatted string into a number with units if possible.
However, you have a number to start with, not a string.
What you need is the Format method taking (Units, UnitType, Double, Boolean, Boolean):
https://apidocs.co/apps/revit/2019/4131eb4b-3d50-9f2b-f876-654b9b54975e.htm
It formats a number with units into a string, which is just what you are asking for.
Example code from the Revit SDK Samples FabricationPartLayout/CS/PartInfo.cs:
private string GetStringFromNumber(Document doc, double number, UnitType unitType) { return UnitFormatUtils.Format(doc.GetUnits(), unitType, number, true, false); }
To convert decimal value of 1.5 to feet and inches (fractional), e.g.: 1.5 -> 1'-6":
GetStringFromNumber( doc, 1.5, UnitType.UT_Length )
I hope this helps.
Best regards,
Jeremy
Jeremy -
thank you!
Yes, that is exactly what I was looking for. I refactored the Python code with the function to mimic the C# Class, and is executing as expected.
Thanks again.
Jeremy -
thank you!
Yes, that is exactly what I was looking for. I refactored the Python code with the function to mimic the C# Class, and is executing as expected.
Thanks again.
Here is the non-obsolete version that works in Revit 2021:
private string GetStringFromNumber(Document doc, double number, ForgeTypeId unitType)
{
return UnitFormatUtils.Format(doc.GetUnits(), unitType, number, false);
}
GetStringFromNumber(Doc, 1.5, SpecTypeId.Length);
Here is the non-obsolete version that works in Revit 2021:
private string GetStringFromNumber(Document doc, double number, ForgeTypeId unitType)
{
return UnitFormatUtils.Format(doc.GetUnits(), unitType, number, false);
}
GetStringFromNumber(Doc, 1.5, SpecTypeId.Length);
Can't find what you're looking for? Ask the community or share your knowledge.