RevitAPI: 新建系统族类型并创建族实例

昨天有客户问到如何通过API实现下面的步骤:

1. 新建一个系统族,并修改参数

2. 使用该系统族创建一个族实例,例如创建墙。

回答:

1. 新建族实例我们可以使用ElementType.Duplicate(string)方法,因为系统族是不允许用户创建的,我们只能复制一个。

2. 创建系统族可以使用形如Wall.Create方法或者Document.Create.NewFloor这样的方法。

下面是复制墙类型并创建墙的例子:

public static Document RevitDoc;
public static Autodesk.Revit.ApplicationServices.Application RevitApp;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    RevitApp = commandData.Application.Application;
    var uiDoc = commandData.Application.ActiveUIDocument;
    if (uiDoc == null)
    {
        message = "Please open a document";
        return Result.Failed;
    }

    RevitDoc = commandData.Application.ActiveUIDocument.Document;
    var uiSel = commandData.Application.ActiveUIDocument.Selection;

    Transaction transaction = new Transaction(RevitDoc, "TestScript.CreateNewInstanceWithNewType");
    transaction.Start();
    try
    {
        var typeFilter = new ElementClassFilter(typeof(WallType));
        FilteredElementCollector wallTypes = new FilteredElementCollector(RevitDoc);
        wallTypes = wallTypes.WherePasses(typeFilter);
        WallType walltype = null;
        foreach (WallType wallType in wallTypes)
        {
            walltype = wallType;
            break;
        }
        if (walltype != null)
        {
            //创建新的墙类型,使用Duplicate方法
            var newtype = walltype.Duplicate(walltype.Name + "_new");

            //可以在这里修改墙类型的参数,例如修改吸收率
            var para = walltype.get_Parameter(BuiltInParameter.ANALYTICAL_ABSORPTANCE);
            if (para != null && para.StorageType == StorageType.Double && para.IsReadOnly == false)
            {
                para.Set(2.0);
            }

            //找到一个标高
            Level level = null;
            var levelFilter = new ElementClassFilter(typeof(Level));
            FilteredElementCollector levels = new FilteredElementCollector(RevitDoc);
            levels = levels.WherePasses(levelFilter);
            foreach (Level element in levels)
            {
                level = element;
                break;
            }

            //创建墙
            var wall = Wall.Create(RevitDoc, Line.CreateBound(new XYZ(0, 0, 0), new XYZ(10, 0, 0)), 
                newtype.Id, level.Id, 20, 0, false, false);
            TaskDialog.Show("wall creation", "wall created, id = " + wall.Id);
        }
        transaction.Commit();
    }
    catch (Exception ex)
    {
        message = ex.ToString();
        transaction.RollBack();
        return Result.Failed;
    }

    return Result.Succeeded;
}


你可能感兴趣的:(Family,RevitAPI)