Missing MEP connectors after copy/paste Revit API CopyElements CopyOptions

Hi, can you please point me in the right direction: I’m able to copy/paste elements (from another opened doc or a link) but the issue is that the ducts and duct fitting elements (or pipes and pipe fittings) are not connected. They are pasted as individual elements that the user needs to connect although the original elements are connected. I could use some help implementing other API methods that also copies the connector of the elements before pasting. Here is my work in progress script

from Autodesk.Revit.DB import *
uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
rvt_year = int(app.VersionNumber)
all_cats = doc.Settings.Categories

# pyrevit
selection = revit.get_selection()

linkedModelName = ''
mylinks = []
if len(selection) > 0:
    for el in selection:
        if isinstance(el, DB.RevitLinkInstance):
            linkedModelName = el.Name.split(':')[0]
            print(linkedModelName)

        if linkedModelName:
            mylinks.append(el)
else:
    forms.alert('At least one linked element must be selected.')

linkInst = mylinks[0] # Check
linkDoc = linkInst.GetLinkDocument()

# Erik Frits
# GET ALL CATEGORIES 
all_categories = doc.Settings.Categories

# FILTER TO ANNOTATION CATEGORIES
cats_model      = [cat for cat in all_categories if cat.CategoryType == CategoryType.Model]
cats_annotation = [cat for cat in all_categories if cat.CategoryType == CategoryType.Annotation]
cats_analytical = [cat for cat in all_categories if cat.CategoryType == CategoryType.AnalyticalModel]
cats_internal   = [cat for cat in all_categories if cat.CategoryType == CategoryType.Internal]

def get_linked_elements_by_category(linkDoc, cat_name):
    cats = [cat for cat in all_cats if cat.CategoryType == CategoryType.Model]
    my_cats = [c for c in cats if c.Name == cat_name]
    
    if not my_cats:
        return []

    revit_cat = my_cats[0]
    cat_filter = ElementCategoryFilter(revit_cat.Id)
    linked_elements = FilteredElementCollector(linkDoc).WherePasses(cat_filter).WhereElementIsNotElementType()
    return linked_elements.ToElementIds()

def copy_elements_to_doc(source_doc, target_doc, element_ids):
    if element_ids:
        copy_options = CopyPasteOptions()
        ElementTransformUtils.CopyElements(source_doc, element_ids, target_doc, None, copy_options)


all_cats = doc.Settings.Categories

# List of cat names to process
cat_names = ['Ducts', 'Duct Fittings']

# Start a transaction
transaction = Transaction(doc, 'Copy Linked elements')
transaction.Start()

# Loop through cat names and copy linked elements
for cat_name in cat_names:
    linked_elements_ids = get_linked_elements_by_category(linkDoc, cat_name)
    copy_elements_to_doc(linkDoc, doc, linked_elements_ids)

transaction.Commit()
print('Linked elements now copied')

Just a Suggestion:
You can try grouping elements and copying the group itself.

Thank you sir, that worked. I tested for just duct, fittings, and placeholders and now connected. I’m hoping pipe is the same as well.

1 Like