Copy Levels And Grids

This post is about how you can copy levels and grids from the linked document to the current document by using IronPython in Revit Dynamo Application.

First to import the modules; clr to load .Net Assemblies as IronPython modules which are RevitAPI.dll for DBRevitServices.dll for DocumentManager and TransactionManager and System for .Net List. Collect the current document and store in doc variable.

import clr,System
clr.AddReference("RevitServices")
clr.AddReference("RevitAPI")

from Autodesk.Revit.DB import *
from System.Collections.Generic import List
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# the current document
doc = DocumentManager.Instance.CurrentDBDocument

The second part, the input linked document store in the linkdoc variable. And copy the grids and levels by using FilteredElementCollector class. FilteredElementCollector has three Constructors (see figure – 1.1 screenshot from revitapidocs). We will be using the first Constructor which has only one argument as a document.

filtered-element-collector
figure – 1.1 FilteredElementCollector Constructor

And applied OfClass method to collect the elements which belong to the specific class. In this case, Level class and Grid class. After that combine, the grid and the level elements list into a single list variable named collector. Collect these element’s Id and store in .Net type List ids variable. Start the transaction.

# input link document
linkdoc = UnwrapElement(IN[0])
# collect grids from the link document
collector = FilteredElementCollector(linkdoc).OfClass(Grid).ToElements()
# collect levels from the link document
levels = FilteredElementCollector(linkdoc).OfClass(Level).ToElements()
# convert python list
collector = list(collector)
# grid list combine with level list
collector.extend(levels)

# dot-net elementId list
ids = List[ElementId]([i.Id for i in collector])
# transaction start
TransactionManager.Instance.EnsureInTransaction(doc)

#copy template from link-doc to current-doc
copied_elements = ElementTransformUtils.CopyElements(linkdoc,ids,doc,Transform.Identity,CopyPasteOptions())

# transaction done
TransactionManager.Instance.TransactionTaskDone()
# get elements from ids
elements = [doc.GetElement(i) for i in copied_elements]
# output
OUT = elements

ElementTransformUtils Class has CopyElements method which can copy a set of elements from document to document. See figure – 1.2 and 1.3.

fig2
figure – 1.2 ElementTransformUtils.CopyElements Method
fig3
figure – 1.3 Parameters Description

Use doc.GetElement instance method to returns output values to copied elements.

Here is sample usage and the complete code is below.

get

import clr,System
clr.AddReference("RevitServices")
clr.AddReference("RevitAPI")

from Autodesk.Revit.DB import *
from System.Collections.Generic import List
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# current document
doc = DocumentManager.Instance.CurrentDBDocument

# input link document
linkdoc = UnwrapElement(IN[0])
# collect grids from the link document
collector = FilteredElementCollector(linkdoc).OfClass(Grid).ToElements()
# collect levels from the link document
levels = FilteredElementCollector(linkdoc).OfClass(Level).ToElements()
# convert python list
collector = list(collector)
# grid list combine with level list
collector.extend(levels)

# dot-net elementId list
ids = List[ElementId]([i.Id for i in collector])
# transaction start
TransactionManager.Instance.EnsureInTransaction(doc)

#copy elements from link-doc to current-doc
copied_elements = ElementTransformUtils.CopyElements(linkdoc,ids,doc,Transform.Identity,CopyPasteOptions())

# transaction done
TransactionManager.Instance.TransactionTaskDone()
# get elements from ids
elements = [doc.GetElement(i) for i in copied_elements]
# output
OUT = elements

Advertisement

4 thoughts on “Copy Levels And Grids

  1. I’m not working. I can’t find copy level and grid from link file.
    Quaser have just copy level and grid from link document. but it doesn’t make this.
    dynamo file upload please.

    Like

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 )

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.