148 lines
6.9 KiB
C#
148 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.DB.Architecture;
|
|
using Autodesk.Revit.UI;
|
|
using Autodesk.Revit.UI.Selection;
|
|
using Sai.RvWrapper.Shared;
|
|
|
|
namespace SZBIM.StandardTools.ParameterManager
|
|
{
|
|
/// <summary>
|
|
/// 项目参数(共享参数)
|
|
/// </summary>
|
|
public class AddProjectParamsHandler : IExternalEventHandler
|
|
{
|
|
private string transName;
|
|
public List<ParamModel> ParameterList { get; set; }
|
|
public List<SharedParamModel> SharedParameters { get; set; }
|
|
|
|
public void Execute(UIApplication uiapp)
|
|
{
|
|
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
|
|
Document doc = uiapp.ActiveUIDocument.Document;
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
transName = "添加项目参数(共享参数)";
|
|
//IList<Reference> elementReferences = uidoc.Selection.PickObjects(ObjectType.Element,
|
|
// new TypesSelectionFilter(doc, typeof(FamilyInstance), typeof(HostObject), typeof(Railing)), "请选择需要添加参数的元素");
|
|
//var familyInstancesSelected = from reference in elementReferences
|
|
// select doc.GetElement(reference);
|
|
var references = uiapp.ActiveUIDocument.Selection.PickObjects(ObjectType.Element, new TypesSelectionFilter(typeof(FamilyInstance), typeof(HostObject), typeof(Railing)), "请选择需要添加参数的元素,并完成选择").ToList();
|
|
var instancesSelected = references.Select(refer => doc.GetElement(refer));
|
|
var categoryList = instancesSelected.GroupBy(ins => ins.Category.Id.IntegerValue)
|
|
.Select(g => g.FirstOrDefault()?.Category)
|
|
.ToList();
|
|
if (!instancesSelected.Any())
|
|
{
|
|
return;
|
|
}
|
|
|
|
SharedParamsUtil util = new SharedParamsUtil();
|
|
var originSharedParametersFile = uiapp.Application.SharedParametersFilename.Clone();
|
|
var tempFile = util.CreateTempSharedParameterFile(app, "TempSharedParameters");
|
|
doc.Invoke(ts =>
|
|
{
|
|
var g = tempFile.Groups.Create("TempGroup");
|
|
var paramDatas = ParameterList.Where(p => p.IsChecked == true);
|
|
foreach (ParamModel paramData in paramDatas)
|
|
{
|
|
bool isExist = util.IsExistSharedParam(doc, paramData.FullProperty, out Definition def, out ElementBinding binding);
|
|
//如果共享参数已经绑定了
|
|
if (isExist)
|
|
{
|
|
//将类别附加到当前已经存在的共享参数
|
|
foreach (Category category in categoryList.Where(category => !binding.Categories.Contains(category)))
|
|
{
|
|
binding.Categories.Insert(category);
|
|
doc.ParameterBindings.ReInsert(def, binding, BuiltInParameterGroup.PG_ADSK_MODEL_PROPERTIES);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (SharedParameters == null)
|
|
{
|
|
CategorySet categories = uiapp.Application.Create.NewCategorySet();
|
|
foreach (var category in categoryList)
|
|
{
|
|
categories.Insert(category);
|
|
}
|
|
|
|
//实例参数绑定
|
|
InstanceBinding newInstanceBinding = uiapp.Application.Create.NewInstanceBinding(categories);
|
|
|
|
var options =
|
|
new ExternalDefinitionCreationOptions(paramData.FullProperty, ParameterType.MultilineText)
|
|
{
|
|
GUID = new Guid(paramData.Guid)
|
|
};
|
|
try
|
|
{
|
|
var definition = g.Definitions.Create(options);
|
|
doc.ParameterBindings.Insert(definition, newInstanceBinding,
|
|
BuiltInParameterGroup.PG_ADSK_MODEL_PROPERTIES);
|
|
}
|
|
finally
|
|
{
|
|
uiapp.Application.SharedParametersFilename = originSharedParametersFile.ToString();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (var sharedParameter in SharedParameters)
|
|
{
|
|
if (sharedParameter.FullParamName == paramData.FullProperty)
|
|
{
|
|
CategorySet categories = uiapp.Application.Create.NewCategorySet();
|
|
|
|
foreach (var category in categoryList)
|
|
{
|
|
categories.Insert(category);
|
|
}
|
|
|
|
InstanceBinding newInstanceBinding =
|
|
uiapp.Application.Create.NewInstanceBinding(categories);
|
|
|
|
var definition = g.Definitions.get_Item(sharedParameter.FullParamName);
|
|
if (definition == null)
|
|
{
|
|
var options =
|
|
new ExternalDefinitionCreationOptions(sharedParameter.FullParamName,
|
|
ParameterType.MultilineText)
|
|
{
|
|
GUID = new Guid(sharedParameter.Guid)
|
|
};
|
|
definition = g.Definitions.Create(options);
|
|
}
|
|
|
|
doc.ParameterBindings.Insert(definition, newInstanceBinding,
|
|
BuiltInParameterGroup.PG_ADSK_MODEL_PROPERTIES);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
uiapp.Application.SharedParametersFilename = originSharedParametersFile.ToString();
|
|
}, transName);
|
|
File.Delete(tempFile.Filename);
|
|
if (string.IsNullOrEmpty(sb.ToString()))
|
|
{
|
|
TaskDialog.Show("提示", "添加完成!");
|
|
}
|
|
else
|
|
{
|
|
TaskDialog.Show("警告", sb.ToString());
|
|
}
|
|
}
|
|
|
|
public string GetName()
|
|
{
|
|
return transName;
|
|
}
|
|
}
|
|
} |