应用程序:ExternalResourceUIServer

Revit 平台:所有

Revit 版本:2015.0

首次发布版本:2015.0

编程语言:C#

技能水平:中等

类别:数据交换

类型:外部应用程序

主题:一个外部资源 UI 服务器类

摘要:

这是一个 IExternalResourceUIServer 的实现类,用于支持 ExternalResourceDBServer 项目中的 SampleExternalResourceServer,并提供消息和其他 UI

相关类:

Autodesk.Revit.UI.IExternalApplication

Autodesk.Revit.UI.IExternalResourceUIServer

项目文件:

Application.cs

包含 UIServerApplication 类,该类继承自接口 IExternalpplication 并实现 OnStartup OnShutdown 方法。在 OnStartup 方法中,样例外部资源 UI 服务器已经被注册到了 Revit 中。

SampleExternalResourceUIServer.cs

含有 SampleExternalResourceUIServer 类,该类是 IExternalResourceUIServer 接口的一个实现。

说明:

此示例提供以下功能:

- SampleExternalResourceDBServer 添加 UI(消息框)。

- 显示来自 SampleExternalResourceDBServer 加载的主题演讲数据的加载结果消息。

- 显示从 SampleExternalResourceDBServer 链接的 Revit 模型的加载结果消息。

- 如果最终用户在浏览 SampleExternalResourceDBServer 的主题演讲或 Revit 链接资源时发生故障,则显示警告。

- 注意:实现 IExternalResourceUIServer 为你的 IExternalResourceServer 提供 UI 是高度推荐的,但实际上并非必需。要说明这一点,请尝试安装和使用带有和不带有 SampleExternalResourceUIServer SampleExternalResourceDBServerDB 服务器应该可以正常运行,但是在没有 UI 服务器的情况下,没有办法知道 Revit DB 服务器之间的交互是否完全成功,也没有办法确定任何错误的原因。

指导:

请按照 ExternalResourceDBServer 的说明进行操作。

1. 加载主题演讲数据时的 UI

期望的结果: 当用户选择一个主题演讲数据资源时,应该会出现下面所示的消息对话框:

 

 

2. 链接一个 Revit 模型时的 UI

期望的结果: 当用户从 SampleExternalResourceDBServer 中选择一个要链接到 Revit Revit 模型时,应该会出现下面所示的消息对话框:

 

源代码:

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

Application.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc. All rights reserved.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.

//
// AUTODESK PROVIDES THIS PROGRAM 'AS IS' AND WITH ALL ITS FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable. 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Autodesk;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExternalService;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;

namespace Revit.SDK.Samples.ExternalResourceUIServer.CS
{
   /// <summary>
   /// <para>Implements the Revit add-in interface IExternalApplication.</para>
   /// <para>An IExternalResourceUIServer can be registered at any time during a Revit session.
   /// However, the most straightforward approach is to register during start-up, in the
   /// OnStartUp method of a Revit external application.  This should be a (UI-level)
   /// IExternalApplication, and NOT an IExternalDBApplication.</para>
   /// </summary>
   class UIServerApplication : IExternalApplication
   {
      #region IExternalApplication Members
      /// <summary>
      /// Registers an instance of a SampleExternalResourceUIServer with the ExternalService
      /// of type ExternalResourceUIService. 
      /// </summary>
      /// <param name="application">An object that is passed to the external application
      /// which contains the controlled application.</param>
      /// <returns>Return the status of the external application.  A result of Succeeded
      /// means that the external application was able to register the IExternalResourceUIServer.
      /// </returns>
      public Result OnStartup(UIControlledApplication application)
      {
         ExternalService externalResourceUIService = ExternalServiceRegistry.GetService(ExternalServices.BuiltInExternalServices.ExternalResourceUIService);
         if (externalResourceUIService == null)
            return Result.Failed;

         // Create an instance of your IExternalResourceUIServer and register it with the ExternalResourceUIService.
         IExternalResourceUIServer sampleUIServer = new SampleExternalResourceUIServer();
         externalResourceUIService.AddServer(sampleUIServer);
         return Result.Succeeded;
      }

