Revit通过API创建共享参数

Revit共享参数是通过创建一个.txt类型的文件来保存相关信息,一旦与项目保存完毕之后,共享参数也就变成了项目参数(项目参数无法通过API创建),项目参数是保存在Revit项目里面的,所以此时这个.txt文件是可以删除的。

 using Autodesk.Revit.DB;
using System;
using System.IO;
using System.Reflection; namespace HelloRevit
{
public class RevitApiUtils
{
/// <summary>
/// 创建共享参数
/// </summary>
/// <param name="doc">Document对象</param>
/// <param name="groupName">共享参数组名</param>
/// <param name="definitionName">共享参数名</param>
/// <param name="builtInCategory">需要参数绑定的类型</param>
/// <param name="builtInParameterGroup">参数所属的属性类型</param>
/// <param name="parameterType">参数数据类型</param>
/// <param name="isInstanceParameter">是否实例参数</param>
public static void AddSharedParameters(Document doc, string groupName, string definitionName, BuiltInCategory builtInCategory, BuiltInParameterGroup builtInParameterGroup, ParameterType parameterType, bool isInstanceParameter)
{
var assemblyPath = Assembly.GetExecutingAssembly().Location;
var index = assemblyPath.IndexOf("\\", StringComparison.Ordinal);
var rootPath = assemblyPath.Substring(, index);
var sharedParameterPath = Path.Combine(rootPath, "RevitSharedParameters.txt");
if (File.Exists(sharedParameterPath))
{
File.Delete(sharedParameterPath);
}
var categorySet = new CategorySet();
var category = doc.Settings.Categories.get_Item(builtInCategory);
categorySet.Insert(category);
if (!File.Exists(sharedParameterPath))
{
try
{
File.Create(sharedParameterPath);
}
catch (Exception)
{
ThrowNewException("异常!");
}
}
//设置共享参数文件
doc.Application.SharedParametersFilename = sharedParameterPath;
//打开共享参数文件
var definitionFile = doc.Application.OpenSharedParameterFile();
if (definitionFile == null)
{
ThrowNewException("无法打开共享参数文件");
return;
}
//获取共享参数组
var definitionGroups = definitionFile.Groups;
//获取参数组
var group = definitionGroups.get_Item(groupName) ?? definitionGroups.Create(groupName);
if (group == null)
{
ThrowNewException("创建共享参数组失败!");
return;
} //获取参数定义
var definition = group.Definitions.get_Item(definitionName);
if (definition == null)
{
var externalDefinitionCreationOptions = new ExternalDefinitionCreationOptions(definitionName, parameterType);
definition = group.Definitions.Create(externalDefinitionCreationOptions);
} ElementBinding binding;
if (isInstanceParameter)
{
binding = doc.Application.Create.NewInstanceBinding(categorySet);
}
else
{
binding = doc.Application.Create.NewTypeBinding(categorySet);
} bool isSuccess = doc.ParameterBindings.Insert(definition, binding, builtInParameterGroup); if (!isSuccess)
{
ThrowNewException("共享参数绑定失败!");
} //删除共享参数txt文件,从某种意义上讲,绑定之后的共享参数就变成了项目参数只针对当前项目存在
File.Delete(sharedParameterPath);
} public static void ThrowNewException(string msg)
{
throw new Exception(msg);
} }
}
上一篇:安装Arch Linux(桌面环境)


下一篇:request.querystring和request.form、session的区别