243 lines
10 KiB
C#
243 lines
10 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data.Common;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Security.Cryptography;
|
|||
|
|
using System.Windows.Data;
|
|||
|
|
|
|||
|
|
using Autodesk.Revit.Attributes;
|
|||
|
|
using Autodesk.Revit.DB;
|
|||
|
|
using Autodesk.Revit.UI;
|
|||
|
|
using Autodesk.Revit.UI.Selection;
|
|||
|
|
|
|||
|
|
namespace ParameterResolve
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Revit执行命令
|
|||
|
|
/// </summary>
|
|||
|
|
[Transaction(TransactionMode.Manual)]
|
|||
|
|
[Regeneration(RegenerationOption.Manual)]
|
|||
|
|
public class ParameterEdit : IExternalCommand
|
|||
|
|
{
|
|||
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|||
|
|
{
|
|||
|
|
//程序UI界面
|
|||
|
|
UIApplication uiapp = commandData.Application;
|
|||
|
|
//获取元素(选择) 显示元素 视图(活动视图)管理(对象)
|
|||
|
|
UIDocument uidoc = uiapp.ActiveUIDocument;
|
|||
|
|
//程序
|
|||
|
|
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
|
|||
|
|
//获取位置和场地 视图(多个视图)管理 获取元素(Revit 项目里的全部元素)
|
|||
|
|
Document doc = uidoc.Document;
|
|||
|
|
//获取所有打开文档
|
|||
|
|
DocumentSet docset = uiapp.Application.Documents;
|
|||
|
|
//当前视图
|
|||
|
|
View view = doc.ActiveView;
|
|||
|
|
|
|||
|
|
var parameterElements = new FilteredElementCollector(doc)
|
|||
|
|
.OfClass(typeof(SharedParameterElement)).Cast<SharedParameterElement>()
|
|||
|
|
.Where(p => IsResolveParameter(p.Name)).ToList();
|
|||
|
|
var groups = parameterElements.GroupBy(p => p.Name).Where(g => g.Count() > 1).ToList();
|
|||
|
|
|
|||
|
|
using (Transaction transaction = new Transaction(doc, "删除重复参数"))
|
|||
|
|
{
|
|||
|
|
transaction.Start();
|
|||
|
|
foreach (var group in groups)
|
|||
|
|
{
|
|||
|
|
if (group.Key == "TC-100-宽度")
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
Definition firstDef = null;
|
|||
|
|
ElementBinding binding = null;
|
|||
|
|
CategorySet cateSet = app.Create.NewCategorySet();
|
|||
|
|
//所有的绑定
|
|||
|
|
var dict = GetDefinitionBindingDic(doc, group.Key);
|
|||
|
|
if (dict.Count == 0)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
firstDef = dict.FirstOrDefault().Key;
|
|||
|
|
binding = dict[firstDef];
|
|||
|
|
//if (dict.All(d => d.Key.ParameterType == ParameterType.Number))
|
|||
|
|
//{
|
|||
|
|
// firstDef = dict.First(d => d.Key.ParameterType == ParameterType.Number).Key;
|
|||
|
|
// binding = dict[firstDef];
|
|||
|
|
//}
|
|||
|
|
//else
|
|||
|
|
//{
|
|||
|
|
// firstDef = dict.First(d => d.Key.ParameterType == ParameterType.Text).Key;
|
|||
|
|
// binding = dict[firstDef];
|
|||
|
|
//}
|
|||
|
|
//所有的绑定的类别
|
|||
|
|
foreach (var item in dict)
|
|||
|
|
{
|
|||
|
|
foreach (Category cate in item.Value.Categories)
|
|||
|
|
{
|
|||
|
|
if (!cateSet.Contains(cate))
|
|||
|
|
{
|
|||
|
|
cateSet.Insert(cate);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//删除多余参数
|
|||
|
|
if (firstDef is InternalDefinition internalDefinition)
|
|||
|
|
{
|
|||
|
|
var idToDel = group.Where(p => p.GetDefinition().Id.IntegerValue != internalDefinition.Id.IntegerValue)
|
|||
|
|
.Select(p => p.Id)
|
|||
|
|
.ToList();
|
|||
|
|
doc.Delete(idToDel);
|
|||
|
|
}
|
|||
|
|
binding.Categories = cateSet;
|
|||
|
|
var res = doc.ParameterBindings.ReInsert(firstDef, binding);
|
|||
|
|
}
|
|||
|
|
transaction.Commit();
|
|||
|
|
}
|
|||
|
|
return Result.Succeeded;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void SystemCommonParameters(Autodesk.Revit.ApplicationServices.Application app, Document doc)
|
|||
|
|
{
|
|||
|
|
var parameterElements = new FilteredElementCollector(doc)
|
|||
|
|
.OfClass(typeof(SharedParameterElement)).Cast<SharedParameterElement>()
|
|||
|
|
.Where(p => IsResolveParameter(p.Name)).ToList();
|
|||
|
|
//存在则使用现有项目参数
|
|||
|
|
using (Transaction transaction = new Transaction(doc, "关联项目参数"))
|
|||
|
|
{
|
|||
|
|
transaction.Start();
|
|||
|
|
var cateSet = app.Create.NewCategorySet();
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Walls));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Floors));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Roofs));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Ceilings));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_StairsRailing));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Ramps));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Stairs));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_DuctCurves));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_PipeCurves));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Conduit));
|
|||
|
|
cateSet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_CableTray));
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
foreach (var parameterElement in parameterElements)
|
|||
|
|
{
|
|||
|
|
var exist = IsExistProjectParam(doc, parameterElement.Name, out var def, out ElementBinding binding);
|
|||
|
|
//存在但未关联到项目信息
|
|||
|
|
//if (!foundBinding.Categories.Contains(Category.GetCategory(doc, BuiltInCategory.OST_ProjectInformation)))
|
|||
|
|
//{
|
|||
|
|
// foundBinding.Categories.Insert(Category.GetCategory(doc, BuiltInCategory.OST_ProjectInformation));
|
|||
|
|
// doc.ParameterBindings.ReInsert(firstDef, foundBinding, BuiltInParameterGroup.PG_IDENTITY_DATA);
|
|||
|
|
//}
|
|||
|
|
if (exist && binding != null)
|
|||
|
|
{
|
|||
|
|
binding.Categories = cateSet;
|
|||
|
|
var res = doc.ParameterBindings.ReInsert(def, binding);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
transaction.Commit();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool IsResolveParameter(string paramName)
|
|||
|
|
{
|
|||
|
|
var array = paramName.Split('-');
|
|||
|
|
if (array.Length >= 3)
|
|||
|
|
{
|
|||
|
|
var prefix = array[0].Trim();
|
|||
|
|
if (prefix is ("ID" or "LC" or "ST" or "GJ" or "MF" or "AM" or "FM" or "TM" or "CM" or "TC"))
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 是否存在该项目参数
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="doc"></param>
|
|||
|
|
/// <param name="paramName"></param>
|
|||
|
|
/// <param name="definition"></param>
|
|||
|
|
/// <param name="binding"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool IsExistProjectParam(Document doc, string paramName, out Definition definition, out ElementBinding binding)
|
|||
|
|
{
|
|||
|
|
DefinitionBindingMap map = doc.ParameterBindings;
|
|||
|
|
var dep = map.ForwardIterator();
|
|||
|
|
var isExist = false;
|
|||
|
|
binding = null;
|
|||
|
|
definition = null;
|
|||
|
|
while (dep.MoveNext())
|
|||
|
|
{
|
|||
|
|
definition = dep.Key;
|
|||
|
|
|
|||
|
|
if (definition.Name == paramName)
|
|||
|
|
{
|
|||
|
|
isExist = true;
|
|||
|
|
binding = dep.Current as ElementBinding;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return isExist;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 查找共享参数元素的项目参数的定义和绑定
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="doc"></param>
|
|||
|
|
/// <param name="paramName"></param>
|
|||
|
|
/// <param name="foundDefinition"></param>
|
|||
|
|
/// <param name="foundBinding"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool TryGetDefinitionBinding(Document doc, SharedParameterElement param, out Definition foundDefinition, out ElementBinding foundBinding, ParameterType pt = ParameterType.Text)
|
|||
|
|
{
|
|||
|
|
DefinitionBindingMap map = doc.ParameterBindings;
|
|||
|
|
var dep = map.ForwardIterator();
|
|||
|
|
foundDefinition = null;
|
|||
|
|
foundBinding = null;
|
|||
|
|
bool isExist = false;
|
|||
|
|
while (dep.MoveNext())
|
|||
|
|
{
|
|||
|
|
foundDefinition = dep.Key;
|
|||
|
|
if (foundDefinition is InternalDefinition internalDefinition && internalDefinition.Id == param.GetDefinition().Id && foundDefinition.Name == param.Name)
|
|||
|
|
{
|
|||
|
|
isExist = true;
|
|||
|
|
foundBinding = dep.Current as ElementBinding;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return isExist;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 找到同名的项目参数的定义和绑定
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="doc"></param>
|
|||
|
|
/// <param name="paramName"></param>
|
|||
|
|
/// <param name="definition"></param>
|
|||
|
|
/// <param name="binding"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static Dictionary<Definition, ElementBinding> GetDefinitionBindingDic(Document doc, string paramName)
|
|||
|
|
{
|
|||
|
|
Dictionary<Definition, ElementBinding> dic = new Dictionary<Definition, ElementBinding>();
|
|||
|
|
DefinitionBindingMap map = doc.ParameterBindings;
|
|||
|
|
var dep = map.ForwardIterator();
|
|||
|
|
ElementBinding binding = null;
|
|||
|
|
Definition definition = null;
|
|||
|
|
while (dep.MoveNext())
|
|||
|
|
{
|
|||
|
|
definition = dep.Key;
|
|||
|
|
|
|||
|
|
if (definition.Name == paramName)
|
|||
|
|
{
|
|||
|
|
binding = dep.Current as ElementBinding;
|
|||
|
|
dic.Add(definition, binding);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return dic;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|