Set material parameter value

Set material parameter value

Yonas89
Collaborator Collaborator
4,387 Views
4 Replies
Message 1 of 5

Set material parameter value

Yonas89
Collaborator
Collaborator

Hello, I am wondering how I could set any material  value programmatically to material parameter  as in this image below? What's the way to iterate through the materials and assign its  value to the parameter?   

question.PNG 

Accepted solutions (1)
4,388 Views
4 Replies
Replies (4)
Message 2 of 5

Yonas89
Collaborator
Collaborator

if anyone face similar problem, I was able to solve this with following snippet:

 public void SetMaterial(FamilySymbol fs)
{
               IList<Parameter> pars = fs.GetOrderedParameters();
                foreach (Parameter p in pars)
                {
                    if (p.Definition.Name == "Material")
                    {      
                        Material m = GetMaterial(doc, "Glass");
                        if (m!= null)
                        {
                            fs.Category.Material = m;
                            p.Set(m.Id);
                        }                   
                    }
                }
}

 public Material GetMaterial (Document doc, string matName)
        {
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            Material mat = null;
            var materials = collector.WherePasses(new ElementClassFilter(typeof(Material))).Cast<Material>();
            foreach(Material m in materials)
            {
                if (m.Name == matName)
                {
                    mat = m;
                    break;
                }
            }

            return mat;
        }
0 Likes
Message 3 of 5

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Yonas,

 

Thank you for your query and solution.

 

Here is an untested optimised version of your second method:

 

 

  public Material GetMaterial (Document doc, string matName)
  {
    return new FilteredElementCollector(doc)
      .OfClass( typeof(Material)))
      .Cast<Material>()
      .Where( m => m.Name == matName );
  }

 

To improve the first, the most important thing to do is determine the built-in parameter enumeration value corresponding to the "Material" parameter.

 

If you don't have it at hand, use RevitLookup to find out.

 

Let's assume it is ELEMENT_MATERIAL.

 

Then you can access the parameter directly using the built-in enum instead of retrieving and iterating over all the element parameters.

 

If you insist on using the name, you can use LookupParameter, if you are sure that there is only one matching that name, or GetParameters taking a string, to retrieve all with a given name.

 

 

  public void SetMaterial(FamilySymbol fs)
  {
    Parameter p = fs.GetParameter( BuiltInParameter.ELEMENT_MATERIAL );
    if (null != p )
    {    
      Material m = GetMaterial(doc, "Glass");
      if (m!= null)
      {
        fs.Category.Material = m;
        p.Set(m.Id);
      }           
    }
  }

 

I hope this helps.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 4 of 5

Yonas89
Collaborator
Collaborator

@jeremytammik,

thanks a lot!

0 Likes
Message 5 of 5

amro14181998
Observer
Observer

sir I can't find this parameter name ELEMENT_MATERIAL   what is it now in revit 2023 call?

0 Likes