public static void CreateSharedParm(Document doc, string paramGroup, string paramName, ParameterType paramType, List<ElementId> listCategoryId, BuiltInParameterGroup type)
{
// 开始事务
using (Transaction transaction = new Transaction(doc, "Create Shared Parameter"))
{
transaction.Start();
// 获取创建共享参数的txt路径
string txtFile = doc.Application.SharedParametersFilename;
// 判断路径是否有效,如果为空,创建txt文件将路径赋值给app.SharedParametersFilename
if (!string.IsNullOrEmpty(txtFile))
{
if (!File.Exists(txtFile))
{
System.IO.StreamWriter sw = System.IO.File.CreateText(txtFile);
sw.Close();
}
DefinitionFile dfile = doc.Application.OpenSharedParameterFile();
DefinitionGroup dg = dfile.Groups.get_Item(paramGroup);
if (dg == null)
dg = dfile.Groups.Create(paramGroup); // 创建一个共享参数分组
Definition df = dg.Definitions.get_Item(paramName);
if (df == null)
{
// 参数创建的选项,包括参数名字,参数类型,用户是不是可以修改。。
ExternalDefinitionCreationOptions edco = new ExternalDefinitionCreationOptions(paramName, paramType);
// 创建参数
df = dg.Definitions.Create(edco);
}
BindingMap bmap = doc.ParameterBindings;
if (!bmap.Contains(df))
{
CategorySet cateSet = doc.Application.Create.NewCategorySet();
// 在Category集合中加入所有的category
foreach (ElementId categoryId in listCategoryId)
{
Category loadCate = Category.GetCategory(doc, categoryId);
// 在Category集合中加入线荷载的category
bool flag = cateSet.Insert(loadCate);
}
InstanceBinding loadInsBd = doc.Application.Create.NewInstanceBinding(cateSet);
bmap.Insert(df, loadInsBd, type);
}
}
// 提交事务
transaction.Commit();
}
}
在Revit API中,对于对模型进行更改(包括创建参数绑定)的操作需要在事务(Transaction)中执行。
RevitAPI中的事务管理:创建共享参数与绑定
该代码片段展示了如何在RevitAPI中使用事务处理创建共享参数,包括定义参数名、类型、权限,并将参数绑定到特定类别。它强调了在模型更改操作中使用Transaction的重要性。
995

被折叠的 条评论
为什么被折叠?



