Revit APP Blog

Information on Revit APP.

Browsing Posts published in June, 2010

You have several ways to retrieve element parameters’ name/value/type etc. Let’s use one simple example to show this. If you want to get one parameter from an element, usually you can through the BuiltInParameter enum or the parameter’s name. Through the BuiltInParameter is efficient and can avoid string localization problem, try to use this way if you can. The detail code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace myRevitApp
{
   public class WallFilter : ISelectionFilter
   {
      /// <summary>
      /// Allow Wall to be selected
      /// </summary>
      /// <param name=”element”>A candidate element in selection operation.</param>
      /// <returns>Return true for wall. Return false for non wall element.</returns>
      public bool AllowElement(Element element)
      {
         return element is Wall;
      }

      /// <summary>
      /// Allow all the reference to be selected
      /// </summary>
      /// <param name=”refer”>A candidate reference in selection operation.</param>
      /// <param name=”point”>The 3D position of the mouse on the candidate reference.</param>
      /// <returns>Return true to allow the user to select this candidate reference.</returns>
      public bool AllowReference(Reference refer, XYZ point)
      {
         return true;
      }
   }

   [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
   [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
   public class Class1 : IExternalCommand
   {
      public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
      {
         // Select elements. Click “Finish” or “Cancel” buttons on the dialog bar to complete the selection operation.
         List<ElementId> elemDeleteList = new List<ElementId>();
         WallFilter wfilter = new WallFilter();
         Reference eRef = commandData.Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element, wfilter, “Please pick a wall.”);
         if (eRef != null && eRef.Element != null)
         {
            // Get parameter through builtin parameter enum, this is efficient, use this way if you know the parameter enum
            Parameter param = eRef.Element.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
            if (param != null)
            {
               TaskDialog.Show(“My Revit APP”, “Wall comments: ” + param.AsValueString());
            }
            else
            {
               TaskDialog.Show(“My Revit APP”, “Cannot find wall comments!”);
            }
            // Get parameter through name
            param = eRef.Element.get_Parameter(“Length”);
            if (param != null)
            {
               TaskDialog.Show(“My Revit APP”, “Wall Length through name: ” + param.AsValueString());
            }
            else
            {
               TaskDialog.Show(“My Revit APP”, “Cannot find wall Length parameter through name!”);
            }
         }
         return Result.Succeeded;
      }
   }
}

Revit 2011 API provides more for task dialog, we can extend our example code based on http://blog.revitapp.com/2010/06/use-revit-like-task-dialog/.

Add following code below the line in Class1 (taskDialog.MainContent = “The name of the selected element is:” + eRef.Element.Name;)

   // Add commmandLink to task dialog
   taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, “View category name”);
   taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, “View element ID”);

 Revit API supports up to 4 command links. And add code to deal with the command link, add code below line (TaskDialogResult tResult = taskDialog.Show();)

   if (TaskDialogResult.CommandLink1 == tResult)
   {
      TaskDialog subDlg = new TaskDialog(“Category Name”);
      if (eRef.Element.Category == null)
         subDlg.MainInstruction = “No category”;
      else
         subDlg.MainInstruction = “Category name: ” + eRef.Element.Category.Name;

      subDlg.Show();
   }
   else if (TaskDialogResult.CommandLink2 == tResult)
   {
      // a simpler way to show a task dialog
      TaskDialog.Show(“Element ID”, “Element ID: ” + eRef.Element.Id.ToString());
   }

The main task dialog looks like:

Revit 2011 API provides better UI for interactive use. Here we start from the simpliest one.

Based on our previous application (http://blog.revitapp.com/2010/06/get-selected-elements/), replace the MessageBox line:

MessageBox.Show(elem.Name);   // Pop message box to show element’s name

with following code:

   // Creates a Revit task dialog to communicate information to the interactive user.
   TaskDialog taskDialog = new TaskDialog(“My Revit APP”);
   taskDialog.MainInstruction = “My Revit APP”;
   taskDialog.MainContent = “The name of the selected element is:” + eRef.Element.Name;

   // Set common buttons and default button. If no CommonButton or CommandLink is added,
   // task dialog will show a Close button by default.
   taskDialog.CommonButtons = TaskDialogCommonButtons.Close;
   taskDialog.DefaultButton = TaskDialogResult.Close;

   // Set footer text. Footer text is usually used to link to the help document.
   taskDialog.FooterText = “<a href=\”
http://bbs.revitapp.com\”>Click here for the Revit APP discussion group</a>”;

   TaskDialogResult tResult = taskDialog.Show();

Build the project and run Revit. You can see the new task dialog like this:

Compare to old way of selecting elements(http://blog.revitapp.com/2010/06/get-selected-elements/), Revit 2011 API provides a new way to select element(s).

Add a new line before the namespace: using Autodesk.Revit.UI.Selection;

Add a new class named WallFilter:

   public class WallFilter : ISelectionFilter
   {
      /// <summary>
      /// Allow Wall to be selected
      /// </summary>
      /// <param name=”element”>A candidate element in selection operation.</param>
      /// <returns>Return true for wall. Return false for non wall element.</returns>
      public bool AllowElement(Element element)
      {
         return element is Wall;
      }

      /// <summary>
      /// Allow all the reference to be selected
      /// </summary>
      /// <param name=”refer”>A candidate reference in selection operation.</param>
      /// <param name=”point”>The 3D position of the mouse on the candidate reference.</param>
      /// <returns>Return true to allow the user to select this candidate reference.</returns>
      public bool AllowReference(Reference refer, XYZ point)
      {
         return true;
      }
   }

Replace the code in class Class1 as:

   // Select elements. Click “Finish” or “Cancel” buttons on the dialog bar to complete the selection operation.
   List<ElementId> elemDeleteList = new List<ElementId>();
   WallFilter wfilter = new WallFilter();
   Reference eRef = commandData.Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element, wfilter, “Please pick a wall.”);
   if (eRef != null && eRef.Element != null)
   {
      MessageBox.Show(eRef.Element.Name);
   }
   return Result.Succeeded;

Then open a document, execute the external tools command, select a wall and you can see the pop up dialog. Also you can see prompt in the status bar.

Based on your first Revit API application (http://blog.revitapp.com/2010/05/build-your-first-revit-api-application/), you can extend it a little further and retrieve selected element’s name.

Replace the code in class Class1 from:

MessageBox.Show(“my first revit application!”);

to:

 // Get selected elements from active document
ElementSet selected = commandData.Application.ActiveUIDocument.Selection.Elements;
if (selected.Size > 0)
{
   // Get the iterator of the element set
   ElementSetIterator it = selected.ForwardIterator();
   if (it.MoveNext())   // Move forward to retrieve element, if you want to show all selected elements, use while instead of if
   {
      Element elem = it.Current as Element;  // Get current element of the iterator
      if (elem != null)
      {
         MessageBox.Show(elem.Name);   // Pop message box to show element’s name
      }
   }
}
Then open a document, select one element, then execute the external tools command, a message box will pop up to show name of the selected element.

Powered by WordPress Web Design by SRS Solutions © 2012 Revit APP Blog Design by SRS Solutions