Family Manager Type Creation Fail


#1

BDDIV-SSH-XX-XX-DY-BI-0150-S0-P78_FamilyTypesMonitorByExcel_ReinardtBronkhorst.dyn (502.2 KB)

I specifically want to handle the case where a user has deleted the last type from a family and then I want to create a new type.

Please refer to: http://help.autodesk.com/view/RVT/2019/ENU/?guid=Revit_API_Revit_API_Developers_Guide_Revit_Geometric_Elements_Family_Documents_Managing_family_types_and_parameters_html

1

The code fails here:

			try: 
				famDoc.FamilyManager.NewType(xTypeName)
				loadOpts = FamilyOption()
				oFamily = famDoc.LoadFamily(doc, loadOpts) 
			except:
				pass

The error message is below, and note the script stops without passing values to the other nodes:

Even though the family type is now in the project:

3

Any help is greatly appreciated

Here is the full code

import clr 
import sys
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *
import math
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices") # Import DocumentManager
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import traceback
clr.AddReference('RevitAPI') #import clr
clr.AddReference('RevitAPIUI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Plumbing import *
from Autodesk.Revit.UI import *
clr.ImportExtensions(Revit.Elements)
import System 
clr.AddReference("DSCoreNodes")
from DSCore import *
class FamilyOption(IFamilyLoadOptions): 
	def OnFamilyFound(self, familyInUse, overwriteParameterValues):
		overwriteParameterValues = False
		return True
	def OnSharedFamilyFound(self, sharedFamily, familyInUse, source, overwriteParameterValues):
		source = FamilySource.Family
		overwriteParameterValues = True
		return True
doc = DocumentManager.Instance.CurrentDBDocument
xCategoryNames = IN[0]				
rCategories = IN[1]					
rFamilyCategories = IN[2] 			
oCategories = IN[3] 				
xFamilyNames = IN[4] 				 
rFamilyNames = IN[5] 				  
oFamilies = UnwrapElement(IN[6]) 	
xTypeNames = IN[7]					
rTypeNames = IN[8]					
oFamilyTypes = IN[9]				
output = []
for i, (xTypeName, xFamilyName, oFamily, rFamilyName, rCategory, rFamilyCategory) in enumerate(zip(xTypeNames, xFamilyNames, oFamilies, rFamilyNames, rCategories, rFamilyCategories), 0):
	if ((rFamilyName == "OK") and (rCategory == "OK") and (rFamilyCategory == "OK")):
		bip = BuiltInParameter.ALL_MODEL_FAMILY_NAME
		provider = ParameterValueProvider(ElementId(bip))
		evaluator = FilterStringEquals();
		rule = FilterStringRule(provider, evaluator, xFamilyName, True);
		filter = ElementParameterFilter(rule); 
		pFamilyTypes = FilteredElementCollector(doc).WhereElementIsElementType().WherePasses(filter).ToElements() 
		if len(pFamilyTypes) > 0:
			foundFamilyType = False
			for pFamilyType in pFamilyTypes:
				parameters = pFamilyType.Parameters
				for parameter in parameters:
					if parameter.Definition.Name == "Type Name":
						if parameter.AsString() == xTypeName: 
							foundFamilyType = True 
							oFamilyTypes.append(pFamilyType)
							rTypeNames.append("OK")
							break 
			if foundFamilyType == False: 
				TransactionManager.Instance.EnsureInTransaction(doc)
				try:
					oFamilyTypes.append(pFamilyTypes[0].Duplicate(xTypeName))
					rTypeNames.append("OK") 
				except:
					oFamilyTypes.append(None)
					rTypeNames.append("ERROR EXCEPT - FAILED TO DUPLICATE") 
				TransactionManager.Instance.TransactionTaskDone()
		elif len(pFamilyTypes) == 0:  
			TransactionManager.Instance.ForceCloseTransaction() 
			famDoc = doc.EditFamily(oFamily) 
			TransactionManager.Instance.EnsureInTransaction(famDoc)
			try: 
				famDoc.FamilyManager.NewType(xTypeName)
				loadOpts = FamilyOption()
				oFamily = famDoc.LoadFamily(doc, loadOpts) 
			except:
				pass
			TransactionManager.Instance.TransactionTaskDone()
			TransactionManager.Instance.ForceCloseTransaction()
			typelist = list()
			output.append(oFamily.GetFamilySymbolIds())
			for famtypeid in oFamily.GetFamilySymbolIds(): 
				typelist.append(doc.GetElement(famtypeid).ToDSType(True))
	else:
		oFamilyTypes.append(None)
		rTypeNames.append("ERROR - SOME FAMILY ISSUE")
OUT = xCategoryNames, rCategories, rFamilyCategories, oCategories, xFamilyNames, rFamilyNames, oFamilies, xTypeNames, rTypeNames, oFamilyTypes, output