简体   繁体   English

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

[英]C# Revit API, how to create a simple wall using ExternalCommand?

I just wanted to learn Revit API and create a simple wall using ExternalCommand. 我只是想学习Revit API并使用ExternalCommand创建简单的墙。 But I cannot figure it out... I think my problem is here: 但是我无法弄清楚...我认为我的问题在这里:

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

When I debug it symbolId always -1. 当我调试它时, symbolId始终为-1。

Can you help me what is wrong with this code snippet? 您能帮我解决此代码段的问题吗?

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;
}

Work through the Revit API getting started material and all will be explained. 通过Revit API入门材料进行工作,并将对所有内容进行说明。 That will save you and others many further questions and answers. 这将为您和其他人节省许多其他的问题和答案。

To address this specific question anyway, GetDefaultFamilyTypeId presumably does not do what you expect it to for wall elements. 无论如何要解决此特定问题, GetDefaultFamilyTypeId可能不会像您期望的那样对墙元素执行任何操作。 In the GetDefaultFamilyTypeId method API documentation , it is used for structural columns, a standard loadable family hosted by individual RFA files. GetDefaultFamilyTypeId方法API文档中 ,它用于结构化列,这是由单个RFA文件托管的标准可加载族。 Walls are built-in system families and behave differently. 墙是内置的系统系列,其行为有所不同。 Maybe GetDefaultFamilyTypeId only works for non-system families. 也许GetDefaultFamilyTypeId仅适用于非系统系列。

To retrieve an arbitrary (not default) wall type, use a filtered element collector to retrieve all WallType elements and pick the first one you find. 要检索任意(非默认)墙类型,请使用过滤的元素收集器来检索所有WallType元素,并选择找到的第一个元素。

Here is a code snippet that picks the first one with a specific name, from The Building Coder discussion on Creating Face Wall and Mass Floor : 这是一个代码段,该代码段摘自The Building Coder讨论的关于创建墙面和地板的讨论:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM