码迷,mamicode.com
首页 > 其他好文 > 详细

Revit二次开发 族参数的获取

时间:2020-06-19 11:52:17      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:sys   table   message   active   二次   mod   ==   symbols   data   

 

本文章代码写的是对族参数的获取,并转成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);
}
}
}
}

 

Revit二次开发 族参数的获取

标签:sys   table   message   active   二次   mod   ==   symbols   data   

原文地址:https://www.cnblogs.com/liaocheng/p/13161914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!