I recently discussed the elimination of warnings about use of deprecated API methods from The Building Coder samples. In a similar vein, and for similar reasons, I also decided to migrate to Revit 2012 and clean up the RevitWebcam sample that I implemented to demonstrate the use of the Idling event when it was first introduced in the Revit 2011 API.
Please note that this sample is intentionally not related to any BIM specific workflow. A webcam just provides an obvious and accessible sample of a continuously updated and changing graphical dataset driven by something outside of Revit. I use this to demonstrate that we can sample this data and create a graphical display of it within the Revit model with no user interaction whatsoever. Other add-ins such as the MultithreadedCalculation Revit SDK sample simulate an externally changing data source. A real-world example might be displaying the graphical analysis visualisation of an asynchronous cloud-based calculation.
I migrated the 2011 version to 2012 in several steps:
- Flat migration and adaption of Idling event handler
- Removal of obsolete API warnings
- Final clean-up
Flat migration
I could have simply copied the existing project and source code files and modified them one by one as required for the 2012 API.
Instead, I used a different approach that I find more efficient, since it requires less manual steps: I created a completely new project using the Visual Studio Revit add-in wizard and then copied the relevant bits of source code that I wished to preserve from the original project to the new one.
Since the add-in wizard creates all the required settings, including generating and installing the add-in manifest, all in one single click, this approach requires less manual intervention. As an additional bonus, it also guarantees that all the application settings are up-to-date and nothing is forgotten.
Here is RevitWebcam2012_1_initial_migration.zip containing the result of the flat migration, which compiles all right but exhibits one little problem when executed:
Adaption of the Idling Event Handler
The flat migration enables the add-in to be compiled successfully. On running it, though, an exception is immediately thrown on the first call to the Idling event handler, since the type of the 'sender' argument changed from Application in the Revit 2011 API to UIApplication in the Revit 2012 API.
I presented a fully transparent method to support both of these versions simultaneously in the modeless loose connector navigator update plus its subsequent enhancement.
The code is still making use of some obsolete API calls, though, so I continued the migration to remove those as well:
Removal of Obsolete API Warnings
Compiling the flat migrated code described above results in the following warnings (copy and paste to an editor to see the untruncated lines):
------ Rebuild All started: Project: RevitWebcam, Configuration: Debug Any CPU ------ C:\a\src\revit\webcam\RevitWebcam\RevitWebcam\RevitWebcam\Command.cs(232,11): warning CS0618: 'Autodesk.Revit.DB.Analysis.AnalysisDisplayLegendSettings.SetTextTypeId(Autodesk.Revit.DB.ElementId, Autodesk.Revit.DB.Document)' is obsolete: 'this method will be obsolete from 2012.' C:\a\src\revit\webcam\RevitWebcam\RevitWebcam\RevitWebcam\Command.cs(398,11): warning CS0618: 'Autodesk.Revit.DB.Analysis.SpatialFieldManager.UpdateSpatialFieldPrimitive(int, Autodesk.Revit.DB.Analysis.FieldDomainPoints, Autodesk.Revit.DB.Analysis.FieldValues)' is obsolete: 'This method is obsolete in Revit 2012; use the overload accepting the result index instead.' C:\a\src\revit\webcam\RevitWebcam\RevitWebcam\RevitWebcam\Command.cs(386,23): warning CS0618: 'Autodesk.Revit.DB.Reference.GeometryObject' is obsolete: 'Property will be removed. Use Element.GetGeometryObjectFromReference(Reference) instead' C:\a\src\revit\webcam\RevitWebcam\RevitWebcam\RevitWebcam\Command.cs(431,31): warning CS0618: 'Autodesk.Revit.DB.Reference.Element' is obsolete: 'Property will be removed. Use Document.GetElement(Reference) instead' C:\a\src\revit\webcam\RevitWebcam\RevitWebcam\RevitWebcam\Command.cs(434,31): warning CS0618: 'Autodesk.Revit.DB.Reference.GeometryObject' is obsolete: 'Property will be removed. Use Element.GetGeometryObjectFromReference(Reference) instead' Compile complete -- 0 errors, 5 warnings
To analyse the exact changes made to fix these, unpack the initial migration and the final version below in two directories side-by-side and use a file comparison tool.
Final Clean-up
Final clean-up included changes to comments and line breaks.
Here is RevitWebcam2012_3_final_cleanup.zip containing the cleaned up code with all obsolete API usage removed.
Here is a snapshot of RevitWebcam up and running in Revit 2012:
The image is upside-down now, which does not worry me, since this is all about getting it to display and update automatically at regular intervals, and all else is of no concern to us.
Disclaimer: This application does not adhere to the recommended rules for interacting with an external asynchronous process, e.g. a modeless dialogue, so this code should not be used as is for production use.
The code presented here is just a test showing some aspects of possible uses of the Revit API functionality. It is by no means a production-level solution. This actually applies to all the code provided in this blog anyway. In truth, it applies just as well to many a commercial application that I find myself forced to use, much to my chagrin :-)