BIM World
A Professional BIM Learning Platform


Accessing Material Data through Revit Secondary Development

Using the lookup method to view materials can be quite misleading.

It took considerable time to identify the cause. To find all materials, you need to search through the Compound Structure Layers.

Please see the attached code demonstrating two ways to retrieve materials:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;

// Reading materials from the namespace
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document revitDoc = commandData.Application.ActiveUIDocument.Document; // Get the active document
            Application revitApp = commandData.Application.Application; // Get the Revit application instance
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Selection sel = uiDoc.Selection;
            Reference ref1 = sel.PickObject(ObjectType.Element, “Select a family instance”);
            Element elem = revitDoc.GetElement(ref1);

            Wall wall = elem as Wall;

            ICollection<ElementId> matId = elem.GetMaterialIds(true);
            #region Retrieve materials from ElementId (this method gets the main material)
            //foreach (var item in matId)
            //{
            //    TaskDialog.Show(“REVIT”, item.ToString());
            //    Material mat2 = revitDoc.GetElement(item) as Material; // Get material by ElementId
            //}
            // Location

            #endregion

            #region Retrieve materials from Compound Structure Layers (this method retrieves all materials related to the element)
            WallType wallType = wall.WallType;
            CompoundStructure compoundStructure = wallType.GetCompoundStructure();
            IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();

            foreach (var item in layers)
            {
                TaskDialog.Show(“revit”, item.MaterialId.ToString());
            }
            // Location
            #endregion

            return Result.Succeeded;
        }
    }
}

xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Accessing Material Data through Revit Secondary Development

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up