简体   繁体   中英

Controlling Visibility Of Imported Categories

I am writing a program to turn off certain "sub-categories" from imported AutoCAD files (such as "DEFPOINTS", which arguably should automatically be hidden or not plot in Revit). The following code seems like it should work, but instead kicks back an odd error.

Transaction Do_Filtered_VG = new Transaction(doc, "FilteredVG")
Autodesk.Revit.DB.View CurrentView = doc.ActiveView;
Categories categories = doc.Settings.Categories; string CatUp = string.Empty;
List<Category> ToBeOff = new List<Category>();
List<string> myCategories = new List<string>();
foreach (Category c in categories)
{
    if (c.Name.ToLower().EndsWith("dwg"))
    {
        List<Category> SubCategories = new List<Category>();
        foreach (Category One_Cat in c.SubCategories)
        {
            CatUp = One_Cat.Name.ToUpper();
            if (CatUp.Contains("DEFPOINTS") || CatUp.Contains("NPLT") || CatUp.Contains("RDFF") || CatUp.Contains("SDFF") || CatUp.Contains("DUCT"))//or other pattern;
            {
                 myCategories.Add(One_Cat.Name); ToBeOff.Add(One_Cat);
            }
        }
    }
}
Do_Filtered_VG.Start();
foreach (Category One_Cat in ToBeOff)
{
    CurrentView.SetVisibility(One_Cat, false);
}
Do_Filtered_VG.Commit();
TestCodeX.If_Tony("Would Turn Off Layers:", TestCodeX.List_To_Dialog(myCategories));

The error given is: Cannot set the visible attribute of the category Level 3 because it is user hiddenA transaction or sub-transaction was opened but not closed". This is odd, because "Level 3" is not a category, but is the ActiveView.

But my transaction is opened and closed. I was not able to find hiddenA in an online search. I also tried using the construction: One_Cat.get_Visibility(CurrentView) (to try to at least see if the category is visible), and this resulted in the same error. I also changed my references from RevitAPI (2014) to (2015) and (2016), with no change in the error. My code properly filtered the layer names I wanted to turn off, as evidenced in my "Would Turn Off Layers:" TaskDialog echo of myCategories. But whenever the .SetVisibility line was not commented out, I get the error described.

Is there some other method of turning off(on) visibility of Categories in the RevitAPI? Or is there some other Transaction method required other than the one I am using?

There are some internal categories that exist, but you can't set the visibility on. I don't have the SDK handy, but there's a property along the lines of "Category.AllowsVisibilityControl[view]" that will tell you whether it is legitimate to hide that category in the current view.

So you would do something like this in your code:

// inside your existing IF about the category name...
if (One_Cat.AllowsVisibilityControl[CurrentView])
{     
    myCategories.Add(One_Cat.Name); ToBeOff.Add(One_Cat);
}

I see one problem with transactions in your code: you create a new transaction, but never call either Start or Commit on it.

Please read The Building Coder topic group on Handling Transactions and Transaction Groups about using transactions and enclosing them in a using statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM