Change Row Order of item in a BOM – Vault 2015 example

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)AutodeskAutodesk Vault 2015 SDKvs12CSharpItemEditor

 

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);
    }
 
}

Comments

One response to “Change Row Order of item in a BOM – Vault 2015 example”

  1. Cornelius Avatar
    Cornelius

    Thanks for this sample.
    I try to transfering the UpdateItemBOMAssociationProperties methode from ItemService from 2014 to 2015. In 2015 there is the AssocPropItem changed to the ItemAssocProp.
    The AssocPropDef i try to update is exisitent end activated. I get the Id.
    I filled the values in that way:
    For ItemAssocProp.AssocId with the ItemAssoc.Id from the element of my bom. ItemAssocProp.PropDefId with AssocPropDef.Id. ItemAssocProp.ValTyp with DataType from AssocPropDef.Typ and the Value with my String “0020”.
    That i attach to my List.
    As parentId i take the ItemAssoc.ParentItemID.
    As ItemAssocIds i give a List with the ItemAssoc.Id.
    So why get i always error 283 if i call UpdateItemBOMAssociationProperties(parentId,List.toArray(),new ItemAssocPropArray[] { new ItemAssocProp[]{ItemAssocProp} })
    Wich entity is bad? How to test this values in 2015?
    I will attach my codesnippet from Vault 2015:
    sRetErr = string.Empty;
    List lItemAssocId = new List();
    ItemAssoc[] ItemAssocs = ItemService.GetItemBOMAssociationsByItemIds(new long[]{ itemIdParent }, false);
    string sPropDispName = “”;
    foreach (ItemAssoc iExistingAssoc in ItemAssocs)
    {
    if (iExistingAssoc.CldItemMasterID == iChildMasterId)
    {
    long idParent = 0, idParent2 = 0, idParent3 = 0, idParent4 = 0;
    List lProps = new List();
    ItemAssocPropArray itemAssocProps = new ItemAssocPropArray();
    for (int iVal = 0; iVal = 0)
    {
    sPropDispName = tVaultProps.GetColCaption(iVal);
    string sPropValue = tVaultProps.GetEntry(0, iVal);
    ItemAssocProp propI = new ItemAssocProp();
    // propI.AssocPropTyp = AssocPropTyp.ItemBOMAssoc;
    propI.AssocId = iExistingAssoc.Id;
    //propI.AssocId = iChildMasterId;
    propI.PropDefId = idPropId;
    propI.ValTyp = m_pApp.GetAssocDataTypeToDisplayName(sPropDispName);
    propI.Val = sPropValue;
    lProps.Add(propI);
    }
    }
    lItemAssocId.Add(iExistingAssoc.Id);
    idParent = iExistingAssoc.ParItemID;
    if (lProps.Count > 0)
    {
    itemAssocProps.Items = lProps.ToArray();
    try
    {
    ItemService.UpdateItemBOMAssociationProperties(idParent, lItemAssocId.ToArray(), new ItemAssocPropArray[] { itemAssocProps });
    }
    catch (Exception e)
    {
    sRetMsg = e.Message;
    return -1;
    }
    }}}

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading