在Autodesk Vault 2014中使用VDF(Vault Development Framework) API獲取所有檔案的屬性資訊

峻祁連發表於2013-12-12
 
這幾天在玩兒Vault API, 從Autodesk Vault 2014開始提供了Vault Development Framework(VDF) API,讓開發工作更簡單了。在Vault 2013裡面可以使用PropertyService 來獲取屬性(包括屬性定義和屬性至),在Vault 2014中的VDF API裡,我們可以通過PropertyManager來獲取。下面程式碼演示瞭如何登入到Vault並取得PropertyManager:
前面的文章提到了使用VDF內建的登入對話方塊登入非常簡單,如果你不用使用那個登入框,也可以自己做介面,使用下面的無介面的登入方式:
 
////login with UI
//VDF.Vault.Currency.Connections.Connection connection = VDF.Vault.Forms.Library.Login(new VDF.Vault.Forms.Settings.LoginSettings());
 
//Another way to log into Vault without UI
VDF.Vault.Results.LogInResult results =
		VDF.Vault.Library.ConnectionManager.LogIn(
				"localhost", "Vault", "Administrator", "",
				VDF.Vault.Currency.Connections.AuthenticationFlags.Standard, null);
if (!results.Success)
{
	Console.WriteLine("Login failed. exit...");
	Console.ReadLine();
	return;
}
 
VDF.Vault.Currency.Connections.Connection connection = results.Connection;
 
VDF.Vault.Services.Connection.IPropertyManager propMgr = connection.PropertyManager;

 

這個例子中,我要遞迴的獲取Vault中所有的目錄和檔案,看到這幾個字,我首先想到的就是用FileManager和FolderManager,不過這兩個傢伙對於獲取檔案本身,比如checkout並下載等工作來說很好用,我其實只要獲取FileInteration進而獲取他們的屬性,所有FileManager和FolderManager並不是好辦法。同事提醒我可以用IEntityOperationManager。 Folder和File等都是Entity,對於這些Entity的操作還有一個更底層的IEntityOperationManager, 其實FileManger和FolderManager也是呼叫的IEntityOperationManager。通過IEntityOperationManager的GetBrowseChildren方法就可以獲取指定entity的所有子實體,就可以實現遞迴獲取所有檔案了。

好了,下面是程式碼:

 

using Autodesk.Connectivity.WebServices;
using System;
using System.Collections.Generic;
using System.Linq;
using VDF = Autodesk.DataManagement.Client.Framework;

namespace VaultLabs
{
  class Lab03
  {
    // We will collect Property Definitions here
    static VDF.Vault.Currency.Properties.PropertyDefinitionDictionary propDefs;

    // The entry point of the program
    //==========================================================================
    static void Main(string[] args)
    {
      try
      {

        ////login with UI
        //VDF.Vault.Currency.Connections.Connection connection
          = VDF.Vault.Forms.Library.Login(new VDF.Vault.Forms.Settings.LoginSettings());

        //Another way to log into Vault without UI
        VDF.Vault.Results.LogInResult results =
            VDF.Vault.Library.ConnectionManager.LogIn(
                "localhost", "Vault", "Administrator", "",
                VDF.Vault.Currency.Connections.AuthenticationFlags.Standard, null);
        if (!results.Success)
        {
          Console.WriteLine("Login failed. exit...");
          Console.ReadLine();
          return;
        }

        VDF.Vault.Currency.Connections.Connection connection = results.Connection;


        if (connection.IsConnected)
        {
          ReadProperties(connection);

          VDF.Vault.Currency.Entities.Folder folder = connection.FolderManager.RootFolder;

          PrintChildren(connection, folder);



          Console.ResetColor();
          Console.WriteLine("");
          Console.WriteLine("Press any key to exit...");
          Console.ReadKey();
        }



      }
      catch (Exception ex)
      {
        Console.WriteLine("ERROR: {0}", ex.Message);
      }
    } // Main()


    // Read all the Property Definitions for the "FILE" Entity Class
    //===============================================================================
    static void ReadProperties(VDF.Vault.Currency.Connections.Connection connection)
    {
      propDefs =
        connection.PropertyManager.GetPropertyDefinitions(
          VDF.Vault.Currency.Entities.EntityClassIds.Files,
          null,
          VDF.Vault.Currency.Properties.PropertyDefinitionFilter.IncludeUserDefined
        );


    }


    // Output information about each file in a folder along with its properties
    //===========================================================================
    static void PrintChildren(VDF.Vault.Currency.Connections.Connection connection,
          VDF.Vault.Currency.Entities.Folder parentFolder)
    {
      Console.ForegroundColor = ConsoleColor.Cyan;
      Console.WriteLine("{0}", parentFolder.FullName);


      IEnumerable<VDF.Vault.Currency.Entities.IEntity> entities = connection
        .EntityOperations.GetBrowseChildren(parentFolder);
      if (entities != null && entities.Count<VDF.Vault.Currency.Entities.IEntity>() > 0)
      {
        foreach (var ent in entities)
        {
          if (ent is VDF.Vault.Currency.Entities.FileIteration)
          {
            VDF.Vault.Currency.Entities.FileIteration fileIteration
              = ent as VDF.Vault.Currency.Entities.FileIteration;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(" {0}", fileIteration.EntityName);
            Console.ForegroundColor = ConsoleColor.Yellow;

            //Now print the properties of the file
            PrintProperties(connection, fileIteration);


          }
          else if (ent is VDF.Vault.Currency.Entities.Folder)
          {
            // Recursively print info about subfolders and files in them
            //-------------------------------------------------------------------------

            VDF.Vault.Currency.Entities.Folder folder
              = ent as VDF.Vault.Currency.Entities.Folder;
            PrintChildren(connection, folder);

          }
        }
      }

    }


    static void PrintProperties(VDF.Vault.Currency.Connections.Connection connection,
                      VDF.Vault.Currency.Entities.FileIteration fileInteration)
    {
      foreach (var key in propDefs.Keys)
      {
        // Print the Name from the Definition and the Value from the Property
        object propValue = connection.PropertyManager.GetPropertyValue(
                  fileInteration, propDefs[key], null);
        Console.WriteLine("  '{0}' = '{1}'",
                        key.ToString(),
                        propValue == null ? "" : propValue.ToString());
      }
    }


  } // class Lab03

} // namespace VaultLabs

相關文章