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

Login to post a comment