获取活动视图对象参数?

Getting Active View Object Parameters?

我是 API 的新手,我正在尝试从活动视图中获取值。我正在使用以下代码作为我正在尝试做的事情的模拟:

public void GetViewProperties()
{
  String viewname;
  String typename;
  String levelname;
  String Output;

  ViewFamilyType VfamType;
  Level lev;

  //Get document and current view
  Document doc = this.ActiveUIDocument.Document;
  View currentView = this.ActiveUIDocument.ActiveView;

  //Find the view family type that matches the active view
  VfamType = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
    .Where(q => q.Name == "1-0-Model").First() as ViewFamilyType;

  //Find the level that matches the active view
  lev = new FilteredElementCollector(doc).OfClass(typeof(Level))
    .Where(q => q.Name == "00").First() as Level;

  //Get the view's current name
  viewname = currentView.Name.ToString();

  //Get the name of the view family type
  typename = VfamType.Name;
  //Get the name of the level
  levelname = lev.Name.ToString();

  //Combine results for task dialog
  Output = "View: " + viewname + "\n" + typename + "-" + levelname;
  //Show results
  TaskDialog.Show("View Properties Test",Output);
}

我现在在作弊,通过名称获取视图类型和级别。我真的希望通过查看活动视图的属性来找到它们。我不知道我是如何访问视图类型和级别名称属性的。我需要让 lambda 使用变量,例如(q => q.Name == Level.name), (q => q.Name == ViewFamilyType.name).

提前致谢!

您可能正在寻找 View.GenLevel属性。这将适用于与级别相关的视图,例如平面视图。注意,如果这个View不是一个关卡生成的,这个属性为null。

这里是你的代码更正:

public void GetViewProperties()
{
  //Get document and current view
  Document doc = this.ActiveUIDocument.Document;
  View currentView = this.ActiveUIDocument.ActiveView;

  //Find the view family type that matches the active view
  var VfamType = (ViewFamilyType)doc.GetElement(currentView.GetTypeId());

  //Find the level that matches the active view
  Level lev = currentView.GenLevel;

  //Get the view's current name
  string viewname = currentView.Name;

  //Get the name of the view family type
  string typename = VfamType.Name;

  //Get the name of the level
  string levelname = lev.Name;

  //Combine results for task dialog
  string Output = "View: " + viewname + "\n" + typename + "-" + levelname;

  //Show results
  TaskDialog.Show("View Properties Test", Output);
}

您不需要使用 FilteredElementCollector 来获取这些信息。如果您需要其他地方,则不需要 Where:只需将您的 lambda 放在 First:

new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
  .First(q => q.Name == "1-0-Model")

如果您需要在您的 lambda 中访问 属性 特定于 class,未在 Element 上定义,您可以使用 Cast:

new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
  .Cast<ViewFamilyType>().First(vft => vft.IsValidDefaultTemplate)

并且请不要在方法的开头声明所有变量。你不是在写 Pascal。在尽可能靠近您使用它们的第一个位置声明变量。它使您的代码更具可读性。变量的声明越接近它的使用位置,以后阅读代码时scrolling/searching要做的事情就越少,自然也会缩小它们的范围。