So I need to load my current family into a list of families opened in the background. Initially I was trying to use Orchid package for this, but that was throwing an error that it failed to overwrite shared family. I am very new to the API but I started poking around in the API docs and found FloadOptions- just what I seemed to need.
I got most of the base for this code from a Gavin Crump node to open files in the background, and from a Python Pyrevit tutorial. Here’s the code as it stands now-
#thanks to Gavin Crump for the base code here
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit import DB
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
clr.AddReference('System')
from System.Collections.Generic import List
currDoc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
def tolist(input):
result = input if isinstance(input, list) else [input]
return result
paths = tolist(IN[0])
docs = []
#opening a list of docs in the background and sending it to docs
for path in paths:
try:
doc = app.OpenDocumentFile(path)
docs.append(doc)
except:
docs.append(None)
#from a Pyrevit Workshop https://www.notion.so/Python-Revit-Coding-Sessions-3cdc3a58519140e2a0df8248e918aed1
class FamilyLoaderOptionsHandler(DB.IFamilyLoadOptions):
def OnFamilyFound(self, familyInUse, overwriteParameterValues):
overwriteParameterValues = True
return True
def OnSharedFamilyFound(self, sharedFamily, familyInUse, source, overwriteParameterValues):
source = DB.FamilySource.Family
overwriteParameterValues = True
return True
#mycode, attempting to iterate the LoadFamily method and Hassa's Floader class over my list of background docs
for doc in docs:
TransactionManager.Instance.EnsureInTransaction(doc)
fload_handler = FamilyLoaderOptionsHandler()
doc.LoadFamily(currDoc, fload_handler)
TransactionManager.Instance.TransactionTaskDone()
it throws this error when run:
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 56, in
Exception: The input argument “document” of function `anonymous-namespace’::Transaction_constructor or one item in the collection is null at line 94 of file E:\Ship\2022_px64\Source\Revit\RevitDBAPI\gensrc\APIUndoTransactionProxy.cpp.
Parameter name: document