Wednesday, June 21, 2017

Everybody Lies....

One of the ongoing themes of the show "House" (a medical drama from a few years back, starring Hugh Laurie as a modern medical Sherlock Holmes) - is that "Everybody Lies". This credo is applied generally to patients, some of whom lie deliberately, and some of whom lie incidentally. In either case, these lies present obstacles and sometimes trainwrecks in the way of getting to the truth...

I've recently run into a couple of examples of this with the Revit API. If you've discovered this page, maybe you're searching and this will help you too.

We've got an addin that's fairly old - goes back to the 2008 or 2009 era of Revit. The app scans all of the elements in the model and attempts to categorize them and process them. We started encountering customer models that could not be processed because what looked like duplicate element IDs. Surely Revit would not allow for multiple element IDs to share the same number, right? Seems fundamental!

After digging for a while in two different support cases, we found issues where when you interrogate elements about their ID or their Category, they lie!

For reasons lost to history, there are several places in the old code where instead of accessing the Id via "Element.Id" or the category via "Element.Category", the code instead used parameters:

Parameter idParam = elem.get_Parameter( BuiltInParameter.ID_PARAM);
Parameter catParam = elem.get_Parameter( BuiltInParameter.ELEM_CATEGORY_ID );

When we started digging into looking at each grouping of a category of elements to be processed, we would see things in the array of elements where some of the elements were "concrete types" like Pipes or Ceilings - but half of the elements to process were just abstract "Element" type objects.

Whatever these "Element" type objects are in the Revit Database, they seem to be related to the "actual" element in question. When you delete the actual element, they go away as well.  But when you ask them for their ID or Category, they respond back with their friend's category. And if you ask them their Id, they respond with their friend's Id.

So - maybe you'll never run into this issue. Certainly if we had been using the more traditional way of retrieving Categories and IDs, we wouldn't have.

But it's probably worth knowing that this is out here, and that Revit is tricky, and you can't always trust an element not to lie.  ;)