Convert to project units

Hello,

I want to move a view on a sheet by a specific mm value.
I want this to work no matter what the project units are.

I got it working by getting the project units and searching for the unit name, then filter different values by bool mask.

I just wanted to know if there is any better way to do this!? Happy about any thoughts!

Hello,

just wanted to ask again if someone has a better solution to my problem.

I want to move a view by 20mm, it should work whatever the project units are.

Maybe there is a better and faster solution possible with python?

Kind regards

Yes, there is a better way available in the Revit API. There is a class called UnitUtils, which takes care of converting some units into another.

https://www.revitapidocs.com/2020.1/cad8b885-8eda-5556-4813-924b7a7e669b.htm

Your current Unit System should be the Millimeters and the desired should be the project units

2 Likes

Thanks @Joelmick :slight_smile:

I did a little search for unitutils, i found a few nodes and tried to put something together, but it does not do what i want it to and i don´t even know what its doing exactly :smiley:

What is this result i get with UnitUtils.Format?
I think it is not what i need because as you mentioned i have to convert to project units.

Edit:

And thats my try at converting to project units:

But don´t know how to put this all together and get correct results.

I started to put a python script together, but again, i don´t really know what i´m even doing.

image

getDocUnits = doc.GetUnits()
getDisplayUnits = getDocUnits.GetFormatOptions(UnitType.UT_Length).DisplayUnits
unitConversion = UnitUtils.ConvertFromInternalUnits(1, getDisplayUnits )

OUT = unitConversion

Hi @gerhard.p,

The Python solution you are now using is to convert back and forth between Revit’s internal units (Feet, dont ask me why feet)

You should use the Convert methode:

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 = UnitType.UT_Length
	currentDisplayUnits = doc.GetUnits().GetFormatOptions(unitType).DisplayUnits
	return UnitUtils.Convert(float(num), DisplayUnitType.DUT_MILLIMETERS, currentDisplayUnits)


OUT = ConvertUnits(IN[0])
1 Like

@Joelmick saved the day :smiley:

Thank you so much it works great, my script is now cleaned up, nice :slight_smile:

So this method does not work for Revit 2022 because the Unit Methods have changed.
I changed the code an it seems to work :slight_smile: 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])
3 Likes