应用程序:StructSample

Revit平台:所有

Revit版本:2011.0

首次发布:8.0

编程语言:C#

技能水平:初级

类别:结构

类型:外部命令

主题:柱子创建和放置

摘要:

该示例演示了如何在所选墙壁中放置一组柱子。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.ElementSet

Autodesk.Revit.DB.Wall

Autodesk.Revit.DB.Parameter

Autodesk.Revit.DB.BuiltInParameter

Autodesk.Revit.DB.FamilyInstance

Autodesk.Revit.DB.Family

Autodesk.Revit.DB.Symbols.FamilySymbol

Autodesk.Revit.DB.Geometry.XYZ

Autodesk.Revit.DB.Geometry.UV

Autodesk.Revit.DB.Geometry.Line

as finding one specialized column symbol from Family and placing column.

项目文件:

Command.cs

该文件包含继承自IExternalCommand接口并实现Execute方法的Command类。该类实现了将柱子放置到墙壁的所有函数,例如从Family中查找一个专用柱子符号并放置柱子。

描述:

该示例主要使用FamilyInstanceFamilyFamilySymbol方法来查找一个专用柱子;然后将该柱子放置到一些选择的墙壁上,距离也都是固定的。(注意:柱子类型和两个柱子之间的距离是硬编码的)。

- 要检索一个Family的所有符号,使用 Autodesk.Revit.Elements.Family.Symbols 属性,并使用 Autodesk.Revit.Symbols.FamilySymbol.Name 来获取专用的柱子类型。

- 要创建新的柱子,使用 Autodesk.Revit.Document.Create.NewFamilyInstance 方法;使用 Autodesk.Revit.Elements.FamilyInstance.Location 来更改或旋转柱子。

- 要获取/设置柱子的底部和顶部水平面,使用 FamilyInstance.get_Parameter() 方法中的 Autodesk.Revit.Parameters.BuiltInParameter.FAMILY_BASE_LEVEL_PARAMFAMILY_TOP_LEVEL_PARAM参数。

- 要获取/设置墙体和柱子的相关信息(向量、长度),使用Autodesk.Revit.Geometry.XYZUVLine类的方法和属性。

说明:

1. 打开Revit,从Revit库文件(M_Wood Timber Column.rfa)中加载“M_Wood Timber Column”柱型族,方式是:在打开的项目中点击“添加构件”,选择柱子,然后从族库中选择“M_Wood Timber Column”柱型族,在项目中将出现“191 x 292mm”类型的柱子。 

2. 在样本项目文件夹下可找到一个.rvt示例文件,名为StructSample.rvt,该文件包含所需的族类型符号和四面墙。您可以选择其中一面或多面墙并运行该样例,运行后会在墙上放置柱子。 

3. 绘制一些墙体并将其顶部和底部约束到属性对话框中的层高上,然后选择它们。

4.运行命令,它将在每面被选中的墙上间隔5英尺放置柱子。

提示:

柱子类型和两个柱子之间的距离是硬编码。

源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

Command.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Windows.Forms;

using Autodesk;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;

namespace Revit.SDK.Samples.StructSample.CS
{
/// <summary>
/// This command places a set of columns in the selected wall.
/// Note that Revit uses Feet as an internal length unit.
///
/// To run this sample,
/// (1) load the column family type of "M_Wood Timber Column", "191 x 292mm"
/// (It is hard-coded in the program.)
/// (2) Draw some walls, and constrain their top and bottom to the levels in the properties dialog.
/// (3) Run the command.
/// It will place columns along each wall with the interval of 5 feet. (The interval is also hard coded.)
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
#region Interface implementation
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref String message, Autodesk.Revit.DB.ElementSet elements)
{
try
{
Autodesk.Revit.UI.UIApplication rvtApp = commandData.Application;
Autodesk.Revit.UI.UIDocument rvtDoc = rvtApp.ActiveUIDocument;
ElementSet ss = new ElementSet();
foreach (ElementId elementId in rvtDoc.Selection.GetElementIds())
{
ss.Insert(rvtDoc.Document.GetElement(elementId));
}

Transaction tran = new Transaction(rvtDoc.Document, "StructSample");
tran.Start();

ElementSet walls = rvtApp.Application.Create.NewElementSet();

// iterate through a selection set, and collect walls which are constrained at the top and the bottom.
foreach (Autodesk.Revit.DB.Element elem in ss)
{
if (elem.GetType() == typeof(Autodesk.Revit.DB.Wall))
{
if (elem.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.WALL_HEIGHT_TYPE) != null
&& elem.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.WALL_BASE_CONSTRAINT) != null)
{
walls.Insert(elem);
}
}
}

// how many did we get?
TaskDialog.Show("Revit", "# of constrained walls in the selection set is " + walls.Size);
if (walls.Size == 0)
{
message = "You must select some walls that are constrained top or bottom";
return Autodesk.Revit.UI.Result.Failed;
}

// next, we need a column symbol. For simplicity, the symbol name is hard-coded here.
Autodesk.Revit.DB.FamilySymbol colType = FindFamilySymbol(rvtDoc.Document, "M_Wood Timber Column", "191 x 292mm");
if (colType == null)
{
TaskDialog.Show("Revit", "failed to got a symbol. Please load the M_Wood Timber Column : 191 x 292mm family");
message = "Please load the M_Wood Timber Column : 191 x 292mm family";

return Autodesk.Revit.UI.Result.Failed;
}

// place columns.
double spacing = 5; // Spacing in feet hard coded. Note: Revit's internal length unit is feet.
foreach (Autodesk.Revit.DB.Wall wall in walls)
{
FrameWall(rvtApp.Application, wall, spacing, colType);
}
tran.Commit();

