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.
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.
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();
}
}
Harry is there a way to turn off the title of a view in a sheet via API? Thanks Marcello
Date: Mon, 16 Sep 2013 13:47:01 +0000 To: marcellojs@hotmail.com
Hi Marcello,
API approach is same as the UI – set the Viewport parameter to 0
Harry
On Mon, Sep 16, 2013 at 9:48 AM, Boost Your BIM – making Revit even better wrote:
> New comment on your post “Deleting DWG Imports and their categories” > Author : M S (IP: 65.55.111.136 , 65.55.111.136) > E-mail : marcellojs@hotmail.com > URL : > Whois : http://whois.arin.net/rest/ip/65.55.111.136 > Comment: > Harry is there a way to turn off the title of a view in a sheet via API? > Thanks Marcello > >
Hi Harry,
Good one thanks, How to delete a linked dwg files.?
thanks
Br
How to you use this code? I do not know to insert it…
Morgan,
ckeck this post it explane how to use these codes with Revit macro.
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!
Hi,
This post about debugging macros might help you figure out what is happening. https://boostyourbim.wordpress.com/2013/01/11/stepping-into-your-macros-and-is-this-an-import-or-a-link/
Hi.
I’m afraid that didn’t get me much wiser. Anyway, thank you, but my imported DWGs will just have to stay in my model until I find another solution.
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 😦
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
I’m having the same problem.
I’m not sure where the syntax error is based on that error, but maybe one of these will help?
What changes need to be made to work in 2017?
Did you get any errors or unexpected results in 2017? I tried it in 2019 and it worked fine
I didn’t get any errors or warnings and have successfully used in Revit 2015. I’ll double check my cut and paste skills since it doesn’t seem to do anything.
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.
Hi Adam – Did you create a Document Macro? If so, please try it as an Application Macro
Harry
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.
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.
Hi – Could you share the RVT file you are using where nothing seems to happen? Or what is the specific name of the category you’d like to remove? Feel free to reach out via the form at https://boostyourbim.wordpress.com/contact/
Thanks, Harry
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.
Hey there, I am having the same issue as some others above. All of our imported categories remain after creating an error free macro and running it. All of our imported categories have various names, but most end in “.dwg”. I am curious how I could specify that in a macro, but since, much like Susana, I am just following instructions, I could use some guidance. Please advise.
Correction, some are removed but others remain, and I am not sure what the distinguishing factor is between them.
Hi Justin,
Does this work for you in the way that you are hoping?
Harry
https://knowledge.autodesk.com/community/screencast/105f5060-a0b6-48e5-b08e-ae2c072e889c
public void DeleteCategories()
{
Document doc = this.ActiveUIDocument.Document;
List dwg_cats = doc
.Settings
.Categories
.Cast()
.Where(q => q.Name.EndsWith(“.dwg”))
.ToList();
List toDelete = new List();
foreach (Category cat in dwg_cats)
{
foreach (Category subcat in cat.SubCategories)
{
if (subcat.Name.StartsWith(“A-ANNO-“))
{
toDelete.Add(subcat.Id);
}
}
}
using (Transaction t = new Transaction(doc, “Delete ” + toDelete.Count + ” subcategories”))
{
t.Start();
doc.Delete(toDelete);
t.Commit();
}
}