Loading Family into another Family and saving

Loading Family into another Family and saving

rvtquestions
Advocate Advocate
1,567 Views
2 Replies
Message 1 of 3

Loading Family into another Family and saving

rvtquestions
Advocate
Advocate

Before I begin let me preface:

  1. I understand there are other similar posts and have read through them, but they are  partial to my question.
  2. What has helped me most was the discussions between @Anonymous & @Anonymous here: https://forums.autodesk.com/t5/revit-api-forum/saving-family-after-editing-with-familymanager/td-p/5...
  3. I am running this in a blank new document project

I am looking to reload a family into another family and save that parent family. Unlike the thread mentioned above, I am using the OpenDocumentFile() class instead of the EditFamily() class.  I get an error stating

"saving is not allowed. File has been opened by another Revit instance"

The portion I am struggling with is:

 

SaveAsOptions saveoptions = new SaveAsOptions();
saveoptions.OverwriteExistingFile = true;
foreach (string parentPath in parentPaths)
{
Document familyDoc = doc.Application.OpenDocumentFile(parentPath); FilteredElementCollector fC = new FilteredElementCollector(familyDoc); ICollection<Autodesk.Revit.DB.Element> nestedFamilies = fC.OfClass(typeof(Autodesk.Revit.DB.Family)).ToElements();


foreach (Autodesk.Revit.DB.Family nestedFamily in nestedFamilies) { if (Some custom condition is met) { Document nfamilyDoc = doc.Application.OpenDocumentFile(filePath of family I want to reload); nfamilyDoc.LoadFamily(familyDoc, Custom iLoad Option); familyDoc.SaveAs(parentPath,saveoptions); nfamilyDoc.Dispose(); } }
}

 

I've tried running with both a manual start and commit transaction and with no transaction. Still same error. In the same thread mentioned above, there was a discussion about a similar issue but again EditFamily was used and not OpenDocumentFile. The solution in that thread was to use doc.SaveAs instead of the family doc but that does not work for my case. Sometimes it actually does Save the new updated family but it is inconsistent with errors. How do I resolve this issue to save the updated parent families? Thank you.

0 Likes
1,568 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk

Close ALL other documents.

 

It sounds to me as if you have a document open that contains a reference to the family you are trying to modify, blocking the modification. 

 

Maybe all loaded families are read-only.

 

Cheers,

 

Jeremy

 



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

0 Likes
Message 3 of 3

rvtquestions
Advocate
Advocate

I realize the possibility that for every family path I open, when the if condition is met I try to load a nested family back into that parent family, that this is where the conflict may exist. So I tried adding familyDoc.Close() right after the if statement to close the parent family I was querying but now I get a new exception.

 

"The referenced object is not valid, possibly because it has been deleted from the database, or its creation was undone."

 

0 Likes