Revit 共享参数
程序员文章站
2022-04-02 09:38:01
...
一
public void GouJianHua()
{
string PathName = @"D:\遍历保存\临时文件\共享参数\";
ICollection<ElementId> eElementId = GuolvSelect.GetViewElementId(doc);
DefinitionFile definitionFile = null;
List<SharingModel> sharinglist = new List<SharingModel>();
PathName = PathName+ doc.Title+".txt";
//提取族参数
foreach (var eId in eElementId)
{
definitionFile = new SharingData().CreaterSharingTxt(uiDoc, PathName);
SharingModel sharingModel = new SharingModel();
Element ele = doc.GetElement(eId);
Category category = ele.Category;
sharingModel.IntegerValue = category.Id.IntegerValue;
sharingModel.BuiltInCategory = (BuiltInCategory)category.Id.IntegerValue;
sharingModel.Group = ele.GetType().Name;
List<CanShu> canShu = new List<CanShu>();
foreach (Parameter parameter in ele.Parameters)
{
if (((InternalDefinition)parameter.Definition).ParameterGroup == BuiltInParameterGroup.INVALID)
{
continue;
}
if (((InternalDefinition)parameter.Definition).ParameterGroup == BuiltInParameterGroup.PG_PHASING)
{
continue;
}
CanShu canShuModel = new CanShu();
canShuModel.Name = parameter.Definition.Name;
canShuModel.AsString = parameter.AsString();
canShuModel.AsValueString = parameter.AsValueString();
canShuModel.AsInteger = parameter.AsInteger();
canShuModel.AsDouble = parameter.AsDouble();
canShuModel.AsElementId = parameter.AsElementId();
canShuModel.ParameterGroup = ((InternalDefinition)parameter.Definition).ParameterGroup;
canShu.Add(canShuModel);
}
sharingModel.canShu = canShu;
sharinglist.Add(sharingModel);
//创建共享参数
new SharingData().CreateSharingData(definitionFile, sharingModel);
}
//获取需要写入共享参数的集合
string sType = "DirectShape";
IList<Element> elList = GuolvSelect.GetSelectFamily(doc, sType);
//共享写入项目
foreach (SharingModel sharingModel in sharinglist)
{
new SharingData().GetSharing(doc, sharingModel);
//共享写入参数
foreach (var item in elList)
{
SharingModel sharingmodel = sharinglist.Where(w => w.nEweId == item.Id).FirstOrDefault();
if (sharingmodel != null)
{
new SharingData().SetCanShu(item.Parameters, doc, sharingmodel);
continue;
}
}
}
}
二
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using BYYJ.RevitTool.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BYYJ.RevitTool
{
public class SharingData
{
/// <summary>
/// 创建共享文本
/// </summary>
/// <param name="uidoc"></param>
/// <param name="PathName"></param>
/// <returns></returns>
public DefinitionFile CreaterSharingTxt(UIDocument uidoc,string PathName)
{
FileHelper.CreateTxt(PathName);
Autodesk.Revit.ApplicationServices.Application RevitApp = uidoc.Application.Application;
RevitApp.SharedParametersFilename= PathName;
DefinitionFile definitionFile = RevitApp.OpenSharedParameterFile();
return definitionFile;
}
/// <summary>
/// 创建共享参数
/// </summary>
/// <param name="definitionFile"></param>
/// <param name="Sharing"></param>
public Definition CreateSharingData(DefinitionFile definitionFile, SharingModel Sharing)
{
//获取参数组的集合
DefinitionGroups groups = definitionFile.Groups;
//获取参数组
DefinitionGroup group = groups.get_Item(Sharing.Group);
Definition definition = null;
if (null == group)
{
group = groups.Create(Sharing.Group);
}
foreach (var item in Sharing.canShu)
{
definition = group.Definitions.get_Item(item.Name);
ExternalDefinitionCreationOptions edco = new ExternalDefinitionCreationOptions(item.Name, ParameterType.Text);// ParameterType.Text 为共享参数类型
if (definition == null)
{
definition = group.Definitions.Create(edco);
}
item.definition = definition;
}
//获取参数的定义
return definition;
}
/// <summary>
/// 项目实现共享参数
/// </summary>
/// <param name="doc"></param>
/// <param name="definition"></param>
/// <param name="Sharing"></param>
public void GetSharing(Document doc, SharingModel Sharing)
{
foreach (var item in Sharing.canShu)
{
if (item.Name == "注释" || item.Name == "标记")
{
continue;
}
CategorySet categories = new CategorySet();
categories.Insert(doc.Settings.Categories.get_Item((BuiltInCategory)Sharing.IntegerValue));// (BuiltInCategory)Sharing.IntegerValu为Category 中的 BuiltInCategory 值
ElementBinding binding = new InstanceBinding(categories);
Transaction tran = new Transaction(doc, "BIM");
tran.Start();
doc.ParameterBindings.Insert(item.definition, binding, item.ParameterGroup);// item.ParameterGroup为Category 中的((InternalDefinition)parameter.Definition).ParameterGroup; 值
tran.Commit();
}
}
/// <summary>
/// 参数赋值
/// </summary>
/// <param name="parameterSet"></param>
/// <param name="doc"></param>
/// <param name="Sharing"></param>
public void SetCanShu(ParameterSet parameterSet, Document doc, SharingModel Sharing)
{
Transaction ta = new Transaction(doc, "BIM");
ta.Start();
foreach (Parameter parameter in parameterSet)
{
CanShu CModel = Sharing.canShu.Where(w => w.Name == parameter.Definition.Name).FirstOrDefault();
if (CModel!=null)
{
if (parameter.IsReadOnly==false)
{
parameter.SetValueString(CModel.AsValueString);
parameter.Set(CModel.AsString);
parameter.Set(CModel.AsDouble);
parameter.Set(CModel.AsElementId);
parameter.Set(CModel.AsInteger);
}
}
}
ta.Commit();
}
}
}
三
public class SharingModel
{
/// <summary>
/// 共享参数组
/// </summary>
public string Group { get; set; }
/// <summary>
/// 共享参数组类型
/// </summary>
public BuiltInParameterGroup ParameterGroup { get; set; }
/// <summary>
/// 需要赋值参数的实例
/// </summary>
public BuiltInCategory BuiltInCategory { get; set; }
/// <summary>
/// 需要赋值参数的实例 枚举Id
/// </summary>
public int IntegerValue { get; set; }
/// <summary>
/// 组参数集合
/// </summary>
public List<CanShu> canShu { get; set; }
/// <summary>
/// 新建构建化eId
/// </summary>
public ElementId nEweId { get; set; }
}
public class CanShu
{
/// <summary>
/// 所创建的共享参数
/// </summary>
public Definition definition { get; set; }
/// <summary>
/// 自定义参数名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 共享参数值 string
/// </summary>
public string AsString { get; set; }
/// <summary>
/// 共享参数值 string
/// </summary>
public string AsValueString { get; set; }
/// <summary>
/// 共享参数值 int
/// </summary>
public int AsInteger { get; set; }
/// <summary>
/// 共享参数值 double
/// </summary>
public double AsDouble { get; set; }
/// <summary>
/// 共享参数值 ElementId
/// </summary>
public ElementId AsElementId { get; set; }
/// <summary>
/// 共享参数组类型
/// </summary>
public BuiltInParameterGroup ParameterGroup { get; set; }
}
上一篇: php语言用什么软件写
下一篇: CAPI函数描述(A-F)