Remove Imported Categories

Last updated December 22, 2022
By Ian Story

When you import CAD files into your Revit project, Revit creates a category to help organize and manage all of the imported file’s layers. When you open up the Visibility and Graphics window, you can see these categories listed within the “Imported Categories” tab – this gives you visibility and graphic setting control over the imported CAD file’s layers.

When you remove a CAD file from the project, Revit is supposed to remove the corresponding category from Visibility and Graphics. Sometimes, however, Revit fails to implement this step, leaving you with an orphaned category heading. We discovered this when trying to clean up our template file.

It turns out removing an orphaned import category is tricky. There is no built-in way in native Revit to interact with these headings. You can’t delete them directly, and Purge Unused doesn’t do it, because Revit no longer recognizes the category as an element. However, it turns out it is still possible to clean out these orphaned categories using a call to the Revit API.

The remainder of this article is a step by step tutorial to clean out orphaned .dwg import categories, using Revit’s Macro Manager.

Step-By-Step Guide

Open up the Macro Manager (Manage Tab -> Macros section -> Macro Manager)

With the Applications tab selected, create a new Module. Name can be anything. Language: C#

Revit will then open an IDE called SharpDevelop, with a pre-generated code template for your module. Tab back to Revit and select the module you just created in the Macro Manager. Click Macro in the Create section.

Give the new Macro a name and click OK.

Tab back to SharpDevelop. You should see a function added to the bottom of the code window, with the header public void <name>() followed by opening and closing curly brackets.

Within the curly brackets, paste the following code:

Document doc = this.ActiveUIDocument.Document;
Categories categories = doc.Settings.Categories;

using (Transaction t = new Transaction(doc,"PurgeImportCategoryDWGs"))
{
  t.Start();
  foreach (Category category in categories)
  {
    if (category.Name.Contains(".dwg"))
    {
      doc.Delete(category.Id);
      foreach (Category x in category.SubCategories)
      {
        doc.Delete(x.Id);
      }
    }
  }
  t.Commit();
}

TaskDialog.Show("Completion Alert", "Macro completed");

Go to the Build menu and click “Build Solution”. The text in the bottom left footer of the window should read “Build finished successfully.” This also saves the updates.

Close out of SharpDevelop. You should see your Module in the Macros list with a plus button next to it. Expand the plus button to see your macro below. Select the macro and click the “Run” button.

If everything performs as expected, this should remove all dwg Imported Categories from the Visibility and Graphics window (except for “Imports in Families” – those need to be dealt with on an individual family basis).

Note that the above script is indiscriminate: it will remove all dwgs in the project, so may not be appropriate for a project with current linked references. But for cleaning up a template file, this should do the trick nicely!

If you need to remove only a specific reference, try changing the category.Name.Contains() string in line 9 to match the name of the category you are trying to remove.