Reading Family files

Reading Family files

Anonymous
Not applicable
2,999 Views
8 Replies
Message 1 of 9

Reading Family files

Anonymous
Not applicable

Hi all,

 

I've been working on an app which reads details of Family (rfa) files and displays them in a WPF pane.  Everything is functioning nicely.  However to access Family details (Name, Category etc.) I'm temporarily loading them into the active document in a transaction, reading and storing the details I want and then rolling back the transaction.  This fine for the small amount of files I'm working with, but has some downsides (for example LoadFamily doesn't appear to create an out if a family already exists within a project).

 

 

I started putting together an app which would use multiple threads to perform these operations, however from what I've read it seems like this is frowned upon and generally not a good idea.

 

Does anybody have any alternative suggestions as to how I could read Family Name, Category etc. but not perform the LoadFamily operation on the current active document?

 

I hope that made sense!

 

 

Cheers,

Adam

 

0 Likes
Accepted solutions (1)
3,000 Views
8 Replies
Replies (8)
Message 2 of 9

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Adam,

 

Happy New Year to you!

 

That sounds like an interesting and useful project.

 

Attempting to call a Revit API method from a separate thread is not only frowned upon, it would probably cause an exception to be thrown, since Revit tries to protect itself against such invalid and corrupting abuse.

 

The reason the call to LoadFamily returns null if the family has already been loaded: Revit only reloads the family if it has been modified externally since it was last loaded. You may be able to override that behaviour using the IFamilyLoadOptions interface; I am not sure, though:

 

http://www.revitapidocs.com/2017/d447ed92-74e1-2125-dd0a-38a5ae85ce53.htm

 

I do have an alternate suggestion for you: rather than loading the family into the current project, you could also just open the family as a separate background document.

 

That ought to create less overhead and therefore perform more efficiently.

 

I hope this helps.

 

Please let us know what you find out and how you end up resolving this.

 

Thank you!

 

Cheers,

 

Jeremy



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

Message 3 of 9

Anonymous
Not applicable

Hi Jeremy,

 

Happy New Year to you also.

 

Thank you for the further clarification regarding threading, I will definitely give up my pursuits in this.  That makes sense now that I think about it, and I'll look into overriding using the IFamilyLoadOptions interface to see if it is possible, even though I won't use this as your alternative suggestions sounds like a far better path for me to follow.

 

Can I accomplish this using the OpenDocumentFile method to open the family file into memory?

 

 

Many thanks for your advice.

Adam

0 Likes
Message 4 of 9

Anonymous
Not applicable

Just to provide an update of the solution I went with in the end:

 

As Jeremy suggested, loading the Family files into memory via OpenDocumentFile was a lot less resource heavy than loading the family into the document, extracting the details and then unloading.

 

 

One problem is that the thumbnail displayed in Windows Explorer isn't present on family files upgraded to the local version of Revit.  I'm assuming this is because there is no view set on the upgraded document, would this be correct?

 

If so, can I set the view on the document without it being active in the UI?

 

 

Cheers,

Adam

0 Likes
Message 5 of 9

Anonymous
Not applicable

Hi all,

 

Never mind, I didn't realise the Document.Close automatically saved the document (my own fault for skimming the documentation), after switching to the Document.Close Method with a bool I didn't need to mess around with the views anymore.

 

 

Many thanks for the help,

Adam

0 Likes
Message 6 of 9

ratno123s
Contributor
Contributor

Happy to new year to all of you,

 

I am working on a similar project which collects the information of a revit family files looping through all folders in a  location and saves it in database.

I am using wpf dockable window which raises an event. The execute method of the event is as follows

 

public void Execute(UIApplication uiapp)
{
this.app = uiapp.Application;
this.doc = uiapp.ActiveUIDocument.Document;



try
{
List<string> rfaFiles = Directory.GetFiles(@"\\dtp714\Revit Library", "*.rfa", SearchOption.AllDirectories).ToList();


for (int i = 0; i < rfaFiles.Count; i++ )
{
C_Family familyNode = getfamilyData(rfaFile); 

/*

The familyNode object has family data which i save to data base

*/

}


}
catch (Exception ex)
{

}

}

 

public C_Family getfamilyData(string familyPath)
{

try
{

Document newDoc = this.app.NewProjectDocument(resource+"DefaultMetric.rte");
Transaction t1 = new Transaction(newDoc, "LoadFamily");
t1.Start();
Family family;
newDoc.LoadFamily(familyPath, out family);
t1.Commit();

C_Family familyNode = new C_Family();

Document familyDoc = doc.EditFamily(family);
FamilyManager fManager = familyDoc.FamilyManager;

/*

the family object and the fManager object is used to get all the information

*/


familyDoc.Dispose();
newDoc.Dispose();
return familyNode;
}
catch (Exception ex)
{
//TaskDialog.Show("Exception", ex.Message);
//TaskDialog.Show("error", ex.StackTrace);
}
return null;
}

 

This code is working fine and I am also getting the datas... but after the code has processed about 200 families the computer starts hanging and the OS shows a message that "Computer is low on memory"

Is it that I am not releasing api resources properly? Please let me know

 

 

 

0 Likes
Message 7 of 9

jeremytammik
Autodesk
Autodesk

Happy New Year to you too!

 

What you are observing is normal.

 

Revit was designed to be driven by a user who closes it down at the end of the day after processing an handful or a couple of handfuls of documents.

 

If you process hundreds of documents automatically in a batch, you must expect any application designed for end user interaction to encounter problem sooner or later.

 

Therefore, when driving such an application for batch processing, you need to deal with such issues gracefully.

 

Here are some examples of similar discussions:

 

http://thebuildingcoder.typepad.com/blog/2015/08/batch-processing-dwfx-links-and-future-proofing.htm...

 

http://thebuildingcoder.typepad.com/blog/2014/12/au-ends-and-batch-rendering-across-several-projects...

 

Cheers,

 

Jeremy



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

Message 8 of 9

ratno123s
Contributor
Contributor

Thanks Jeremy for the suggestions. 

 

There is one more problem that I am facing. Sometimes when the path is too long the doc.LoadFamily(familypath, out family) gives a null family...

But when trying to load a family manually in revit (Insert->LoadFamily) the families are getting loaded..

 

So how does revit manages to handle such long paths whereas doing the same through api gives us null family object??

 

0 Likes
Message 9 of 9

jeremytammik
Autodesk
Autodesk

That sounds very strange.

 

We should probably take a closer look.

 

Could you please provide a minimal reproducible case for me to pass on to the development team for further analysis?

 

In order to explore this matter further, we will need to provide a minimal reproducible case for them to analyse in depth:

 

  • A short exact description of what you are trying to achieve.
  • The behaviour you observe versus what you expect, and why this is a problem.
  • A complete yet minimal Revit sample model to run a test in.
  • A complete yet minimal macro embedded in the sample model or Visual Studio solution with add-in manifest that can be compiled, loaded, run and debugged with a single click to analyse its behaviour live in the sample model.
  • Detailed step-by-step instructions for reproducing the issue, e.g. which element to pick, what command to launch etc.

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b

 

Thank you!

 

Cheers,

 

Jeremy



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

0 Likes