Tuesday, November 15, 2011

[Revit API]– Revit Remote Boot - A technique to run an API add-in on a particular Revit model without user interaction

At last years Autodesk University, someone mentioned that they were looking for a way to run an export on their model once a day, on a schedule. This is not something that the Revit API is generally used for, but it  got me thinking of ways that I could make it happen. While flying around the USA after AU I hacked together some prototype code which did just this, but left it shelved. For my AU 2011 virtual class, I decided to finish it off and make it public, in case someone else wants to use it.
The program is called ‘Revit Remote Boot’, and is really more of a ‘system’ than a program, there are a number of moving parts to make it all happen. Put simply, it is a couple of interacting programs that allow you to perform an operation on a Revit model from a script.
Take a look at this video, which is a small part of my AU Virtual class, for a full demonstration of what it does. Note that while it talks about using Revit Server to create a local model, if you are running just a pure file server, this is fine as well – you could just directly open the file. It’s just that running Revit Server opens up some interesting possibilities and was the topic of my AU presentation.

Revit Remote Boot Demonstration from Rod Howarth
(you should head to the Vimeo page and watch it in high definition there to read the text)
Basically your application can save an xml file in a certain directory, which ads a ‘job’ to the queue. Then, using the RevitServerToolCommand, you can, from a batch file, create a local copy of the Revit Model you want to work on, and open it in Revit. From there, a Revit API add-in will detect that you’ve opened a model that corresponds to a job, and will run the other Revit API add-in that was specified in the job file. After this is done, the job is marked complete, and Revit is closed.
In my example, I’ve done a simple export to DWF of the model, the idea being you could set this up to run at 1am every night, and export the DWF model to a certain location – perhaps for viewing in Design Review on mobile devices. However, you could use it for any Revit API add-in. For instance, you could have a high powered server which you setup as a local Revit Server just purely to run this Revit Remote Boot. On this server you could create a job to run a structural analysis add-in, or other computationally expensive stuff. This way you can setup your own “cloud”, running Revit directly. You could also run certain audits on a model, for example, you could create an add-in that counts the number of warnings present in the model and saves this to a database, for displaying in a ‘hall of shame’ on your company intranet. Smile
This is highly experimental, and has some pitfalls, so should be considered as a proof of concept, rather than a production ready program. The main pitfall is that any errors that come up are shown as dialog boxes, and there is no easy way to deal with this in Revit. To get around this I’ve used AutoHotKey (http://www.autohotkey.com/) to detect certain dialogs and close them, but if there are unexpected ones, it will fall over.
If you are interested in the code behind this, check it out here: https://github.com/RodH257/RevitRemoteBoot you can use the ‘zip’ button there to download it if you don’t have git.
I’d love to hear your thoughts on this, and whether you think Autodesk should work towards making this kind of thing easier to do?

Saturday, November 5, 2011

[General Dev]–How to push to two Git remote locations at once

Now that BitBucket supports Git, I’ve been utilizing both GitHub and BitBucket for my project hosting, and have been looking into migrating to BitBucket for my private repositories (as they are free). This is part of the beauty of using a distributed version control system, you can have your code in multiple locations, rather than one central Subversion server.
In Git, I could add two remotes and each time I want to push/pull code I would type something like:
git push github master
git push bitbucket master
However, I want to make things as easy as possible for myself, and I’m simply using these two repositories as a mirror of each other, so I’ll be telling git to push to multiple URL’s with the same remote.
To do this, I need to edit the .git/config file for my project. You can navigate here via the command line, or in Windows you can turn on hidden file display and open the .git folder in Windows Explorer
In this file, I’ve already added one of my remotes using the git remote add command, as you can see here:
To make it push to BitBucket at the same time as GitHub, you simply duplicate this URL line and add the BitBucket URL to it
Then if you do a git push origin master (where master is name of your branch), you will see it will push to these URLs in succession:
Unfortunately if you have a password protected ssh key it will still ask twice for your password.
Now each time you push to your origin remote, you’ll be taking full advantage of gits distributed nature, and will have two remote copies of your code.