Edit Family from project document

Edit Family from project document

dirk.neethling
Advocate Advocate
5,094 Views
2 Replies
Message 1 of 3

Edit Family from project document

dirk.neethling
Advocate
Advocate

I want to parameterise a Family from inside a project document. In a dialog, the user can choose a Family from a dropdown, then edit the paramters to create a new .rfa file, and then use that new loaded family in the project document. When I run the follwing code, the value for symbol is null:

 

Document familyDoc = doc.Application.OpenDocumentFile(FamilyPath);

using (Transaction trans = new Transaction(familyDoc, "trill"))

{

trans.Start();

var good = familyDoc.LoadFamilySymbol(FamilyPath,

FamilyName,

new FamilyLoadingOverwriteOption(),

out symbol);

trans.Commit();

}

 

Any ideas?

 

0 Likes
5,095 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

Hey Dirk, 

 

To edit a family within the project document you'll need to use the doc.EditFamily() command. 

 

2 days ago i asked a question on this forum about the use of this command, you can take a look: 

http://forums.autodesk.com/t5/revit-api/doc-editfamily-python/td-p/6214074

 

Inside the EditFamily function you need to put the loaded family definition document instances, from what i found on other forums/threads is that you get this by making an FilteredElementCollector and filter it for your specific family. So far i don't have a working example for Families, i do have one for FamilySymbols tho (this is in python btw): 

 

collector = FilteredElementCollector( doc )                           
collector.OfCategory( BuiltInCategory.OST_StructuralFraming )          
collector.OfClass( FamilySymbol ).ToElements()

 

for f in collector: 

        print f.Family.Name

 

Hope my 2 cents help 😉 Once you have the full answer to this question you've answered my question in the link above aswell. 

cheers

0 Likes
Message 3 of 3

Charles.Piro
Advisor
Advisor

Hi,

 

for access, edit and save a family in project, here is one method :

 

public Result Execute(ExternalCommandData extCmdData, ref string msg, ElementSet elmtSet)
        {
            UIApplication uiApp = extCmdData.Application;
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            Application app = uiApp.Application;
            Document doc = uiDoc.Document;
ocument docfamily;
            Family fam;
            Family famille;
            FamilySymbol famSym = null;

            try
            {

                Reference refsel = uiDoc.Selection.PickObject(ObjectType.Element, "Select Family Instance");
using (Transaction trans = new Transaction(doc))
                {
                    trans.Start("document");

                    Element elmnt = doc.GetElement(refsel.ElementId);
                    FamilyInstance fyInst = elmnt as FamilyInstance;
                    fam = fyInst.Symbol.Family;

                    trans.Commit();
                }
docfamily = doc.EditFamily(fam);
                docfamily.SaveAs(@"C:/Temp/NewFamily.rfa");
//If necessary, you can open a transaction in family :
using (Transaction trans = new Transaction(docfamily))
                {
                    trans.Start("family");
trans.Commit();
                }
                docfamily.Save();
docfamily.LoadFamily(doc, new CustomFamilyLoadOption());
}
            catch (Exception e)
            {
                msg = e.Message;
                return Result.Failed;
            }
            return Result.Succeeded;
        }
public class CustomFamilyLoadOption : IFamilyLoadOptions
        {
            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)
            {
                source = FamilySource.Family;
                overwriteParameterValues = true;
                return true;
            }
        }

Smiley Wink



PIRO Charles
Developer

PIRO CIE
Linkedin