应用程序:TagBeam
Revit平台:全部
Revit版本:2011.0
首次发布于:2008.0
编程语言:C#
技能水平:入门级
类别:注释
类型:ExternalCommand
主题:为梁或钢筋创建标记;创建文本。
摘要:
本示例演示了如何在选择的梁的开头和结尾创建标记,如何在选择的钢筋的第一条曲线的结尾创建钢筋标记,以及如何创建文本。
相关类:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.FamilyInstance
Autodesk.Revit.DB.LocationCurve
Autodesk.Revit.DB.Family
Autodesk.Revit.DB.FamilySymbol
Autodesk.Revit.DB.Document
Autodesk.Revit.DB.IndependentTag
Autodesk.Revit.DB.Structure.Rebar
Autodesk.Revit.DB.TextNote
项目文件:
Command.cs
这个文件包含了“Command”类,它继承了“IExternalCommand”接口并实现了“Execute”方法;类TagRebar继承了“IExternalCommand”接口,用于在选定的钢筋的第一段曲线末端创建一个钢筋标记;类 CreateText 继承了“IExternalCommand”接口,用于在选定的钢筋末端创建一个文本节点。
TagBeamData.cs
它包含一个名为“TagBeamData”的类,用于获取选定的梁,在文档中获取标记符号,并在梁的起点和终点创建标记。
TagBeamForm.cs
这个文件包含一个名为“TagBeamForm”的表单类,用户可以在其中选择标记模式、标记类型、标记方向以及标记是否带有引线。
描述:
- 从Document.Selection中获取所选梁对象。
- 通过判断familySymbol的category来从Document.elements中获取三种标记模式中的Family。
- FamilyInstance.Location.Curve.EndPoint[]属性可以获取起点和终点。
- 使用Autodesk.Revit.Creation.Document.NewTag(View, Element, Boolean, TagMode, TagOrientation, XYZ)方法创建标记。
- 使用Rebar.Curves属性获取所选Rebar的曲线。
- 使用Autodesk.Revit.DB.TextNote.Create方法创建文本注释。
说明:
1. 画一些梁并选择它们。
2. 运行标记梁命令。
3. 选择标记模式-按类别标记、多类别标记或材料标记,然后选择此模式的标记族符号。选择标记方向并选择标记是否有引线。
4. 点击“确定”按钮以标记梁的起点和终点,或点击“取消”按钮以终止标记。
5. 画一根钢筋并选择它。
6. 运行标记钢筋命令。
7. 样本将会在所选钢筋的第一个曲线结束处标记。
8. 选择钢筋并运行创建文字命令。
9. 样本将在所选钢筋的末端创建一个文字节点。
源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
FamilySymbolWrapper.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.Collections.Generic;
using System.Text;
using Autodesk.Revit.DB;
namespace Revit.SDK.Samples.TagBeam.CS
{
/// <summary>
/// A wrapper of family symbol
/// </summary>
public class FamilySymbolWrapper
{
FamilySymbol m_familySymbol;
/// <summary>
/// Family symbol
/// </summary>
public FamilySymbol FamilySymbol
{
get { return m_familySymbol; }
}
/// <summary>
/// Display name
/// </summary>
public string Name
{
get { return m_familySymbol.Family.Name + " : " + m_familySymbol.Name; }
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="tagSymbol"></param>
public FamilySymbolWrapper(FamilySymbol familySymbol)
{
m_familySymbol = familySymbol;
}
};
}
TagBeamData.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.Collections;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit;
using Autodesk.Revit.UI.Selection;
namespace Revit.SDK.Samples.TagBeam.CS
{
/// <summary>
/// Tag beam data class.
/// </summary>
public class TagBeamData
{
//Required designer variable.
private Autodesk.Revit.DB.View m_view; // current view
private Autodesk.Revit.Creation.Document m_docCreator; // document creation
private UIDocument m_revitDoc;
/// <summary>
/// Selected beams
/// </summary>
private List<FamilyInstance> m_beamList =
new List<FamilyInstance>();
/// <summary>
/// Tag types whose category is "Structural Framing Tags"
/// </summary>
private List<FamilySymbolWrapper> m_categoryTagTypes =
new List<FamilySymbolWrapper>();
/// <summary>
/// Tag types whose category is "Multi-Category Tags"
/// </summary>
private List<FamilySymbolWrapper> m_multiCategoryTagTypes =
new List<FamilySymbolWrapper>();
/// <summary>
/// Tag types whose category is "Material Tags"
/// </summary>
private List<FamilySymbolWrapper> m_materialTagTypes =
new List<FamilySymbolWrapper>();
/// <summary>
/// Initializes a new instance of TagBeamData.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command</param>
public TagBeamData(ExternalCommandData commandData)
{
//Get beams selected
m_revitDoc = commandData.Application.ActiveUIDocument;
m_docCreator = m_revitDoc.Document.Create;
m_view = m_revitDoc.Document.ActiveView;
ElementSet elementSet = new ElementSet();
foreach (ElementId elementId in m_revitDoc.Selection.GetElementIds())
{
elementSet.Insert(m_revitDoc.Document.GetElement(elementId));
}
ElementSetIterator itor = elementSet.ForwardIterator();
while (itor.MoveNext())
{
FamilyInstance familyInstance = itor.Current as FamilyInstance;
if ((familyInstance != null) && (familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Beam))
{
m_beamList.Add(familyInstance);
}
}
if (m_beamList.Count < 1)
{
throw new ApplicationException("there is no beam selected");
}
//Get the family symbols of tag in this document.
FilteredElementCollector collector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document);
IList<Element> elements = collector.OfClass(typeof(Family)).ToElements();
foreach (Family family in elements)
{
if (family != null && family.GetFamilySymbolIds() != null)
{
List<FamilySymbol> ffs = new List<FamilySymbol>();
foreach (ElementId elementId in family.GetFamilySymbolIds())
{
ffs.Add((FamilySymbol)(commandData.Application.ActiveUIDocument.Document.GetElement(elementId)));
}
foreach(FamilySymbol tagSymbol in ffs)
{
try
{
if (tagSymbol != null)
{
switch (tagSymbol.Category.Name)
{
case "Structural Framing Tags":
m_categoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol));
continue;
case "Material Tags":
m_materialTagTypes.Add(new FamilySymbolWrapper(tagSymbol));
continue;
case "Multi-Category Tags":
m_multiCategoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol));
continue;
default:
continue;
}
}
}
catch (Exception)
{
continue;
}
}
}
}
}
/// <summary>
/// Tag families with specified mode
/// </summary>
/// <param name="mode">mode of tag families to get</param>
/// <returns></returns>
public List<FamilySymbolWrapper> this[TagMode mode]
{
get
{
switch (mode)
{
case TagMode.TM_ADDBY_CATEGORY:
return m_categoryTagTypes;
case TagMode.TM_ADDBY_MATERIAL:
return m_materialTagTypes;
case TagMode.TM_ADDBY_MULTICATEGORY:
return m_multiCategoryTagTypes;
default:
return null;
}
}
}
/// <summary>
/// Tag the beam's start and end.
/// </summary>
/// <param name="tagMode">Mode of tag</param>
/// <param name="tagSymbol">Tag symbol wrapper</param>
/// <param name="leader">Whether the tag has leader</param>
/// <param name="tagOrientation">Orientation of tag</param>
public void CreateTag(TagMode tagMode,
FamilySymbolWrapper tagSymbol, bool leader,
TagOrientation tagOrientation)
{
foreach(FamilyInstance beam in m_beamList)
{
//Get the start point and end point of the selected beam.
Autodesk.Revit.DB.LocationCurve location = beam.Location as Autodesk.Revit.DB.LocationCurve;
Autodesk.Revit.DB.Curve curve = location.Curve;
Transaction t = new Transaction(m_revitDoc.Document);
t.Start("Create new tag");
//Create tag on the beam's start and end.
Reference beamRef = new Reference(beam);
IndependentTag tag1 = IndependentTag.Create(m_revitDoc.Document,
m_view.Id, beamRef, leader, tagMode, tagOrientation, curve.GetEndPoint(0));
IndependentTag tag2 = IndependentTag.Create(m_revitDoc.Document,
m_view.Id, beamRef, leader, tagMode, tagOrientation, curve.GetEndPoint(1));
//Change the tag's object Type.
tag1.ChangeTypeId(tagSymbol.FamilySymbol.Id);
tag2.ChangeTypeId(tagSymbol.FamilySymbol.Id);
t.Commit();
}
}
}
}