revit 二次开发之读取参数

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)。

 

转载于:https://www.cnblogs.com/Xincubus/p/6691566.html

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Revit是一款专业的BIM软件,它允许用户进行二次开发以满足特定需求。在Revit中,我们可以通过添加族参数来扩展族的功能和灵活性。 为了添加族参数,我们首先需要打开Revit软件并加载我们要进行二次开发的族文件。然后,我们通过进入族编辑器来访问族参数。在族编辑器中,我们可以找到“族参数”按钮,并点击它来添加新的参数。 在“族参数”对话框中,我们可以选择不同的参数类型,如整数、文本、长度等等。根据我们的需求,我们可以设置参数的名称、默认值、数据类型和单位。 一旦我们添加了参数,我们可以将其应用于族中的特定元素。例如,如果我们想在窗户族中添加一个参数用于控制窗户开启方式,我们可以在族参数对话框中创建一个文本类型的参数,命名为“开启方式”。 在添加了族参数后,我们可以使用Revit API进行二次开发Revit API是一组用于自动化和扩展Revit功能的编程接口。通过使用Revit API,我们可以编写脚本或插件来操作和管理Revit模型。 例如,我们可以使用Revit API编写一个插件,通过读取和修改族参数来批量修改模型中的元素。这样,我们就能够根据具体要求自定义和管理族参数,实现更高效的模型创建和编辑。 总之,通过Revit二次开发和族参数的添加,我们能够为Revit软件添加新的功能和灵活性,以满足我们的特定需求。这为我们提供了一个更强大、更个性化的BIM工作流程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值