By Joe Ye
This article shows the code how to create a column in Revit. It uses the minimum code to create a column. Hope it is helpful to Revit programming beginners. Through this sample, you can see the process how to create an element. It uses the automatic transaction.
Imports System
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
<Transaction(TransactionMode.Automatic)>
Public Class CreateColumn
Implements Autodesk.Revit.UI.IExternalCommand
'Creates a column of predefined size
Public Function Execute( _
ByVal commandData As ExternalCommandData, ByRef _
message As String, ByVal elements As ElementSet) As _
Result Implements IExternalCommand.Execute
Dim geo As New Autodesk.Revit.DB.XYZ
geo = New XYZ(5.0, 5.0, 5.0)
Dim doc As Document = _
commandData.Application.ActiveUIDocument.Document
'Get access to the symbol that you want to create
Dim sym As Autodesk.Revit.DB.FamilySymbol _
= FindColumnSymbol(doc)
MsgBox(sym.Name)
'Create a new instance of the column
Dim col As Autodesk.Revit.DB.FamilyInstance = _
doc.Create.NewFamilyInstance(geo, sym,
[Structure].StructuralType.Column)
If col Is Nothing Then
MsgBox("Sorry, Column could not be created")
End If
Return Result.Succeeded
End Function
' Now find the Family for this category
Public Function FindColumnSymbol(ByVal doc As _
Document) As Autodesk.Revit.DB.FamilySymbol
Dim collector As FilteredElementCollector = _
New FilteredElementCollector(doc)
collector.OfCategory(BuiltInCategory.OST_Columns _
).OfClass(GetType(FamilySymbol))
Dim symbol As FamilySymbol = Nothing
If (collector.ToElementIds().Count > 0) Then
symbol = collector.FirstElement()
End If
Return symbol
End Function
End Class