By Wayne Brill
Below is a Vault 2015 API example that changes the Row Order of an item in an Item BOM. (In the API this is BOMOrder of the ItemAssoc).
Details about Editing Items
When you call EditItems() the returned object(s) supersede all former representations of that item. You may notice that the returned item has a different ID than the item whose revID was passed in.
The association IDs for a given item’s rows are different between item iterations. Therefore, the association IDs of the rows of the item whose revID was passed to EditItems are different from the association IDs of the rows of the item returned by EditItems.
So in short:
1) Use EditItems() before manipulating an item’s rows.
2) Call GetItemBOMAssociationsByItemIds() with the id of the item returned from the call to EditItems() to get the rows for the uncommitted item.
3) Call UpdateItemBOMAssociations() with the id of the item returned from the call to EditItems() to update the rows for the uncommitted item. You can also use the ParItemID of the ItemAssoc object if its more convenient.
Note: You don’t have to call EditItems() if the item you are working with is already uncommitted and reserved to your session. For example the item returned from AddItemRevision() is uncommitted and reserved to the calling session and you do not need to call EditItems().
C# example
To use this example add a button to the main form in this SDK sample:
C:\Program Files (x86)\Autodesk\Autodesk Vault 2015 SDK\vs12\CSharp\ItemEditor
private void button1_Click(object sender, EventArgs e)
{
if (m_selectedItem == null)
{
MessageBox.Show("Select an Item first");
return;
}
ItemService itemSvc =
m_connection.WebServiceManager.ItemService;
//Need to use the RevId of the item
long[] itemRevIds = new long[]
{ m_selectedItem.RevId };
// Lock the item
Item[] itemsArray_Editing =
itemSvc.EditItems(itemRevIds);
//Get the id of the Item being edited
long[] itemIds_Editing = new long[]
{ itemsArray_Editing[0].Id };
try
{
//Get the BOM items
ItemAssoc[] itemAssocArray =
itemSvc.GetItemBOMAssociationsByItemIds
(itemIds_Editing, false);
//Another way to get the array of longs
//for the Id of Items returned from EditItems
//ItemAssoc[] itemAssocArray =
// itemSvc.GetItemBOMAssociationsByItemIds(
//itemsArray_Editing.Select(i => i.Id).ToArray(),
// recurse: false
// );
//Get arrays to use
// for UpdateItemBOMAssociations()
int numberOfItems = itemAssocArray.Length;
long[] childIds = new long[numberOfItems];
long[] itemAssocIds = new long[numberOfItems];
double[] quantities =
new double[numberOfItems];
bool[] isStatic = new bool[numberOfItems];
bool[] isIncluded = new bool[numberOfItems];
int[] bomOrder = new int[numberOfItems];
string[] positionNums =
new string[numberOfItems];
BOMEditAction[] actions =
new BOMEditAction[numberOfItems];
//Set the values - only changing the BOMOrder
for (int i = 0; i < numberOfItems; i++)
{
// add the Id of the existing
//items to the array
itemAssocIds[i] = itemAssocArray[i].Id;
childIds[i] = itemAssocArray[i].CldItemID;
quantities[i] = itemAssocArray[i].Quant;
isStatic[i] = itemAssocArray[i].IsStatic;
isIncluded[i] =
itemAssocArray[i].IsIncluded;
// bomOrder[i] =
// itemAssocArray[i].BOMOrder + 1;
bomOrder[i] = i + 2;
positionNums[i] =
itemAssocArray[i].PositionNum;
actions[i] = BOMEditAction.Update;
}
//Update using the values set above
ItemBOM itemBOM =
itemSvc.UpdateItemBOMAssociations
(itemAssocArray[0].ParItemID,
itemAssocIds, childIds, quantities,
isStatic, isIncluded, bomOrder,
positionNums, actions,
BOMViewEditOptions.Defaults);
//Need to commit the items - removes the lock
itemSvc.UpdateAndCommitItems
(itemsArray_Editing);
}
catch (Exception ex)
{
//Undo the edit items when a problem occurs
itemSvc.UndoEditItems(itemIds_Editing);
}
}