Message 1 of 4
Getting Materials used by Instances in the project?

Not applicable
09-03-2015
04:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I am trying to create a list of MaterialIds of materials that are not used within the document. I am doing this by getting all the instances within the document and using Instance.GetMaterialIds(bool) on each instance found within "nTypes" in the code bellow.
The string "materialIdsGotten" is used to collect all the ids of materials used by instances but for some reason even though i am using a fairly large project it is only collecting two materials. I'm guessing i missussing Instance.GetMaterialIds(bool) wrong. Can you please help me?
FilteredElementCollector nTypes = new FilteredElementCollector(doc).WhereElementIsNotElementType(); FilteredElementCollector Materials = new FilteredElementCollector(doc).OfClass(typeof(Material)); List<ElementId> MaterialIds = Materials.ToElementIds().ToList<ElementId>(); string materialIdsGotten = ""; foreach (Element e in nTypes) { if (e is Instance) { Instance i = (Instance)e; if (e.Category != null) { if (e.Category.HasMaterialQuantities == true) { ICollection<ElementId> iMaterialIds = i.GetMaterialIds(true); foreach (ElementId eId in iMaterialIds) { materialIdsGotten += doc.GetElement(eId).Name + "\n"; if (MaterialIds.Contains(eId)) { MaterialIds.Remove(eId); //remove from list of Ids to be deleted. } } } else { ICollection<ElementId> iMaterialIds = i.GetMaterialIds(false); foreach (ElementId eId in iMaterialIds) { materialIdsGotten += doc.GetElement(eId).Name +"\n"; if (MaterialIds.Contains(eId)) { MaterialIds.Remove(eId); //remove from list of ids to be deleted } } } } } }
Thank you in advance