Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

How can i set a Density to a Material using C# code ?

Anonymous

How can i set a Density to a Material using C# code ?

Anonymous
Not applicable

Hello ,

 

How can i set a value for Density to a Material usingC# code ?

 

 

0 Likes
Reply
1,999 Views
6 Replies
Replies (6)

Anonymous
Not applicable

Hi @Anonymous

 

Have you tried :

 

Parameter elementParameter = element.get_Parameter(BuiltInParameter.PHY_MATERIAL_PARAM_STRUCTURAL_DENSITY);

elementParameter.set("What you want");

 

?

 

I don't know the type of the element you want to set the density but I think this is a clue

 

Regards,

 

GM

0 Likes

MarryTookMyCoffe
Collaborator
Collaborator

I thing I know what you what, it goes like that

Material material = .....<- here goes your material
PropertySetElement pse;
ThermalAsset thAsset;
thAsset = new ThermalAsset("your name", ThermalMaterialType.Solid);

thAsset.Name = "yourname";
thAsset.Behavior = StructuralBehavior.Isotropic;
thAsset.ThermalConductivity = 0.00;
thAsset.SpecificHeatOfVaporization = 0.0;
thAsset.Porosity = 0.0;
thAsset.Density = 0.0;
thAsset.Emissivity = 0.0;
thAsset.Reflectivity = 0.0;
thAsset.Permeability = 0.0;
thAsset.ElectricalResistivity = 0.0;
thAsset.Compressibility = 0.0;
//in put thermal asset to a PropertySet pse = PropertySetElement.Create(doc, thAsset); //now singh a propertyset to a material material.SetMaterialAspectByPropertySet(MaterialAspect.Thermal, pse.Id);

 

set and get ThermaAsset and PropertySetElement is not perfect in revit and will give you a lots of trouble 😕

 

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug

Anonymous
Not applicable

Thank you i used this code 2 days ago and it works but now i want to check if this asset already exist or not do u know how can todo it ?

0 Likes

MarryTookMyCoffe
Collaborator
Collaborator

there is a way, but it is a most stupid way

                        pse = asset;
                        try
                        {
                            thAsset = pse.GetThermalAsset();
                        }
                        catch
                        {
                            thAsset = new ThermalAsset("pusty", ThermalMaterialType.Solid);
                            pse.SetThermalAsset(thAsset);
                        }

so in basic you need to catch a exception, because no one thought, it  would be a good idea to make .isThermalAssteExist().

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug

Anonymous
Not applicable

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

 

 

0 Likes

MarryTookMyCoffe
Collaborator
Collaborator

Don't know if you still have problem with it(I was on vacation), here are my thoughts .
It seems that in dynamo errors are little different than from API using c#.
a) it look like your Asset get some null property or with invalid value, maybe name is blank or you put NaN somewhere.

b)GetElement can return null if id is not used , check first what GetElement return you first. I thing it is possible to get null on GetElement if you create it in the same transaction.
I hope you solve your problem.

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes