Recently, I sought to create a routine in Dynamo/Python to copy or duplicate elements from an instance family from one XYZ coordinate to another XYZ coordinate. Unfortunately, I encountered difficulties in the process. Below is the code I have developed so far, along with the error:
import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *
clr.AddReference(‘RevitServices’)
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
def copy_elements(selected_elements):
from_xyz = XYZ(0, 0, 0)
to_xyz = XYZ(200, 200, 0)
for element_id in selected_elements:
element = doc.GetElement(element_id)
if element:
new_element_id = doc.CopyElement(element_id, to_xyz - from_xyz)
new_element = doc.GetElement(new_element_id)
uidoc.Selection.SetElementIds(List[ElementId]([new_element_id]))
selected_element_ids = IN[0]
copy_elements(selected_element_ids)
OUT = “Elements copied successfully.”
Now, you might be wondering about the reason behind this approach, thinking, “Why not use the copy function in REVIT?”. The future intention of this code is to read a list of points from various existing objects and redistribute them throughout the project to new coordinates.
I also considered the possibility of creating new points; however, this would introduce an additional complication due to the parameters that would need adjustment. Therefore, it’s not just about inserting new points but rather reflecting what already exists for each new point.
I hope I have clarified my motivation behind this request, and I appreciate any assistance in implementing the transfer of elements from an instance family.
So you are copying the original element, or you want to make a new instance and leave the existing one in place?
For the former there is a Element.MoveByVector node which will make sorry work of the task.
For the later taking the existing points, moving those by a vector, and creating new instance shouldn’t be too hard via standard nodes and should scale to a few thousand instances well enough. No need for Python here.
import clr
import sys
import System
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *
from System.Collections.Generic import List
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
cond=IN[0]
element_select=[]
if cond:#More than one elt
ref = uidoc.Selection.PickObjects(ObjectType.Element)
element_select = [doc.GetElement(i) for i in ref]
else:
ref = uidoc.Selection.PickObject(ObjectType.Element)
element_select=[doc.GetElement(ref)]
setids=List[ElementId]()
for e in element_select:
a=e.Id
setids.Add(a)
vec=XYZ(4/0.3048,0,0)
TransactionManager.Instance.EnsureInTransaction(doc)
b=ElementTransformUtils.CopyElements(doc,setids,vec)
TransactionManager.Instance.TransactionTaskDone()
OUT = b
Thank you very much for your quick response. In answer to your question, yes, I am copying existing elements from the project and replicating them in other parts of my project. For a better understanding, I work with Structural Masonry projects, and many walls are identical in the design. So, I select a wall that is similar to the others and apply the copy and rotate command. Here’s an example below:
Thank you very much for the quick response and for sharing your code. Although my question may seem simple at first glance, it’s incredibly helpful for my development. I invite you to read my previous response where I explain my need to update walls in a structural masonry project. Here, I’m posting the entire project.
It’s quite common to replicate identical walls, especially when there’s a need for updates. Therefore, I aim for my routine to recognize identical walls and update the others accordingly.
In summary: With your tip, I can continue my development. Congratulations and thank you very much.