简体   繁体   English

控制导入类别的可见性

[英]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). 我正在编写一个程序,以从导入的AutoCAD文件中关闭某些“子类别”(例如“ DEFPOINTS”,可以说应该将其自动隐藏或不在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. 给出的错误是:无法设置类别3的可见属性,因为它是用户隐藏的事务或子事务已打开但未关闭”。这很奇怪,因为“等级3”不是类别,而是ActiveView 。

But my transaction is opened and closed. 但是我的交易是打开和关闭的。 I was not able to find hiddenA in an online search. 我无法在在线搜索中找到hiddenA。 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. 我还尝试使用以下构造:One_Cat.get_Visibility(CurrentView)(以尝试至少查看类别是否可见),这导致了相同的错误。 I also changed my references from RevitAPI (2014) to (2015) and (2016), with no change in the error. 我还将我的引用从RevitAPI(2014)更改为(2015)和(2016),但未更改错误。 My code properly filtered the layer names I wanted to turn off, as evidenced in my "Would Turn Off Layers:" TaskDialog echo of myCategories. 我的代码正确过滤了我想关闭的图层名称,如myCategories的“将关闭图层:” TaskDialog回显所示。 But whenever the .SetVisibility line was not commented out, I get the error described. 但是,每当未注释掉.SetVisibility行时,我都会得到描述的错误。

Is there some other method of turning off(on) visibility of Categories in the RevitAPI? 还有其他方法可以关闭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. 我没有便利的SDK,但是在“ Category.AllowsVisibilityControl [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. 我在您的代码中看到一个与事务有关的问题:您创建了一个新事务,但是从不对其调用StartCommit

Please read The Building Coder topic group on Handling Transactions and Transaction Groups about using transactions and enclosing them in a using statement. 请阅读有关处理事务和事务组的Building Coder主题组,以了解如何使用事务并将其包含在using语句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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