LoadFamily(Document, IFamilyLoadOptions) method in Dynamo, Python

Dear Dynamo community,

Does anyone has the experience to use LoadFamily(Document, IFamilyLoadOptions) in a python node to load a family back in to the project.

everytime i try to do this its telling me: “The document must not be modifiable before calling LoadFamily. Any open transactions must be closed.prior to the call.”

dynamo script:

the python script:

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
clr.AddReference('RevitAPI')
clr.AddReference("RevitServices")
from Autodesk.DesignScript.Geometry import *
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#// method for unwrapping dynamo elements to revitones // UnwrapElement()
# The inputs to this node will be stored as a list in the IN variables.
fams = UnwrapElement(IN[0])




# classes

class famloadopt(IFamilyLoadOptions):
	def OnFamilyLoad(familyInUse, overwriteParameterValues):
		overwriteParameterValues = True
		return True
	def OnSharedFamilyFound(sharedFamily, familyInUse, source, overwriteParameterValues):
		overwriteParameterValues = True
		return True


# Place your code below this line
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication


if doc.IsModifiable == True:
	output = "document is modifiable"
else:
	output = "document is not modifiable"
	famdoc = doc.EditFamily(fams[0])
	
	
	
	TransactionManager.Instance.EnsureInTransaction(famdoc)	
	famman = famdoc.FamilyManager
	famman.AddParameter("TESTPARAMETER2", BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, False) 
	TransactionManager.Instance.ForceCloseTransaction()
	



	if famdoc.IsModifiable == True:
		output = "familydocument is modifiable"
	elif doc.IsModifiable == True:
		output = "document is modifiable while loading"
	elif doc.IsReadOnly == True:
		output = "document is readonly while loading"
	else:
		TransactionManager.Instance.EnsureInTransaction(doc)
		try:
			famdoc.LoadFamily(doc, famloadopt()) 
		except Exception, e:
			output = e
		TransactionManager.Instance.TransactionTaskDone()
	




# Assign your output to the OUT variable.
OUT = output

thanks in advance

I have seen solutions that foreclose transactions before loading families. You actually do foreclose transactions but you open new transactions just before laoding the family. I think removing those lines near the end that have EnsureInTransaction and TransactionTaskDone should fix this.

i think i tried this before,
you mean like this:

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
clr.AddReference('RevitAPI')
clr.AddReference("RevitServices")
from Autodesk.DesignScript.Geometry import *
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#// method for unwrapping dynamo elements to revitones // UnwrapElement()
# The inputs to this node will be stored as a list in the IN variables.
fams = UnwrapElement(IN[0])




# classes

class famloadopt(IFamilyLoadOptions):
	def OnFamilyLoad(familyInUse, overwriteParameterValues):
		overwriteParameterValues = True
		return True
	def OnSharedFamilyFound(sharedFamily, familyInUse, source, overwriteParameterValues):
		overwriteParameterValues = True
		return True


# Place your code below this line
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication


if doc.IsModifiable == True:
	output = "document is modifiable"
else:
	output = "document is not modifiable"
	famdoc = doc.EditFamily(fams[0])
	
	
	
	TransactionManager.Instance.EnsureInTransaction(famdoc)	
	famman = famdoc.FamilyManager
	famman.AddParameter("TESTPARAMETER2", BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, False) 
	TransactionManager.Instance.ForceCloseTransaction()
	



	if famdoc.IsModifiable == True:
		output = "familydocument is modifiable"
	elif doc.IsModifiable == True:
		output = "document is modifiable while loading"
	elif doc.IsReadOnly == True:
		output = "document is readonly while loading"
	else:

		try:
			famdoc.LoadFamily(doc, famloadopt()) 
		except Exception, e:
		output = e






# Assign your output to the OUT variable.
OUT = output

i get the following:
family loading failed


so whats failing here?

Hi @Coen_Ooijevaar

Try this:

import clr
clr.AddReference('ProtoGeometry')
clr.AddReference('RevitAPI')
clr.AddReference("RevitServices")
from Autodesk.DesignScript.Geometry import *
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Place your code below this line
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
#// method for unwrapping dynamo elements to revitones // UnwrapElement()
# The inputs to this node will be stored as a list in the IN variables.
fams = UnwrapElement(IN[0])


class FamilyOption(IFamilyLoadOptions):
	def OnFamilyFound(self, familyInUse, overwriteParameterValues):
		overwriteParameterValues = True
		return True

	def OnSharedFamilyFound(self, sharedFamily, familyInUse, source, overwriteParameterValues):
		return True

try:
	result=[]
	for i in fams:
		family = UnwrapElement(i)
		TransactionManager.Instance.ForceCloseTransaction()
		famDoc = doc.EditFamily(family)
		if famDoc != None:
			famman = famDoc.FamilyManager
			TransactionManager.Instance.EnsureInTransaction(famDoc)
			famman.AddParameter("TESTPARAMETER2", BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, False)
			TransactionManager.Instance.TransactionTaskDone()
			result.append(famDoc.LoadFamily(doc, FamilyOption()))

				
except Exception, ex:
    result.append(ex.message)
# Assign your output to the OUT variable.
OUT = result 

5 Likes

Hey Kulkul,

Wow, it works, freakin awesome.

now i just got to figure out what you did differently from what i did. i guess it has to do with whenever you forceclose/ensure/taskdone transactions right?

anyway, thanks alot.

You where having unnecessary items like doc.Ismodifiable, By default all families are modifiable. And also i am closing transaction before editing family, this will ensure that no Transaction is open. Minimize as much as possible. Does that makes sense?

yeah i wanted to have feedback on whatever is going on.

Aha! that makes sense
thank again.