revit中使用api读取元素的参数主要有两种方法:
1,使用Element.Parameters获得元素所有参数,然后通过遍历参数名找到需要的参数。
我们以读取墙的面积参数为例,代码如下:
1 using System.Text;
2 using System.Threading.Tasks;
3 using Autodesk.Revit.DB;
4 using Autodesk.Revit.UI;
5 using Autodesk.Revit.ApplicationServices;
6 using Autodesk.Revit.UI.Selection;
7 using Autodesk.Revit.Attributes;
8 using System.Windows.Forms;
9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Diagnostics;
13 using System.IO;
14
15 namespace Xincubus
16 {
17 [Transaction(TransactionMode.Manual)]
18 public class Test1 : IExternalCommand
19 {
20 public Result Execute(ExternalCommandData document, ref string message, ElementSet elements)
21 {
22 UIApplication uiapp = document.Application;
23 Document doc = uiapp.ActiveUIDocument.Document;
24 FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
25 wallCollector.WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_Walls)).WhereElementIsNotElementType();
26 double wallArea = 0;
27 double wallsArea =0;
28 foreach (Wall wall in wallCollector)
29 {
30 ParameterSet parameters = wall.Parameters;
31 foreach (Parameter parameter in parameters)
32 {
33 if (parameter.Definition.Name == "面积")
34 {
35 wallArea = parameter.AsDouble();
36 wallsArea = wallsArea+wallArea;
37 }
38 }
39 }
40 MessageBox.Show("所有墙面积之和为" + wallsArea.ToString() + "。");
41 return Result.Succeeded;
42 }
43 }
44 }
2.使用Element.get_Parameter()来获得()中限定的参数。
Element.get_Parameter()总共可以通过加载四类参数属性来获得参数值,分别是:
Element.get_Parameter(BuiltInParameter builtInParam)
Element.get_Parameter(string name) (已更改为IList<Parameter> GetParameters(string name) 和LookUpParameter(string name))
Element.get_Parameter(Guid guid)
Element.get_Parameter(Definition definition)
下面分别叙述这四种方法;
①Element.get_Parameter(BuiltInParameter builtInParam)
通过BuiltInParameter来读取参数,首先需要知道参数的BuiltInParameter值,使用look up工具可以找到revit自带参数的BuiltInParameter值,还是以墙的面积参数为例:
可以得到墙的面积参数的BuiltInParameter值为HOST_AREA_COMPUTED,代码如下:
1 [Transaction(TransactionMode.Manual)]
2 public class Test2 : IExternalCommand
3 {
4 public Result Execute(ExternalCommandData document, ref string message, ElementSet elements)
5 {
6 UIApplication uiapp = document.Application;
7 Document doc = uiapp.ActiveUIDocument.Document;
8 FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
9 wallCollector.WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_Walls)).WhereElementIsNotElementType();
10 double wallArea = 0;
11 double wallsArea = 0;
12 foreach (Wall wall in wallCollector)
13 {
14 Parameter parameter= wall.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED);
15 wallArea = parameter.AsDouble();
16 wallsArea = wallsArea + wallArea;
17
18
19 }
20 MessageBox.Show("所有墙面积之和为" + wallsArea.ToString() + "。");
21 return Result.Succeeded;
22 }
23 }
②Element.get_Parameter(string name)(原)
Element.get_Parameter(string name),在revit2015之后将此函数一分为二,
Ⅰ,IList<Parameter> GetParameters(string name) ,即通过查找参数名字获得所有名称为name的参数,不举例了。
Ⅱ,LookUpParameter(string name),即通过查找参数名字获得第一个名称为name的参数,同样我们以墙的面积为例:
1 [Transaction(TransactionMode.Manual)]
2 public class Test3 : IExternalCommand
3 {
4 public Result Execute(ExternalCommandData document, ref string message, ElementSet elements)
5 {
6 UIApplication uiapp = document.Application;
7 Document doc = uiapp.ActiveUIDocument.Document;
8 FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
9 wallCollector.WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_Walls)).WhereElementIsNotElementType();
10 double wallArea = 0;
11 double wallsArea = 0;
12 string parameterName = "面积";
13 foreach (Wall wall in wallCollector)
14 {
15 Parameter parameter = wall.LookupParameter(parameterName);
16 wallArea = parameter.AsDouble();
17 wallsArea = wallsArea + wallArea;
18 }
19 MessageBox.Show("所有墙面积之和为" + wallsArea.ToString() + "。");
20 return Result.Succeeded;
21 }
22 }
③Element.get_Parameter(Guid guid)
通过Guid来读取参数,Guid的值在创建共享参数的TXT文件中。
事实上TXT文件中还有一个Group值以及Name关系到下一种方法,在此截图说明:
通过Guid获得的参数不能是revit自带的参数,只能是后期添加的共享参数(Definition亦是如此),我们以上述截图中创建的“测试”参数为例,通过Guid获得参数的的示例代码如下:
1 [Transaction(TransactionMode.Manual)]
2 public class Test4 : IExternalCommand
3 {
4 public Result Execute(ExternalCommandData document, ref string message, ElementSet elements)
5 {
6 UIApplication uiapp = document.Application;
7 Document doc = uiapp.ActiveUIDocument.Document;
8 FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
9 wallCollector.WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_Walls)).WhereElementIsNotElementType();
10 string walltest = "";
11 string wallstest = "";
12 Guid guid = new Guid ("cca4f606-7bd5-413f-97f3-68b9689c5e9b");
13 foreach (Wall wall in wallCollector)
14 {
15 Parameter parameter = wall.get_Parameter(guid);
16 walltest = parameter.AsString();
17 wallstest= wallstest + walltest+"\n";
18 }
19 MessageBox.Show("所有墙的测试参数值分别为" + wallstest );
20 return Result.Succeeded;
21 }
22 }
④Element.get_Parameter(Definition definition)
Element.get_Parameter(Definition definition)同样只能获得共享参数,且方法相对繁琐,需要创建Definition文件,建议通过api创建共享参数的可以使用此方法。同样以“测试”为例,代码如下:
1 [Transaction(TransactionMode.Manual)]
2 public class Test5: IExternalCommand
3 {
4 public Result Execute(ExternalCommandData document, ref string message, ElementSet elements)
5 {
6 UIApplication uiapp = document.Application;
7 Document doc = uiapp.ActiveUIDocument.Document;
8 FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
9 wallCollector.WherePasses(new ElementCategoryFilter(BuiltInCategory.OST_Walls)).WhereElementIsNotElementType();
10 string walltest = "";
11 string wallstest = "";
12 Autodesk.Revit.ApplicationServices.Application app = document.Application.Application;
13 app.SharedParametersFilename = @"C:\Users\xincubus\Desktop\test.txt";
14 DefinitionFile definitionFile = app.OpenSharedParameterFile();
15 DefinitionGroup group = definitionFile.Groups.get_Item("wall");
16 Definition definition = group.Definitions.get_Item("测试");
17 foreach (Wall wall in wallCollector)
18 {
19 Parameter parameter = wall.get_Parameter(definition);
20 walltest = parameter.AsString();
21 wallstest = wallstest + walltest + "\n";
22 }
23 MessageBox.Show("所有墙的测试参数值分别为" + wallstest);
24 return Result.Succeeded;
25 }
26 }
以上一共有6种方法,
Element.Parameters遍历法速度最慢,不推荐使用。
Element.get_Parameter(BuiltInParameter builtInParam) 速度最快,但是共享参数无法光靠此函数获得,因为所有共享参数的BuiltInParameter 值均为INVALID,推荐需获得revit自带参数的时候使用。
IList<Parameter> GetParameters(string name) 和LookUpParameter(string name)速度比Element.get_Parameter(BuiltInParameter builtInParam)、Element.get_Parameter(Guid guid)和Element.get_Parameter(Definition definition)都慢,但比Element.Parameters快,建议在特殊情况下使用。
Element.get_Parameter(Guid guid)和Element.get_Parameter(Definition definition)速度相似,且都是用于共享参数的,我个人比较喜欢事先将共享参数TXT文件先创建好,然后使用Element.get_Parameter(Guid guid);倘若读者是通过api创建共享参数TXT文件的,由于事先已经定义了Definition变量,可以选择Element.get_Parameter(Definition definition)。