113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using Autodesk.Revit.Attributes;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.UI;
|
|
|
|
namespace Szmedi.Test
|
|
{
|
|
[Transaction(TransactionMode.Manual)]
|
|
[Regeneration(RegenerationOption.Manual)]
|
|
public class AddProjectParameter : IExternalCommand
|
|
{
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|
{
|
|
var uiApp = commandData.Application;
|
|
var doc = uiApp.ActiveUIDocument.Document;
|
|
|
|
// 定义参数名称和类型
|
|
var parameterName = "MyProjectParameter";
|
|
var parameterType = ParameterType.Text;
|
|
|
|
// 创建参数定义
|
|
using (var t = new Transaction(doc, "Add Project Parameter"))
|
|
{
|
|
t.Start();
|
|
|
|
// 创建参数定义
|
|
var options = new ExternalDefinitionCreationOptions(parameterName, parameterType);
|
|
var definitionFile = doc.Application.OpenSharedParameterFile();
|
|
if (definitionFile == null)
|
|
{
|
|
message = "No shared parameter file found.";
|
|
return Result.Failed;
|
|
}
|
|
|
|
var group = definitionFile.Groups.Create("MyGroup");
|
|
var definition = group.Definitions.Create(options);
|
|
|
|
// 绑定参数到所有类别
|
|
var categories = new CategorySet();
|
|
foreach (Category category in doc.Settings.Categories)
|
|
{
|
|
if (category.AllowsBoundParameters)
|
|
{
|
|
categories.Insert(category);
|
|
}
|
|
}
|
|
|
|
Binding binding = new InstanceBinding(categories);
|
|
doc.ParameterBindings.Insert(definition, binding);
|
|
|
|
t.Commit();
|
|
}
|
|
|
|
return Result.Succeeded;
|
|
}
|
|
|
|
private static void NewMethod(ExternalCommandData commandData)
|
|
{
|
|
var doc = commandData.Application.ActiveUIDocument.Document;
|
|
|
|
// 开启一个事务
|
|
using (var trans = new Transaction(doc, "Add Project Parameter"))
|
|
{
|
|
trans.Start();
|
|
|
|
// 获取项目参数定义的名称
|
|
var parameterName = "MyProjectParameter";
|
|
|
|
// 检查参数是否已经存在
|
|
var bindingMap = doc.ParameterBindings;
|
|
Definition parameterDefinition = null;
|
|
|
|
foreach (var obj in bindingMap)
|
|
{
|
|
Debug.WriteLine(obj);
|
|
if (obj is Definition def)
|
|
{
|
|
if (def.Name == parameterName)
|
|
{
|
|
parameterDefinition = def;
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// 如果参数不存在,创建一个新的参数定义
|
|
if (parameterDefinition == null)
|
|
{
|
|
var categories = new CategorySet();
|
|
categories.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Walls)); // 将参数应用于墙体
|
|
|
|
// 创建参数定义
|
|
var options = new ExternalDefinitionCreationOptions(parameterName, ParameterType.Text);
|
|
parameterDefinition = doc.Application.OpenSharedParameterFile().Groups.Create("CustomParameters").Definitions.Create(options);
|
|
|
|
// 绑定参数到项目
|
|
var binding = doc.Application.Create.NewInstanceBinding(categories);
|
|
doc.ParameterBindings.Insert(parameterDefinition, binding, BuiltInParameterGroup.PG_DATA);
|
|
}
|
|
|
|
trans.Commit();
|
|
}
|
|
}
|
|
}
|
|
}
|