创建新话题
已回答

How to Retrieve Item Descriptions Using the Client API with Python?

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:

image


最佳答案

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.


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: 


for part in parts:
    print(SWDescription.DescriptionToPlainText(part.DefObj.Description, swa.SWConnection.Instance.Broker))


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

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' 

 

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

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:

print(part)       => SystemWeaver.ClientAPI.API.TswPart
print(type(part)) => <class 'SystemWeaver.Common.IswObject'>
print(dir(part))  => ['Broker', 'DisplayImageIndex', 'Equals', 'FieldAsString', 'GetHashCode', 'GetType', 'Handle', 'HandleStr', 'IsNil', 'Name', 'ObjectAccess', 'Remove', 'Swot', 'ToString', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'get_Broker', 'get_DisplayImageIndex', 'get_Handle', 'get_HandleStr', 'get_IsNil', 'get_Name', 'get_ObjectAccess', 'set_Name']

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:

print(parts)       => SystemWeaver.Common.TswParts
print(type(parts)) => <class 'SystemWeaver.Common.IswParts'>

    

回答

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.


SystemWeaver Support

登录 发表评论