Create New Family Types ,BUT with error of "Exception: A sub-transaction can only be active inside an open Transaction"

python
dynamo

#1

Hi ,
I want to Create many new door Types by given varies door types and specific door names, I know there is a node called “ElementType.Duplicate” in Sprins Package, but i want to use the NewType() method to write my code,to make things simple, I write a test code. Everything runs well until i add the code “familyManager.NewType(doorName)” ,the exception is shown in the pic.
can anyone help me out? any suggestion will be appreciated.
NewDoorType.dyn (4.9 KB)

import clr
clr.AddReference(“RevitAPI”)

import Autodesk

from Autodesk.Revit.DB import *

import os

import Autodesk.Revit.DB
from Autodesk.Revit.DB import IFamilyLoadOptions

import sys
import System
pyt_path = r’C:\Program Files (x86)\IronPython 2.7\Lib’
sys.path.append(pyt_path)
import os
from os import path

Import DocumentManager and TransactionManager

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

#Import ToDSType(bool) extension method
clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)

def EditFamilyTypes(document,familySymbolInput,doorName):
TransactionManager.Instance.ForceCloseTransaction()
if None == document or None == familySymbolInput:
return
family = familySymbolInput.Family
familyDoc = document.EditFamily(family)
familyManager = familyDoc.FamilyManager
familyTypes = familyManager.Types
familyTypesItor = familyTypes.ForwardIterator()
familyTypesItor.Reset()
types = []
bl = []
newTypes = []
while familyTypesItor.MoveNext():
familyType = familyTypesItor.Current
types.append(familyType)

for type in types:
	if type.Name != doorName:
		# bl.append("True")
		newTypes.append(familyManager.NewType(doorName))
return newTypes			

#Unwrap
doorType = UnwrapElement(IN[0])
doorName = UnwrapElement(IN[1])

#Start Transaction
doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)
#=============================================================================================
newDoorType = EditFamilyTypes(doc,doorType,doorName)

#End Transaction
TransactionManager.Instance.TransactionTaskDone()
#=============================================================================================
OUT = newDoorType


#2

So…try use the sub transaction ))

trans = Autodesk.Revit.DB.SubTransaction()
trans.Start()

#you action

trans.Commit()

#3

probably (im not sure)#you action its:

for type in types:
	if type.Name != doorName:
		# bl.append("True")
		newTypes.append(familyManager.NewType(doorName))
return newTypes

#4

if its not work try another way

trans = Transaction(doc, 'bla bla')

trans.Start()

#you action

trans.Commit()

#5

Thanks for your quick reaction!
this way seems doesn’t work .the exception is:
“警告:IronPythonEvaluator.EvaluateIronPythonScript 操作失败。
Traceback (most recent call last):
File “”, line 61, in
File “”, line 47, in EditFamilyTypes
Exception: A sub-transaction can only be active inside an open Transaction.”


#6

so) open main transaction!))


#7
TransactionManager.Instance.EnsureInTransaction(doc)
...
trans = Transaction(doc, 'bla bla')

trans.Start()

#you action

trans.Commit()

...

 TransactionManager.Instance.ForceCloseTransaction()

#8

you mean that way to open the main transaction?


#9

trans = Transaction(doc, 'bla bla')

is not work?
try all that I wrote above


#10

Still doesn’t work,


#11

Still doesn’t work, I have upload the dyn file above, wold you help me to find out the problem?


#12

ok


#13

@Jackie_Yin
say what is final goal ?
is create new door type?


#14

The final goal is to create many doors (like single flush doors,fire doors and so on ) from AuotoCAD and rebuild them in Revit. the final barrier is what i have posted above.


#15

@til.shviger
I have stumbled in this step for many days, NOT making any progress, hope someone can help me out,Anyway thanks for your attention!


#16

but it looks like as you try to create new family type?


#17

I could be wrong , but your code will not work

let me show you little example

doorType = IN[0]

family=doorType.Family
names = IN[1]
doc = DocumentManager.Instance.CurrentDBDocument

familyDoc = doc.EditFamily(family)
TransactionManager.Instance.EnsureInTransaction(familyDoc)

new_fam=familyDoc.FamilyManager.NewType(names)

TransactionManager.Instance.ForceCloseTransaction()

familyDoc.SaveAs("NEW.rfa")
familyDoc.Close()

OUT = new_fam