版权声明:此文由黑夜の骑士创作,转载请注明出处,交流qq1056291511 https://blog.csdn.net/birdfly2015/article/details/90299545
一.背景
小伙伴们在revit二次开发时,可能需要在族环境中获得所有的族类型,然后对其一波操作
二.思路
1.打开族文件.参看注释1
2.使用族管理器FamilyManager,参看注释2
3.获得此族所有的族类型FamilyTypeSet,参看注释3
4.操作所有族类型或者某个族类型的方法,参看注释4、5
5.保存,关闭此族文档,参看注释6、7
三.代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
namespace heiyedeqishi
{
[Transaction(TransactionMode.Manual)]
class Revit_API_Executable1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document document = uidoc.Document;
//1族文件地址,这样我们就能打开族文档,并且获得这个文档的document
string filePath = @"D:\黑夜的骑士\某个族.rfa";
Document doc = document.Application.OpenDocumentFile(filePath);
//2获得族管理器
FamilyManager familyManager = doc.FamilyManager;
try
{
//因为修改族是对revit内部数据进行修改,所以需要开启事物
Transaction trans = new Transaction(doc, "修改族类型");
trans.Start();
//3得到当前所有的族类型集合
FamilyTypeSet familyTypeSet = doc.FamilyManager.Types;
//4需要对所有族类型进行操作的话
foreach (FamilyType familyType in familyTypeSet)
{
//设置当前操作对象为此族类型
familyManager.CurrentType = familyType;
//小伙伴们的精彩操作
//...
}
//5从所有族类型中获取某个族类型,假设名字为"墙1"
string singleFamilySymbolName = "墙1";
foreach (FamilyType familyType in familyTypeSet)
{
if (familyType.Name == "墙1")
{
//将此族类型设置为当前的操作对象
doc.FamilyManager.CurrentType = familyType;
//小伙伴们的精彩操作
//...
}
}
//事物结束
trans.Commit();
//6对文档进行保存
doc.Save();
//7关闭打开的这个文档
doc.Close();
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
}
四、注意事项
在项目环境,或者模板环境,我们是可以通过FamilySymbol来进行族的相关操作的,但是在族环境中,使用FamilyType会很方便