Unit conversion during "set parameter value" using python

Hi Guys,

I have been working on a small use case “Dynamic script to update parametric values”.
Based on user inputs the script will update the specific parameters of a family instance with new values from excel.

When I am trying to do it, it is updating values from excel but into different conversions. On the other hand dynamo node is updating properly.

My code:

import clr
import sys
sys.path.append(r'C:\Program Files\IronPython 2.7\Lib',)
libraries = IN[0]
sys.path.append(libraries)

import pandas as pd

clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
from Revit import *

clr.ImportExtensions(Revit.Elements)

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

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

from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory, BuiltInParameter, ParameterValueProvider, ElementId, ElementParameterFilter, FilterNumericEquals, FilterElementIdRule


doc = DocumentManager.Instance.CurrentDBDocument


#Inputs
elements = UnwrapElement(IN[1])
parameter = IN[2][0]
path = IN[3]
sheetName = IN[4][0]
column = IN[5][0]



#Excel
excelWorkbook = pd.read_excel(path, sheet_name=sheetName)
excelWorkbook = (excelWorkbook.drop_duplicates(subset=["properties.Text.IDK Positionsnummer"])).reset_index()

TransactionManager.Instance.EnsureInTransaction(doc)
#Revit family instances
for elem in elements:
    IDK_elementPosNum = elem.LookupParameter("IDK Positionsnummer").AsString()
    
    for i in range(len(excelWorkbook)):
        IDK_excelPosNum = excelWorkbook["properties.Text.IDK Positionsnummer"][i]
        
        if IDK_elementPosNum == IDK_excelPosNum:
            elem.LookupParameter(parameter).Set(excelWorkbook[column][i])
            break
        else:
            pass
TransactionManager.Instance.TransactionTaskDone()

OUT = 0

I tried using doc.GetUnits().GetFormatOptions(UnitType.UT_Length).DisplayUnits, and some more workaround procedure from forum. But getting errors.

How to make the unit conversions? How to make it dynamic but not like hard coding “UT_length, UT_MassDensity…”?

@SeanP

Whole script as PNG for reference:

@kuladeep561 ,

check out this topic… maybe it fits to your issue :wink:

Thanks @Draxl_Andreas , I will give a try.

But My main problem here is unit should convert according to user input parameter.
eg: if user selects volume, that should convert to kg/m3
if length, it should convert to meter which are Display units in the document or we can call it project units.

@kuladeep561 ,

here is a variation :wink:

1 Like