Script / Add-on to conditionally remove components??

Script / Add-on to conditionally remove components??

3d_low
Enthusiast Enthusiast
757 Views
4 Replies
Message 1 of 5

Script / Add-on to conditionally remove components??

3d_low
Enthusiast
Enthusiast

I have a very automated design where i use parameters to control various aspect of the design.

Depending on certain parameter value, i have some parts that are needed / non-needed in the design.

 

Is there a way to run a script that will remove all the "non-needed" parts automatically while referring to parameters 

Sorta like If param.value = 1 then remove X.component

 

 

0 Likes
758 Views
4 Replies
Replies (4)
Message 2 of 5

Jorge_Jaramillo
Collaborator
Collaborator

Hi @3d_low ,

 

I made a script to delete components by name.

This is the script:

 

app = adsk.core.Application.get()
des = adsk.fusion.Design.cast(app.activeProduct)

def remove_components_parametrically():
    try:
        REMOVE_PARAMETER = 'remove_components'
        app.log(f'----------------------------------------')
        remove_parameter = des.userParameters.itemByName(REMOVE_PARAMETER)
        if remove_parameter:
            comp_names = remove_parameter.comment.split(',')
            app.log(f'Components to delete: {comp_names}')
            comp_stack = [des.rootComponent]
            while len(comp_stack):
                comp = comp_stack.pop()
                for occ in comp.allOccurrences:
                    if occ.component:
                        if occ.component.name in comp_names:
                            app.log(f'"{occ.component.name}" deleted')
                            occ.deleteMe()
                        else:
                            comp_stack.append(occ.component)
        else:
            app.log(f'"{REMOVE_PARAMETER}" user parameter not found')
    except:
        app.log(f'{traceback.format_exc()}')

 

 

This how it works:

1. Define a user parameter with name "remove_components" with any value, and use its description to add the components names to be deleted separated by comma (whitout spaces):

wtallerdemadera_2-1661386276214.png

I added "sphere,box" in my test.

 

2. Run the script, calling the function remove_components_parametrically()

All components at any level whose names are in the parameter description will be deleted.

 

This is the model I used:

wtallerdemadera_1-1661386185644.png

 

And this is the result:

wtallerdemadera_3-1661386371269.png

Two spheres and a box were deleted.

 

Let me know if this is what you're looking for.

 

Regards,
Jorge

Message 3 of 5

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

I made an improvement over the previous version, so a search could be done by component name itself or by full path of component, in case you know that full path of the component in the design tree.

So, in the example I post the component "sphere" appears twice in the model.  If you want to delete just the occurence under "Component4" you can specify "/Component4/sphere" instead of "sphere", and the sphere in the root component wont be deleted.

 

This is the code with improvement:

 

def remove_components_parametrically():
    try:
        REMOVE_PARAMETER = 'remove_components'
        app.log(f'----------------------------------------')
        remove_parameter = des.userParameters.itemByName(REMOVE_PARAMETER)
        if remove_parameter:
            comp_names = remove_parameter.comment.split(',')
            app.log(f'Components to delete: {comp_names}')
            comp_stack = [(des.rootComponent, '')]
            while len(comp_stack):
                (comp, path) = comp_stack.pop()
                for occ in comp.allOccurrences:
                    if occ.component:
                        full_name = f'{path}/{occ.component.name}'
                        if occ.component.name in comp_names:
                            app.log(f'"{occ.component.name}" deleted')
                            occ.deleteMe()
                        elif full_name in comp_names:
                            app.log(f'"{full_name}" deleted')
                            occ.deleteMe()
                        else:
                            comp_stack.append((occ.component, f'{path}/{occ.component.name}'))
        else:
            app.log(f'"{REMOVE_PARAMETER}" user parameter not found')
    except:
        app.log(f'{traceback.format_exc()}')

 

 

And this is the result:

wtallerdemadera_0-1661409613912.png

 

Regards,
Jorge

0 Likes
Message 4 of 5

3d_low
Enthusiast
Enthusiast

Hey Jorge,

 

Thanks for the input. Seems like this is a start to the solution I need.

I don't really have the time to learn to program this on my own. Will you be interested in doing the work for compensation?

0 Likes
Message 5 of 5

Jorge_Jaramillo
Collaborator
Collaborator

Hi @3d_low ,

 

Nice to know it could help you.
And yes, you can contact me if you need further assistance to implement what you need.

 

Regards,
Jorge

0 Likes