Retrieve a Material ID via api & assign to an Integer Variable

Retrieve a Material ID via api & assign to an Integer Variable

AdvancedBIMSystems
Enthusiast Enthusiast
695 Views
3 Replies
Message 1 of 4

Retrieve a Material ID via api & assign to an Integer Variable

AdvancedBIMSystems
Enthusiast
Enthusiast

I can obviously Snoop DB with RevitLookup to obtain a named Material Id :Capture.JPG

 

How would I go about this with code, assuming I know the material name already?

0 Likes
Accepted solutions (1)
696 Views
3 Replies
Replies (3)
Message 2 of 4

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @AdvancedBIMSystems ,

Try using this below code

FilteredElementCollector collector= new FilteredElementCollector(doc).OfClass(typeof(Material));
                IEnumerable<Material> materialsEnum= collector.ToElements().Cast<Material>();
                var materialReturn1 = from materialElement in materialsEnum where materialElement.Name.Contains("Your MATERIAL NAME") select materialElement;

                Material material = materialReturn1.First() as Material;

                //Material ID
                int materialId = material.Id.IntegerValue;

 I hope this helps.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 4

RPTHOMAS108
Mentor
Mentor

By some form of filtering e.g.

 

public Result TObj121(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
		{
			UIApplication UIApp = commandData.Application;
			UIDocument UIDoc = UIApp.ActiveUIDocument;
			if (UIDoc == null)
				return Result.Cancelled;
			Document Doc = UIDoc.Document;

			FilteredElementCollector FEC = new FilteredElementCollector(Doc);
			ElementClassFilter ECF = new ElementClassFilter(typeof(Material));
			List<Element> Mats = FEC.WherePasses(ECF).ToElements().ToList();

			Element Mat_El = Mats.Find(x => x.Name == "Gold");
			if (Mat_El == null)
				return Result.Cancelled;

			int M_Id = Mat_El.Id.IntegerValue;

			return Result.Succeeded;
		}

 

0 Likes
Message 4 of 4

AdvancedBIMSystems
Enthusiast
Enthusiast

Thanks very much, that works perfectly!

0 Likes