Jeremy Tammik blogging on the Revit API

November 09, 2011

Pick Corners and Create Floor

I presented an example demonstrating
floor creation and
pointed out that
setting a view using
the ActiveView property cannot be achieved within a transaction.

Ishwar Nagwani, Technical Consultant in our Bangalore organisation, contributed a sample command to create a new a floor which demonstrates both of these aspects in conjunction with the interactive PickBox method to determine the floor corners.

It makes use of the FindElement method, which we have seen in various incarnations to select an element of a given type with a specified name:

///<summary>/// Helper function to find an element of /// the given type and the name. /// For example, use this to find a Reference /// or Level with the given name.///</summary>Element findElement(
Document doc,
Type targetType,
string targetName )
{
// Get the elements of the given typeFilteredElementCollector col
= newFilteredElementCollector( doc )
.OfClass( targetType );
// Return first one with the given nameElement targetElement = null;
foreach( Element e in col )
{
if( e.Name.Equals( targetName ) )
{
targetElement = e;
break;
}
}
return targetElement;
}

By the way, here is another more succinct implementation to achieve the same thing using generics.
I first implemented it using the generic First method, which has the disadvantage of throwing an exception if no element matching the requirements is found.
Luckily, right after the first publication of this, Jon Smith pointed out in a comment below that it can easily be remedied by using FirstOrDefault instead, in which case the implementation is nice and short with no disadvantages at all:

That method is used in the mainline Execute method of the external command to select a level and a plan view to create the floor on, before switching to that view in order to pick the floor corner points:

Once the level has been selected and the view has been set, the PickCreateFloor method is called to select the floor type to use, prompt the user to pick two corner points, use those to create the floor profile, and generate the floor element based on that: