Showing posts with label Revit API 101. Show all posts
Showing posts with label Revit API 101. Show all posts

Wednesday, April 27, 2016

Delete Empty Elevation Marker Tags

It is pretty common for a model to end up with a bunch of abandoned elevation marker tags. There are a bunch of ways that this can happen and depending on how you have your elevation tag symbols defined can be difficult to find visually for removal.



One of the supreme beings that I used to work with at CASE built a means of doing this in Dynamo.

This is a simple routine that finds these and eradicates them from your model...


Wednesday, September 19, 2012

Getting all File Based Family Elements by Category

I haven't shared any code on here in a while and thought this might be a good time to get back in the general sharing mode. I get asked a bunch about how the best way is to collect elements in the model especially file based component families (FamilySymbol).

This sample should work in Revit 2012 as well as Revit 2013.

Here's a quick and easy way to get all elements in your model of FamilySymbol organized by Category. We'll use a basic Dictionary class to hold the elements since Dictionaries are super fast to access values from without any iteration required.

First you will need to declare your dictionary somewhere in your code as either a private or public property (you decide), I'm using a property in this example.


  ''' <summary>
  ''' All Family Symbol Elements in the Model by Category
  ''' </summary>
  ''' <value></value>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Property FamilySymbolElements As Dictionary(Of Category, List(Of FamilySymbol))


The next thing is to provide a subroutine where you can perform the gathering of the elements. After we instantiate a fresh dictionary to hold the data, we build a filtered element collector that collects elements by their class type. The class type we're interested in is FamilySymbol.

We then iterate over the elements returned by the collector and check if the category that the element belongs to is in our dictionary. Where the category does not exists as a key in our dictionary, we add a fresh list containing the single element. Where it already exists, we get the list containing the elements and add the new element to the list and apply the updated list back as the value to the dictionary for that category.


  ''' <summary>
  ''' Get all Family Symbol Elements
  ''' </summary>
  ''' <remarks></remarks>
  Private Sub GetSymbols()
 
    ' Fresh Dictionary
    FamilySymbolElements = New Dictionary(Of Category, List(Of FamilySymbol))
 
    ' Collector
    Using col As New FilteredElementCollector(Doc)
      col.OfClass(GetType(FamilySymbol))
      For Each x In col.ToElements
 
        Try
 
          ' Cast and Collect
          Dim m_fs As FamilySymbol = TryCast(x, FamilySymbol)
 
          ' Does the Category already exist in the system
          Dim m_cat As Category = x.Category
          If Not FamilySymbolElements.ContainsKey(m_cat) Then
 
            ' New List
            Dim m_l As New List(Of FamilySymbol)
            m_l.Add(m_fs)
            FamilySymbolElements(m_cat) = m_l
 
          Else
 
            Try
 
              ' Updated List
              Dim m_l As New List(Of FamilySymbol)
              FamilySymbolElements.TryGetValue(m_cat, m_l)
              m_l.Add(m_fs)
 
              ' Update the Key
              FamilySymbolElements(m_cat) = m_l
 
            Catch ex As Exception
 
            End Try
 
          End If
 
        Catch ex As Exception
 
        End Try
 
      Next
    End Using
 
  End Sub


Now we have a complete list of FamilySymbol elements in the model organized by category.

Thursday, April 19, 2012

Revit API 101.1

This is yet another installment of the ongoing Revit API 101 installment that can be viewed in numeric order from 101.#. Each are free so long as you agree to buy the writer a beer when you next see him: @AYBABTM (Austin, TX or next conference/gathering). While some basic understanding of .NET terminology and understanding is assumed in these posts, you'll do just fine if you follow the images and explanations.

I'll attempt to keep the posts slim and to the point, so if you have questions, please post them in the comments. Just remember, there's no crying in .NET.

The Revit API Namespaces

There are two DLL references that make up the entire Revit API, only one of which is required for all Revit API projects.

  • RevitAPI.dll                     (required for all Revit API projects)
  • RevitAPIUI.dll                 (required to access the application user interface objects)

The Required Transaction Attribute for All Revit API Class Implementations

There is now only one required API attribute implementation required for all Revit API implementations. The TransactionMode attribute needs to always be set immediately above the main class declaration as shown below. The TransactionMode attribute informs the Revit API as to how the pass or fail is handled for any given attempt to make changes to a Revit document using the Revit API. There are three settings for the TransactionMode attribute. The Automatic option is soon to be Obsolete, so that really only leaves two valid settings to chose from. The read-only setting is just that, so if you need to make changes to a model document, you should set your transaction attribute to Manual. We'll get into how to build and manage transactions in a future post.


The Revit API Implementations (3)

There are three ways to gain access to the .NET Revit API customization environment. The IExternalcommand implementation is by far the most common and will be discussed in our first real code example. The IExternalApplication interface is the second type and is used either to gain access to the IExternalCommand functions or events (document or application). The third and less common implementation is the IExternalDBApplication implementation which is similar to the IExternalCommand interface except that it cannot access any of the RevitAPIUI.dll namespaces.


The sample API Templates demonstrate implementations of the IExternalCommand interface in the Command class as well as the IExternalApplication interface in the Application class. The IExternalDBApplication class is similar to the IExternalApplication class except for minor differences as shown below.

Sample IExternalCommand Interface


Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection

