Units conversion issue and Set parameter value

Hi,

I’m facing an issue working inside a family (Planting) and trying to set the value of a parameter (Height) with data|shapes [Set Active Family Parameter By Name], after retrieving the max height value from an imported CAD geometry. Length unit is set to millimeters (relevant below).

#All the credit for this node goes to Dimitar Venkov @5devene
#Thanks for all the usefull stuff you share Dimitar!

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

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

doc = DocumentManager.Instance.CurrentDBDocument
UIunit = Document.GetUnits(doc).GetFormatOptions(UnitType.UT_Length).DisplayUnits

def convunit(x):
	return UnitUtils.ConvertToInternalUnits(x,UIunit)

param_name = IN[0]
param_value = IN[1]

param_list = doc.FamilyManager.GetParameters()
names = [p.Definition.Name for p in param_list]
par1 = None
if param_name in names:
	par1 = param_list[names.index(param_name)]
if par1 != None:
	TransactionManager.Instance.EnsureInTransaction(doc)
	try :
		try:
			doc.FamilyManager.Set(par1,convunit(param_value))
			OUT = 'Success'
		except:
			doc.FamilyManager.Set(par1,param_value)		
			OUT = 'Success'
	except:
		OUT = 'Check parameter type'
		
	TransactionManager.Instance.TransactionTaskDone()
else :
	OUT = 'No parameter found by that name'

The issue is as follows :
With the default node above (supposedly working with internal units), I only ever get a null result, and the Hauteur parameter (Height, pardon my french) remains at its default value

I altered the script to get rid of anything related to unit conversion and finally got it to work, BUT the value is now wrong. It seems as if the node is reading the input value as inches and converting it all by itself to millimeters.

My Python proficiency is laughable at best (painfully working on it), and while I read some relevant topics around here dealing with related issues, I’m still a bit lost regarding the next steps, so I could use some hindsight regarding either issue.

Thanks in advance!

Here are a couple of Python definitions to review.

import clr,sys

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

# Revit Services for Transactions
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Document Essentials
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

def convert_to_internal_unit_length(unitL):
    # Convert a value from an internal unit for length
    revit_version=int(app.VersionNumber)
    if revit_version < 2021:
        uid = DisplayUnitType.DUT_MILLIMETERS
    else:
        uid = UnitTypeId.Millimeters # type: ignore pylance error
    return UnitUtils.ConvertToInternalUnits(unitL, uid)

def convert_from_internal_unit_length(unitL):
    # Convert a value from an internal unit for length
    revit_version=int(app.VersionNumber)
    if revit_version < 2021:
        uid = DisplayUnitType.DUT_MILLIMETERS
    else:
        uid = UnitTypeId.Millimeters # type: ignore pylance error
    return UnitUtils.ConvertFromInternalUnits(unitL, uid)
2 Likes

After some trial and error on my end, it eventualy worked using convert_to_internal_unit_length ! Thanks @Ewan_Opie !

Marking it as solved, but a last question if you don’t mind : how come my data|shapes node still used the deprecated DisplayUnits ? Something wrong on my side while installing/downloading, or do you notice the same issue ?

Anyhow, thanks again !
SetHeight_topZ.dyn (24.5 KB)

Glad to be of help :slightly_smiling_face:

With respect to the node error from the package, it may just be that the package version you are using pre-dates the Revit API changes (such as SpecType Ids) that are relied upon for unit conversion.

Updating the package should fix the issue, if @Mostafa_El_Ayoubi has had a chance to do updates, everyones busy :sweat_smile:

2 Likes

Thanks for pointing this out ! Will update this.

3 Likes

Hi, I have a similar issue, “Set Active Family Parameter By Name” node doesn’t work in Revit 2023. It throws a null. It works fine in 2021. I’ve tried with the latest release too, It’s the same.