Skip to content

fix the set_attribute funcntion#42

Open
WeiTing1991 wants to merge 3 commits intomainfrom
set-atrribute-arg-type-issue
Open

fix the set_attribute funcntion#42
WeiTing1991 wants to merge 3 commits intomainfrom
set-atrribute-arg-type-issue

Conversation

@WeiTing1991
Copy link
Member

@WeiTing1991 WeiTing1991 commented Sep 18, 2025

What type of change is this?

It was the issue with set_attribute take wrong type of argument. The function in cadwork api:

import attribute_controller as ac
ac.set_user_attribute(element_id_list: List[ElementId], number: UserAttributeId, user_attribute: str)

it should take number: int(user attribute id) and user_attribute is string

  • Bug fix in a backwards-compatible manner.
  • New feature in a backwards-compatible manner.
  • Breaking change: bug fix or new feature that involve incompatible API changes.
  • Other (e.g. doc update, configuration, etc)

Checklist

  • I added a line to the CHANGELOG.md file in the Unreleased section under the most fitting heading (e.g. Added, Changed, Removed).
  • Test with cadwork3d app

@mhelmrei
Copy link
Contributor

@Brunner246 ? could you have a look?thx

Copy link
Contributor

@mhelmrei mhelmrei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


"""
ac.set_user_attribute([self.id], name, value)
ac.set_user_attribute_name(number=attribute_number, user_attribute_name=name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whenever I try calling cwapi3d functions using kwargs I get a TypeError, sure this works?

def remove_attribute(self, name):
# actully this only use the user_attribute number; no pass the elment id
# I am not sure how it defines which element to remove the attribute from
def remove_attribute(self, attribute_number):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user attributes system seems to be working like:

set_user_attribute_name - adds a new attribute name to some general attributes database and assigns it an attribute number. this has to happen once and not per element. multipe calls with different names but same number overwrite the name of the same attribute.

Now this attribute can be set on elements:
set_user_attribute sets a user attribute using its number and a value onto an element

Now the tricky part that's a bit confusing to me:
To remove an attribute, we have to remove its value - which uniquely identifies it in the user attributes item list (get_user_attribute_list_items).

This is done using delete_item_from_user_attribute_list. If the same value is set on multiple elements, all of them will get cleared..

delete_user_attribute deletes the attribute from the global attribute list and will only be successful if no elements are using this attribue. meaning, all the values set to this attribute have to first be cleared using delete_item_from_user_attribute_list.

in other words, delete_user_attribute won't do anything here if the user attribute is currently set on any elemetns.

confusing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example code:

# adds attribute with number 11 and name 'AttrName` to the global user attributes database
>>> ac.set_user_attribute_name(11, "AttrName")

# only here it's actually set on elements
>>> ac.set_user_attribute([4625], 11, "AttrValue4625")
>>> ac.set_user_attribute([4624], 11, "AttrValue4624")

>>> ac.get_user_attribute_list_items(11)
['', 'AttrValue4624', 'AttrValue4625']

# returns `False` and does nothing because attribute number 11 is in use
>>> ac.delete_user_attribute(11)
False

# attributes first need to be removed from the elements using their.. value..
>>> ac.delete_item_from_user_attribute_list(11, 'AttrValue4625')
True
>>> ac.delete_item_from_user_attribute_list(11, 'AttrValue4624')
True

# Only now, attribute number 11 can be removed from the database
>>> ac.delete_user_attribute(11)
True

@mhelmrei
Copy link
Contributor

@chenkasirer Thanks.You’re right—kwargs to cwapi3d throw TypeError, so I switched set_attribute to positional args. I also updatet remove_attribute to optionally remove by value using delete_item_from_user_attribute_list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants