Revit Massings By Rooms

This post is about creating Revit massings elements by given rooms elements with IronPython in Revit Dynamo Application.

Firstly, import these modules: clr to load .Net Assemblies as IronPython modules which are RevitAPI.dll and RevitServices.dll and from these modules, we import DB, DocumentManager and TransactionManager classes. Collect the current document and store in doc.

import clr, System

clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# current document
doc = DocumentManager.Instance.CurrentDBDocument

Provide room element(s) as input and get Mass BuiltInCategory object from Revit BuiltInCategory Enumeration. Collect object Id by using the second overload constructor of ElementId class ElementId(BuiltInCategory). Keep room(input) elements Unwrapping to access API methods and Properties, make a list to store the output elements and start the transaction.

# input
elems = IN[0];

# get mass category object
MassBICObj = System.Enum.ToObject(BuiltInCategory,BuiltInCategory.OST_Mass)

# mass object id
objId = ElementId(MassBICObj)

# make list
if not isinstance(elems,list):
	elems = UnwrapElement([elem])
else:
	elems = UnwrapElement(elems)

# output result
res = []

# transaction start
TransactionManager.Instance.EnsureInTransaction(doc)

Loop each room, first collect name and number with LookupParameter method and create naming for massing element by concatenating collected name and number. Get room geometry, which returns a list but we only need the first index, from this geometry list. And we create a DirectShapeType by using static method of DirectShapeType class which required three arguments; doc, element name and objectId (which is the mass category id). We need to store each newly created DirectShapeType in the DirectShapeLibrary. Finally, we create the DirectShape element instance which is the massing element that we want and append this massing element to the output list.

# each room
for e in elems:
	# get room name
	name = e.LookupParameter("Name").AsString()
	# get room number
	number = e.LookupParameter("Number").AsString()
	elem_name = number+"_"+name
	# collect geometry element
	geoobj = e.Geometry[Options()]
	# make enum to get geometry object
	enum = geoobj.GetEnumerator();
	shape = [i for i in enum][0]

	# create direct shape type
	shapeType = DirectShapeType.Create(doc,elem_name,objId)
	# set shape to geometry list shape
	shapeType.SetShape([shape])
	# get dslibrary
	lib = DirectShapeLibrary.GetDirectShapeLibrary(doc)
	# add type to dslibrary
	lib.AddDefinitionType(elem_name,shapeType.Id)
	# create element by shapetype
	element = DirectShape.CreateElementInstance(doc,shapeType.Id,objId,elem_name,Transform.Identity)
	# set typeId
	element.SetTypeId(shapeType.Id)
	# append to output
	res.append(element)
# transaction end
TransactionManager.Instance.TransactionTaskDone()

OUT = res

Here is sample usage and the complete code is below.

mass-by-room
Sample Usage
import clr, System

clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# current document
doc = DocumentManager.Instance.CurrentDBDocument

# input
elems = IN[0];

# get mass category object
MassBICObj = System.Enum.ToObject(BuiltInCategory,BuiltInCategory.OST_Mass)

# mass object id
objId = ElementId(MassBICObj)

# make list
if not isinstance(elems,list):
	elems = UnwrapElement([elem])
else:
	elems = UnwrapElement(elems)

# output result
res = []

# transaction start
TransactionManager.Instance.EnsureInTransaction(doc)

# each room
for e in elems:
	# get room name
	name = e.LookupParameter("Name").AsString()
	# get room number
	number = e.LookupParameter("Number").AsString()
	elem_name = number+"_"+name
	# collect geometry element
	geoobj = e.Geometry[Options()]
	# make enum to get geometry object
	enum = geoobj.GetEnumerator();
	shape = [i for i in enum][0]

	# create direct shape type
	shapeType = DirectShapeType.Create(doc,elem_name,objId)
	# set shape to geometry list shape
	shapeType.SetShape([shape])
	# get dslibrary
	lib = DirectShapeLibrary.GetDirectShapeLibrary(doc)
	# add type to dslibrary
	lib.AddDefinitionType(elem_name,shapeType.Id)
	# create element by shapetype
	element = DirectShape.CreateElementInstance(doc,shapeType.Id,objId,elem_name,Transform.Identity)
	# set typeId
	element.SetTypeId(shapeType.Id)
	# append to output
	res.append(element)
# transaction end
TransactionManager.Instance.TransactionTaskDone()

OUT = res
Advertisements

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.