// return succeeded info.
return Autodesk.Revit.UI.Result.Succeeded;
}
catch (Exception ex)
{
message = ex.ToString();
return Autodesk.Revit.UI.Result.Failed;
}
}
#endregion

/// <summary>
/// find Column which will be used to placed to Wall
/// </summary>
/// <param name="rvtDoc">Revit document</param>
/// <param name="familyName">Family name of Column</param>
/// <param name="symbolName">Symbol of Column</param>
/// <returns></returns>
private FamilySymbol FindFamilySymbol(Document rvtDoc, string familyName, string symbolName)
{
FilteredElementCollector collector = new FilteredElementCollector(rvtDoc);
FilteredElementIterator itr = collector.OfClass(typeof(Family)).GetElementIterator();
itr.Reset();
while (itr.MoveNext())
{
Autodesk.Revit.DB.Element elem = (Autodesk.Revit.DB.Element)itr.Current;
if (elem.GetType() == typeof(Autodesk.Revit.DB.Family))
{
if (elem.Name == familyName)
{
Autodesk.Revit.DB.Family family = (Autodesk.Revit.DB.Family)elem;
foreach (Autodesk.Revit.DB.ElementId symbolId in family.GetFamilySymbolIds())
{
Autodesk.Revit.DB.FamilySymbol symbol = (Autodesk.Revit.DB.FamilySymbol)rvtDoc.GetElement(symbolId);
if (symbol.Name == symbolName)
{
return symbol;
}
}
}
}
}
return null;
}

/// <summary>
/// Frame a Wall
/// </summary>
/// <param name="rvtApp">Revit application></param>
/// <param name="wall">Wall as host to place column objects</param>
/// <param name="spacing">spacing between two columns</param>
/// <param name="columnType">column type</param>
private void FrameWall(Autodesk.Revit.ApplicationServices.Application rvtApp, Autodesk.Revit.DB.Wall wall,
double spacing, Autodesk.Revit.DB.FamilySymbol columnType)
{
Autodesk.Revit.DB.Document rvtDoc = wall.Document;

// get wall location
Autodesk.Revit.DB.LocationCurve loc = (Autodesk.Revit.DB.LocationCurve)wall.Location;
Autodesk.Revit.DB.XYZ startPt = loc.Curve.GetEndPoint(0);
Autodesk.Revit.DB.XYZ endPt = loc.Curve.GetEndPoint(1);

// get wall's vector
Autodesk.Revit.DB.UV wallVec = new Autodesk.Revit.DB.UV(
endPt.X - startPt.X,
endPt.Y - startPt.Y);

// get the axis vector
Autodesk.Revit.DB.UV axis = new Autodesk.Revit.DB.UV(1.0, 0.0);

Autodesk.Revit.DB.ElementId baseLevelId = wall.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId();
Autodesk.Revit.DB.ElementId topLevelId = wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).AsElementId();

// get wall length and vector
double wallLength = wallVec.GetLength();
wallVec = wallVec.Normalize();

// get # of column
int nmax = (int)(wallLength / spacing);

TaskDialog.Show("Revit", "wallLength = " + wallLength + "\r\nspacing = " + spacing.ToString() + "\r\nnmax = " + nmax.ToString());

// get angle of wall and axis
double angle = wallVec.AngleTo(axis);

// place all column
Autodesk.Revit.DB.XYZ loc2 = startPt;
double dx = wallVec.U * spacing;
double dy = wallVec.V * spacing;
for (int i = 0; i < nmax; i++)
{
PlaceColumn(rvtApp, rvtDoc, loc2, angle, columnType, baseLevelId, topLevelId);

loc2 = new XYZ(loc2.X + dx,
loc2.Y + dy,
loc2.Z);
}

// place column at end point of wall
PlaceColumn(rvtApp, rvtDoc, endPt, angle, columnType, baseLevelId, topLevelId);
}

/// <summary>
/// create a column instance and place it on the wall line.
/// </summary>
/// <param name="rvtApp">revit application</param>
/// <param name="rvtDoc">revit document</param>
/// <param name="point2">location for placing column</param>
/// <param name="angle">column angle</param>
/// <param name="columnType">column type placed in Wall</param>
/// <param name="baseLevelId">level id for base level where column is placed</param>
/// <param name="topLevelId">level id for top level where column is placed</param>
private void PlaceColumn(Autodesk.Revit.ApplicationServices.Application rvtApp, Document rvtDoc, Autodesk.Revit.DB.XYZ point2,
double angle, FamilySymbol columnType, Autodesk.Revit.DB.ElementId baseLevelId, Autodesk.Revit.DB.ElementId topLevelId)
{
Autodesk.Revit.DB.XYZ point = point2;

// Note: Must use level-hosted NewFamilyInstance!
Level instLevel = (Level)rvtDoc.GetElement(baseLevelId);
if (!columnType.IsActive)
columnType.Activate();
Autodesk.Revit.DB.FamilyInstance column = rvtDoc.Create.NewFamilyInstance(point, columnType,
instLevel, Autodesk.Revit.DB.Structure.StructuralType.Column);
if (column == null)
{
TaskDialog.Show("Revit", "failed to create an instance of a column.");
return;
}

// rotate column to place it to right location
Autodesk.Revit.DB.XYZ zVec = new Autodesk.Revit.DB.XYZ(0, 0, 1);
Autodesk.Revit.DB.Line axis = Line.CreateUnbound(point, zVec);
column.Location.Rotate(axis, angle);

// Set the level Ids
Parameter baseLevelParameter = column.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.FAMILY_BASE_LEVEL_PARAM);
Parameter topLevelParameter = column.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.FAMILY_TOP_LEVEL_PARAM); ;
baseLevelParameter.Set(baseLevelId);
topLevelParameter.Set(topLevelId);
}
}
}