Autodesk.Revit.Exceptions.InvalidOperationsException: This method can only be used with family documents.
CreateParams fails when it tries to reload the family into the project, if done manually changes (overwrite parameters in dialogue) are saved.
I want to make sure that different elemtents in a project has the required parameters.
public void CreateParams()
{
Autodesk.Revit.DB.Document myDoc = this.ActiveUIDocument.Document;
Autodesk.Revit.DB.Element curElement = myDoc.GetElement("be5b66d0-a60d-49ad-b755-521427f202e0-002c4c27");
FamilyInstance fInstance = curElement as FamilyInstance;
FamilySymbol FType = fInstance.Symbol;
Family Fam = FType.Family;
Autodesk.Revit.DB.Document myFamDoc = myDoc.EditFamily(Fam);
Transaction tran = new Transaction(myFamDoc,"Create");
tran.Start();
Parameter p1 = FType.get_Parameter("P1");
if (p1==null)
{
FamilyParameter fp1 = myFamDoc.FamilyManager.AddParameter("P1",BuiltInParameterGroup.PG_IDENTITY_DATA,ParameterType.Text,false);
}
Parameter p2 = FType.get_Parameter("P2");
if (p2==null)
{
FamilyParameter fp2 = myFamDoc.FamilyManager.AddParameter("P2",BuiltInParameterGroup.PG_IDENTITY_DATA,ParameterType.Text,false);
}
Parameter p3 = fInstance.get_Parameter("P3");
if (p3==null)
{
FamilyParameter fp3 = myFamDoc.FamilyManager.AddParameter("P3",BuiltInParameterGroup.PG_IDENTITY_DATA,ParameterType.Text,true);
}
tran.Commit();
tran.Dispose();
Transaction doctran = new Transaction(myDoc,"Doc");
doctran.Start();
myDoc.LoadFamily(myFamDoc,new LoadOptions());
doctran.Commit();
doctran.Dispose();
myFamDoc.Close();
}
public class LoadOptions: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;
}
}
Dear Mårten,
Thank you for your query.
I ran into the same problem back in 2011, i.e. using Revit 2012.
Trying to load the family document directly simply failed.
I worked around it by saving the family to a file and loading it from there:
http://thebuildingcoder.typepad.com/blog/2011/06/creating-and-inserting-an-extrusion-family.html
This problem has been solved since then, though, so your code should work fine in the current version without saving the family to a file.
One more comment: you can save yourself the calls to dispose of the transactions by encapsulating them in a 'using' statement:
I hope this helps.
Best regards,
Jeremy
So I solved the problem.
1. No transaction for the document
2. reverse the order of who loads who.
final code
myFamDoc.LoadFamily(myDoc,new LoadOptions());
myFamDoc.Close(false);