Open and activate new family document from standard template

Open and activate new family document from standard template

DanielKP2Z9V
Advocate Advocate
1,821 Views
8 Replies
Message 1 of 9

Open and activate new family document from standard template

DanielKP2Z9V
Advocate
Advocate

Code below doesn't open and activate new family from the standard template despite working fine with .rfa or .rvt files. OpenAndActivateDocument returns null.

 

 

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;

public class CreateAndActivateNewFamily : IExternalCommand
{
    public Result Execute(
        ExternalCommandData commandData, 
        ref string message, 
        ElementSet elements)
    {
        UIApplication uiapp = commandData.Application;

        // Define the path to the family template
        string familyTemplatePath = @"C:\ProgramData\Autodesk\RVT 2024\Family Templates\English\Metric Detail Item.rft";

        // Open and activate the new family document
        UIDocument uidoc = uiapp.OpenAndActivateDocument(familyTemplatePath);
        

        return Result.Succeeded;
    }
}

 

0 Likes
Accepted solutions (1)
1,822 Views
8 Replies
Replies (8)
Message 2 of 9

Mohamed_Arshad
Advisor
Advisor
Accepted solution

Hi @DanielKP2Z9V 

 

    OpenAndActivateDocument Method (String) method supports only Revit Project, Project Template and Family Document. Family Templates are not supported in this method.

Workaround: you can save as the Revit Family Template and Open and Active the Family Document.

 

Workaround Code Snippet

 

String conceptualmassTemplatePath = famTemplatePath + @"\Conceptual Mass\Mass.rft";
           if (System.IO.File.Exists(conceptualmassTemplatePath))
           {
              //Family Template Not Supported
              //uiApp.OpenAndActivateDocument(conceptualmassTemplatePath); 
              Document familyDocument = uiApp.Application.NewFamilyDocument(conceptualmassTemplatePath);
              if (null == familyDocument)
              {
                 throw new Exception("Cannot open family document");
              }

              String fileName = Guid.NewGuid().ToString() + ".rfa";
              familyDocument.SaveAs(fileName);
              familyDocument.Close();

              uiApp.OpenAndActivateDocument(fileName);
}

 

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 9

DanielKP2Z9V
Advocate
Advocate

Really? Documention of the OpenAndActivateDocument Method says that "The file can be either a Revit project, template, or family document".

 

I've tried the workaround you proposed, but it caused Revit to hang for half a minute, while normally the template opens almost immediately.

 

This doesn't seem fair.

0 Likes
Message 4 of 9

Mohamed_Arshad
Advisor
Advisor

HI @DanielKP2Z9V 

 

  Yes, as you say it cause a minute hang, because it is doing 2 processes, or you can propose Idea in Autodesk forum. So that we can get a solution for this 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 5 of 9

DanielKP2Z9V
Advocate
Advocate

You are right, I've submitted it

 

By the way, do you know if the same thing could be automated in GUI in some way as opposed to going through Revit API? In other words, are you aware of any more or less reliable method to invoke newfamily command and then also choose Detail item file in the next dialog? I know this is off-topic, but if you know something like that, it might help me a lot in bridging such limitations in the API. I suppose this wouldn't be even Revit, but more Windows-specific method then?

0 Likes
Message 6 of 9

Mohamed_Arshad
Advisor
Advisor

HI @DanielKP2Z9V 

 

   You raised a Question in Forum that is good, better to raise a Idea in Autodesk Ideas. https://forums.autodesk.com/t5/revit-ideas/idb-p/302 


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 7 of 9

DanielKP2Z9V
Advocate
Advocate

Thanks again, I don't know how that happened. I've reposted it to the ideas.

 

I couldn't find a way to delete it, but left a note and hopefully some admin will remove it.

Message 8 of 9

Mohamed_Arshad
Advisor
Advisor

No Worries, if someone know any other workaround they will answer on it, let it be there


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 9 of 9

chas.barrott
Contributor
Contributor

I'd also like the family template option to work. The save as work around is clunky and takes too long. Hopefully Autodesk makes it available soon. Seems logical

0 Likes