Tuesday, May 17, 2011

Sunday, May 8, 2011

[Revit] Running a local Revit Server on Windows 7 safely–free utility

Autodesk Revit Server is a product that comes bundled with Revit 2011 and 2012. It provides a way for you to better coordinate Revit access over a wide area network by hosting your central model in Revit Server and connecting your Revit to that instead of opening a .rvt file on a network drive. This is particularly useful for companies with multiple offices, as Revit Server allows you to setup a ‘central’ server and ‘local’ servers are different locations. Saves to the Local servers are done over a LAN, and the local server then synchronizes the model back to the central server in an optimized fashion. Thus providing an alternative to expensive WAN optimization devices such as those provided by Riverbed.


One usage instance that many people are interested in, is having a local Revit server instance installed on their laptop for when they work remotely. That way saves to central over a VPN are done quickly, and Revit Server will synchronize the model back to the office in the background. The problem is, Revit Server only supports Windows Server 2008 R2, which is very expensive. However there is a way around this, you can, unofficially install Revit Server on your Windows 7 computer. I’ve had success in getting this setup, and so has Dave Baldacchino from the blog Do U Revit. Check out his post on the topic and read the ‘setup’ section for instructions on how you can get it running. There is some danger in this approach, as Robert from the blog ‘Don’t think: Do Revit’ warns, that shutting the server down while it is still trying to synchronize back to the central server can cause some issues.

This is a problem, as Revit Server has no inbuilt way to tell when it is synchronizing. To solve this issue, I’ve created a small utility that sits in your system tray, and tells you when Revit Server is communicating with anyone. I call it ‘Revit Server Monitor’

image

You run the program, and minimize it to your computers tray. You will then see a green arrow to indicate that the Revit Server is not currently communicating with anyone.

image

Or if any sort of communication is in progress, you will see a red arrow.

image

So if there is a red arrow, don’t shut your computer down. Wait for a green arrow to be safe.

This works by reading port 808, which is the primary port that Revit Server uses to communicate. Using the WinPCap utility, my code can monitor traffic on this port, if it detects any, it will show the red signal. If there has been no traffic on that port for 5 seconds, the signal will turn green. You can download the tool from here. To set it up, you must first install WinPCap, which is included in the zip, and then just run the RevitServerMonitor exe (perhaps add it to your computers startup folder as well so you don’t forget to run it). The program itself requires .NET 3.5 to run, but if you have Revit installed you will have that installed as well. If you are a little worried about having software that monitors packets running on your PC, feel free to browse the source code here, it’s fairly simple!

If you are running a Revit Server on your Windows PC, do try it and let me know how it goes, I’m not guaranteeing it will prevent model corruption but I’d love to hear your feedback on how well it worked.

Download the software here.

View the source code here.

Tuesday, May 3, 2011

[Revit API] How to allow add-ins to be loaded off a network drive, in code

I previously posted about how in Revit 2012, you can no longer store your add-ins on a network drive, unless you edit the revit.exe.config. This is a bit of a pain if you have to go to each users machine and edit this file individually, especially given that if you make a typo, your Revit won’t open.

Here’s how to do this edit in C# code, so if you make an installation utility to run on your users computers, you can add this bit of code in to do the changes for you.

Configuration revitExeConfig = ConfigurationManager.OpenExeConfiguration(
      @"C:\Program Files\Autodesk\Revit Structure 2012\Program\Revit.exe");
ConfigurationSection section = revitExeConfig.GetSection("runtime");
string xml = section.SectionInformation.GetRawXml();
if (!xml.Contains("<loadFromRemoteSources enabled=\"true\" />"))
{
    xml = xml.Replace("<runtime>", "<runtime> \r\n <loadFromRemoteSources enabled=\"true\" />");
    section.SectionInformation.SetRawXml(xml);
    revitExeConfig.Save();
}

 

Of course, I’ve hard coded in Revit Structure 2012 here as Revit installation to edit. You could change this to the installation that suits yours, or alternatively you could use the RevitAddinUtility.dll that ships with Revit. If you add a reference to that to your project you can then get the install location of all Revit products using:

foreach (RevitProduct product in RevitProductUtility.GetAllInstalledRevitProducts())
{
    //no point editing the Revit 2011 exe config files
    if (product.Version == RevitVersion.Revit2012)
    {
        string productLocation = product.InstallLocation + "program\\revit.exe";
    }
}

 

If you put that all together with the above code you can do something like:

public void EditExeConfigs()
{
    foreach (RevitProduct product in RevitProductUtility.GetAllInstalledRevitProducts())
    {
        //no point editing the Revit 2011 exe config files
        if (product.Version == RevitVersion.Revit2012)
        {
            string productLocation = product.InstallLocation + "program\\revit.exe";
            Configuration revitExeConfig = ConfigurationManager.OpenExeConfiguration(productLocation);
            ConfigurationSection section = revitExeConfig.GetSection("runtime");
            string xml = section.SectionInformation.GetRawXml();
            if (!xml.Contains("<loadFromRemoteSources enabled=\"true\" />"))
            {
                xml = xml.Replace("<runtime>", "<runtime> \r\n <loadFromRemoteSources enabled=\"true\" />");
                section.SectionInformation.SetRawXml(xml);
                revitExeConfig.Save();
            }
        }
    }
}

Which will edit the exe.config file for all 3 Revit products if they are installed, or do nothing if they aren’t.