using System;
using System.Collections;
using Autodesk.Connectivity.WebServices;
using Autodesk.Connectivity.WebServicesTools;
namespace ItemAttachment
{
class App
{
private WebServiceManager ServiceManager;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
App app = new App();
app.Run();
}
catch(System.Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
}
}
private void Run()
{
Login();
Item item = GetItem();
try
{
ArrayList files = GetFiles();
ArrayList attachments = new ArrayList();
foreach(File file in files)
{
Attmt attmt = new Attmt();
attmt.FileId = file.Id;
attmt.Pin = false;
attachments.Add(attmt);
}
ServiceManager.ItemService.UpdateAndCommitItem(item,
0,false, null, null,null,
(Attmt[])attachments.ToArray(typeof(Attmt)),
null, 2);
Console.WriteLine("Item {0} was succesfully updated.",
item.ItemNum);
Console.ReadLine();
}
catch
{
ServiceManager.ItemService.UndoEditItems(new long[] { item.Id });
Console.WriteLine("Error while editing item.");
}
Logout();
}
private void Login()
{
Console.Write("User Name: ");
string userName = Console.ReadLine();
Console.Write("Password: ");
string password = Console.ReadLine();
Console.Write("Server: ");
string server = Console.ReadLine();
Console.Write("Vault: ");
string vault = Console.ReadLine();
UserPasswordCredentials userpasswordCred = new UserPasswordCredentials(
server, vault, userName, password);
ServiceManager = new WebServiceManager(userpasswordCred);
}
private void Logout()
{
ServiceManager.SecurityService.SignOut();
}
private Item GetItem()
{
Console.Write("Create new item [Y/N] <Yes>: ");
string answer = Console.ReadLine();
if (0 == answer.Length)
{
answer = "Y";
}
Item item = null;
if (0 == string.Compare(answer, "Y", true))
{
Cat[] categories = ServiceManager.CategoryService.
GetCategoriesByEntityClassId("ITEM", true);
long catId = -1;
foreach (Cat category in categories)
{
if ((category.SysName == "Part"))
{
catId = category.Id;
}
}
item = ServiceManager.ItemService.AddItemRevision(catId);
}
else
{
Console.Write("Enter item number: ");
string itemNo = Console.ReadLine();
Item itemRev = ServiceManager.ItemService.GetLatestItemByItemNumber(itemNo);
item = ServiceManager.ItemService.EditItem(itemRev.RevId);
}
return item;
}
private ArrayList GetFiles()
{
ArrayList results = new ArrayList();
do
{
Console.Write("Enter full document name (incl. path): ");
string filePath = Console.ReadLine();
if (0 == filePath.Length)
{
break;
}
File[] files = ServiceManager.DocumentService.FindLatestFilesByPaths(new string[] { filePath } );
foreach(File file in files)
{
if (-1 == file.Id)
{
continue;
}
results.Add(file);
}
} while(true);
return results;
}
}
}