Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ValueParsingOptions - convert decimal values to Feet and Inches fractional

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
studio-a-int
3084 Views, 4 Replies

ValueParsingOptions - convert decimal values to Feet and Inches fractional

studio-a-int
Advocate
Advocate

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.

 

0 Likes

ValueParsingOptions - convert decimal values to Feet and Inches fractional

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.

 

4 REPLIES 4
Message 2 of 5
recepagah12
in reply to: studio-a-int

recepagah12
Advocate
Advocate

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.

0 Likes

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.

Message 3 of 5
jeremytammik
in reply to: studio-a-int

jeremytammik
Autodesk
Autodesk
Accepted solution

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 Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

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 Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 4 of 5
studio-a-int
in reply to: jeremytammik

studio-a-int
Advocate
Advocate

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.

0 Likes

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.

Message 5 of 5
stefanome
in reply to: jeremytammik

stefanome
Collaborator
Collaborator

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.

Post to forums  

Autodesk Design & Make Report