Adding an existing item as part to an item using C# API
S
Sunday Okonkwo
创建话题
超过6年 以前
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.
最佳答案
F
Fredrik Dannberg
说
超过6年 以前
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:
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);
}
}
}
}
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:
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
S
Sunday Okonkwo
说
超过6年 以前
This solved my issue thanks
M
Magnus W
说
将近4年 以前
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.
Sunday Okonkwo
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.
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:
Below is the class containing the extension method mentioned in the comments of the above example:
Best regards,
Fredrik
- 最早在前
- 热门
- 最新在前
Sorted by 最早在前Fredrik Dannberg
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:
Below is the class containing the extension method mentioned in the comments of the above example:
Best regards,
Fredrik
Sunday Okonkwo
This solved my issue thanks
Magnus W
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.
-
Support for Autotest of API applications
-
Major version exception in API
-
Part multiplicity exception in API
-
Get enum attribute value description
-
Detecting connection status?
-
Item type name from SID
-
Last Changed date isn't updating
-
Script filer och API
-
How do I return a DataTable instead of IswItems?
查看所有19话题