Revit API 操作共享参数和项目参数

1.获取共享参数

 1 private string GetSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp)
 2 {
 3     StringBuilder str = new StringBuilder();
 4     DefinitionFile definitionFile = revitApp.OpenSharedParameterFile();
 5     DefinitionGroups groups = definitionFile.Groups;
 6     foreach (DefinitionGroup group in groups)
 7     {
 8         foreach (Definition definition in group.Definitions)
 9         {
10             string name = definition.Name;
11             ParameterType type = definition.ParameterType;
12             str.AppendLine(string.Format("{0}---{1}", name, type.ToString()));
13         }
14     }
15     return str.ToString();
16 }

2.创建共享参数

 1 private void CreateSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp, Autodesk.Revit.DB.Document revitDoc)
 2 {
 3     try
 4     {
 5         string sharedParametersFilename = @"C:\shared-parameters.txt";
 6         string groupName = "MyGroup";
 7         string definitionName = "MyDefinition";
 8         ParameterType parameterType = ParameterType.Text;
 9         CategorySet categorySet = new CategorySet();
10         Category wallCategory = revitDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls);
11         categorySet.Insert(wallCategory);
12         bool instanceParameter = true;
13         BuiltInParameterGroup parameterGroup = BuiltInParameterGroup.PG_DATA;
14         if (!System.IO.File.Exists(sharedParametersFilename))
15         {
16             try
17             {
18                 System.IO.StreamWriter sw = System.IO.File.CreateText(sharedParametersFilename);
19                 sw.Close();
20             }
21             catch (Exception)
22             {
23                 throw new Exception(string.Format("Can't create shared parameter file:{0}", sharedParametersFilename));
24             }
25         }
26         revitApp.SharedParametersFilename = sharedParametersFilename;
27 
28         DefinitionFile definitionFile = revitApp.OpenSharedParameterFile();
29         if (definitionFile == null)
30         {
31             throw new Exception("Can not open shared parameter file");
32         }
33 
34         DefinitionGroups groups = definitionFile.Groups;
35         DefinitionGroup group = groups.get_Item(groupName);
36         if (group == null)
37         {
38             group = groups.Create(groupName);
39         }
40         Definition definition = group.Definitions.get_Item(definitionName);
41         if (definition == null)
42         {
43             definition = group.Definitions.Create(new ExternalDefinitionCreationOptions(definitionName, parameterType));
44         }
45         ElementBinding binding = null;
46         if (instanceParameter)
47         {
48             binding = revitApp.Create.NewInstanceBinding(categorySet);
49         }
50         else
51         {
52             binding = revitApp.Create.NewTypeBinding(categorySet);
53         }
54         bool insertSuccess = revitDoc.ParameterBindings.Insert(definition, binding, parameterGroup);
55         if (!insertSuccess)
56         {
57             throw new Exception("Failed to bind definition to category");
58         }
59     }
60     catch (Exception ex)
61     {
62         throw ex;
63     }
64 }

3.获取项目参数(API无法创建项目参数的)

 1 private string GetProjectParam(Autodesk.Revit.DB.Document revitDoc)
 2 {
 3     StringBuilder strBuild = new StringBuilder();
 4     BindingMap map = revitDoc.ParameterBindings;
 5     DefinitionBindingMapIterator dep = map.ForwardIterator();
 6     while (dep.MoveNext())
 7     {
 8         Definition definition = dep.Key;
 9         string definitionName = definition.Name;
10         ParameterType parameterType = definition.ParameterType;
11         InstanceBinding instanceBinding = dep.Current as InstanceBinding;
12         if (instanceBinding != null)
13         {
14             CategorySet categorySet = instanceBinding.Categories;
15             strBuild.AppendLine(string.Format("Name:{0} Type:{1} CategorySet:{2}", definitionName, parameterType, categorySet.GetType().ToString()));
16         }
17         else
18         {
19             strBuild.AppendLine(string.Format("Name:{0} Type:{1}", definitionName, parameterType));
20         }
21     }
22     return strBuild.ToString();
23 }

 

4.创建项目参数

Revit API无法创建项目参数,目前没有找到创建方法。

 

posted on 2018-01-08 16:32  小夏coding  阅读(1114)  评论(0编辑  收藏  举报

导航