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
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

# 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:
    doc=app.OpenDocumentFile(docpath)
    documents.append(doc)
for path in paths:
    family_doc = app.OpenDocumentFile(path)
    families.append(family_doc)

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


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

OUT = []


# Start Transaction



#core data processing
for path in paths:
    famDoc = app.OpenDocumentFile(path)
    #TransactionManager.Instance.EnsureInTransaction(famDoc)
    newfam = famDoc.LoadFamily(doc, FamilyOption())
    #NewFamilyInstance()
    familyInst = famDoc.FamilyCreate.NewFamilyInstance(pt.ToXyz(), famsymbol, Structure.StructuralType.NonStructural)
    OUT.append(path)
    TransactionManager.Instance.TransactionTaskDone()
    #famDoc.Close(False)

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:

1 Like

did you find a solution?

Hi Ruben.
Yes I got around to making a working solution which was very case spesific and not scalable to other uses without greater modifications.
I don’t have the code at hand at the moment, but I manually look up each nested family and keep loading them into separate document variables until i get to where I want.
Then i create a new XYZ(0,0,0) and insert the family I want to insert to the innermost family before then loading the documents back up to the main family, closing each transaction around each document modification.

Hope that made sense. I can come back with some code at a later point if you need me to :slight_smile:
Regards
A

2 Likes