Create familyinstance in innermost nested family from selected element

I would love to insert an instance of selected element in a model into a nested family file.
I have a family file “basisfamily.rfa” and a project im working on.
In the projectfile I have a family “genericfamily.rfa”.
I want to select “genericfamily.rfa” with Dynamo, then run a script that loads in, and inserts an instance of “genericfamily.rfa” with the familysymbol of the selected instance into the innermost nested subfamily of “basisfamily.rfa”, then copy and rename “basisfamily.rfa” to “genericfamily2.rfa” and load this new family back into the project.

Does anyone know what the heck i mean, and if this is possible?

Yes it is possible.
Try a little bit, look for “open document”, “load document”, “save document”…update this post with some news and thought, we will help you

Hi @FabioDeAgostini, thanks for your reply.
What I have done so far is to search around for possible solutions, and I found some code to get me going.
What I have accomplished so far is as follows:
Select element to insert into nested family.
This family gets saved to file, then loaded into the top level of nested family (I would like to get into the innermost nested family before loading).
When I then try to insert an instance of this family (and type) I get a huge “System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.” which seems to connect to “Autodesk.Revit.Creation.ItemFactoryBase.NewFamilyInstance”.

Ill attach the .dyn file, .rfa file and code here:
Nested Families.dyn (27.3 KB)
BasisFamily.rfa (568 KB)
I am trying to load any selected element’s family into the innermost nested family of “Basisfamily.rfa”

import clr
from Autodesk.Revit.DB import *
import Revit

# Revit and Dynamo modules
from Autodesk.Revit.DB import Document, FamilySource, IFamilyLoadOptions
from Autodesk.Revit.DB.Structure import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

currdoc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

#input assigned to IN variable
paths = IN[0]
docpaths = IN[1]
famsymbol = UnwrapElement(IN[2])
pt = IN[3]

#wrap input inside a list if not a list.
if not isinstance(paths, list): 
    paths = [paths]
if not isinstance(docpaths, list): 
    docpaths = [docpaths]

#ensure loaded families can overwrite existing families.
class FamilyOption(IFamilyLoadOptions):
    def OnFamilyFound(self, familyInUse, overwriteParameterValues):
        overwriteParameterValues = True
        return True

    def OnSharedFamilyFound(self, sharedFamily, familyInUse, source, overwriteParameterValues):
        source = FamilySource.Family
        overwriteParameterValues = True
        return True     

#core data processing
documents = []
families = []
for docpath in docpaths:
for path in paths:
    family_doc = app.OpenDocumentFile(path)

for document in documents:
    map(lambda family: family.LoadFamily(document, FamilyOption()),

# output assigned to the OUT variable
#OUT = [paths, docpaths]

OUT = []

# Start Transaction

#core data processing
for path in paths:
    famDoc = app.OpenDocumentFile(path)
    newfam = famDoc.LoadFamily(doc, FamilyOption())
    familyInst = famDoc.FamilyCreate.NewFamilyInstance(pt.ToXyz(), famsymbol, Structure.StructuralType.NonStructural)

map(lambda x: x.Close(False), families)
map(lambda x: x.Close(True), documents)
# End Transaction
1 Like

I didn’t check everything but, it is a critical point managing correctly the transactions: remember dynamo “live” in a document, but you are working on many others.
ForceCloseTransaction() is a more powerful way to close the document (look for switching view in dynamo)

1 Like

Thanks for your reply.
I am not able to make good use of this information. I hope anybody can help me further towards a working solution.

Thanks :slight_smile: