I am trying to add a parameter to a Revit family. When I open the Revit Family document and execute the code below in a macro, I get this error:
System.Exception: Document regeneration failed. at MacroModule.executeMacro_(MacroModule*, AString* MacroName) at MacroModule.executeMacro_(MacroModule*, AString* ) at UIMacroGeneralManager.runMacro(UIMacroGeneralManager*, MacroModule* pModule, AString* macroName)
Any idea how to solve this issue?
public void FamilyInstanceParameters()
{
Document document = this.ActiveUIDocument.ActiveView.Document;
if (!document.IsFamilyDocument)
{
TaskDialog.Show("Info", "This is not a FamilyDocument");
}
else
{
try
{
Transaction transaction = new Transaction(document);
transaction.Start("Param");
// Get the family document category
Family family = document.OwnerFamily;
Category category = family.FamilyCategory;
FamilyType familyType = document.FamilyManager.NewType("New Type A");
document.FamilyManager.CurrentType = familyType;
// Parameter group
BuiltInParameterGroup builtInParamGroup = BuiltInParameterGroup.PG_IDENTITY_DATA;
document.Regenerate();
FamilyParameter familyParameter = document.FamilyManager
.AddParameter("parameterName", builtInParamGroup, category, false);
document.FamilyManager.Set(familyParameter, "parameterValue");
transaction.Commit();
transaction.Dispose();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}