Hi there,
I know this is a super old post but I'm wondering if there's by any chance you could help me out.
So I built upon your script to i). if the material is new, write density & thermal conductivity ii). if it's an existing one, modify these two parameters.
I could get it run most of the time, but sometimes it will send the following error messages:
a. Internal error at line 37 (see bold and red highlight below)
b : 'NoneType' object has no attribute 'LookupParameter' at line 46 (or 47 sometimes)
As I'm in Dynamo using IronPython, the codes are slightly different but the overall structure is pretty much the same.
Any help is much appreciated.
Regards,
Jamie
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);
thAsset.Density = D_Convert(d);
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()