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