Autodesk.Revit.DB.UnitType.UT_Length

Hi!

Just a noob question :slight_smile:
I’m in trouble with those lines of code from a script written by learndynamo.com


getDocUnits = doc.GetUnits()
getDisplayUnits = getDocUnits.GetFormatOptions(Autodesk.Revit.DB.UnitType.UT_Length).DisplayUnits

Does anybody know how to solve this? I’m working on Revit 2023.

Thanks!

UnitType is no more. It was replaced with ForgeTypeId.
Use GetSpecTypeId()
https://www.revitapidocs.com/2021.1/64822a98-a253-3300-cdbb-17973687878f.htm

1 Like

Hi @dgeorge
here an example

import sys
import clr

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

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

#Get Important vars
doc = DocumentManager.Instance.CurrentDBDocument

UIunit = doc.GetUnits().GetFormatOptions(SpecTypeId.Length).GetUnitTypeId()
nameUnit = LabelUtils.GetLabelForUnit(UIunit)

OUT = nameUnit, UIunit.TypeId
2 Likes

Thanks so much! The script just ran fine over those lines :slight_smile:
I have another error, at line 34… I probably missed something:


import sys
import clr

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

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

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

#The inputs to this node will be stored as a list in the IN variables.
curves = IN[0]
views = UnwrapElement(IN[1])
heights = IN[2]
output = []
doc = DocumentManager.Instance.CurrentDBDocument

getDocUnits = doc.GetUnits().GetFormatOptions(SpecTypeId.Length).GetUnitTypeId()
getDisplayUnits = LabelUtils.GetLabelForUnit(getDocUnits)

for curveList, viewList, height in zip(curves, views, heights):

	**height = Autodesk.Revit.DB.UnitUtils.ConvertFromInternalUnits(height, getDisplayUnits )**
	
	tempList = []
	for curve, view in zip(curveList,viewList):
		
		viewCropManager = view.GetCropRegionShapeManager()		
		cLoop = viewCropManager.GetCropShape()[0]
		cLoopCurves = [x.ToProtoType() for x in cLoop]
		
		PointA = cLoopCurves[3].EndPoint
		PointD = cLoopCurves[3].StartPoint
		curveMP = Curve.PointAtParameter(curve, 0.5)

		if Geometry.DistanceTo(curveMP,curve.EndPoint) < Geometry.DistanceTo(curveMP, PointA):
			PointA = curve.EndPoint		
			
		if Geometry.DistanceTo(curveMP,curve.StartPoint) < Geometry.DistanceTo(curveMP, PointD):
			PointD = curve.StartPoint			
		
		PointB = Point.ByCoordinates(PointA.X, PointA.Y, PointA.Z + height)
		PointC = Point.ByCoordinates(PointD.X, PointD.Y, PointD.Z + height)
			
		LineA = Line.ByStartPointEndPoint(PointA,PointB).ToRevitType()
		LineB = Line.ByStartPointEndPoint(PointB,PointC).ToRevitType()
		LineC = Line.ByStartPointEndPoint(PointC,PointD).ToRevitType()
		LineD = Line.ByStartPointEndPoint(PointD,PointA).ToRevitType()
		
		curveLoop = Autodesk.Revit.DB.CurveLoop.Create([LineA, LineB, LineC, LineD])

		TransactionManager.Instance.EnsureInTransaction(doc)		
		viewCropManager.SetCropShape(curveLoop)
		TransactionManager.Instance.TransactionTaskDone()
		
		tempList.append(view.ToDSType(False))

	output.append(tempList)
#Assign your output to the OUT variable.
OUT[0] = output
OUT[1] = nameUnit
OUT [2] = UIunit.TypeId

try to replace

height = Autodesk.Revit.DB.UnitUtils.ConvertFromInternalUnits(height, getDisplayUnits )

by

height = Autodesk.Revit.DB.UnitUtils.ConvertFromInternalUnits(height, getDocUnits )

and

OUT[0] = output
OUT[1] = nameUnit
OUT [2] = UIunit.TypeId

by

OUT = output, nameUnit, UIunit.TypeId