Unstable Python Scripts to Change/Create Thermal Assets

Hi all,

Building upon the brilliant discussion in the following threads, I created a script that changes/creates material thermal conductivity & density.
https://forum.dynamobim.com/t/material-creation-override-with-python/14597/5
https://forums.autodesk.com/t5/revit-api-forum/create-new-material-and-add-new-physical-and-thermal-assets-with/m-p/7318588#M24676
The script first identifies if the material is in the library already, and changes
the existing thermal conductivity & density based on my input or create new material and write these two properties into its thermal asset. What I find very odd is that this script works properly most of the time but sometimes it sends me the error message “internal error at line 44”. Just wondering if anyone knows what’s happening and would offer me some workarounds.


Any help is much appreciated.

Regards,
Jamie

Huge apology for uploading the python script in a text file. I didn’t work out how to embed the script in the question :face_with_raised_eyebrow:
DynamoPython.txt (2.3 KB)

[Edit Moderation : add code]

import clr
clr.AddReference("RevitAPI")
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)

#Define functions to convert units
def T_Convert(l):
	th1=[]
	unit1= DisplayUnitType.DUT_WATTS_PER_METER_KELVIN
	th1.append(UnitUtils.ConvertToInternalUnits(l,unit1))
	return th1[0]
def D_Convert(l):
	th1=[]
	unit2 = DisplayUnitType.DUT_KILOGRAMS_PER_CUBIC_METER
	th1.append(UnitUtils.ConvertToInternalUnits(l,unit2))
	return th1[0]
	
for n,th,d in zip(IN[0],IN[1],IN[2]):
	if Material.IsNameUnique(doc,n):
#Create new material 
		new = Material.Create(doc, n);
		material = doc.GetElement(new);
		thAsset = ThermalAsset(n, ThermalMaterialType.Solid);
		thAsset.Name = n;
		thAsset.Behavior = StructuralBehavior.Isotropic;
		thAsset.ThermalConductivity = T_Convert(th);
		SpecificHeatOfVaporization = 0.0;
		thAsset.Porosity =0.0;
		thAsset.Density = D_Convert(d);
		thAsset.Emissivity = 0.0;
		thAsset.Reflectivity = 0.0;
		thAsset.Permeability = 0.0;
		thAsset.ElectricalResistivity = 0.0;
		thAsset.Compressibility = 0.0;
		pse = PropertySetElement.Create(doc, thAsset);
		material.SetMaterialAspectByPropertySet(MaterialAspect.Thermal, pse.Id);
#Update existing material 
	else:
		TransactionManager.Instance.EnsureInTransaction(doc)
		namePar = ParameterValueProvider(ElementId(BuiltInParameter.MATERIAL_NAME))
		fRule = FilterStringRule(namePar,FilterStringEquals(),n, True)
		filter = ElementParameterFilter(fRule)
		exist_mat = FilteredElementCollector(doc).OfClass(Material).WherePasses(filter).ToElements()
		for em in exist_mat:
			TH=doc.GetElement(em.ThermalAssetId).LookupParameter("Thermal Conductivity")
			DEN=doc.GetElement(em.ThermalAssetId).LookupParameter("Density")
			new_th=TH.Set(T_Convert(th))
			new_den=TH.Set(D_Convert(d))
		
			TransactionManager.Instance.TransactionTaskDone()

Hi @jaaamie,

Your dynamo script worked well with me, Im using Revit 2020.2.3 and Dynamo 2.3.
Try to update your Revit and Dynamo. By the way, what version of Revit and Dynamo are you using?

-biboy

Hi Biboy,

Yes, I can get it right most of the times, but sometimes I get the errors.
I was using 2020.2.1 and then updated my revit and dynamo. The issue still persists. Any idea?

Regards
Jamie

Hi @jaaamie,

Sorry i cannot help you, tried to delete the material 10 times and run your script, I did not encounter any problems.

-Biboy

Right… Thank you a lot. Might still have something to do with Revit on my machine

Some further updates…
After some more researches, I now suspect if this is because of the interaction with Revit when using the SetMaterialAspectByPropertySet Method. I know there were some wrinkles in previous versions
see the link below:
https://forums.autodesk.com/t5/revit-api-forum/how-to-edit-a-physical-or-thermal-asset/td-p/8220699
I’m not an expert and I’m hoping someone could shed some light on this.

Regards,
Jamie