Copy dwg or cadlinks from one project to another

Hi all,

What i want:
I want to copy dvg files or the linked ojbects of these from one project to a new project.

What i currently get:
I manage to copy the cadlinks from one document to another, when i go to the manage links i can se all the dwg, but they are not in my enviroment…

Are i heading in the wrong direction here ? Or is this not possible if i dont have the acutale .dwg file
itself in a folder ?

My code:

from Autodesk.Revit import DB
from Autodesk.Revit.DB import *

import clr
clr.AddReference("System")
from System.Collections.Generic import List


doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application


#import
from pyrevit import script, revit, DB, forms
from System.Collections.Generic import List

########################################################################################################################
# Linkedobjects collector
allcadlinks = list(FilteredElementCollector(doc).
                OfClass(CADLinkType).
                WhereElementIsElementType().ToElements())
########################################################################################################################
#Get the doc
docTo = revit.doc
# Select background document
docFrom = forms.select_open_docs(title="Select a document", button_name="Select", width=500, multiple=False)

#Make sure the choice is suitable
if not docFrom:
    script.exit()

elif docFrom.IsFamilyDocument:
    forms.alert("Choose a document wich is not family document", title="Script cancelled")
    script.exit()
########################################################################################################################


ids_link = [e.Id for e in allcadlinks]
id_copy_link = List[DB.ElementId](ids_link)
cp_Opt_link = DB.CopyPasteOptions()
t_iden_link = DB.Transform.Identity

with revit.Transaction("Copying the links"):
     DB.ElementTransformUtils.CopyElements(docFrom, id_copy_link, docTo, t_iden_link, cp_Opt_link)
     msg = str(len(ids_piping)) + " elements copied"
     forms.alert(msg, title = "Script completed", warn_icon=False)

This is because you are copying the LinkType and not its placed Instance in the other model

I never coded something like this, and I have been using JOTools transfer single from Juan Osbourne to do just that, never came back, not worth the coding IMHO

Thaks for reply @Jean-Marc
Hmmm then i will have to try and figure out how to grab the instance. I tried to find it earlier, but i could not manage to find it in RevitLookup earlier. Do you know were i can find it ?

The copying is a small step of a bigger flow, and it will help me alot in my daily work if i can get it to work, so i dont want to use JOTools transfer single. Whant this to be tailored to the flow i want to achive, so for me it will deff be worth the coding :slight_smile:

Your code should work partially if you look for the import instance class instead of the cad link type

The copy may not be the right approach

  • Maybe recreate the cad link in your destination file from scratch using the information from your source file?

To help you understand links and their hierarchy:
https://thebuildingcoder.typepad.com/files/cp3455_revit_links_api_presentation.pdf

a discussion very similar to your concern with @AndreaTas