      /// <summary>
      /// Implements the OnShutdown event.
      /// <para>The server implementer may wish to perform clean-up tasks here.  However, in the
      /// simplest case, no server-related code is required, and the server will be
      /// destroyed as Revit shuts down.</para>
      /// </summary>
      /// <param name="application">An object that is passed to the external application
      /// which contains the controlled application.</param>
      /// <returns>Return the status of the external application.</returns>
      public Result OnShutdown(UIControlledApplication application)
      {
         return Result.Succeeded;
      }

      #endregion IExternalApplication Members
   }
 }

SampleExternalResourceUIServer.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExternalService;
using Autodesk.Revit.UI;

using Revit.SDK.Samples.ExternalResourceDBServer.CS;


namespace Revit.SDK.Samples.ExternalResourceUIServer.CS
{
   class SampleExternalResourceUIServer : IExternalResourceUIServer
   {

      // Methods that must be implemented by a server for any of Revit's external services
      #region IExternalServer Implementation
      /// <summary>
      /// Return the Id of the server. 
      /// </summary>
      public System.Guid GetServerId()
      {
         return m_myServerId;
      }

      /// <summary>
      /// Return the Id of the service that the server belongs to. 
      /// </summary>
      public ExternalServiceId GetServiceId()
      {
         return ExternalServices.BuiltInExternalServices.ExternalResourceUIService;
      }

      /// <summary>
      /// Return the server's name. 
      /// </summary>
      public System.String GetName()
      {
         return "SDK Sample ExtRes UI Server";
      }

      /// <summary>
      /// Return the server's vendor Id. 
      /// </summary>
      public System.String GetVendorId()
      {
         return "ADSK";
      }

      /// <summary>
      /// Return the description of the server. 
      /// </summary>
      public System.String GetDescription()
      {
         return "Simple UI server for the Revit SDK sample external resource server";
      }

      #endregion IExternalServer Implementation



      #region IExternalResourceUIServer Interface Implementation

      /// <summary>
      /// Return the Id of the related DB server. 
      /// </summary>
      /// 
      public System.Guid GetDBServerId()
      {
         return m_myDBServerId;
      }


      /// <summary>
      /// Reports the results of loads from the DB server (SampleExternalResourceServer).
      /// This method should be implemented to provide UI to communicate success or failure
      /// of a particular external resource load operation to the user.
      /// </summary>
      /// <param name="doc">The Revit model into which the External Resource was loaded.
      /// </param>
      /// <param name="loadDataList">Contains a list of ExternalResourceLoadData with results
      /// for all external resources loaded by the DB server.  It is possible for the DB server
      /// to have loaded more than one resource (for example, loading several linked files
      /// when a host file is opened by the user).
      /// </param>
      public void HandleLoadResourceResults(Document doc, IList<ExternalResourceLoadData> loadDataList)
      {
         foreach (ExternalResourceLoadData data in loadDataList)
         {
            ExternalResourceType resourceType = data.ExternalResourceType;

            // This message will be posted in a dialog box at the end of this method.
            String myMessage = String.Empty;

            ExternalResourceLoadContext loadContext = data.GetLoadContext();
            ExternalResourceReference desiredRef = data.GetExternalResourceReference();
            ExternalResourceReference currentlyLoadedRef = loadContext.GetCurrentlyLoadedReference();

            LoadOperationType loadType = loadContext.LoadOperationType;

            switch (loadType)
            {
               case LoadOperationType.Automatic:
                  myMessage = "This is an Automatic load operation. ";
                  break;

               case LoadOperationType.Explicit:
                  myMessage = "This is an Explicit load operation. ";
                  break;

               default:
                  myMessage = "There is no load type information!! ";
                  break;
            }


            bool bUnrecognizedStatus = false;
            if (data.LoadStatus == ExternalResourceLoadStatus.ResourceAlreadyCurrent)
            {
               if (data.GetLoadContext().LoadOperationType == LoadOperationType.Explicit)
               {
                  string resourcePath = currentlyLoadedRef.InSessionPath;
                  myMessage += "\n No new changes to load for link: " + resourcePath;
               }
               else
                  continue;
            }
            else if (data.LoadStatus == ExternalResourceLoadStatus.Uninitialized)
            {
               myMessage += "\n The load status is uninitialized - this generally shouldn't happen";
            }
            else if (data.LoadStatus == ExternalResourceLoadStatus.Failure)
            {
               myMessage += "\n The load failed and the reason is unknown.";
            }
            else if (data.LoadStatus == ExternalResourceLoadStatus.Success)
            {
               if (resourceType == ExternalResourceTypes.BuiltInExternalResourceTypes.KeynoteTable)
               {
                  string resourcePath = data.GetExternalResourceReference().InSessionPath;
                  myMessage += "\n Version " + data.GetLoadContent().Version + " of keynote data \'" + resourcePath + "\' has been loaded successfully";
               }
               else if (resourceType == ExternalResourceTypes.BuiltInExternalResourceTypes.RevitLink)
               {
                  string resourcePath = data.GetExternalResourceReference().InSessionPath;
                  LinkLoadContent ldrlc = (LinkLoadContent)(data.GetLoadContent());
                  string destinationPath = ModelPathUtils.ConvertModelPathToUserVisiblePath(ldrlc.GetLinkDataPath());
                  myMessage += "\n Version " + data.GetLoadContent().Version +
                               " of the file: " + resourcePath +
                               " has been downloaded into the cached folder: " + destinationPath +
                               " for this Revit Link.";
               }
            }
            else
            {
               myMessage += "Unrecognized external resource load status.";
               bUnrecognizedStatus = true;
            }


            if (!bUnrecognizedStatus && resourceType == ExternalResourceTypes.BuiltInExternalResourceTypes.RevitLink)
            {
               // For Revit links, the UI server can also obtain a RevitLinkLoadResult which contains detailed
               // information about the status of the attempt to load the local copy of the link into Revit.
               LinkLoadContent ldrlc = (LinkLoadContent)(data.GetLoadContent());
               LinkLoadResult loadResult = ldrlc.GetLinkLoadResult();
               if (loadResult != null)
               {
                  myMessage += "\n LinkLoadResultType: " + loadResult.LoadResult.ToString("g");
               }
            }
            System.Windows.Forms.MessageBox.Show(myMessage, "UI Server for SDK Sample External Resource Server");
         }
      }


