Start a new topic
Answered

Adding an existing item as part to an item using C# API

Hi I have created an item in a library and would like to add this item as a part to an existing item in the same library. I dont understand what arguments to pass to the item.AddPart() function. 


Best Answer

Hi!


To add a part to an Item you have to know the Meta model in that specific data base. The Meta model concept is explained in the beginning of this article: Meta modelling basics


Below is an example of how to create Items and use the AddPart function given the following Meta model:

image


            IswLibrary library = SWConnection.Instance.Broker.GetLibrary(SWHandleUtility.ToHandle("x1300000000000017"));
            
            IswItem component = library.CreateItem("ARCT", "Component");            
            IswItem requirementContainer = library.CreateItem("REQC", "Requirement Container");
            IswItem interfaceItem = library.CreateItem("ARSI", "Interface");
            IswItem interfaceItem2 = library.CreateItem("ARSI", "Interface2");

            // Create the Part with SID="REQS" on component that points to requirementContainer. SetPartObj is used when Part multiplicity is 0..1
            component.SetPartObj("REQS", requirementContainer);
            
            // Create the Part with SID="ARRP" on component that points to interfaceItem. AddPart is used when Part multiplicity is *
            component.AddPart("ARRP", interfaceItem);

            // A better option is to create an extension Method that solves this logic for you
            component.AddOrSetPart("ARRP", interfaceItem2);

  Below is the class containing the extension method mentioned in the comments of the above example:

 

using SystemWeaver.Common;
using SystemWeaverAPI;

namespace API_Test
{
    static class ExtensionMethods
    {
        public static void AddOrSetPart(this IswItem owningItem,string partSid, IswItem defObj)
        {
            IswPartType partType = SWConnection.Instance.Broker.swPartType(partSid);

            if (partType.Multiplicity.Equals(SWMultiplicity.Single))
            {
                owningItem.SetPartObj(partType, defObj);
            }
            else
            {
                owningItem.AddPart(partType, defObj);
            }
        }
    }
}

Best regards, 

Fredrik


Answer

Hi!


To add a part to an Item you have to know the Meta model in that specific data base. The Meta model concept is explained in the beginning of this article: Meta modelling basics


Below is an example of how to create Items and use the AddPart function given the following Meta model:

image


            IswLibrary library = SWConnection.Instance.Broker.GetLibrary(SWHandleUtility.ToHandle("x1300000000000017"));
            
            IswItem component = library.CreateItem("ARCT", "Component");            
            IswItem requirementContainer = library.CreateItem("REQC", "Requirement Container");
            IswItem interfaceItem = library.CreateItem("ARSI", "Interface");
            IswItem interfaceItem2 = library.CreateItem("ARSI", "Interface2");

            // Create the Part with SID="REQS" on component that points to requirementContainer. SetPartObj is used when Part multiplicity is 0..1
            component.SetPartObj("REQS", requirementContainer);
            
            // Create the Part with SID="ARRP" on component that points to interfaceItem. AddPart is used when Part multiplicity is *
            component.AddPart("ARRP", interfaceItem);

            // A better option is to create an extension Method that solves this logic for you
            component.AddOrSetPart("ARRP", interfaceItem2);

  Below is the class containing the extension method mentioned in the comments of the above example:

 

using SystemWeaver.Common;
using SystemWeaverAPI;

namespace API_Test
{
    static class ExtensionMethods
    {
        public static void AddOrSetPart(this IswItem owningItem,string partSid, IswItem defObj)
        {
            IswPartType partType = SWConnection.Instance.Broker.swPartType(partSid);

            if (partType.Multiplicity.Equals(SWMultiplicity.Single))
            {
                owningItem.SetPartObj(partType, defObj);
            }
            else
            {
                owningItem.AddPart(partType, defObj);
            }
        }
    }
}

Best regards, 

Fredrik

This solved my issue thanks

It is important here to understand that there is a fundamental difference between these calls. With AddPart you are always adding information, you are not destroying any previous relationships between the given item and something else. With SetPartObj, you are often replacing one relationships with another which often needs more confirmation from the end user that this is what is intended.


Another example is, if you create a loop and add with multiple items and do AddPart on each, you will get a list with all those items. If you do the same with SetPartObj, only the last item will be referenced.

Login to post a comment