Opne New family file via API

Opne New family file via API

Anonymous
Not applicable
572 Views
2 Replies
Message 1 of 3

Opne New family file via API

Anonymous
Not applicable

Hello all,

 

I am currently trying to automatically open a family file from local folder (here is my code):

 

public void OpenFamily(ExternalCommandData commandData)
{
    UIApplication uiapp = commandData.Application;
    bool hasOpenedFile = this.HasOpenedFile(uiapp);
    string path = "";

    if (hasOpenedFile)
    {
        System.Windows.MessageBox.Show("Please close the current project before importing a family!");
    }
    else
    {
        string fileImportPath = "file path here"

        UIDocument doc = uiapp.OpenAndActivateDocument(fileImportPath);

    }

}

 

 

private bool HasOpenedFile(UIApplication uiapp)
{
    if (uiapp.ActiveUIDocument != null)
    {
       return true;
    }
    return false;
}

 

If I open a Revit and try to open the file, everything works fine. The problem occurs if before that I had another family/project opened and after closing it I try to open the current family (actually there is no longer an active document).

The exception thrown is here OpenAndActivateDocument(fileImportPath) with message : "An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: BuildWindowCore failed to return the hosted child window handle.
"

Any ideas what may cause the problem and how can I fix it?

 

Thanks!

0 Likes
573 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk

You are probably doing something wrong.  🙂

 

Maybe it will work better if you open the new family before closing the old one.

 

When you close the last family, Revit has to move to a zero-document state.

 

That may change things more than when some document remains open.

 

We ran into a couple of issues in the past closing the last active document:

 

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 3

Anonymous
Not applicable

Hi Jeremy,

 

Thanks for your replay 🙂 Actually now I simplified it to just having a family open and trying to open anotherone trough the API and the same problem accures... 

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    UIApplication uiapp = commandData.Application;
    UIDocument doc = uiapp.OpenAndActivateDocument("pathToFileHere");

    return Result.Succeeded;
}

 

Again if first thing when openig revit is loading the family trough the API, it works fine.

0 Likes