So this method does not work for Revit 2022 because the Unit Methods have changed.
I changed the code an it seems to work heres a comparison of the code lines:
#Revit 2022
unitType = SpecTypeId.Length
currentDisplayUnits = doc.GetUnits().GetFormatOptions(unitType).GetUnitTypeId()
return UnitUtils.Convert(float(num), UnitTypeId.Millimeters, currentDisplayUnits)
#Revit 2021
unitType = UnitType.UT_Length
currentDisplayUnits = doc.GetUnits().GetFormatOptions(unitType).DisplayUnits
return UnitUtils.Convert(float(num), DisplayUnitType.DUT_MILLIMETERS, currentDisplayUnits)
And here is the full code for Revit 2022:
import clr
#Import the Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
#Import the Revit Services
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
#Take a number an converts it from mm to Project Units
def ConvertUnits(num):
unitType = SpecTypeId.Length
currentDisplayUnits = doc.GetUnits().GetFormatOptions(unitType).GetUnitTypeId()
return UnitUtils.Convert(float(num), UnitTypeId.Millimeters, currentDisplayUnits)
OUT = ConvertUnits(IN[0])