How do you get reference do the currently active sketch (Python)??

How do you get reference do the currently active sketch (Python)??

Anonymous
Not applicable
3,896 Views
10 Replies
Message 1 of 11

How do you get reference do the currently active sketch (Python)??

Anonymous
Not applicable

I want to write a script that runs in the currently active sketch. So, in the UI I want to activate any sketch I want to on any plane or face I want to, so *I* will manually get the UI into sketch mode, and then I want run my script to let it drawing new lines in the sketch.

 

So, what's the API reference access "the current sketch"?

 

 

0 Likes
Accepted solutions (1)
3,897 Views
10 Replies
Replies (10)
Message 2 of 11

BrianEkins
Mentor
Mentor
Accepted solution

You want to use the activeEditObject property of the Application object.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 11

JesusFreke
Advocate
Advocate

I think Application.activeEditTarget is what you're looking for.

 

Also, you might consider using UserInterface.selectEntity

0 Likes
Message 4 of 11

Anonymous
Not applicable

Should I be able to find activeEditObject on the API Object Model pdf???

 

https://help.autodesk.com/cloudhelp/ENU/Fusion-360-API/images/Fusion.pdf

 

 

0 Likes
Message 5 of 11

JesusFreke
Advocate
Advocate

It looks like that chart only has Objects, not methods.

 

If it had every method of every object, it would be enormous 🙂

Message 6 of 11

erik
Advocate
Advocate

As @JesusFreke writes, it is for objects in the different areas of the Fusion API. So no, you shouldn't be able to find a property of an object in the API Object Model.

 

I would refer to the Fusion API where one can find every object along with its methods and properties under Programming Interface -> Fusion 360 API Reference Manual -> Objects.

Here you can find the Application object with its property activeEditObject.

 

I hope this answers your question.

 

Btw, please mark one or more answers as the solution if they answered your question so others can use this thread for reference.

 

Cheers,

Erik

 

 

Message 7 of 11

Anonymous
Not applicable

Thanks! This is a very helpful resource. 

 

It is clear that the full API of Objects, Methods, and Properties of the Fusion 360 platform is non-trivial. It is a complex model, understandably, and one must simply invest many hours to become versed in this programming area.  I would have scoured the docs a LOONG time to find this reference. It sure helps to have humans who are a few steps ahead to pass along this knowledge. Eventually, the student (me) will gain some insight and hopefully intuition to discover things on their own more easily.

 

0 Likes
Message 8 of 11

erik
Advocate
Advocate

You are absolutely right, the API is complex and it can take some time to get used to using it as a reference book and search for information.

 

I strongly recommend you continue to ask questions here on the forum if you are ever in doubt how to get some information or do a specific task in Fusion 360. I have gained a lot of knowledge from browsing the forum and asking questions myself.

 

- Erik

Message 9 of 11

Anonymous
Not applicable

For others learning from this thread, to get an actual reference to the "current sketch" using this property, you have to "cast" to activeEditObject to a Sketch object, using the adsk.fusion.Sketch.cast() method.

 

        # Get current sketch
        app = adsk.core.Application.get()
        sketch = adsk.fusion.Sketch.cast(app.activeEditObject)
Message 10 of 11

JesusFreke
Advocate
Advocate

No, you don't have to cast it 🙂

 

Here's a quick test, in a document with an active sketch:

 

import adsk.core
import adsk.fusion


def run(_):
    sketch = adsk.core.Application.get().activeEditObject
    print(sketch)
    print(type(sketch).__name__)

    sketch = adsk.fusion.Sketch.cast(sketch)
    print(sketch)
    print(type(sketch).__name__)

Which results in:

 

<adsk.fusion.Sketch; proxy of <Swig Object of type 'std::vector< adsk::core::Ptr< adsk::fusion::Sketch > >::value_type *' at 0x000001CFA6ABC300> >
Sketch
<adsk.fusion.Sketch; proxy of <Swig Object of type 'std::vector< adsk::core::Ptr< adsk::fusion::Sketch > >::value_type *' at 0x000001CFA6ABC300> >
Sketch

As you can see, the object was already a sketch before the cast.

 

Furthermore, you can take a look at the implementation of the cast method itself. i.e. in <Autodesk>\webdeploy\production\<GUID>\Api\Python\packages\adsk\fusion.py

 

Sketch.cast = lambda arg: arg if isinstance(arg, Sketch) else None

 

As you can see, it just returns the unmodified argument, as long as it is a sketch, of course.

 

Since Application.activeEditObject could be different types of objects, it's best to do a type check on it before using it. You could use Sketch.cast() and check the return value for None, or just do the isinstance check directly yourself.

 

Message 11 of 11

dzid_
Contributor
Contributor

I am getting:

active_sketch = adsk.core.Application.get().activeEditObject

print(active_sketch)
<adsk.fusion.Sketch; proxy of <Swig Object of type 'std::vector< adsk::core::Ptr< adsk::fusion::Sketch > >::value_type *' at 0x000001729C61E030> >

 

The sketch is opened.

0 Likes