Set material parameter programmatically.

Set material parameter programmatically.

mohd.fadel
Enthusiast Enthusiast
1,459 Views
2 Replies
Message 1 of 3

Set material parameter programmatically.

mohd.fadel
Enthusiast
Enthusiast

Hello,

 

I'm trying to set the custom parameter of a material programmatically. This is my code so far:

 

 

foreach (ElementId materialId in f.GetMaterialIds(false))
            {
                Material material = f.Document.GetElement(materialId) as Material;
                Element el = doc.GetElement(elemId);
                ParameterSet parameters = el.Parameters;
                foreach (Parameter param in parameters)
                {
                    if (param.Definition.Name == "USER_DEFINED_FIELD")
                    {
                        switch (param.StorageType)
                        {
                            case Autodesk.Revit.DB.StorageType.String:

                                if (String.IsNullOrWhiteSpace(param.AsString()))
                                {
                                        param.Set("Hello");
                                        param.SetValueString("Hello");
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }        
            }

 

 

Furthermore, I have this code wrapped in a transaction. However, nothing is happening. Any ideas?

 

Any help is appreciated.

 

M.Fadel 

0 Likes
Accepted solutions (1)
1,460 Views
2 Replies
Replies (2)
Message 2 of 3

mohd.fadel
Enthusiast
Enthusiast

Sorry correction,

 

The

 

Material material = f.Document.GetElement(materialId) as Material;

 

 line is for when i tried implementing Jeremy's post [http://thebuildingcoder.typepad.com/blog/2013/01/read-material-asset-parameter.html]

 

However, i didn't know what to do here:

 

PropertySetElement property = doc.GetElement(
    material.StructuralAssetId ) as PropertySetElement;
 
  Parameter paramTensionParallel
    = property.get_Parameter( BuiltInParameter
      .PHY_MATERIAL_PARAM_TENSION_PARALLEL );

 

Is the first method not valid (without the initialization of a Material variable)?

 

M.Fadel

0 Likes
Message 3 of 3

mohd.fadel
Enthusiast
Enthusiast
Accepted solution

Update:

 

I was able to figure out the issue. It was:

 

foreach (ElementId materialId in f.GetMaterialIds(false))

that was causing the issue.

 

I just used a regular FilteredElementCollector instead of a FamilyInstanceCollector and iterated through that collection's elements.

 

Thank you.

 

M.Fadel

0 Likes