Error when create New FamilyType

I have transaction so dont understand this error.
Please help me
Thank Advance!

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument


AllFamily = FilteredElementCollector(doc).OfClass(FamilySymbol).ToElements()

for i in AllFamily:
	if i.Family.Name == "H_Horizontal":
		FName = i

family = FName.Family
familyDoc = doc.EditFamily(family)
familyManager = familyDoc.FamilyManager

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

familyManager.NewType("HSb40N")

TransactionManager.Instance.TransactionTaskDone()

OUT = familyManager

image

Pretty clear message.
This transaction is trying to be opened as a sub-transaction. Are there other transactions floating around in your code?
Maybe try a more direct approach:

t = Transaction(doc, "My App")
with t:
    t.Start()
    try:
        familyManager.NewType("HSb40N")
        t.Commit()
    except:
        t.RolBack()
2 Likes

Reason with transaction because you are in edit family document enviroment, it is sub process inside a document. That why, open a new transaction with document is no meaning, your transaction need is in family document, let try with this code :

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(familyDoc)
# do some thing 
ransactionManager.Instance.TransactionTaskDone()

1 Like

Hi,

in addition to what has been said, try to force close current dynamo transaction before editing family

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument


AllFamilyType = UnwrapElement(IN[0])

for f_type in AllFamilyType:
	family = f_type.Family
	familyDoc = doc.EditFamily(family)
	familyManager = familyDoc.FamilyManager
	TransactionManager.Instance.ForceCloseTransaction()
	#Do some action in a Transaction
	TransactionManager.Instance.EnsureInTransaction(familyDoc)
	
	familyManager.NewType("HSb40Nyyzz")
	
	TransactionManager.Instance.TransactionTaskDone()

OUT = AllFamilyType
2 Likes

Hi
I tried run it, but show this error

image

Hi,

try this version

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument


AllFamilyType = UnwrapElement(IN[0])
AllFamilyIds = set([x.Family.Id for x in AllFamilyType])
AllFamily = [doc.GetElement(xId) for xId in AllFamilyIds]
name_to_add = "HSb40Nyyzz"

for family in AllFamily:
	familyDoc = doc.EditFamily(family)
	familyManager = familyDoc.FamilyManager
	allF_types_name = [x.Name for x in familyManager.Types]
	if name_to_add not in allF_types_name:
		TransactionManager.Instance.ForceCloseTransaction()
		#Do some action in a Transaction
		TransactionManager.Instance.EnsureInTransaction(familyDoc)
		
		familyManager.NewType(name_to_add)
		
		TransactionManager.Instance.TransactionTaskDone()

OUT = AllFamilyType
1 Like

Hi

I runned that code but seem like no family type "HSb40Nyyzz"create.

image

After edit, please save family and load to project again, coding process is similar with when you work manual.

  • Edit family
  • Add new Type
  • Save family
  • Load to project
2 Likes