Deleting DWG Imports and their categories

Recent posts have looked at how to delete ImportInstance elements. This led to the question about how to get rid of those categories that are left behind and visible in the Visibility/Graphics and Object Styles dialog.

Capture

In some ways, import instances and family instances are quite similar. Revit stores a single definition of the family/import and then places one or more instances of the family/import in the model.

With the FamilyInstance class in the API we can get the FamilySymbol and from the FamilySymbol we can get the Family. ImportInstance has no such properties. But we can see from the status prompt & tooltip when these elements are selected that there is a similar hierarchy in both FamilyInstance and ImportInstance elements.

Untitled

The solution is that if you want to delete both the import instances and the associated import categories, then the thing do do is delete the category, not just the instance.

This code builds a unique list of element id’s that represent the categories of imported elements.  Deleting the import category also deletes all instances of that import.

public void deleteImportCategories()
{
    Document doc = this.ActiveUIDocument.Document;   

    IList<ElementId> categoryIds = new List<ElementId>();

    foreach (ImportInstance ii in  new FilteredElementCollector(doc)
        .OfClass(typeof(ImportInstance))
        .Cast<ImportInstance>()
        .Where(i => i.IsLinked == false))
    {
        ElementId catId = ii.Category.Id;
        if (!categoryIds.Contains(catId))
            categoryIds.Add(catId);
    }

    using (Transaction t = new Transaction(doc,"Delete Import Categories"))
       {
        t.Start();
        doc.Delete(categoryIds);
        t.Commit();
    }
}
Advertisements

10 thoughts on “Deleting DWG Imports and their categories

  1. Hi. I run the macro without any errors, but nothing seems to happen. Can you please post a youtube-video where you insert the code in the module and run it?

    Thanks!

  2. Can this be extended to delete unwanted/unused Object Styles and Line Styles? I have used transfer project standards to bring settings into families but it copies hidden attributes also. I have updated my naming convention in my project template but the old names are locked into my families and repopulate my projects. It’s a nightmare that we can’t purge or browse these settings 😦

  3. Imported objects… and using…

    ‘removeImports.ThisDocument’ does not contain a definition for ‘ActiveUIDocument’ and no extension method ‘ActiveUIDocument’ accepting a first argument of type ‘removeImports.ThisDocument’ could be found (are you missing a using directive or an assembly reference?) (CS1061) – C:\Users\ron.Allen\AppData\Local\Temp\{3379BD2E-7C02-4BBA-A37C-3D4CAD041BDD}\Revit\DocHookups7688\4763933056\removeImports\Source\removeImports\ThisDocument.cs:42,27

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s