1.获取共享参数
private string GetSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp)
{
StringBuilder str = new StringBuilder();
DefinitionFile definitionFile = revitApp.OpenSharedParameterFile();
DefinitionGroups groups = definitionFile.Groups;
foreach (DefinitionGroup group in groups)
{
foreach (Definition definition in group.Definitions)
{
string name = definition.Name;
ParameterType type = definition.ParameterType;
str.AppendLine(string.Format("{0}---{1}", name, type.ToString()));
}
}
return str.ToString();
}
2.创建共享参数
private void CreateSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp, Autodesk.Revit.DB.Document revitDoc)
{
try
{
string sharedParametersFilename = @"C:\shared-parameters.txt";
string groupName = "MyGroup";
string definitionName = "MyDefinition";
ParameterType parameterType = ParameterType.Text;
CategorySet categorySet = new CategorySet();
Category wallCategory = revitDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls);
categorySet.Insert(wallCategory);
bool instanceParameter = true;
BuiltInParameterGroup parameterGroup = BuiltInParameterGroup.PG_DATA;
if (!System.IO.File.Exists(sharedParametersFilename))
{
try
{
System.IO.StreamWriter sw = System.IO.File.CreateText(sharedParametersFilename);
sw.Close();
}
catch (Exception)
{
throw new Exception(string.Format("Can't create shared parameter file:{0}", sharedParametersFilename));
}
}
revitApp.SharedParametersFilename = sharedParametersFilename;
DefinitionFile definitionFile = revitApp.OpenSharedParameterFile();
if (definitionFile == null)
{
throw new Exception("Can not open shared parameter file");
}
DefinitionGroups groups = definitionFile.Groups;
DefinitionGroup group = groups.get_Item(groupName);
if (group == null)
{
group = groups.Create(groupName);
}
Definition definition = group.Definitions.get_Item(definitionName);
if (definition == null)
{
definition = group.Definitions.Create(new ExternalDefinitionCreationOptions(definitionName, parameterType));
}
ElementBinding binding = null;
if (instanceParameter)
{
binding = revitApp.Create.NewInstanceBinding(categorySet);
}
else
{
binding = revitApp.Create.NewTypeBinding(categorySet);
}
bool insertSuccess = revitDoc.ParameterBindings.Insert(definition, binding, parameterGroup);
if (!insertSuccess)
{
throw new Exception("Failed to bind definition to category");
}
}
catch (Exception ex)
{
throw ex;
}
}
3.获取项目参数(API无法创建项目参数的)
private string GetProjectParam(Autodesk.Revit.DB.Document revitDoc)
{
StringBuilder strBuild = new StringBuilder();
BindingMap map = revitDoc.ParameterBindings;
DefinitionBindingMapIterator dep = map.ForwardIterator();
while (dep.MoveNext())
{
Definition definition = dep.Key;
string definitionName = definition.Name;
ParameterType parameterType = definition.ParameterType;
InstanceBinding instanceBinding = dep.Current as InstanceBinding;
if (instanceBinding != null)
{
CategorySet categorySet = instanceBinding.Categories;
strBuild.AppendLine(string.Format("Name:{0} Type:{1} CategorySet:{2}", definitionName, parameterType, categorySet.GetType().ToString()));
}
else
{
strBuild.AppendLine(string.Format("Name:{0} Type:{1}", definitionName, parameterType));
}
}
return strBuild.ToString();
}
4.创建项目参数
Revit API无法创建项目参数,目前没有找到创建方法。