Resolving duplicate tags Step 1 – finding elements

The easiest way to get started with the API is to create a macro. Macros can be specific to an individual Revit file, or they can be “application” macros that available to any file opened by this installation of Revit. In this situation I will make an Application macro.

macromanager

The first thing to do is create a “Module”, which is just a fancy word for a group of macros. You can create a single module to store all your macros, or use modules to group similar macros. When you create a module you specify what language will be used, either VB.NET or C#. I prefer C#.

Then create a macro.

createmacro

Now Revit launches the SharpDevelop development environment and creates the initial code for your macro:

public void MakeMarksUnique()
{
}

Start by going up to the top of the file and adding these lines after “using System;”

using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

Then in the body of the MakeMarksUnique function, add the following;

        public void MakeMarksUnique()
        {
            Document document = this.ActiveUIDocument.Document;
            FilteredElementCollector collector = new FilteredElementCollector(document);
            IList<Element> elements = collector.OfCategory(BuiltInCategory.OST_Doors).ToList();
            string s = "";
            foreach (Element e in elements)
            {
                s += e.Name + ", ";
            }
            TaskDialog.Show("Revit", elements.Count + "\n" + s);
        }

Hit F8 to compile the code (there should be no errors in the Errors window at the bottom of SharpDevelop).

Then go back to Revit, select the macro, and click Run. The next post will discuss what happens and why.

Advertisements

10 thoughts on “Resolving duplicate tags Step 1 – finding elements

  1. First, let me start by saying THANKS for taking the time to put this together!

    I’m attempting to run through the code you entered but am running into this error message:

    Using the generic type ‘System.Collections.Generic.IList’ requires 1 type arguments (CS0305)

    it is referencing this line of code:

    IList elements = collector.OfCategory(BuiltInCategory.OST_Doors).ToList();

    thoughts?

    • Hi David,

      You are welcome!

      I have correct that line. I think what happened was that “Element” inside the angle brackets was being treated as an HTML tag by WordPress and not getting parsed correctly.

      Please let me know how it goes with this sample and if you have ideas for any others.

      Harry

  2. I would also like to thank you for this.
    As I am also attempting to run this code I am running to another error which is that it does not recognice the ActiveUIDocument (Document document = this.ActiveUIDocument.Document;). So am I missing some steps here. Just so you know that I am new at this so this could be more my mistake then this code not working for me.

    Kristjan

    • Hi Kristjan,

      My guess is that you made a Document macro instead an Application macro. This is specified by which tab is selected in the Macro Manager dialog when you create the module and macro.

      For a document macro, get the document with:
      Document document = this.Document;

      For an application macro, get the document with:
      Document document = this.ActiveUIDocument.Document;

      Does that help?

      Harry

      • Thanks Harry for taking the time to answer my question.
        It was as simple as that, just my laziness of not clicking the help button. But sometimes you just have to get little bit of directions from more experience people.
        I will try to come up with more advance questions in the future, as I will follow along.

        Kristjan

  3. Great blog! I’ve read the other posts and didn’t see (but might have missed it) – is there a resource that lists the syntax for the available elements in Revit in case we wanted to do this on something other than doors?

  4. I’m a complete noob when in comes to the Revit API / Macros etc … Thanks for putting these posts together. I went through and typed out the code for this Macro and I’ve got this one error at the last line. Not sure what it means:

    Type or namespace definition, or end-of-file expected (CS1022) – C:\ProgramData\Autodesk\Revit\Macros\2013\Architecture\VstaMacros\AppHookup\StartUp\Source\StartUp\ThisApplication.cs:53

    • In case anyone else is encountering similar problems, here are 3 things to check for:

      1) Make sure that you have the right number of “}” at the end of your file to balance all of the “{” characters

      2) When you type IList in SharpDevelop, it will automatically convert it to IList. You need to replace the T with the class that is being used in the list, such as IList

      3) When typing “elements” in the foreach statement on the last line below, SharpDevelop’s auto-suggest feature will offer a many suggestions such as elements, ElementSet, and ElementSlowFilter. Make sure your code ends up having “elements” and not one of these other suggestions.

      Document document = this.ActiveUIDocument.Document;
      FilteredElementCollector collector = new FilteredElementCollector(document);
      IList elements = collector.OfCategory(BuiltInCategory.OST_Doors).ToList();
      foreach (Element e in elements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s