How to Retrieve Item Descriptions Using the Client API with Python?
A
Anton Ahl
创建话题
2个月 以前
Running the code snippet below prints the names of all the parts in the item, but how do I get the item descriptions of all the parts?
import SystemWeaver as sw
import SystemWeaverAPI as swa
...
handle = sw.Common.SWHandleUtility.ToHandle(handle_str)
item = swa.SWConnection.Instance.Broker.GetItem(handle)
parts = item.GetAllParts()
for part in parts:
print(part.Name) # How to get the item plain text descriptions for all parts?
To clarify, the text that would appear in this box in the GUI:
最佳答案
S
SystemWeaver Support
说
2个月 以前
Hi Anton,
Try this out and see if it can work for you:
For part in parts:
print(SWDescription.DescriptionToPlainText(IswItem(IswPart(part).DefObj).Description, swa.SWConnection.Instance.Broker))
This should print the item Descriptions of all the parts' items.
For part in parts:
print(SWDescription.DescriptionToPlainText(IswItem(IswPart(part).DefObj).Description, swa.SWConnection.Instance.Broker))
This should print the item Descriptions of all the parts' items.
SystemWeaver Support
A
Anton Ahl
说
2个月 以前
Sure but the problem I have is that running part.DefObj does not return a generic IswObject; instead, it raises the error: AttributeError: 'IswObject' object has no attribute 'DefObj'.
part.DefObj returns a generic IswObj, because we don't know which DefObj the part has. But if you are just going through items, you can cast the part.DefObj to IswItem and then get the description.
Hopefully this can be helpful.
SystemWeaver Support
A
Anton Ahl
说
2个月 以前
Yes, thanks. There seems to be a problem with part.DefObj though. Printing at each step in the code yields this:
import SystemWeaver as sw
import SystemWeaverAPI as swa
...
handle = sw.Common.SWHandleUtility.ToHandle(handle_str)
item = swa.SWConnection.Instance.Broker.GetItem(handle)
print(item)
parts = item.GetAllParts()
print(parts)
for part in parts:
print(part)
print(part.DefObj)
print(item)
=> SystemWeaver.ClientAPI.API.TswLibraryItem
print(parts)
=> SystemWeaver.Common.TswParts
print(part)
=> SystemWeaver.ClientAPI.API.TswPart
print(part.DefObj)
=> AttributeError: 'IswObject' object has no attribute 'DefObj'
SystemWeaver Support
说
2个月 以前
Hi Anton,
To be able to get the item Descriptions of all the parts to print, you can test out the below code:
for part in parts:
print(SWDescription.DescriptionToPlainText(part.DefObj.Description, swa.SWConnection.Instance.Broker))
Anton Ahl
Running the code snippet below prints the names of all the parts in the item, but how do I get the item descriptions of all the parts?
To clarify, the text that would appear in this box in the GUI:
Hi Anton,
Try this out and see if it can work for you:
This should print the item Descriptions of all the parts' items.
SystemWeaver Support
- 最早在前
- 热门
- 最新在前
Sorted by 最新在前SystemWeaver Support
Hi Anton,
Try this out and see if it can work for you:
This should print the item Descriptions of all the parts' items.
SystemWeaver Support
Anton Ahl
Sure but the problem I have is that running part.DefObj does not return a generic IswObject; instead, it raises the error: AttributeError: 'IswObject' object has no attribute 'DefObj'.
Here is what printing part outputs:
This seems strange. I would expect print(type(part)) to output <class 'SystemWeaver.Common.IswPart'>, not <class 'SystemWeaver.Common.IswObject'>.
For parts, it prints as expected:
SystemWeaver Support
Hi Anton,
part.DefObj returns a generic IswObj, because we don't know which DefObj the part has. But if you are just going through items, you can cast the part.DefObj to IswItem and then get the description.
Hopefully this can be helpful.
SystemWeaver Support
Anton Ahl
Yes, thanks. There seems to be a problem with part.DefObj though. Printing at each step in the code yields this:
SystemWeaver Support
Hi Anton,
To be able to get the item Descriptions of all the parts to print, you can test out the below code:
You can also read more about how to work with Descriptions in one of our articles: Introduction to the SystemWeaver API in C# : SystemWeaver
If you are interested in learning more, we offer SystemWeaver API training classes which you can read more about here: Interested in SystemWeaver API Training? : SystemWeaver
I hope this information could be helpful.
SystemWeaver Support
-
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
-
Adding an existing item as part to an item using C# API
-
Last Changed date isn't updating
-
Script filer och API
-
How do I return a DataTable instead of IswItems?
查看所有19话题