How do you make your graphs unit-indepenedent?

Hi all,

How do you make your script unit-independent?

Example 1: I have a script that make a finish floor, but it does not work if the project is set to metric.
Example 2: A script asks the user for a distance, but the prompt and the number depend on the document units… If I write “Enter thickness” without the correct units (“ft”, “inches”, “cm”, etc.) the results will vary with the current doc units settings.

What I had done was a custom node that inspects the current doc units, and returns a numeric factor that I need to multiply any and all distances so that the results are predictable (basically converts everything to inches). Now it does not work anymore because of python 3…

So, again, ho do you guys make sure the units settings of the current doc don’t screw up your graph?

Thank you

1 Like

@Giovanni_Succi ,

at my knowlege, all graphes that you write in python are in feet, so you need Unit Conversion at all.

KR

Andreas

1 Like

Generally most people don’t. The right way is the unit conversion utilities in the API or the forge unit types in later builds, but most firms I’ve worked at seem to just assume the unit based on their locality and templates.

Personally I use a library in Python to handle units using the above methods when units become relevant in pyrevit, but for Dynamo I just assume the units in most cases unless it’s a heavily used script.

Understood, thank you.

Could anyone then post the little python code that returns the current doc units?

I had it for python2 but now it does not work on 3…

Thank you

@Giovanni_Succi ,

f.e.

import clr
import sys
import System

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument


wall = UnwrapElement(IN[0])

bottomext = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsDouble(),UnitTypeId.Feet,UnitTypeId.Millimeters

topext = wall.get_Parameter(BuiltInParameter.WALL_TOP_OFFSET).AsDouble(),UnitTypeId.Feet,UnitTypeId.Millimeters  


OUT = bottomext, topext

Apologies, I meant this script, but adapted to IronPython3

thank you

import clr

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

import System

doc = DocumentManager.Instance.CurrentDBDocument

result = Document.GetUnits(doc).GetFormatOptions(UnitType.UT_Length).DisplayUnits

OUT = result
1 Like

Did you know, there is a NON-ZERO chance that a monkey could type the Constitution by punching a keyboard?

I did it.

I feel like crying.

import clr

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

import System

doc = DocumentManager.Instance.CurrentDBDocument

unit = doc.GetUnits().GetFormatOptions(SpecTypeId.Length).GetUnitTypeId()

result = LabelUtils.GetLabelForUnit(UnwrapElement(unit))

OUT = result

image