Batch DWG link creation

Hi guys,

I am trying to do batch linking of DWGs. The intended workflow is to just state a folder path, and then load everything into one view, and sort the DWG-s to their respective floors afterwards. Here is my code. I think the error I get is a weird one, given that both the expected and the given argument are valid arguments according to the documentation. What am I doing wrong? Any help is appreciated :slight_smile:

Error:
TypeError: expected DGNImportOptions, got DWGImportOptions.

Code:
‘’’ Load all links in given path’’’

from Autodesk.Revit.DB import DWGImportOptions, ElementId,ImportInstance, ImportPlacement,ImportUnit,LinkLoadResult,Transaction,TransactionGroup

import glob

class Revit():

    uidoc = __revit__.ActiveUIDocument

    doc = __revit__.ActiveUIDocument.Document

revit=Revit()

    # ----------

opt = DWGImportOptions()

opt.Placement = ImportPlacement.Origin

opt.AutoCorrectAlmostVHLines=False

opt.ThisViewOnly = False

opt.Unit = ImportUnit.Millimeter

linkId = ElementId.InvalidElementId

tgroup = TransactionGroup(revit.doc,"Fast load links")

tgroup.Start()

folderpath = "C:\\CAD\\BIM 360\\Path\\To\\Files

filequery = "*.dwg"

search = folderpath+filequery

filenames = glob.glob(search)

for file in filenames:

    t = Transaction(revit.doc,"Load link")

    t.Start()

    # result = LinkLoadResult()  # Didn't work

    print(file)

    linkId = ElementId.InvalidElementId

    # ImportInstance.Create(revit.doc,revit.doc.ActiveView,file,opt,result) # Didn't work

    revit.doc.Link(file,opt,revit.doc.ActiveView,linkId)

    t.Commit()

tgroup.Assimilate()

This one should do.
I fixed quite a few things
imported the whole pyrevit module but I guess core +forms should be enough
I added the folder choice.
used the import instance piece that did not work
removing ‘result’ helped
all in all it feels like it’s only missing the option choices to get a nice tool into pyRevit ;p
cheers
:point_down:

Thank you very much @Jean-Marc ! :grin:

The script doesn’t run for me. What is the revit variable(neither I nor the script recognize it)? I have used that wrapper-class for the revit instance to avoid simultaneous pyrevit scripts colliding (or something like that, I’ll admit i have adopted a colleagues practice without fully understanding it). But I am used to declaring a new Transaction variable for each transaction ( t=Transaction(“Name”) ), but you seem to use it like a member function.

I was too quick, I tried to clean up the code and removed the ‘from pyrevit import forms’ line
my bad
I added a try except to take care of the case when someone cancels the folder selection window
here is the code

from Autodesk.Revit.DB import DWGImportOptions, ElementId,ImportInstance, ImportPlacement,ImportUnit,LinkLoadResult,Transaction,TransactionGroup
import glob
from pyrevit import *
from pyrevit import forms

doc = __revit__.ActiveUIDocument.Document

opt = DWGImportOptions()
opt.Placement = ImportPlacement.Origin
opt.AutoCorrectAlmostVHLines=False
opt.ThisViewOnly = False
opt.Unit = ImportUnit.Millimeter
linkId = ElementId.InvalidElementId

folderpath = forms.pick_folder("Pick folder containing DWG files")
try:
    filequery = "\*.dwg"
    search = folderpath+filequery
    filenames = glob.glob(search)
    
    with revit.Transaction("Load CADs from folder"):
        for file in filenames:
            print(file)
            linkId = ElementId.InvalidElementId
            ImportInstance.Create(doc,doc.ActiveView,file,opt) # removed result
            print("Loaded")
except:
    print("You forgot to pick a folder")

Like you I used a practice from somebody else to simplify the transaction. I don’t see why you would want to go back one step like unlinking just the last few files and not the whole linking process through
I never got too deep in the transaction theme to be able to answer properly!

Ref my last question:
The try-except catches everything including:
NameError: name ‘revit’ is not defined…

I turned things around to get to you approach of transactions:

from Autodesk.Revit.DB import DWGImportOptions, ElementId,ImportInstance, ImportPlacement,ImportUnit,LinkLoadResult,Transaction,TransactionGroup
import glob
from pyrevit import *
from pyrevit import forms

doc = __revit__.ActiveUIDocument.Document

opt = DWGImportOptions()
opt.Placement = ImportPlacement.Origin
opt.AutoCorrectAlmostVHLines=False
opt.ThisViewOnly = False
opt.Unit = ImportUnit.Millimeter
linkId = ElementId.InvalidElementId

folderpath = forms.pick_folder("Pick folder containing DWG files")
if folderpath == None:
    forms.alert('You forgot to pick a folder', title='pyRevit DWGs Loader', ok=True, cancel=False, yes=False, no=False, retry=False)
else:
    filequery = "\*.dwg"
    search = folderpath+filequery
    filenames = glob.glob(search)

    tgroup = TransactionGroup(doc,"Fast load links")
    tgroup.Start()
    for file in filenames:
        t = Transaction(doc,"Load link")
        t.Start()
        print(file)
        linkId = ElementId.InvalidElementId
        ImportInstance.Create(doc,doc.ActiveView,file,opt) # removed result
        print("Loaded")
        t.Commit()
    tgroup.Assimilate()
1 Like

Removing the result-part did the trick, thank you! :slightly_smiling_face:

2 Likes