179 lines
7.3 KiB
C#
179 lines
7.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.DB.Architecture;
|
|
using Autodesk.Revit.UI;
|
|
using Sai.RvWrapper.Shared;
|
|
|
|
namespace SZBIM.StandardTools.ParameterManager
|
|
{
|
|
public class RemoveParamsEventHandler : IExternalEventHandler
|
|
{
|
|
public void Execute(UIApplication uiapp)
|
|
{
|
|
//获取元素(选择) 显示元素 视图(活动视图)管理(对象)
|
|
UIDocument uidoc = uiapp.ActiveUIDocument;
|
|
//程序
|
|
Document doc = uidoc.Document;
|
|
//获取所有打开文档
|
|
FilteredElementCollector collector = new FilteredElementCollector(doc);
|
|
ElementClassFilter instanceFilter = new ElementClassFilter(typeof(FamilyInstance));
|
|
ElementClassFilter hostobjectFilter = new ElementClassFilter(typeof(HostObject));
|
|
ElementClassFilter railingFilter = new ElementClassFilter(typeof(Railing));
|
|
IList<ElementFilter> filters = new List<ElementFilter>
|
|
{
|
|
instanceFilter,
|
|
hostobjectFilter,
|
|
railingFilter
|
|
};
|
|
LogicalOrFilter orFilter = new LogicalOrFilter(filters);
|
|
collector.WherePasses(orFilter);
|
|
var elements = collector.ToElements();
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
using (TransactionGroup tg = new TransactionGroup(doc, "清理轨交标准参数"))
|
|
{
|
|
tg.Start();
|
|
|
|
|
|
foreach (Element element in elements)
|
|
{
|
|
var paramMap = element.ParametersMap;
|
|
using (Transaction trans = new Transaction(doc, "清理项目参数绑定"))
|
|
{
|
|
trans.Start();
|
|
foreach (Parameter param in paramMap)
|
|
{
|
|
if (Regex.IsMatch(param.Definition.Name, @"^[A-Z][A-Z](-)\d(00-)"))
|
|
{
|
|
if (param.IsShared)
|
|
{
|
|
uidoc.Document.ParameterBindings.Remove(param.Definition);
|
|
}
|
|
//if (param.IsShared)
|
|
//{
|
|
|
|
// var binding = doc.ParameterBindings.get_Item(param.Definition);
|
|
// if (binding is TypeBinding)
|
|
// {
|
|
// var typeBinding = (TypeBinding)binding;
|
|
|
|
// try
|
|
// {
|
|
|
|
// if (typeBinding.Categories.Size == 1)
|
|
// {
|
|
// uidoc.Document.ParameterBindings.Remove(param.Definition);
|
|
// }
|
|
// typeBinding.Categories.Erase(element.Category);
|
|
|
|
// }
|
|
// catch (Exception e)
|
|
// {
|
|
// sb.AppendLine(e.Message);
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (binding is InstanceBinding)
|
|
// {
|
|
// var instanceBinding = (InstanceBinding)binding;
|
|
// try
|
|
// {
|
|
// if (instanceBinding.Categories.Size == 1)
|
|
// {
|
|
// uidoc.Document.ParameterBindings.Remove(param.Definition);
|
|
// }
|
|
// instanceBinding.Categories.Erase(element.Category);
|
|
// }
|
|
// catch (Exception e)
|
|
// {
|
|
// sb.AppendLine(e.Message);
|
|
// }
|
|
// }
|
|
//}
|
|
}
|
|
}
|
|
|
|
trans.Commit();
|
|
}
|
|
|
|
bool isNeedEditFamily = false;
|
|
foreach (Parameter param in paramMap)
|
|
{
|
|
if (Regex.IsMatch(param.Definition.Name, @"^[A-Z][A-Z](-)\d(00-)"))
|
|
{
|
|
isNeedEditFamily = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (isNeedEditFamily && element is FamilyInstance instance)
|
|
{
|
|
Document famdoc = null;
|
|
try
|
|
{
|
|
Family family = instance.Symbol.Family;
|
|
|
|
if (family.IsEditable)
|
|
{
|
|
famdoc = doc.EditFamily(family);
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine($"{family.Name} 不可编辑。");
|
|
}
|
|
|
|
if (famdoc != null)
|
|
{
|
|
using (Transaction ts = new Transaction(famdoc, "删除参数"))
|
|
{
|
|
ts.Start();
|
|
var familyParameters = famdoc.FamilyManager.GetParameters()
|
|
.Where(fp => Regex.IsMatch(fp.Definition.Name, @"^[A-Z][A-Z](-)\d(00-)"));
|
|
foreach (var fp in familyParameters)
|
|
{
|
|
famdoc.FamilyManager.RemoveParameter(fp);
|
|
}
|
|
|
|
ts.Commit();
|
|
}
|
|
|
|
famdoc.LoadFamily(doc, new LoadFamilyOptions());
|
|
famdoc.Close(false);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
sb.AppendLine(famdoc?.Title + "发生错误!");
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(sb.ToString()))
|
|
{
|
|
TaskDialog.Show("错误", sb.ToString());
|
|
}
|
|
|
|
tg.Assimilate();
|
|
}
|
|
}
|
|
|
|
public string GetName()
|
|
{
|
|
return "清理轨交参数";
|
|
}
|
|
|
|
private void PerformDeleteParameters()
|
|
{
|
|
var result = TaskDialog.Show("警告", "是否清理所有轨道交通标准参数?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
|
|
|
|
if (result == TaskDialogResult.Yes)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
} |