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: 

Error When Adding Sketch Objects On Select

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
keithJHSV9
581 Views, 4 Replies

Error When Adding Sketch Objects On Select

I have a selection input that only accepts sketch points, and want to draw a circle on top of whichever points are clicked. This function is hooked up to executePreview:

def update_sketch(self):
        ao = apper.AppObjects()
        if not self.sketch:
            sketches = ao.root_comp.sketches
            self.sketch = sketches.add(self.currentSelection[0].parentSketch.referencePlane)
        else:
            for obj in self.sketch.sketchCurves:
                if obj.isValid:
                    obj.deleteMe()
        for point in self.currentSelection:
            self.sketch.sketchCurves.sketchCircles.addByCenterRadius(point.geometry, 2)

To me, it seems like this function should create a fresh sketch if it does not exist, and delete all existing curves if it does. After, for each selected point, add a circle.

 

The actual behavior upon testing is that when only one point is selected, everything works great. However, when another point is selected, the following error is thrown:

File "C:/Users/k/AppData/Local/Autodesk/webdeploy/production/48ac19808c8c18863dd6034eee218407ecc49825/Api/Python/packages\adsk\fusion.py", line 32381, in addByCenterRadius
    return _fusion.SketchCircles_addByCenterRadius(self, centerPoint, radius)
RuntimeError: 4 : An API Object refers to a deleted Object

and the original circle disappears (even with the deletion code removed).

What am I doing wrong?

Labels (4)
4 REPLIES 4
Message 2 of 5
BrianEkins
in reply to: keithJHSV9

Each time the executePreview event is fired, you do whatever work is needed to create a preview.  All that work is contained within a single transaction.  The next time the executePreview is fired, the transaction is aborted, taking back to the state when the command was first invoked, and allowing you to draw the next preview, which will be captured in a new transaction.  This means you are not building on top of any previous work done in this invocation of the command but are building from scratch for each preview.    

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5
keithJHSV9
in reply to: BrianEkins

Correct me if I am wrong, but in the class I am using to manage these events I am using a property to save the selection information. At that point, it is using a reference to the selection in the command inputs. Does this mean that I have to deep copy the information to properly save it? Not quite sure if I understand what you are saying. . .

Message 4 of 5
kandennti
in reply to: keithJHSV9

Hi @keithJHSV9 .

 

I could not reproduce it because I could not try it with that only source code.

 

I have created this add-in using Apper.

 

Every time I select a point in the sketch, I use the executePreview event to call this function.

https://github.com/kantoku-code/Fusion360_Two-turn_Mobius_strip/blob/main/Two-turn%20Mobius%20strip/... 

 

As far as I am aware, anything created by the executePreview event will be deleted by itself before the next event fires.

Message 5 of 5
BrianEkins
in reply to: BrianEkins

To clarify what I was saying, here's what you need to do for your specific case.

 

When your command is running and the user selects a point, the executePreview event is fired. In reaction to the event, you create a sketch and use the selected point to draw a sketch circle.

 

When they select a second point, the executePreview is again fired. As part of the execute preview event firing, Fusion has aborted all work that was done in the previous executePreview.  Which means you're starting from scratch and need to create a sketch and now draw two circles because two points have been selected.

 

Selecting more points, results in the same process of aborting all previous work and you creating a sketch and drawing a circle for each selected point.

 

If the user unselects a point the same thing happens and the result is correct because you're again creating a new sketch and drawing a circle for each selected point.  This time the unselected point is ignored because it's not in the set of selected entities.

 

The main thing to understand is that each time the executePreview event fires that you are not just creating the changes from the last executePreview but are creating everything again because it was all removed as a result of Fusion aborting all of the previous work.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

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

Post to forums  

Autodesk Design & Make Report