Revit Project Units - Revit 22+

Hey Guys, I hope this is in the correct category. In Dynamo I am using a Python Script to first get the Project unit length.
For the life of me I can’t figure out how to set the units to either “Feet and Fractional inches” or “Millimeters”.
I am getting lost between all of the different blogs. I am fairly new to writhing/cannibalizing code.


import clr
clr.AddReference("RevitServices")
import RevitServices 
from RevitServices.Persistence import DocumentManager
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application


clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

results=[]

doc = DocumentManager.Instance.CurrentDBDocument

unitTypes = [SpecTypeId.Length]
for unitType in unitTypes:
	formatOption = doc.GetUnits().GetFormatOptions(unitType)
	symUnit = formatOption.GetUnitTypeId()
	symType = formatOption.GetSymbolTypeId()
	try:dispSym = LabelUtils.GetLabelForSymbol(symType)
	except:dispSym = None
	dispUnit = LabelUtils.GetLabelForUnit(symUnit)
results.append([dispUnit,dispSym])
    

OUT = results

err yeah, so i can’t tell you how to pull your project unit types using python but what i can say is play with this method and you might find your correct answer.

If you assume all measurements coming from revit are in ft & inch then convert all your number handling to float and / by 304.8

if you need to do calculation etc do them on the metric units as needed and then convert the final mm result to ft and inch by * by 304.8 before you push it back to the revit enviroment.

I had a recent example where i was taking numbers from an exel sheet, doing some calculations with python and then pushing them into parameters in revit, the dynamo graph output showed my number as i expected by when placed in the revit enviroment it was * by 304.8 giving me a whack number.

After re-reading your questions i don’t even think i am answering the correct question, but there you go. if it helps, it helps :slight_smile:

Thank you, Ill dive more into it. Ultimatly I need to change my units from Feet and Fractional inches to Millimeter so I can run a script. then when complete, I need to change it back. Trying to build it into my script instead of doing it manually.

You need to change your project units between ft&inch and mm and then swap them back after your script has finished running?

Im confused?

Correct. In my Alpha-Numeric script, I have to change the project units from Ft-in to mm then run the script. When I am done I have to change it back to ft-in.

I feel like your getting confused thinking Revit’s unit system will have an impact on your Python calculations. From my understanding, it won’t

The unit systems used in the API is indipendant of whatever unit are set within the project and thus is ALWAYS ft and inches.

For exmaple, used inside the code must be like

Data = IN[0] #Comes as ft & Inches
Calculation = float(Data[0])*304.8 #Covert to mm
### Do you calculations and push to Result Var
Result = float(Calculation[0])/304.8 #Covert to Imperial
Out = Result

in addition to the above, if you are actually trying to populate a number parameter inside revit, don’t reconver the number back to mm (Unless you want to visually check it inside dynamo) the number you push into revit should be imperial, and it will show in youer project as metric.

I’m not super experienced myself so to handle it to begin with i’d go about passing the thing you want striaght through a python node with like +1 or something silly and then see what the result is, then you can see what the resulting number comes out as.

I appreciate it, I’ll try this out in the AM. I probably am getting confused lol. All I know is, I have to change the project units to mm to run the script correctly.

Use the UnitUtils class Convert method like this

mm = UnitUtils.Convert(ffi, UnitTypeId.FeetFractionalInches, UnitTypeId.Millimeters)

and back

ffi = UnitUtils.Convert(mm, UnitTypeId.Millimeters, UnitTypeId.FeetFractionalInches)
1 Like

If my units are currently in ffi and want to switch to mm, where do I tie this in? Sorry, I am new to this.


unitTypes = [SpecTypeId.Length]
for unitType in unitTypes:
	formatOption = doc.GetUnits().GetFormatOptions(unitType)
	symUnit = formatOption.GetUnitTypeId()
	symType = formatOption.GetSymbolTypeId()
	try:dispSym = LabelUtils.GetLabelForSymbol(symType)
	except:dispSym = None
	dispUnit = LabelUtils.GetLabelForUnit(symUnit)
results.append([dispUnit])

Ok, here is what I got. It converts my units from whatever to FFI. Works perfect.

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')
clr.AddReference('ProtoGeometry')

from Autodesk.Revit.DB import FormatOptions, SpecTypeId, UnitTypeId
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from Autodesk.DesignScript.Geometry import *

# Get the current Revit document
doc = DocumentManager.Instance.CurrentDBDocument

# Start a transaction to modify the project units
TransactionManager.Instance.EnsureInTransaction(doc)

# Get the units from the document
unit = doc.GetUnits()

# Create the format options for Feet and fractional inches
format = FormatOptions(UnitTypeId.FeetFractionalInches)
format.Accuracy = 0.010416667

# Set the format options for length units
unit.SetFormatOptions(SpecTypeId.Length, format)

# Set the modified units back to the document
doc.SetUnits(unit)

# Commit the transaction
TransactionManager.Instance.TransactionTaskDone()

3 Likes