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: 

Load Family again from RFA file without knowing family name

3 REPLIES 3
Reply
Message 1 of 4
muaslam
1075 Views, 3 Replies

Load Family again from RFA file without knowing family name

Hi,

 

I need to load some specific family symbols from an RFA file. I have fully qualified path of the family file and names of the variations to load. Users of the add-in can load some symbols in the first go. So, I load a complete RFA family at first, then, I delete the undesired symbols in the same transaction. Later, user may want to load some more symbols of the same family (different from the ones loaded previously). LoadFamily method won't load same family if the family is already loaded. How can I load the family again without having any information except RFA filename and some symbol names?

 

 

Previously, I have tried using doc.LoadFamilySymbol method, there I had another issue that if a user working on Revit 2016 wanted to load some 10 family symbols from an RFA designed in Revit 2015 or earlier. Upgrading RFA box used to appear as many times as number of family symbols were being loaded.

 

Here's how my code looks like,

 

internal static bool LoadFamilySymbols (string filename, string[] variations, Document doc)
{
    Family family = null;
    List<FamilySymbol> preLoadedFamilySymbols = new FilteredElementCollector (doc).OfClass (typeof (FamilySymbol)).Cast<FamilySymbol> ().ToList ();

    using ( Transaction transaction = new Transaction (doc, "Load Family Symbols") )
    {
        try
        {
            transaction.Start ();
            bool result = doc.LoadFamily (filename, new FamilyOptions (), out family);
            List<FamilySymbol> familySymbols = new FilteredElementCollector (doc).OfClass (typeof (FamilySymbol)).Cast<FamilySymbol> ().ToList ();
            foreach ( FamilySymbol familySymbol in familySymbols )
            {
                if ( family.Name.Equals (familySymbol.Family.Name) )
                {
                    if ( variations.Contains (familySymbol.Name) || preLoadedFamilySymbols.Contains (familySymbol) )
                        continue;
                    else
                        doc.Delete (familySymbol.Id);
                }
            }
            transaction.Commit ();
        }
        catch
        {
            transaction.RollBack ();
            return false;
        }
    }
    return true;
}

 

Any ideas?

 

 

Thanks,

Umar Aslam

3 REPLIES 3
Message 2 of 4
FAIR59
in reply to: muaslam

If the loaded family and the family in the family-document are the same  (no changes , same creation date??) Revit failes to load.

 

Solution, make a dummy change in the family-document before loading.

 

                    famdoc = revit.Application.Application.OpenDocumentFile(_path);
                    if (famdoc == null) return Result.Cancelled;

                    using (Transaction t1 = new Transaction(famdoc, "dummy"))
                    {
                        t1.Start();  
                        FamilyParameter par = famdoc.FamilyManager.AddParameter("sadgafh", BuiltInParameterGroup.INVALID, ParameterType.Length, false);
                        famdoc.FamilyManager.RemoveParameter(par);
                        t1.Commit();
                    }
                    Family _loadedFam = null;
                    try
                    {
                        _loadedFam = famdoc.LoadFamily(document, new FamilyOptionalLoader1());
                    }
                    catch (Exception ex)
                    {
                        TaskDialog.Show("catch", ex.ToString());
                    }
                    famdoc.Close(false);
                    return Result.Succeeded;

 

 

Message 3 of 4
muaslam
in reply to: FAIR59

Hi,

 

I made the changes as you told but still it doesn't load family after loading it once. I am passing an instance of this FamilyOptions class to the FamilyOptions variable. Can you please tell me if I am doing something wrong here.

 

 

internal class FamilyOptions : IFamilyLoadOptions
{

 

    #region IFamilyLoadOptions Members

 

    public bool OnFamilyFound (bool familyInUse, out bool overwriteParameterValues)
    {
        overwriteParameterValues = true;
        return true;
    }

 

    public bool OnSharedFamilyFound (Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
    {
        overwriteParameterValues = true;
        source = FamilySource.Family;
        return true;
    }

 

    #endregion
}

 

 

Thanks,
Umar

Message 4 of 4
FAIR59
in reply to: muaslam

Your implementation of the  IFamilyLoadOptions is correct. The Parameter-values revert to the ones from the FamilyFile.

 

When I run my code, Revit loads the family every time ( Load Family in the Undo transaction-List )

 

I presume Revit throws a Exception somewhere. So you'd have to find out where.

Use this for every try/catch block

                    try
                    {
                         ....
                    }
                    catch (Exception ex)
                    {
                        TaskDialog.Show("catch", ex.ToString());
                    }

Then you get an  idea where and what goes wrong.

 

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