      /// <summary>
      /// Use this method to report any problems that occurred while the user was browsing for External Resources.
      /// Revit will call this method each time the end user browses to a new folder location, or selects an item
      /// and clicks Open.
      /// </summary>
      public void HandleBrowseResult(ExternalResourceUIBrowseResultType resultType, string browsingItemPath)
      {
         if (resultType == ExternalResourceUIBrowseResultType.Success)
            return;

         String resultString = resultType.ToString("g");

         // While executing its SetupBrowserData() method, the "DB server" - SampleExternalResourceServer - can store
         // detailed information about browse failures that occurred (user not logged in, network down, etc.).
         // Subsequently, when Revit calls this method, the details can be read from the DB server and reported to the user.
         ExternalService externalResourceService = ExternalServiceRegistry.GetService(ExternalServices.BuiltInExternalServices.ExternalResourceService);
         if (externalResourceService == null)
         {
            System.Windows.Forms.MessageBox.Show("External Resource Service unexpectedly not found.");
            return;
         }
         SampleExternalResourceDBServer myDBServer = externalResourceService.GetServer(GetDBServerId()) as SampleExternalResourceDBServer;
         if (myDBServer == null)
         {
            System.Windows.Forms.MessageBox.Show("Cannot get SampleExternalResourceDBServer from ExternalResourceService.");
            return;
         }
         // ... Retrieve detailed failure information from SampleExternalResourceServer here.

         String message = String.Format("The browse result for <{0}> was: <{1}>.", browsingItemPath, resultString);
         System.Windows.Forms.MessageBox.Show(message);
      }

      #endregion IExternalResourceUIServer Interface Implementation



      #region SampleExternalResourceUIServer Implementations
      #endregion SampleExternalResourceUIServer Implementations



      #region SampleExternalResourceUIServer Member Variables

      private static Guid m_myServerId = new Guid("E9B6C194-62DE-4134-900D-BA8DF7AD33FA");
      private static Guid m_myDBServerId = new Guid("5F3CAA13-F073-4F93-BDC2-B7F4B806CDAF");

      #endregion SampleExternalResourceUIServer Member Variables

   }
}