C# Revit API,如何使用 ExternalCommand 创建一个简单的墙?

吉福

我只是想学习 Revit API 并使用 ExternalCommand 创建一个简单的墙。但我无法弄清楚......我认为我的问题在这里:

var symbolId = document.GetDefaultFamilyTypeId(new ElementId(BuiltInCategory.OST_Walls));

当我调试它时symbolId总是-1。

你能帮我这个代码片段有什么问题吗?

public Autodesk.Revit.UI.Result Execute(
    Autodesk.Revit.UI.ExternalCommandData command_data,
    ref string message,
    Autodesk.Revit.DB.ElementSet elements)
{
    var document = command_data.Application.ActiveUIDocument.Document;

    var level_id = new ElementId(1526);
    // create line
    XYZ point_a = new XYZ(-10, 0, 0);
    XYZ point_b = new XYZ(10, 10, 10);
    Line line = Line.CreateBound(point_a, point_b);

    using (var transaction = new Transaction(doc))
    {
        transaction.Start("create walls");

        Wall wall = Wall.Create(doc, line, level_id, false);
        var position = new XYZ(0, 0, 0);
        var symbolId = document.GetDefaultFamilyTypeId(new ElementId(BuiltInCategory.OST_Walls));
        if (symbolId == ElementId.InvalidElementId) {
            transaction.RollBack();
            return Result.Failed;
        }

        var symbol = document.GetElement(symbolId) as FamilySymbol;
        var level = (Level)document.GetElement(wall.LevelId);
        document.Create.NewFamilyInstance(position, symbol, wall, level, StructuralType.NonStructural);

        transaction.Commit();
    }

    return Result.Succeeded;
}
杰里米·塔米克

完成Revit API 入门材料,所有内容都将得到解释。这将为您和其他人节省更多的问题和答案。

无论如何,要解决这个特定问题,GetDefaultFamilyTypeId大概不会对墙元素做您期望的事情。GetDefaultFamilyTypeId方法 API 文档中,它用于结构柱,这是由单个 RFA 文件托管的标准可加载族。墙是内置系统族,行为不同。也许GetDefaultFamilyTypeId只适用于非系统家庭。

要检索任意(非默认)墙类型,请使用过滤元素收集器检索所有WallType元素并选择您找到的第一个元素。

以下是从The Building Coder讨论中关于创建面墙和体量楼层选择具有特定名称的第一个代码段的代码片段

WallType wType = new FilteredElementCollector( doc )
  .OfClass( typeof( WallType ) )
  .Cast<WallType>().FirstOrDefault( q
    => q.Name == "Generic - 6\" Masonry" );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章