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
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
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!