Mon, 03/17/2025 - 14:11
Forums:
Hi all,
I am using the lastest version of pythonocc-core. I just want to extract the name of shape / label and print it out:
# Get all labels containing shapes
labels = TDF_LabelSequence()
shape_tool.GetFreeShapes(labels)
# Iterate through all labels and print their names
for i in range(labels.Length()):
label = labels.Value(i + 1) # Labels are 1-indexed
if label.IsNull():
print("Null label encountered, skipping...")
continue
# Use FindAttribute to check for and retrieve the name attribute
name_attr = TDataStd_Name()
if label.FindAttribute(TDataStd_Name.GetID(), name_attr):
name = name_attr.Get()
print(f"Shape Name: {name}")
else:
print("Unnamed Shape")
But then, the following error appeared:
An error occurred: Wrong number or type of arguments for overloaded function 'TDF_Label_FindAttribute'.
Possible C/C++ prototypes are:
TDF_Label::FindAttribute(Standard_GUID const &,opencascade::handle< TDF_Attribute > &)
TDF_Label::FindAttribute(Standard_GUID const &,Standard_Integer const,opencascade::handle< TDF_Attribute > &)
Could you please help me to overcome this issue, as I have not found any example regarding FindAttribute() in python. :-(
Thank you so much!
Mon, 03/17/2025 - 14:16
Hello.
Please use the handle type as a second argument.
Best regards, Dmitrii.
Mon, 03/17/2025 - 14:24
Thank yu @Dmitrii for your tip. Unfortunately, the Handle_TDataStd_Name is not found / cannot be imported from OCC.Core.TDataStd.
Well, I do not know whether I am using the proper class for handle or not. :-(
Following is my full code:
Mon, 03/17/2025 - 14:59
Well, look into code, I see that a lot of object that not marked as Handle already handle. shape_tool is a handle, doc is handle. But attribute for some reason - not.
There are some problem from wrapping side, please share bugs with OCC-Py project.
Or try to create variable with basic type and allocate the name attribute:
TDF_Attribute anAttr = TDataStd_Name();
Best regards, Dmitrii.
Mon, 03/17/2025 - 15:36
It did not help with TDF_Attribute :-(.
Mon, 03/17/2025 - 15:40
I already submitted an Issue by pythonocc-core at https://github.com/tpaviot/pythonocc-core/issues/1415. But, personally, I think I missed an important point of
FindAttribute().Mon, 03/17/2025 - 16:37
As a workaround you may try using static method
RWMesh::ReadNameAttribute(). It will not allow distinguishing labels without name from labels with an empty name"", though.Mon, 03/17/2025 - 17:00
Thank you for your info. How can I find RWMesh Class? As I remember, there is a Package namely e.g. `OCC.Core.RWMesh`.
Tue, 03/18/2025 - 08:08
lanluu wrote:
Fri, 04/25/2025 - 06:10
Hello.
I encountered the same problem while using FindAttribute(). Is there any solution to bypass this method while trying to get other attributes like real or array?
Please let me know.
Tue, 12/30/2025 - 11:39
For anyone coming across this thread, the present solution is to use label.GetLabelName()