Revit modify materials - Method not found

Revit modify materials - Method not found

SwainStrain
Enthusiast Enthusiast
927 Views
4 Replies
Message 1 of 5

Revit modify materials - Method not found

SwainStrain
Enthusiast
Enthusiast

Hi everybody, 

 

i'm trying to modify the appearance of the "Default" material with the AppearanceAssetEditScope class. I've created the SetDiffuseColor class:

 

private static void SetDiffuseColor(Material material, Color color)
        {
            ElementId appearanceAssetId = material.AppearanceAssetId;

            AppearanceAssetElement assetElem = material.Document.GetElement(appearanceAssetId) as AppearanceAssetElement;

            // A transaction is necessary.  Multiple changes to the same asset can be made in one transaction if required.

            using (Transaction t = new Transaction(assetElem.Document, "Change material color"))
            {
                t.Start();

                using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(assetElem.Document))
                {
                    Asset editableAsset = editScope.Start(assetElem.Id);   // returns an editable copy of the appearance asset

                    // unlike AppearanceAssetElement.GetRenderingAsset(), the edit scope

                    // sets up the asset for editing using the classes and utilities from

                    // MaterialsManager.  The scope can keep a mapping from asset to Protein

                    // class members that are obtained from the asset, to permit use of

                    // Protein validation.  If the Asset was not obtained from an edit scope,

                    // the setter operations for asset properties must throw.

                    // Try to change values

                    AssetPropertyDoubleArray4d genericDiffuseProperty = editableAsset["generic_diffuse"] as AssetPropertyDoubleArray4d;

                    genericDiffuseProperty.SetValueAsColor(color);  // 4 length double asset properties often represent colors.

                    // Shortcut methods should be added to work directly with Color objects

                    // for these types.

                    // Commit the entire edit scope.  If the appearance asset element is used in one or more materials, they will be

                    // updated to match any changes made.

                    editScope.Commit(true);
                }

                t.Commit();
            }
}

 

and use it in my main Command class: 

 

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication app = commandData.Application;
            UIDocument uidoc = app.ActiveUIDocument;
            Document doc = uidoc.Document;

            CreateMaterial(doc);

            //Collect Materials
            FilteredElementCollector coll2 = new FilteredElementCollector(doc).OfClass(typeof(Material));
            IList<Element> mats = coll2.ToElements() as IList<Element>;

            Material myMaterial = null;

            Color matColor = new Color(255,255,255);

            foreach (var m in mats) if (m.Name == "Default")
                {
                    myMaterial = m as Material;
                }

            SetDiffuseColor(myMaterial, matColor);


            return Result.Succeeded;
        }

 

The result is the following : 

2021-02-06 10_03_13-New Message - Autodesk Community.png

 i don't understand what am i doing wrong, any help is appreciated.

 

Thank you very much!

0 Likes
Accepted solutions (2)
928 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor
Accepted solution

From the edit scope there is likely a class of AssetProperties. This has default 'item' property that takes an integer for the index of the Asset (not a string).

 

It has another property named FindByName use that as is done in the example in RevitAPI.chm for AppearanceAssetEditScope.

0 Likes
Message 3 of 5

SwainStrain
Enthusiast
Enthusiast

Thank you @RPTHOMAS108 ! It seems it works fine now for "Default" material!

However, if i try to do the same thing with a material I've just created, it gives another error:

 

Paslet87_0-1612615309401.png

 

 

2021-02-06 13_40_29-Autodesk Revit 2021.1 - [materials.rvt - Sheet_ A001 - Title Sheet].png

 

Is it because I've created this material with the Material.Create method?

 

Paslet87_1-1612615420793.png

Thank you very much again!

 

 

0 Likes
Message 4 of 5

TripleM-Dev.net
Advisor
Advisor
Accepted solution

Hi,

 

Check if 

material.AppearanceAssetId

is a valid ElementId, I believe creating a material doesn't include the AppearanceAsset.

After creating the Material also add a AppearanceAsset.

 

The Revit UI adds a 'Generic' asset (Temporarily I may add!) if one is missing when opening the MaterialBrowser.

This will only become part of the Material after a edit is done on the material in the browser.

 

- Michel

0 Likes
Message 5 of 5

SwainStrain
Enthusiast
Enthusiast

Many thanks @TripleM-Dev.net , this is exactly what i've done, it works  fine now.

 

Thank you all!

0 Likes