AU2011-CP4453 - Everything in Place With Revit MEP Programming v1
AU2011-CP4453 - Everything in Place With Revit MEP Programming v1
CP4453 This class discusses working programmatically with Revit MEP models. The main focus is
on the MEP API enhancements in Revit 2012 and two new MEP related APIs, the detailed energy
analysis model and the conceptual energy analysis API. The major enhancements cover the new
placeholder elements, pipe settings, insulation and lining. An overview of the entire Revit MEP API and
existing samples is provided. All MEP domains including HVAC, electrical and plumbing are addressed.
We show how to analyse existing systems and create new MEP models from scratch. We cover
mechanical and electrical system traversal, display of system hierarchies in a tree view, all types of MEP
components, and automatic calculation and sizing based on room and space requirements. Please note
that prior .NET programming and Revit MEP product experience is required and that this class is not
suitable for beginners.
Learning Objectives
At the end of this class, you will be able to:
• Manage, analyze, create and modify HVAC, electrical and plumbing Revit MEP models, systems, and
components programmatically
• Understand the major enhancements of the MEP API in Revit 2012: placeholder elements, pipe
settings, insulation and lining, detailed energy analysis and conceptual energy analysis
• Understand and reuse functionality provided by the standard Revit MEP SDK and custom ADN
samples
Introduction ............................................................................................................................. 3
MEP Application Requirements ........................................................................................... 3
The Generic Revit API......................................................................................................... 3
Revit MEP API Evolution ..................................................................................................... 3
Analysis .................................................................................................................................. 4
EnergyDataSettings and gbXML ......................................................................................... 4
Conceptual Energy Analysis ................................................................................................ 4
Detailed Energy Analysis Model .......................................................................................... 4
Spaces and Zones .............................................................................................................. 5
Model Inspection Utilities ..................................................................................................... 5
Hierarchical Systems and Connectors .................................................................................... 5
Hierarchical System Structure ............................................................................................. 6
MEP Model ......................................................................................................................... 6
Connectors .......................................................................................................................... 6
Connector Definition Elements ............................................................................................ 6
Electrical ................................................................................................................................. 8
HVAC and Plumbing ............................................................................................................... 8
HVAC and Pipe Systems..................................................................................................... 8
Duct and Pipes .................................................................................................................... 9
Fittings ................................................................................................................................ 9
Connectors .......................................................................................................................... 9
Element Creation ................................................................................................................ 9
The Revit MEP 2012 API ...................................................................................................... 10
Product Features ............................................................................................................... 10
Major API Enhancements .................................................................................................. 10
Placeholder Ducts and Pipes ............................................................................................ 11
Insulation and Lining ......................................................................................................... 11
Pipe Settings and Sizes .................................................................................................... 11
Small Enhancements and Changes .................................................................................. 12
Sample Applications ............................................................................................................. 12
AddSpaceAndZone ........................................................................................................... 13
AutoRoute ......................................................................................................................... 14
AvoidObstruction ............................................................................................................... 14
CreateAirHandler .............................................................................................................. 14
EnergyAnalysisModel ........................................................................................................ 15
PanelSchedule .................................................................................................................. 16
PowerCircuit ...................................................................................................................... 16
TraverseSystem ................................................................................................................ 17
AdnRme Sample ............................................................................................................... 17
MEP Placeholder Sample.................................................................................................. 19
Further Reading .................................................................................................................... 20
2
Everything in Place with Revit MEP
Programming
Introduction
MEP Application Requirements
Revit MEP is a flavour of Revit for work in the mechanical, electrical and plumbing domains.
HVAC, i.e. heating, ventilation and air conditioning, is considered part of the mechanical domain.
Efficient work in these areas requires strong model analysis tools in addition to read, write and
creation access to the components and data used to model the electrical, HVAC and piping
systems in Revit MEP:
• Model Analysis Tools
o MEP project information
o Green Building XML, gbXML
o Spaces and zones
• MEP System Tools
o Electrical systems, components, properties and parameters
o Duct and pipe systems, components, properties and parameters
o Element creation and modification
o System traversal and analysis
3
Everything in Place with Revit MEP
Programming
application for HVAC air terminal analysis and sizing, first presented at Autodesk University
2007, previously named mep and now renamed to AdnRme for Revit 2012.
Revit 2009 introduced the first MEP-specific API support, including classes for electrical and
mechanical equipment, lighting device and fixture, connector, electrical system, space and zone,
the MEP model property on family instances, and the ability to create and modify MEP elements.
In Revit 2010, the MEP-specific API was a focal point of the API development and greatly
enhanced, especially the support for HVAC and piping systems. It introduced the
Autodesk.Revit.MEP namespace, which replaced by the more specific namespaces listed
above.
Revit MEP 2011 introduced cable trays and conduit elements and electrical panel schedules and
consolidated most of the MEP related API classes into the new more specific namespaces. At
that point, the MEP-specific API was more or less equally evolved as the architectural and
structural flavours, and becoming widely used.
The main Revit 2012 API enhancements include placeholder elements, new insulation and lining
functionality, and access to pipe settings and sizes. We will look at these topics in greater depth
after the general MEP API overview.
Analysis
Extensive and detailed model analysis tools are required for efficient modelling in all MEP
disciplines. Some of the important features include the MEP project information, support for
gbXML, the Green Building XML, spaces and zones for MEP-specific building area definition
and management, and various model inspection utility methods.
4
Everything in Place with Revit MEP
Programming
5
Everything in Place with Revit MEP
Programming
MEP Model
The Revit 2009 API introduced the MEPModel and Connector classes as fundamentals for
programmatically representing and accessing the MEP model. MEPModel is a base class for the
derived classes
• ElectricalEquipment
• LightingDevice
• LightingFixture
• MechanicalEquipment
• MechanicalFitting
On family instances, it can be accessed through the FamilyInstance.MEPModel property. One of
its main purposes is to provide access to the connector manager. This property returns null in a
non-MEP Revit flavour.
Connectors
We have logical and physical connections between MEP components. Logical connections are
used in the electrical domain, where wires are annotation elements not intended to represent the
real-world model. Physical connections are used in all MEP domains. In the mechanical and
plumbing domains, the connectors define and transmit the sizing information from one part to
the next.
A physical connector is part of an MEP element such as a duct, pipe, fitting, or piece of
equipment. The Connector class is used to represent connectors in the project environment.
The connector defines geometry, e.g. its oval, round or rectangular shape, and dimensions such
as width, height and radius. It also maintains a coordinate system and system data such as
domain, system type, flow, direction. You obviously cannot read the radius property if the
connector shape is not round, so you need to check the shape first before reading some of the
dimensions.
6
Everything in Place with Revit MEP
Programming
This is achieved using connectors which are not elements, nor family instances, and do not live
in the project file. They are contained within the elements that they connect.
On the other hand, within the family definition, the connectors are represented by stand-alone
elements.
There are different types of connectors for the different types of systems. A single piece of MEP
equipment can have none, some or all connector types.
For instance, a piece of HVAC equipment such as a cooler may have electrical connections for
power, pipes for cooling water in- and outlets, and ducts for air in- and outlets. These connectors
are defined in the family editor. When the equipment is inserted into the model, the connectors
are used to hook it up to neighbouring elements, in this case electrical systems, pipes and ducts.
We thus have one set of classes for defining connector elements, residing in the family
document. We have another class to represent the connectors on the resulting family instances,
once inserted into a project document. The former are the DuctConnector, PipeConnector and
ElectricalConnector classes, the latter the Connector class.
Inside a family document, building your equipment, you can add e.g. a DuctConnector to the
model. This defines some information on the equipment hosting it. When you insert that
equipment into the model and query it for its connectors, it returns Connector instances to you.
DuctConnector, PipeConnector and ElectricalConnector are the classes you see and use in the
family editor. These objects are the MEP connector definition classes used as a part of the
building blocks to define an MEP part:
7
Everything in Place with Revit MEP
Programming
Electrical
Electrical systems are organised hierarchically in Revit MEP. The root elements are panels.
Each panel is connected to a number of systems, also known as circuits. Each system or circuit
consists of a number of circuit elements, some of which may be further panels, and so on,
recursively.
Cables and wires may not be modelled individually, so the connections between the electrical
system components can be defined by logical connectors rather than physical ones.
The classes ElectricalEquipment, LightingDevice, LightingFixture, ElectricalSystem,
specialisations of the base MEPModel and MEPSystem classes represent electrical components
and systems. These elements all have a connector manager, whose Connectors property
returns logical as well as physical type connectors:
ElectricalSystem sys;
ConnectorSet connectors = sys.ConnectorManager.Connectors;
The connector manager can be used to query both physical and logical connectors to determine
how the different components of an electrical system are connected together and construct the
system hierarchy from that information. This is demonstrated by the AdnRme electrical sample.
8
Everything in Place with Revit MEP
Programming
Fittings
The fittings are represented by family instances, which are inserted in the model using dedicated
New*Fitting methods on the Autodesk.Revit.Creation.Document class, where * can be replaced
by Elbow, Tee, Cross, Takeoff, Transition, and Union. The fitting properties and their shape and
dimensions can be accessed through the FamilyInstance.MEPModel property.
Connectors
The basic features of connectors and the different classes used to represent them in the family
and project contexts were discussed above. Additional HVAC-specific connector features
include properties to read the flow, coefficient and demand. They also provide access to
physical properties like Origin, Angle, Height, Width and Radius.
We have read and write access to so-called assigned connector properties. These are connector
properties that can be overridden by the user and are dependent on the overall network
condition, including characteristics like Flow, Flow Config, Coefficient, Loss etc. These depend
on the system being well connected, the configuration of the family within the system, and how
the family is constructed.
Connector size and location can be modified. Connectors can be disconnected to insert a new
fitting into a network or connect two ducts into a transition.
• The family connector elements define Flow, Flow Configuration, Coefficients, and Loss
Method.
• Read duct, pipe, and fitting connector properties such as Flow, Coefficient, and Demand.
• Access physical connector properties e.g. Origin, Angle, Height, Width, Radius.
• Change connector size and location.
• Connect and disconnect.
Element Creation
The creation document provides a number of methods for the creation of HVAC and plumbing
elements within the project environment:
• NewMechanicalSystem and NewPipingSystem to create a new system.
9
Everything in Place with Revit MEP
Programming
Product Features
Here are some of the key features of Revit MEP 2012 from a product point of view:
• Placeholder elements
• Insulation and lining
• Parallel pipe and conduit runs
• Sloped piping enhancements: settings, tooltips, connection
• System browser enhancements: filtering, hovering and selection
• System enhancements: graphic overrides and warnings, disconnect markers, materials,
calculation control, connector labels
The following API enhancements obviously build on and provide access to this functionality.
10
Everything in Place with Revit MEP
Programming
Furthermore, the Revit platform API enhancements include some features which are especially
useful in the MEP domain, such as new classes for the analysis of room and space 3D
geometry, new geometry functionality, and new support for handling linked files.
In the following sections, let us take a more detailed look at the most important aspects.
For more information, please refer to the What’s New section in the Revit API help file
RevitAPI.chm included in the Revit SDK, which is available from the Revit Developer Center at
http://www.autodesk.com/developrevit.
11
Everything in Place with Revit MEP
Programming
• PipeSizeSettings – The main class to access pipe sizes with one singleton object of this
type in a document accessible through the static GetPipeSizeSettings method. It stores
PipeConnections per each pipe material, using the id of the pipe material element as key
to access. PipeSizeSettingIterator defines an iterator over a set of PipeSizeSettings
items, each of which is a KeyValuePair<ElementId, PipeConnections>.
• PipeConnections – Represents a set of pipe connection types. Stores PipeSchedules per
each pipe connection type, using the id of the pipe connection type as the access key.
PipeConnectionIterator defines an iterator over a set of PipeConnections items, each of
which is a KeyValuePair<ElementId, PipeSchedules>.
• PipeSchedules – Represents a set of pipe schedules. Stores PipeSizes per each pipe
schedule type, using the id of the pipe schedule type as key to access.
PipeScheduleIterator defines an iterator over the PipeSchedules
KeyValuePair<ElementId, PipeSizes> items.
• PipeSizes – Stores a set of MEP pipe sizes, with the ability to add and remove from the
set as needed. PipeSizeIterator defines an iterator over PipeSizes.
• MEPSize – Stores the basic size information for an MEP duct, pipe, cable tray, or
conduit.
Sample Applications
The MEP API is illustrated by a number of sample applications including those provided in the
standard Revit SDK, non-SDK ones created by the ADN DevTech developer support team, and
some from The Building Coder blog.
The Revit SDK includes the following MEP specific samples:
• AddSpaceAndZone: handling space and zone elements.
12
Everything in Place with Revit MEP
Programming
• AutoRoute: route a set of ducts and fittings between a source and two sinks, the air
supply equipment and two air outlet terminals.
• AvoidObstruction: detect and resolve obstructions between ducts, pipes, and beams.
• CreateAirHandler: create a family representing an air handler and its connectors.
• EnergyAnalysisModel: create an EnergyAnalysisModel, query and display its face
structure in a tree view.
• PanelSchedule: use of the electrical panel schedule API.
• PowerCircuit: manipulation of electrical power circuits.
• TraverseSystem: traverse a well-connected mechanical or piping system in the direction
of flow.
The main non-SDK sample application is named AdnRme. It implements some HVAC oriented
commands making use of the generic Revit API to analyse and manipulate duct elements for
automatic air terminal sizing. Others implement an electrical sample for analysis and display of
an electrical system in a tree view reproducing the Revit system browser structure or showing
the full connection hierarchy tree:
• HVAC air terminal analysis and sizing.
• Electrical system connectivity analysis and hierarchical display.
Finally, we already looked at the MepPlaceholders sample included in the class materials,
demonstrating the new Revit MEP 2012 API features. It was also partially discussed by The
Building Coder.
Here is a more detailed look at these samples:
AddSpaceAndZone
This 2009 sample creates the spaces and zones required for HVAC analysis, and implements
the following main features:
• Retrieve and list all existing spaces and zones in a specified level using an element filter.
• Create new spaces for each closed wall loop or space separation with the NewSpaces
method.
• Create a new zone element in a specified level and phase.
• Add and remove spaces in a zone using the AddSpaces and Remove methods.
13
Everything in Place with Revit MEP
Programming
AutoRoute
Automatically create and route a set of ducts and fittings between a source, the air supply
equipment, and the sink, two air outlet terminals. It creates a new mechanical system, ducts,
fittings and connections using the methods NewMechanicalSystem, NewDuct, NewElbowFitting,
NewTeeFitting and Connector.ConnectTo. The routing between the source and sink objects is
automatically defined, so these can be moved freely prior to executing the command. To define
the route, it first determines the bounding box of all the three elements, then uses the middle line
or quarter lines on the X and Y axes as the trunk with a series of ducts that will be connected to
the elements with fittings and ducts. This sample also demonstrates use of the .NET framework
Trace class to create a log file AutoRoute.log.
AvoidObstruction
Detect and resolve obstructions between ducts, pipes, and beams. This sample uses the ray
casting intersection analysis method FindReferencesWithContextByDirection. Only the pipe
centre line is used to detect obstructions, the radius is ignored. Only pipes are modified, none of
the other elements. The pipe’s original direction remains unchanged. It is split into several
segments according to the obstructions detected, and elbows are inserted to reroute it around
the obstruction. The example project included with the sample demonstrates the automatic
resolution of collisions between pipes and beams, ducts, and other pipes.
CreateAirHandler
Use the family API in the family editor context to create an air handler and add connectors.
The sample first checks the family template type using the OwnerFamily.FamilyCategory
property. It creates the geometrical shape of the air handler using the NewExtrusion method on
14
Everything in Place with Revit MEP
Programming
EnergyAnalysisModel
The new API provides access to the contents of a project's detailed energy analysis model, as
generated by and seen in the Export to gbXML and the Heating and Cooling Loads features.
This analysis produces an analytical thermal model from the physical model of a building. The
analytical thermal model is composed of spaces, zones and planar surfaces that represent the
actual volumetric elements of the building.
New classes in Autodesk.Revit.DB.Analysis namespace for the energy analysis detail model
itself, creation options, openings, spaces, surfaces and loops can be used to generate and
analyze the contents of the detailed energy analysis model. The EnergyAnalysisDetailModel
Create method creates and populates the model with appropriate options selected. The methods
GetAnalyticalSpace, GetAnalyticalSurfaces, GetAnalyticalOpenings and
GetAnalyticalShadingSurfaces extract entities from the analysis model. The method Destroy
cleans up the Revit database after finishing with the analysis results.
• Produce analytical thermal model from physical building model
• Retrieve energy analysis detail model and present as tree view
• Similar to Export to gbXML and Heating and Cooling Loads
• Model is composed of spaces, zones and planar surfaces
o EnergyAnalysisDetailModel.Create()
o GetAnalyticalSpaces, GetAnalyticalSurfaces, GetAnalyticalOpenings,
GetAnalyticalShadingSurfaces
The EnergyAnalysisModel SDK sample in the RvtSamples Analysis group generates a detailed
energy analysis model, queries it for its surfaces depending on user-specified options, and lists
15
Everything in Place with Revit MEP
Programming
them in a tree view. A sample model EnergyAnalysisModel.rvt is provided in the Samples >
GeometryAPI > EnergyAnalysisModel folder.
PanelSchedule
The Revit 2011 SDK includes the MEP specific sample application PanelSchedule. It defines
three external commands to demonstrate the use of the new panel schedule API:
• PanelScheduleExport – retrieve the panel schedule view data and export to CSV or
HTML.
• InstanceViewCreation – create a panel schedule view instance for a selected electrical
panel.
• SheetImport – place panel schedule views on a sheet view.
PowerCircuit
This RME electrical sample provides functionality similar to the legacy RME Circuit Editor
toolbar. It shows how to operate power circuits, as well as how to handle interactive element
selection in Revit, implement a toolbar user interface for an external command, and use the
.NET ResourceManager class to manage the images and localisable string resources. It mainly
exercises classes from the Autodesk.Revit.MEP namespace and the MEPModel and
ElectricalSystem classes. It provides the following functionality:
• Create a power circuit with selected elements.
• Edit a power circuit, or add or remove an element to or from a circuit.
• Select a panel for a circuit, or disconnect panel from a circuit if the circuit has a panel.
Creating a new circuit is achieved by the creation document method NewElectricalSystem,
passing in a list of circuit elements and an electrical system type from the ElectricalSystemType
enumeration, which can currently be one of Data, PowerCircuit, Telephone, Security, FireAlarm,
NurseCall, Controls, and Communication.
For editing the circuit, the elements make use of the FamilyInstance MEPModel property, which
provides access to its connector manager and electrical systems. The elements to be added to a
circuit should have unused electrical connectors with same voltage definition and pole numbers.
The entire user interface is presented by two toolbars with buttons representing the operations
listed above.
16
Everything in Place with Revit MEP
Programming
TraverseSystem
Traverse a well-connected mechanical or piping system in the direction of flow and dump the
traversal into an XML file. The MEP electrical sample presented below demonstrates a similar
traversal of electrical systems.
The TraverseSystem sample defines two classes TreeNode and TraversalTree. A TreeNode
object represents an element in the system and contains traversal connection clues. The class
TraversalTree traverses the specified system in the direction of flow by generating a tree-like
structure of TreeNode objects.
A connected mechanical or piping system is retrieved from a selected element which can be the
system itself or any of its elements. If the selected element belongs to more than one well-
connected system, the system with the most elements will be chosen. The traversal begins at
the system base equipment, if found. If the system has no base equipment, it will have an open
connector which is the only point that can connect to a base equipment. The owner of the open
connector will be chosen as the starting point. The connected elements relationships in the
system are analysed by their connectors’ flow directions and generate the traversal tree-like
structure.
How is the system retrieved? If the selected element is the system itself, it is either a
MechanicalSystem or PipingSystem instance, both derived from the MEPSystem class.
Otherwise, it may be a fitting, such as a T join or elbow, represented by a family instance, or a
duct or pipe, represented by a MEPCurve instance. Both fittings and MEP curves have an
associated connector manager,
The well-connectedness is reported by the MechanicalSystem IsWellConnected property, the
number of elements in a system by Elements.Size, and the base equipment through
BaseEquipment. If base equipment is null, an open connector is searched for by starting at an
arbitrary element and recursively iterating through all its connectors and neighbouring system
elements. Once the starting element has been defined, the traversal tree is built by a second
recursive iteration through all connectors, neighbouring elements, their connectors, etc.
AdnRme Sample
This non-SDK sample implements a number of external commands addressing HVAC air
terminal analysis and sizing as well as the hierarchical display of an electrical system. Some
aspects of the sample not directly related to the MEP API are the external application
implementation, about box, and progress bar.
17
Everything in Place with Revit MEP
Programming
The HVAC part of the MEP sample was originally implemented based on Revit 2008. Later the
electrical part and an external application to define a custom user interface to access the sample
commands was added:
The application includes an about box command presenting the application description and
version number. The version number and description displayed in the form is queried from the
executing .NET assembly:
The HVAC sample implements a progress bar, since the processing of a large number of
elements may take a noticeable amount of time:
The application resides in the assembly AdnRme.dll and defines the namespace AdnRme. Its
external commands can either be accessed by installing the external application interface
defining the custom ribbon panel, or by setting up command buttons in the Revit ribbon to
access them individually.
The main aspects of the electrical and HVAC sample are described below. For full details,
please refer to the materials from the Autodesk University 2010 class on the Revit MEP API,
CP316-3U All Systems Go in Autodesk Revit MEP Programming.
18
Everything in Place with Revit MEP
Programming
The standard RME system browser displays electrical components in a three-level flat list, and
the complete hierarchical structure of the connection tree is not immediately apparent. This
sample inspects the electrical systems and reproduces the structure and information displayed
by the system browser as well as the full connection hierarchy. It implements the following
commands:
• CmdElectricalConnectors
• CmdElectricalSystemBrowser
• CmdUnhostedElements
The first two analyse the electrical system hierarchical structure and present it to the user in a
tree view in a modeless dialogue. Being modeless, it remains visible and can be navigated after
the command has terminated. You can leave the form open and switch back and forth between
it and Revit to explore the electrical system simultaneously from both points of view. Note,
however, that updates to the model will not be reflected in the dialogue until the command is re-
executed. A sample model elec_project.rvt is provided.
• Connection hierarchy tree
• Obsolete pre-connector approach
• System browser structure
• Find un-hosted elements
19
Everything in Place with Revit MEP
Programming
Further Reading
• A good starting point for all Revit developers is the resources listed on the Revit
Developer Center – http://www.autodesk.com/developrevit. These include:
o The Revit SDK including the API help file RevitAPI.chm and the Developer Guide
"Revit 2011 API Developer Guide.pdf".
o Training material, recorded presentations, especially the DevTV recordings, and
the DevTV Visual Studio Revit add-in templates.
o Information on joining the Autodesk Developer Network –
http://www.autodesk.com/joinadn.
o Information on training classes and webcasts –
http://www.autodesk.com/apitraining.
o Links to the Autodesk discussion groups – http://www.autodesk.com/discussion.
Click on the Revit link to access the Revit API discussion groups.
• Jeremy Tammik’s Revit API blog The Building Coder –
http://thebuildingcoder.typepad.com and some Revit MEP API related posts:
o http://thebuildingcoder.typepad.com/blog/2011/06/the-revit-mep-2012-api.html
o http://thebuildingcoder.typepad.com/blog/2011/06/modifying-cable-tray-
shape.html
o http://thebuildingcoder.typepad.com/blog/2011/07/mep-placeholders.html
• For ADN members, there is a wealth of Autodesk API information on the members- only
ADN website – http://adn.autodesk.com. ADN members can ask unlimited API questions
through our DevHelp Online interface.
• Materials from previous MEP API Autodesk University classes.
20