<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic deleteMe Method Not Deleting All Sketch Curves In For Loop in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/deleteme-method-not-deleting-all-sketch-curves-in-for-loop/m-p/10941332#M6605</link>
    <description>&lt;P&gt;&lt;SPAN&gt;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.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://knowledge.autodesk.com/community/screencast/0edd1c56-c426-480e-9492-47e9d9a131f4" target="_blank" rel="noopener nofollow noreferrer"&gt;Here is a link to the screencast&lt;/A&gt;. I've attached the test design file here. The code is below:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;#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()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 10 Feb 2022 08:30:10 GMT</pubDate>
    <dc:creator>therealsamchaney</dc:creator>
    <dc:date>2022-02-10T08:30:10Z</dc:date>
    <item>
      <title>deleteMe Method Not Deleting All Sketch Curves In For Loop</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/deleteme-method-not-deleting-all-sketch-curves-in-for-loop/m-p/10941332#M6605</link>
      <description>&lt;P&gt;&lt;SPAN&gt;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.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://knowledge.autodesk.com/community/screencast/0edd1c56-c426-480e-9492-47e9d9a131f4" target="_blank" rel="noopener nofollow noreferrer"&gt;Here is a link to the screencast&lt;/A&gt;. I've attached the test design file here. The code is below:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;#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()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Feb 2022 08:30:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/deleteme-method-not-deleting-all-sketch-curves-in-for-loop/m-p/10941332#M6605</guid>
      <dc:creator>therealsamchaney</dc:creator>
      <dc:date>2022-02-10T08:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: deleteMe Method Not Deleting All Sketch Curves In For Loop</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/deleteme-method-not-deleting-all-sketch-curves-in-for-loop/m-p/10941414#M6606</link>
      <description>&lt;P&gt;Hey there !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are two small mistakes in your code.&lt;/P&gt;&lt;P&gt;The first one is that there a parentheses missing on the deleteMe code in the delete_all function line 45.&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;curvesList = [curve for curve in curves]
# delete_projected(curvesList)
delete_all(curvesList)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 10 Feb 2022 09:02:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/deleteme-method-not-deleting-all-sketch-curves-in-for-loop/m-p/10941414#M6606</guid>
      <dc:creator>tykapl.breuil</dc:creator>
      <dc:date>2022-02-10T09:02:53Z</dc:date>
    </item>
    <item>
      <title>Re: deleteMe Method Not Deleting All Sketch Curves In For Loop</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/deleteme-method-not-deleting-all-sketch-curves-in-for-loop/m-p/10941481#M6607</link>
      <description>&lt;P&gt;You can also delete item in reverse order:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;#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()))
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Feb 2022 09:35:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/deleteme-method-not-deleting-all-sketch-curves-in-for-loop/m-p/10941481#M6607</guid>
      <dc:creator>JeromeBriot</dc:creator>
      <dc:date>2022-02-10T09:35:49Z</dc:date>
    </item>
  </channel>
</rss>

