我是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)。
提前致谢!
答案 0 :(得分:0)
您可能正在寻找 View.GenLevel 属性。这适用于与级别相关的视图,例如计划视图。请注意,如果此视图不是由级别生成的,则此属性为null。
答案 1 :(得分:0)
以下是您的代码更正:
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中访问特定于类的属性(未在Element
上定义),则可以使用Cast
:
new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
.Cast<ViewFamilyType>().First(vft => vft.IsValidDefaultTemplate)
请不要在方法开头声明所有变量。你不是在写Pascal。将变量声明为尽可能靠近您使用它们的第一个点。它使您的代码更具可读性。声明变量越接近它所使用的位置,稍后读取代码时你必须做的滚动/搜索越少,它自然也会缩小范围。