Find Centroid of wall in Revit API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Please help me with this code. I can get some elements selected in Revit, filter only wall, but I have problem to move from selected ID's of walls to solid so I can use the ComputeCentroid.
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)>
Public Class GCentroid
Implements IExternalCommand
Dim _uiApp As UIApplication
Dim _uiDoc As UIDocument
Dim _doc As Document
Public Function Execute(commandData As ExternalCommandData, ByRef message As String, elements As ElementSet) As Autodesk.Revit.UI.Result Implements IExternalCommand.Execute
Try
' Select some elements in Revit before invoking this command
Dim uidoc As UIDocument = commandData.Application.ActiveUIDocument
Dim selection As Selection = uidoc.Selection
Dim selectedIds As ICollection(Of ElementId) = uidoc.Selection.GetElementIds()
If 0 = selectedIds.Count Then
TaskDialog.Show("Revit", "You haven't selected any elements.")
Else
Dim info As [String] = "Ids of selected elements in the document are: "
For Each id As ElementId In selectedIds
info += vbLf & vbTab + id.ToString
Next
TaskDialog.Show("Revit", info)
ChangeSelection(uidoc)
End If
Catch e As Exception
message = e.Message
Return Autodesk.Revit.UI.Result.Failed
End Try
Return Autodesk.Revit.UI.Result.Succeeded
End Function
Private Sub ChangeSelection(uidoc As UIDocument)
' Get selected elements from current document.
Dim selectedIds As ICollection(Of ElementId) = uidoc.Selection.GetElementIds()
Dim opt As Options = New Options
Dim element As Element = Nothing
Dim solid As Solid = Nothing
Dim obj As GeometryObject
opt.DetailLevel = ViewDetailLevel.Coarse
TaskDialog.Show("Revit", "Number of selected elements: " + selectedIds.Count.ToString())
' Go through the selected items and filter out walls only.
Dim selectedWallIds As ICollection(Of ElementId) = New List(Of ElementId)()
For Each x As ElementId In selectedIds
element = uidoc.Document.GetElement(x)
If TypeOf element Is Wall Then
selectedWallIds.Add(x)
obj = element.Geometry(opt)
MessageBox.Show(obj.Id.ToString)
solid = TryCast(obj, Solid)
'HERE I'M RUNING INTO PROBLEM - CAN'T USE ACTUALY THE "solid" TO ComputeCentroid
'Dim centre As XYZ = solid.ComputeCentroid
'MsgBox(centre.X, centre.Y, centre.Z)
End If
Next
End Sub
End Class