- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I am trying to create a command that opens all the detail items in a project, cleans the family by turning off annotation categories (for the thumbnail), delete materials, etc.
So far I have managed to use the EditFamily method to open the families, hide the categories, and then reload back to the project with LoadFamily(targetDoc,IFamilyLoadOptions). However, I cannot delete the materials. I have a method that successfully work if I test it directly inside the family.
for (int i = 0; i < detailItems.Count(); i++)
{
Family fam = detailItems[i];
if (!fam.IsEditable) continue;
Document famdoc = doc.EditFamily(fam);
HideCategories(famdoc);
DeleteMaterials(famdoc);
Family outputfamily = famdoc.LoadFamily(doc, new FamilyLoadOptions());
//bool isclosed = famdoc.Close(false);
}
So in the code above the Hide Categories method works: after reloading back the family, if I edit it I can see that the categories are hidden. However, the DeleteMaterials method is not working. It is curious because if I create a material collector after running the method it returns 0 materials. But when I open the family again, all the materials are there.
Here is the code for deleting materials:
private void DeleteMaterials(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
List<ElementId> materialIds = collector.OfClass(typeof(Material)).Select(x => x.Id).ToList();
using (Transaction t = new Transaction(doc, "delete materials"))
{
t.Start();
doc.Delete(materialIds);
t.Commit();
}
}
And here the IFamilyLoadOptions:
public class FamilyLoadOptions : IFamilyLoadOptions
{
public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
{
overwriteParameterValues = true;
return true;
}
public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
{
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
}
I suspect it has to do something with the transactions. But I don't receive any warnings. I also try to regenerate after deleting the materials, but it didn't work.
Any ideas? Many thanks
Solved! Go to Solution.