Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

deleteMe Method Not Deleting All Sketch Curves In For Loop

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
therealsamchaney
197 Views, 2 Replies

deleteMe Method Not Deleting All Sketch Curves In For Loop

I wrote a little script to find all projected sketch curves and delete them all. The script accurately counts that there are 10 projected sketch curves, then I call deleteMe method on all of them (in a for loop) but several remain untouched. It seems to be exactly every other curve that remains untouched. 

It seems strange that they are not all deleted. I've even tried deleting all curves in the sketch but it still only deletes half of the sketches.

Why is the deleteMe method only deleting half of the curves and skipping the others? Is there some trick to iterating over the sketchCurve objects in the collection? Is there something wrong with the code? Is this a bug?

Here is a link to the screencast. I've attached the test design file here. The code is below:

 

#Author-Sam Chaney
#Description-Selects and deletes all projected sketch entities

import adsk.core, adsk.fusion, adsk.cam, traceback, sys

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)
        active_component = design.activeComponent
        sketches = active_component.sketches
        active_sketch = adsk.core.Application.get().activeEditObject #Use this instead of chosen_sketch if you want to use active sketch
        
        sketch_name, cancelled  = ui.inputBox('Enter exact name of sketch to delete projected entities')
        if cancelled:
            ui.messageBox('Cancelled!')
            sys.exit('Cancelled')
        if sketches.itemByName(sketch_name) is None:
            ui.messageBox(f'Sketch named "{sketch_name}" not found')
            sys.exit('Sketch not found')

        chosen_sketch = sketches.itemByName(sketch_name)
        
        curves = chosen_sketch.sketchCurves
        ui.messageBox(f'Curve count is {curves.count}')
        lines = curves.sketchLines
        points = chosen_sketch.sketchPoints

        def is_projected(entity):
            projected_entities =[]
            if entity.isLinked:
                projected_entities.append(entity)
            return projected_entities

        def delete_projected(collection):
            for entity in collection:
                # if entity.isLinked:
                    # ui.messageBox('entity was linked')
                entity.deleteMe()
                
        def delete_all(collection):
            for entity in collection:
                entity.deleteMe # should delete all curves in sketch no matter what
                    
        delete_projected(curves)
        delete_all(curves) # even this only deletes 5 lines. It's every other line

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

2 REPLIES 2
Message 2 of 3

Hey there !

 

There are two small mistakes in your code.

The first one is that there a parentheses missing on the deleteMe code in the delete_all function line 45.

The second is that the collection you delete from is the direct reference to the sketch collection. That means that after every deletion it gets reindexed. When you delete the first element, the second element becomes the first with the reindexation and the second element you delete is the third and so on, this is why you only delete every other line. To solve this simply iterate through a copy of the collection. Something like this:

curvesList = [curve for curve in curves]
# delete_projected(curvesList)
delete_all(curvesList)
Message 3 of 3
JeromeBriot
in reply to: tykapl.breuil

You can also delete item in reverse order:

#Author-Sam Chaney
#Description-Selects and deletes all projected sketch entities

from audioop import reverse
import adsk.core, adsk.fusion, adsk.cam, traceback, sys

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)
        active_component = design.activeComponent
        sketches = active_component.sketches
        active_sketch = adsk.core.Application.get().activeEditObject #Use this instead of chosen_sketch if you want to use active sketch

        sketch_name, cancelled  = ui.inputBox('Enter exact name of sketch to delete projected entities')
        if cancelled:
            ui.messageBox('Cancelled!')
            sys.exit('Cancelled')
        if sketches.itemByName(sketch_name) is None:
            ui.messageBox(f'Sketch named "{sketch_name}" not found')
            sys.exit('Sketch not found')

        chosen_sketch = sketches.itemByName(sketch_name)

        curves = chosen_sketch.sketchCurves
        ui.messageBox(f'Curve count is {curves.count}')
        lines = curves.sketchLines
        points = chosen_sketch.sketchPoints

        def is_projected(entity):
            projected_entities =[]
            if entity.isLinked:
                projected_entities.append(entity)
            return projected_entities

        def delete_projected(collection):
            for entity in reversed(collection):
                entity.deleteMe()

        def delete_all(collection):
            for entity in reversed(collection):
                entity.deleteMe()

        delete_projected(curves)
        delete_all(curves) # even this only deletes 5 lines. It's every other line

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report