''' <summary>
''' Revit 2013 Command Class 
''' </summary>
''' <remarks></remarks>
<Transaction(TransactionMode.Manual)>
Public Class Command

    Implements IExternalCommand

    ''' <summary>
    ''' Command Entry Point
    ''' </summary>
    ''' <param name="commandData">Input argument providing access to the Revit application and documents</param>
    ''' <param name="message">Return message to the user in case of error or cancel</param>
    ''' <param name="elements">Access elements</param>
    ''' <returns>Cancelled, Failed or Succeeded</returns>
    Public Function Execute(ByVal commandData As ExternalCommandData,
                            ByRef message As String,
                            ByVal elements As ElementSet) As Result Implements IExternalCommand.Execute
        Try
            ' Add Your Code Here

            ' Return Success
            Return Result.Succeeded

        Catch ex As Exception

            ' Failure Message
            message = ex.Message
            Return Result.Failed

        End Try

    End Function
End Class

Sample IExternalApplication Interface


Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection

''' <summary>
''' Revit 2013 API Application Class
''' </summary>
''' <remarks></remarks>
<Transaction(TransactionMode.Manual)> 
Class Application

    Implements IExternalApplication

    ''' <summary>
    ''' Fires off when Revit Session Starts
    ''' </summary>
    ''' <param name="application">The UI controlled application</param>
    ''' <returns>Returned status</returns>
    Public Function OnStartup(ByVal application As UIControlledApplication) _
            As Result Implements IExternalApplication.OnStartup

        Try

            ' Add your code here

            ' Return Success
            Return Result.Succeeded

        Catch ex As Exception

            ' Return Failure
            Return Result.Failed

        End Try

    End Function

    ''' <summary>
    ''' Fires off when Revit Session Ends
    ''' </summary>
    ''' <param name="application">The UI controlled application.</param>
    ''' <returns>Returned status</returns>
    Public Function OnShutdown(ByVal application As UIControlledApplication) _
            As Result Implements IExternalApplication.OnShutdown

        ' Return Success
        Return Result.Succeeded

    End Function

End Class

Sample IExternalDBApplication Interface


Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection

''' <summary>
''' Revit 2013 API DBApplication Class
''' </summary>
''' <remarks></remarks>
<Transaction(TransactionMode.Manual)>
Public Class DBApplication

    Implements IExternalDBApplication

    ''' <summary>
    ''' DB Application Shutdown
    ''' </summary>
    ''' <param name="application">The controlled application</param>
    ''' <returns>Returned status</returns>
    ''' <remarks></remarks>
    Public Function OnShutdown(application As ControlledApplication) _
            As ExternalDBApplicationResult Implements IExternalDBApplication.OnShutdown

        ' Return Success
        Return Result.Succeeded

    End Function

    ''' <summary>
    ''' DB Application Startup
    ''' </summary>
    ''' <param name="application">The controlled application</param>
    ''' <returns>Returned status</returns>
    ''' <remarks></remarks>
    Public Function OnStartup(application As ControlledApplication) _
            As ExternalDBApplicationResult Implements IExternalDBApplication.OnStartup

        Try

            ' Add your code here

            ' Return Success
            Return Result.Succeeded

        Catch ex As Exception

            ' Return Failure
            Return Result.Failed

        End Try

    End Function

End Class

Tuesday, April 17, 2012

Revit API Training 101.01

I've been getting lots of requests from folks to post a "Getting Started with the Revit API" or "Revit API 101" series, so here is the introduction to such a series! I better see lots of views on these posts! Don't let me down.

Sample Code on GitHub

I've setup a git repository for you guys to download the source code for the samples (as they become available). Revit API 101 Samples on GitHub: http://github.com/rudderdon/Revit101 .

Introduction

This is the first of what might be an endless series on how to get going with the Revit API. Since Revit 2013 just came out, these topics will focus on 2013. If Revit 2012 is all that you have installed, don't worry. The Revit 2012 and 2013 API's differ only slightly and I'll do my best to point out what is different between the two versions in my posts as we run into those differences.


Getting Started, the Development Environment

The first thing that I would recommend for someone that is bran spanking new at programming altogether would be to download and install an Integrated Development Environment (IDE) suitable for .NET development. The Application Programming Interface (API) for Revit is based on the Microsoft .NET Framework 4.0. There are several free IDE platforms out there that you can use, but if you have access to or can afford it, I recommend Microsoft Visual Studio Professional (VS). The latest official versions of VS at the time of this post is VS 2010. There is a free BETA version for 2011 out that you can use, but it will stop working later in the summer of 2012 unless you purchase a license.

Download and Purchase VS 2010 Professional
Download VS 2011 BETA
Download VS 2010 Express Versions (FREE)

Other non Microsoft Sanctioned .NET IDE's
SharpDevelop (Free)
MonoDevelop

Download and Explore the Revit SDK

The Software Development Kit for Revit contains several samples as well as key documentation that can keep you moving in the right direction. The 2012 SDK has a lot more samples than the 2013 version, but you can download either from the Autodesk Revit website. You can also read through Autodesk's tutorial entitled "My First Revit Plug-in."


The next post will cover the Revit API implementations and how they work. We will also have our first sample piece of code to work with.

Wednesday, April 4, 2012

Revit API 101 - Coming Soon

I've been getting lots of requests for a Revit 101 (maybe 201 and beyond as well) course... I am going to make this happen because I care! You will get the (almost) one on one experience except through the internet and maybe twitter :) dot dot dot

Stay tuned for a series of links to free learning resources if not links to additional resources on this blog to get what you deserve (Revit API learning'). I need to figure out the best way to deliver. Maybe videos and such, but definitely an insane amount of tips, etc.

But first enjoy some killer architecture: http://archinect.com/blog/article/43505385/architectural-lolcats (there will be a quiz)