在Autodesk Vault 2014中使用VDF(Vault Development Framework) API获取所有文件的属性信息
这几天在玩儿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