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();
    }
}

21 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

  4. Apologies for resurrecting an old post – Harry, I am trying to delete CAD files in imports in families. When trying to build the solution, I get the following error:

    CADDel.ThisDocument does not contain a definition for ‘ActiveUIDocument’ and no extension method ‘ActiveUIDocument’ accepting a first argument of type CADDel.ThisDocument could be found (are you missing a using directive or an assembly reference?)(CS1061

    What am I missing or doing wrong? This is for Revit 2019
    Thank you for any assistance you can provide.

  5. That fixed that error. Thank you for such a rapid response. Now the problem is that the macro does not seem to work on the .DWG files within the Imported Categories… They still show up after the macro is run, when looking at Visibility Graphics.

    I think I will just make sure to build the macros in the application environment from now on. Thank you again, Harry.

  6. Hello,
    I’m trying to use this macro to purge an old dwg information from object styles list (it got imported when transferring view styles from another project – huge mistake).
    I even changed the link view to “import me.dwg” as in the above image, but nothing seems to happen. Can some twick to the code be done to force the removal of a category with that specific name (or other defined by the user)?
    I followed all the above instructions and I’m using revit 2020. This is my first code creation. I don’t know C#, I just follow instructions.
    I will be grateful if anyone can help or has any kind of suggestion to make the impossible purge.

      • Harry support was fantastic.
        I’m leaving to solution here in case it can help anyone else out there.

        > The code used was:
        __________________________________________________
        public void deleteCategory()
        {
        string categoryName = “import me”;

        Document doc = this.ActiveUIDocument.Document;
        Category cat = doc.Settings.Categories.Cast().FirstOrDefault(q => q.Name.StartsWith(categoryName));

        if (cat == null)
        {
        TaskDialog.Show(“Error”, “Category not found whose name starts with ” + categoryName);
        return;
        }

        using (Transaction t = new Transaction(doc, “d”))
        {
        t.Start();
        doc.Delete(cat.Id);
        t.Commit();
        }
        }
        __________________________________________________

        > And it has to be inserted after:
        __________________________________________________
        public partial class ThisApplication
        {
        __________________________________________________
        > And before:
        __________________________________________________
        private void Module_Startup(object sender, EventArgs e)
        {
        __________________________________________________

        See the video in the link above for more information on the full process of how to create a macro.
        This macro only works if you rename the category to “import me.dwg”.
        If you need it to something else I can try to change the name in the code above.

        Thank you so much again Harry. I will definitely remember you if we ever need any specific custom macros in the future.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s