Create and read component attributes

Create and read component attributes

tomas.bangoA2EPZ
Advocate Advocate
685 Views
5 Replies
Message 1 of 6

Create and read component attributes

tomas.bangoA2EPZ
Advocate
Advocate

Hello I am trying to create a table to which the user will be able to write information to the components whether they are purchased or not. If they are the user fills in the supplier. The TablecommandInput works beautifully I am stuck on getting the information entered into the components. I use attributes that I assign using component.attributes.

 

 

 

 

 

This is how I create attributes:
design=adsk.fusion.Design.cast(app.activeProduct)
root=design.rootComponent
set_occ=root.allOccurrences.itemByName(comp_nm)
m_prop=set_occ.attributes.add('iProp'.format(comp_nm),'iVlastnost'.format(comp_nm),'-')
m_prop.value='+' #Change in value

This is how I try read attributes:
design=adsk.fusion.Design.cast(app.activeProduct)
root=design.rootComponent
set_occu=root.allOccurrences.itemByName(set_cpnm)
set_occu.attributes.itemByName('iProp'.format(set_cpnm),'iVlastnost'.format(set_cpnm)).value=#The user input from table

#comp_nm and set_cpnm are string (component name)

 

 

 

 

 

I know the attribute is created because I am able to change its value. However, if I try to write the information that the user has filled in I am unable to fill it into a created attribute. Because there are none there. The error im getting is ('NoneType' object has no attribute 'value) because I am not able to find the attribute group or the attribute that was created 

 

Could someone please advise me how to properly create attributes so that they can be read later at any time? Or some other possible way?

 

0 Likes
Accepted solutions (1)
686 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor

Hi @tomas.bangoA2EPZ .

 

I can't judge this one on its own.

 

is not set to None?

set_occu=root.allOccurrences.itemByName(set_cpnm)
0 Likes
Message 3 of 6

tomas.bangoA2EPZ
Advocate
Advocate

I have two defs that I just send the component names to. One (add_ATT) creates a list of different properties to list them in a table. The other (set_ATT) sets values filled in a table. idx is used to call certain from the list, set_Props is list of properties i want to write into component

 

#1st ONE
def add_ATT(comp_nm: str,idx_ATT: int):
    global _indexAtt
    app = adsk.core.Application.get()
    ui  = app.userInterface
    design=adsk.fusion.Design.cast(app.activeProduct)
    root=design.rootComponent
    set_occ=root.allOccurrences.itemByName(comp_nm)

    PropList=[]
    #Propertie General komponenty
    #compo=design.allComponents.itemByName(comp_nm) .partNumber
    compo=set_occ.component.partNumber
    copmpo_des=set_occ.component.description
    comp_ATT=set_occ.component.attributes
    #Propertie Fyzikalni a Vlatní
    physicPros=set_occ.physicalProperties
    PropList.append(physicPros.mass)
    PropList.append(physicPros.density)
    PropList.append(physicPros.area)
    PropList.append(physicPros.volume)
    m_prop=comp_ATT.add('iProp'.format(comp_nm),'iVlastnost'.format(comp_nm),'-') #set_occ.attributes.add
    PropList.append(comp_ATT.itemByName('iProp'.format(comp_nm),'iVlastnost'.format(comp_nm)).value)
    PropList.append(compo)
    PropList.append(comp_nm)

    _indexAtt=_indexAtt+1

    return PropList[idx_ATT]
#2nd ONE
def set_ATT(set_cpnm: str,set_Props):
    app = adsk.core.Application.get()
    ui  = app.userInterface
    design=adsk.fusion.Design.cast(app.activeProduct)
    root=design.rootComponent
    set_occu=root.allOccurrences.itemByName(set_cpnm)
    set_occu.component.attributes.groupNames


    att_grp=set_occu.attributes.itemByName('iProp'.format(set_cpnm),'iVlastnost'.format(set_cpnm)) #.value = set_Props[0] #Špatně navedeno?
    if att_grp.isValid==True:
        set_occu.attributes.itemByName('iProp'.format(set_cpnm),'iVlastnost'.format(set_cpnm)).value = set_Props[0]
    set_occu.component.partNumber = set_Props[1]

 

 Thx for reply btw and im not setting anything to None, should I?

0 Likes
Message 4 of 6

kandennti
Mentor
Mentor

@tomas.bangoA2EPZ .

 

I still don't understand it.

I was wondering, is the return value of the add_ATT function correct?

return PropList

is not it?

0 Likes
Message 5 of 6

tomas.bangoA2EPZ
Advocate
Advocate

I guess it should be. idx_ATT is used to call value from list. idx4 is value loaded from added attribute

dbg01.png

0 Likes
Message 6 of 6

tomas.bangoA2EPZ
Advocate
Advocate
Accepted solution

I dont know where was the problem but solution is using Components instead of occurences

Function to create attribute in all components
def run(context):
    app = adsk.core.Application.get()
    ui  = app.userInterface

    design=adsk.fusion.Design.cast(app.activeProduct) 
    #cmpnm='Komponenta'
    all_comp=design.allComponents
    count_cmp=all_comp.count
    for z in range(0,count_cmp):
        pick_cmp=all_comp.item(z)
        asgn_att=pick_cmp.attributes.add('TestGRP','TestATT','-')
        ui.messageBox(asgn_att.value)
    pass
Function to check attribute in component
def run(context):
    ui=None
    asgn_att=None
    try:

        app = adsk.core.Application.get()
        ui  = app.userInterface

        design=adsk.fusion.Design.cast(app.activeProduct) 
        cmpnm='Podsestava2' #Component Name to check attribute
        all_comp=design.allComponents
        pick_cmp=all_comp.itemByName(cmpnm)

        asgn_att=pick_cmp.attributes.itemByName('TestGRP','TestATT')
        ui.messageBox(asgn_att.value)
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))