Quantcast
Channel: RevitNetAddinWizard & NavisworksNetAddinWizard
Viewing all articles
Browse latest Browse all 872

Revit .NET Document.LoadFamilySymbol and IFamilyLoadOptions

$
0
0

As a matter of fact, we addressed them a bit in an early post, and presented a few references there. In this post, let’s further reveal some secrets about them, none of which has clearly been delivered elsewhere, needless to say, in the following misleading messages on web. It is the exact reason why we recently had to post a few articles on this blog to get the whole mess cleared up.

How to programmatically load a new symbol from an existing Family in a Revit project

“You will find that neither of the following overloaded methods will work for you:
public bool Document::LoadFamilySymbol(string filename, string name)
public bool Document::LoadFamilySymbol(string filename, string name, out FamilySymbol symbol)
You will have to use the third version of this overloaded method:

public bool Document::LoadFamilySymbol(string filename, string name, IFamilyLoadOptions familyLoadOptions, out FamilySymbol symbol)”

To make the story complete, here are our posts that helped to clarify the matter:

Document.LoadFamilySymbol Method (String, String, IFamilyLoadOptions, FamilySymbol)
http://spiderinnet.typepad.com/blog/2013/10/documentloadfamilysymbol-method-string-string-ifamilyloadoptions-familysymbol.html

Work Or *NOT* - Document::LoadFamilySymbol(string filename, string name, out FamilySymbol symbol)
http://spiderinnet.typepad.com/blog/2013/10/work-or-not-documentloadfamilysymbolstring-filename-string-name-out-familysymbol-symbol.html

Document::LoadFamilySymbol(string filename, string name) Really *NOT* Work?
http://spiderinnet.typepad.com/blog/2013/10/documentloadfamilysymbolstring-filename-string-name-out-familysymbol-symbol-really-not-work.html

Revit .NET Document.LoadFamilySymbol(string, string) Really *NOT* Work?!?!
http://spiderinnet.typepad.com/blog/2013/09/documentloadfamilysymbolstring-filename-string-name-work-or-not.html

In this post however, we’d like to point out the following facts:

  • In the Revit API 2011, the IFamilyLoadOptions works together well with the signatures of both LoadFamily and LoadFamilySymbol methods having the interface specified as an argument.
  • In the Revit API 2012, the IFamilyLoadOptions may or may not work together with the signatures of both LoadFamily and LoadFamilySymbol methods having the interface specified as an argument.
  • In the Revit API 2013, the IFamilyLoadOptions does not work well together with the signatures of both LoadFamily and LoadFamilySymbol methods having the interface specified as an argument.
  • In the Revit API 2014, the IFamilyLoadOptions continually does not work well together with the signatures of both LoadFamily and LoadFamilySymbol methods having the interface specified as an argument.

The first statement has been proved many times, so we won’t waste words here.

The following code can verify the third and the fourth.

    public class FamilyOptionalLoader1 : IFamilyLoadOptions
    {
        #region IFamilyLoadOptions Members

        public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
        {
            if( !familyInUse )
            {
                overwriteParameterValues = true;
                return true;
            }
            else
            {
                MessageBox.Show("The family is in use.");
                overwriteParameterValues = false;
                return false;
            }
        }

        public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
        {
            if (!familyInUse)
            {           
                source = FamilySource.Family;
                overwriteParameterValues = true;
                return true;
            }
            else
            {
                MessageBox.Show("The shared family is in use.");
                source = FamilySource.Family;
                overwriteParameterValues = false;
                return false;
            }
        }

        #endregion
}


                string libPath = CachedDoc.Application.GetLibraryPaths()["Imperial Library"];
                string rfaFullName = Path.Combine(libPath, @"Columns\Doric Column.rfa");

                ElementId symbolId = FamilyOptionalLoader1_Loader(CachedDoc, rfaFullName);
                if (symbolId != ElementId.InvalidElementId)
                    MessageBox.Show("The family was successfully loaded.");
                else
                    MessageBox.Show("The family was NOT loaded.");



        public static ElementId FamilyOptionalLoader1_Loader(RvtDocument doc, string filename)
        {
            Transaction trans = new Transaction(doc, "FamilyOptionalLoader1_Loader");
            trans.Start();

            ElementId id;
            try
            {
                Family family;
                doc.LoadFamily(filename, new FamilyOptionalLoader1(), out family);

                id = family.Id;
                trans.Commit();
            }
            catch
            {
                trans.RollBack();
                id = ElementId.InvalidElementId;
            }

            return id;
        }


                string libPath = CachedDoc.Application.GetLibraryPaths()["Imperial Library"];
                string rfaFullName = Path.Combine(libPath, @"Columns\Doric Column.rfa");

                ElementId symbolId = FamilyOptionalLoader2_Loader(CachedDoc, rfaFullName, "16'  Height (10 2/3 x Dia.)");
                if (symbolId != ElementId.InvalidElementId)
                    MessageBox.Show("The symbol was successfully loaded.");
                else
                    MessageBox.Show("The symbol was NOT loaded.");



        public static ElementId FamilyOptionalLoader2_Loader(RvtDocument doc, string filename, string symbolname)
        {
            Transaction trans = new Transaction(doc, "FamilyOptionalLoader2_Loader");
            trans.Start();

            ElementId id;
            try
            {
                FamilySymbol familySymbol;
                doc.LoadFamilySymbol(filename,symbolname,  new FamilyOptionalLoader1(), out familySymbol);

                id = familySymbol.Id;
                trans.Commit();
            }
            catch
            {
                trans.RollBack();
                id = ElementId.InvalidElementId;
            }

            return id;
        }

The implementations of the IFamilyLoadOptions interface won’t be called up in either Revit 2013 or 2014 no matter whether the family (its symbols) is used/referenced/changed or not in the current Revit project.

Therefore, here we have to revise our words a bit as said in the early post. If the Revit API 2013 or 2014 is of concern, please just forget about the following signature of the Document.LoadFamilySymbol method.

public bool Document::LoadFamilySymbol(string filename, string name, IFamilyLoadOptions familyLoadOptions, out FamilySymbol symbol)

Using the other two signatures of the Document.LoadFamilySymbol method makes things just simple and reliable!

In terms of the second statement, please feel free to do another experiment to figure it out. Something new and interesting might just be found there.

By the way, the IFamilyLoadOptions implementations and the loader wrappers as quoted in this post were all created automatically by the Family Optional Loader as provided in RevitAddinWizard (Revit Addin Wizard), which can be got from the Download link at the bottom of the blog index page.


Viewing all articles
Browse latest Browse all 872

Trending Articles