Wednesday, 4 February 2009

Calculating building height

I saw a query on the Revit API discussion groups about how to calculate the height of a building, so I thought I'd have a quick go. Admittedley they wanted it in C#, but I like my VB.

Loop through your elements looking for a level type, and when you find one use the level.ProjectElevation property to get its height - in decimal feet of course. At first I added these up to get total height, but then realised that ProjectElevation returns the elevation of the level relative to the project origin, so all we want is the highest figure we can find of all our levels.

Dim elementIterator As Autodesk.Revit.ElementIterator
elementIterator = revitApp.ActiveDocument.Elements
     
Dim height As Double

While (elementIterator.MoveNext())

'look for level types
If TypeOf elementIterator.Current Is Autodesk.Revit.Elements.Level Then

Dim level As Elements.Level = elementIterator.Current

'is current value higher than previous values?
If level.ProjectElevation > height Then
    height = level.ProjectElevation
End If


End If

End While

MessageBox.Show("Height: " & height)
      

UPDATE: This only retrieves the elevation of the uppermost level, which isn't the height of the building - see my next post for a different approach!

5 comments:

  1. Hi Ed,
    A better way to select all levels is to use the selection filters (2008+).

    Dim TypeFilter As Autodesk.Revit.Filter = MyApp.Create.Filter.NewTypeFilter(GetType(Autodesk.Revit.Elements.Level))
    Dim CatFilter As Autodesk.Revit.Filter = MyApp.Create.Filter.NewCategoryFilter(Autodesk.Revit.BuiltInCategory.OST_Levels)
    Dim LvlFilter As Autodesk.Revit.Filter = MyApp.Create.Filter.NewLogicAndFilter(CatFilter, TypeFilter)

    Dim result As New List(Of Autodesk.Revit.Element)
    Dim NumLevels As Integer = MyDoc.Elements(LvlFilter, result)

    ReplyDelete
  2. Thanks Krispy5, it's my old skool habits that I keep slipping into - quick and dirty coding :)

    Type filters are definitely more elegant!

    ReplyDelete
  3. Hmmm I've just realised this doesn't give the height of the building - it gives me the elevation of the uppermost level - i.e. the level at which it is placed.

    I'll hopefully find some time today to add the extra bit on! :)

    ReplyDelete
  4. Krispy5, in your suggestion you're using both a type and category (with built-in category OST_Levels) filter to get your levels. Is this just a belt-and-braces approach you always use? The type filter alone does the job in my tests.

    Could a level type ever be found in a different category? I'd be interested to know if I should be using more category filters like this.

    ReplyDelete
  5. I remember that one or the other would also return the types of level as well as the actual instances of levels, so i usually use this method as a "catch all"

    ReplyDelete

Comments are moderated, so you'll have to wait a little bit before they appear!