Edit family in project, delete materials, reload back to the project

Edit family in project, delete materials, reload back to the project

h_echevarria
Contributor Contributor
294 Views
3 Replies
Message 1 of 4

Edit family in project, delete materials, reload back to the project

h_echevarria
Contributor
Contributor

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

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

h_echevarria
Contributor
Contributor
Accepted solution

Well...

I was about to give up and then I realised that I was using Regenerate() inside the family docs and not in the target Document. Using Regenerate() in the target document works!

 

0 Likes
Message 3 of 4

h_echevarria
Contributor
Contributor

False alarm, it seems like it's working because it keeps the family open in the background. So when I click edit it opens the already opened family which has no materials. But if I close the families programmatically it will still load the family with all the materials. Strangely, some of the actions work, and others don't. For example, I can hide categories and change units, but I cannot delete materias and I  cannot change line patterns

0 Likes
Message 4 of 4

h_echevarria
Contributor
Contributor

Update: It turns out that you get the same behaviour non-programmatically ಥ_ಥ

0 Likes