0

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);
        }
    }
}

3 Answers 3

1

Run your code in the debugger and determine exactly which line is causing the problem. If it is the call to document.Regenerate, maybe it can simply be skipped. For numerous detailed solutions, please search the Revit API discussion forum for add family parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply. The error occurs at "FamilyParameter familyParameter = document.FamilyManager .AddParameter("parameterName", builtInParamGroup, category, false);". I suspect that something other than the code might be the problem.
0

I solved the issue by using the below method overload:

public FamilyParameter AddParameter( string parameterName, ForgeTypeId groupTypeId, ForgeTypeId specTypeId, bool isInstance )

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;


            // ParameterName en value
            string parameterName = "parameterNameLength";
            int parameterValue = 20;

            FamilyParameter familyParameter = document.FamilyManager.AddParameter(parameterName: parameterName, groupTypeId: GroupTypeId.Constraints, specTypeId: SpecTypeId.Length, isInstance: false);


            document.FamilyManager.Set(familyParameter, parameterValue);

            transaction.Commit();
            transaction.Dispose();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}

Comments

0

This is how to add text parameter

    public void AddParameter(Document familyDoc)
    {
        using(Transaction tx = new Transaction(familyDoc, "AddParameter"))
        {
            tx.Start();
            
            familyDoc.FamilyManager.NewType("New Type A"); //add new type - "new type" becomes current type
            FamilyParameter familyParameter = familyDoc.FamilyManager.AddParameter("parameterName", GroupTypeId.IdentityData, SpecTypeId.String.Text, false); //method overload to add text parameter
            familyDoc.FamilyManager.Set(familyParameter, "parameterValue");
            //familyDoc.FamilyManager.SetFormula(familyParameter, "\"parameterValue\"");
            
            tx.Commit();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.