Elements copied from linked model in wrong location

I managed to copy the elements from linked model but they are in the wrong location. I’m a beginner at python but I think there is something to do with transforming the elements to match the current model coordinates? But I’m not sure which code to use or where to put it.

import clr

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('RevitServices')
from RevitServices.Persistence import *
from RevitServices.Transactions import *
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

from System.Collections.Generic import List
import System

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]

links = UnwrapElement(tolist(IN[0]))
cats = UnwrapElement(tolist(IN[1]))

for link in links:
	linkDoc = link.GetLinkDocument()
	catId = List[ElementId](c.Id for c in cats)
	filter = ElementMulticategoryFilter(catId)
	fec = FilteredElementCollector(linkDoc).WhereElementIsNotElementType().WherePasses(filter)

elem = list(fec)

ids = List[ElementId]([i.Id for i in elem])

TransactionManager.Instance.EnsureInTransaction(doc)

copied_elements = ElementTransformUtils.CopyElements(linkDoc,ids,doc,Transform.Identity,CopyPasteOptions())

TransactionManager.Instance.TransactionTaskDone()

elements = [doc.GetElement(i) for i in copied_elements]

OUT = elements

Try to use GetTotalTransform() instead of Transform.Identity.
Since they are in a link, they have a transform in the link, and related to the actual document.
To understand better, use RevitLookUp (you can found on jeremy tammik blog) and compare transforms

2 Likes

Thanks @FabioDeAgostini, I did just that and it worked. It looks pretty messy but it is a working script.

import clr

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('RevitServices')
from RevitServices.Persistence import *
from RevitServices.Transactions import *
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

from System.Collections.Generic import List
import System

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]

links = UnwrapElement(tolist(IN[0]))
cats = UnwrapElement(tolist(IN[1]))

for link in links:
	linkDoc = link.GetLinkDocument()
	t = link.GetTotalTransform()
	catId = List[ElementId](c.Id for c in cats)
	filter = ElementMulticategoryFilter(catId)
	fec = FilteredElementCollector(linkDoc).WhereElementIsNotElementType().WherePasses(filter)

elem = list(fec)

ids = List[ElementId]([i.Id for i in elem])

# transaction start
TransactionManager.Instance.EnsureInTransaction(doc)

#copy elements from link-doc to current-doc
copied_elements = ElementTransformUtils.CopyElements(linkDoc,ids,doc,t,CopyPasteOptions())

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