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
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()