Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Opening families from an already open document

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
856 Views, 2 Replies

Opening families from an already open document

I have a document/project that is open, it contains some families, some available on disk, some not.

 

currently I am using : 

 

familyDoc = this.revitApplication.Application.OpenDocumentFile(filename); 

 

 

to open a view for that family and displaying it, this works fine for families that are on disk and I know the path to.

 

it would simplify things a hell of a lot for me if I could just open the families from the project since Im already showing a list of families in the project.

I already have the family objects as such from the document.

when I do :

 familyDoc = Family.Document;

 

I just get the document for the open document/project, not the family as a document.

 

any ideas ?

 

 

Regards,

Emil

Tags (3)
2 REPLIES 2
Message 2 of 3
ollikat
in reply to: Anonymous

Hi

 

The Document property of the Family comes from the Element class. The documentation states:

 

"Returns the Document in which the Element resides."

 

So as you already observed, that's not the way of getting the Document object of the actual family.

 

As far as I know the only way to access Family's Document object is to call Document.EditFamily() method. It will return the Document object of the Famil it self.

 

There's one major drawback with this method. It hits the performance badly. So if you have many many families to lookup for, then you might have a problem. Another disadvantage is that hosting project document cannot have an open transactions when you call EditFamily.

 

Hope this helps.

Message 3 of 3
Anonymous
in reply to: ollikat

I did the following, it didnt quite solve my problem but it works.

 

Family fam = findFamilyByName(senderText);
string filepath = saveFamilyToDisk(fam, @"c:\Windows\temp\");
familyDoc = this.revitApplication.Application.OpenDocumentFile(filepath);
                 

using(Transaction t = new Transaction(familyDoc))
{
    t.Start("Adjust family settings");

    if (familyDoc != null)
    {
        Autodesk.Revit.UI.PreviewControl vc = PreviewElementHost.Child as Autodesk.Revit.UI.PreviewControl;
        if (vc != null) { vc.Dispose(); }

            IEnumerable<Autodesk.Revit.DB.View> views
            = new FilteredElementCollector(familyDoc)
                .OfClass(typeof(Autodesk.Revit.DB.View))
                .Cast<Autodesk.Revit.DB.View>()
                .Where<Autodesk.Revit.DB.View>(v => v.CanBePrinted);

            views.First().AreAnnotationCategoriesHidden = true;
            t.Commit();
            PreviewElementHost.Child = new Autodesk.Revit.UI.PreviewControl(familyDoc, views.First().Id);

private Family findFamilyByName(string senderText)
        {

            FilteredElementIterator familyIter = new FilteredElementCollector(this.revitApplication.ActiveUIDocument.Document).OfClass(typeof(Family)).GetElementIterator();

            while (familyIter.MoveNext())
            {
                Family doorFamily = familyIter.Current as Family;

                if(doorFamily.Name == senderText)
                {
                    return doorFamily;
                }
            }
            return null;
        }

        private string saveFamilyToDisk(Family fam, string path)
        {
            string filepath = path + fam.Name + ".rfa";
            try
            {
                Document doc = this.revitApplication.ActiveUIDocument.Document;
                Document famDoc = doc.EditFamily(fam);
                SaveAsOptions sao = new SaveAsOptions();
                sao.OverwriteExistingFile = true;
                famDoc.SaveAs(filepath, sao);
                famDoc.Close(false);
                return filepath;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

 

basicly I have to save the family to disk so I can use 

 

OpenDocumentFile

to open the file from disk, if OpenDocumentFile on the other hand could take say, a stream as a parameter, I could have saved it to memorystream and sent that in to OpenDocumentFile.

 

This causes a bit of overhead, but it works.

 

Regards,

Emil

 

Tags (3)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community