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: 

Saving family after editing with FamilyManager

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Anonymous
4855 Views, 8 Replies

Saving family after editing with FamilyManager

Hello!

 

We are developing a solution that uploads Revit Family files to DMS server. First we added a few shared parameters that have the information of DMS record to our families.

 

if (doc.IsFamilyDocument)
{
    Family f = doc.OwnerFamily;
    FamilyManager manager = doc.FamilyManager;
//Check if parameters have already been added to Family and it's Symbols.
//If not then adding parameters using Transaction. if (manager.get_Parameter("DMS_DOCNUMBER") == null) { using (Transaction trans = new Transaction(doc, "PARAM_SET")) { trans.Start(); manager.AddParameter("DMS_DOCNUMBER", BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, true); manager.AddParameter("DMS_DOCVERSION", BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, true); manager.AddParameter("DMS_DOCTYPE", BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, true); manager.AddParameter("DMS_DOCPART", BuiltInParameterGroup.PG_IDENTITY_DATA, ParameterType.Text, true); trans.Commit(); } }
//Create FamilyParameter reference objects after parameters have been added or checked if added FamilyParameter docnr = manager.get_Parameter("DMS_DOCNUMBER"); FamilyParameter docvers = manager.get_Parameter("DMS_DOCVERSION"); FamilyParameter doctype = manager.get_Parameter("DMS_DOCTYPE"); FamilyParameter docpart = manager.get_Parameter("DMS_DOCPART");

//Setting parameter values for every Symbol in the family foreach (FamilyType ft in manager.Types) { if (!MaterialHasDocument(ft, manager)) { DocumentObj docObj = CreateDMSDocument("RVT"); using (Transaction trans = new Transaction(doc, "SET_PARAM")) { trans.Start(); manager.CurrentType = ft; manager.Set(docnr, docObj.docNumber); manager.Set(docvers, docObj.docVersion); manager.Set(doctype, docObj.docType); manager.Set(docpart, docObj.docPart); trans.Commit(); }
//This is where we upload the .rfa file
//MANY MANY ERRORS UploadOriginal(docObj, f, doc); } } } else { TaskDialog.Show("Info", "Family view not open."); }

 Everything works as it should up until UploadOriginal method call.

string path = Path.GetTempPath();
string name = family.Name;
string fName = name + ".rfa";
string fPath = path + fName;

//Revit throws an error on this line saying that Family is not editable
//What could cause this mayhem?
//To upload .rfa Family file I need to save it as a file first and that's what I try to do until mighty ERROR occurs Document famDoc = doc.EditFamily(family); famDoc.SaveAs(fPath); famDoc.Close(false); //application related code following...

 

What could be the reason of this error? How could I fix it? Must it be overwritten like a baus (BOSS)?

 

8 REPLIES 8
Message 2 of 9
jeremytammik
in reply to: Anonymous

Dear Raunoveb,

 

I implemented an add-in to update fonts in loaded families yesterday.

 

For some of the loaded families, EditFamily threw that same exception saying "Family is not editable".

 

I added an exception handler to skip them:

 

  try
  {
    r1.FamilyDocument
      = doc.EditFamily( f );

  }
  catch( Autodesk.Revit.Exceptions.ArgumentException ex )
  {
    r1.Skipped = true;
    results.Add( r1 );
    Debug.Print( "Family '{0}': {1}", f.Name, ex.Message );
    continue;
  }

 

Here is an excerpt of the add-in log; the families marked 'skipped' are the ones I mean:

 

result_message.png

 

I simply assumed that is normal.

 

Cheers,

 

Jeremy

 

p.s. cool video  🙂



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 9
Anonymous
in reply to: jeremytammik

I managed to skip those families by simply adding if(family.IsEditable) block around the code. Unfortunately for us these families we try to process must be processed so we can't just skip them.

I've had 2 ideas that could explain those errors:

  1. FamilyManager (fm) somehow locks the family currently open in Family Document and only allows us to Edit the family after it's released the family. However even after fm.Dispose() the EditFamily method threw that exception.
  2. (This idea could be related to first idea). One cannot just simply write a family to file just after the shared parameters have been changed/added. A "save" command must be somehow executed in order to "unlock" this family. However once again I found no methods in either FamilyManager or Family class that would allows us to do that.

I have danced around this problem for 3 days and have ran out of ideas. Any help "unlocking" these families would be greatly appreciated.

Message 4 of 9
jeremytammik
in reply to: Anonymous

Dear Raunoveb,

 

Thank you very much for pointing out the IsEditable predicate. I successfully replaced my exception handler by simply checking that instead.

 

Regarding your locked families, have you tried ensuring that absolutely no other documents are open, only the one and only family that you are trying to modify?

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 9
Anonymous
in reply to: jeremytammik

Thanks for your comment about any other open documents. I suddenly realized that in my main program I already had FamilyDocument open. When I used UploadOriginal() method I gave doc as an input and then tried:

var famDoc = doc.EditFamily(family);
famDoc.SaveAs(path);
famDoc.Close(false);

 That created another Family Document instance of the same family and that's why it threw this "Family not editable" exception. I changed it to:

doc.SaveAs(path);
//Can't close it since I have this family view open in Revit and API 
//doesn't have permission to close visibly open documents in Revit. //doc.Close(false);

 However now I've got problems with getting the Family.Name value from doc.OwnerFamily.Name. It always returns "" and all the files saved look like this "/folder/.rfa", when they should look something like this "/folder/NightLamp.rfa"

Message 6 of 9
jeremytammik
in reply to: Anonymous

Dear Raunoveb,

 

Cool.

 

Progress.

 

How about using the document title instead of the family name?

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 7 of 9
Anonymous
in reply to: jeremytammik

Using doc.Title did the trick. However it's weird that OwnerFamily.Name returned blank response.

Thanks for everything. And since your second answer provided most help regarding to the main question I'll mark that one as a solution.

Message 8 of 9
jeremytammik
in reply to: Anonymous

Dear Raunoveb,

 

Cooler still.

 

That was a quick solution.

 

Thank you for marking the solution and for the interesting discussion.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 9 of 9
jeremytammik
in reply to: jeremytammik

Dear Raunoveb,

 

I published another discussion on modifying, saving and reloading families on The Building Coder today:

 

http://thebuildingcoder.typepad.com/blog/2014/09/modifying-saving-and-reloading-families.html

 

Since this thread is related to that, I summarised it there as well.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

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

Post to forums  

Rail Community


Autodesk Design & Make Report