Revit二次开发 族参数的获取

 

本文章代码写的是对族参数的获取,并转成DataTable可作为数据源直接绑定在form窗体控件上

流程思路为:

1.在固定位置找到族文件,加载到项目中

2.读取族文件中所有类型:Get_FamilySymbol(Family family);

3.获取族文件参数: GetParameter(Element e, string header)

4. 获取族文件中所有类型参数:getSymbolpara()

5.将族类型信息参数转成DataTable表格:ShowAllBuildinPara()

6.卸载族文件

using System.Collections.Generic;
using System.Data;
using System.Text;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace ceshi
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
  Family family = null;
  public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  {
    Document doc = commandData.Application.ActiveUIDocument.Document;
    string s = @"C:\xx.rfa"; 
    Transaction tran = new Transaction(doc);
    tran.Start("载入族");
    bool b = doc.LoadFamily(s,out family);
    tran.Commit();
    if (b){TaskDialog.Show("11","加载成功");} 
    Get_FamilySymbol(family);//获取族类型
    Element element = family as Element;
    GetParameter(element,"11");//获取族文件参数
    getSymbolpara();//获取族类型参数
    ShowAllBuildinPara();//将族类型信息参数转成table表格
    Transaction trandelete = new Transaction(doc);//创建事物
    trandelete.Start("删除族");//开启事物 
    trandelete.Commit();//关闭事物
    return Result.Succeeded;
} 
public void Get_FamilySymbol(Family family)//获取族类型
{
StringBuilder message = new StringBuilder("选择的族文件名称: " + family.Name+ "\n " + "\n ");
ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds(); 
if (familySymbolIds.Count == 0){message.AppendLine("Contains no family symbols.");}
else
{
message.AppendLine("文件中有以下类型 : ");
foreach (ElementId id in familySymbolIds)
{
FamilySymbol familySymbol = family.Document.GetElement(id) as FamilySymbol; 
message.AppendLine("\nName: " + familySymbol.Name);
foreach (ElementId materialId in familySymbol.GetMaterialIds(false))
{
Material material = familySymbol.Document.GetElement(materialId) as Material;
message.AppendLine("\nMaterial : " + material.Name);
}
}
}
TaskDialog.Show("Revit", message.ToString());
}
public void GetParameter(Element e, string header)//获取族文件参数
{
StringBuilder sb = new StringBuilder();//得到所有的枚举名称
string[] strs = System.Enum.GetNames(typeof(BuiltInParameter));
foreach (string str in strs)
{
BuiltInParameter paramEnum =(BuiltInParameter)System.Enum.Parse(typeof(BuiltInParameter), str);// 查看枚举名称对应的BuiltInParameter
Parameter tmpParam = e.get_Parameter(paramEnum);// 通过BuiltInParameter的到参数
if (tmpParam != null)
{
string paraName = "";
paraName = tmpParam.Definition.Name;
string val = tmpParam.AsValueString();
sb.Append(e.Name + ":\t" + str + "(" + paraName + ")" + " \t= \t" + val + "\r\n");
}
}
TaskDialog.Show(header, sb.ToString());
}
public void getSymbolpara()//获取族类型参数
{
ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
StringBuilder message = new StringBuilder("选择的族文件名称: " + family.Name );
foreach (ElementId id in familySymbolIds)
{
FamilySymbol familySymbol = family.Document.GetElement(id) as FamilySymbol;
message.AppendLine("\nName: " + familySymbol.Name);
foreach (Parameter p in familySymbol.Parameters)
{ 
string sname= p.Definition.Name;
string svalue = p.AsValueString();
message.AppendLine(sname+" : "+svalue);
}
}
TaskDialog.Show("Revit", message.ToString());
}
public void ShowAllBuildinPara()//将族类型信息参数转成table表格
{
ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
ElementId[] ids = new ElementId[familySymbolIds.Count];
familySymbolIds.CopyTo(ids, 0);
var id = ids[0];
DataTable dataTable = new DataTable();
FamilySymbol familySymbol = family.Document.GetElement(id) as FamilySymbol;
dataTable.Columns.Add("类型名称");
foreach (Parameter p in familySymbol.Parameters)
{
if (p.Definition.Name == "类别"||p.Definition.Name == "类型名称")
continue;
dataTable.Columns.Add(p.Definition.Name);
}
foreach (ElementId rowId in ids)
{
FamilySymbol symbol = family.Document.GetElement(rowId) as FamilySymbol;
DataRow row = dataTable.NewRow();
row["类型名称"] = symbol.Name;
foreach (Parameter p in symbol.Parameters)
{
if (p.Definition.Name == "类别"||p.Definition.Name == "类型名称")
continue;
row[p.Definition.Name] = p.AsValueString();
}
dataTable.Rows.Add(row);
}
}
}
}

 

posted @ 2020-06-19 11:05  廖先生  阅读(1932)  评论(0编辑  收藏  举报