Tracking selections external to fusion360

Tracking selections external to fusion360

kymC6Q8L
Enthusiast Enthusiast
722 Views
4 Replies
Message 1 of 5

Tracking selections external to fusion360

kymC6Q8L
Enthusiast
Enthusiast

Hello,

 

My usecase is this; Im trying to track the selection of things in a fusion file that makes up the exported part that we export to stl for printing or export as another format for other manufacturing processes.

A problem occurs, when the exported file is lost or corrupt, we want the traceability to file the fusion file and the bodies/components that are required to export out a new version of the file. 

 

The current workflow we have for this tracking, introduces allot of human error.

 

 

@kandennti has done some great work in his text command threads, but i found the text commands to be unreliable lately, mostly as keys within the returned dictionaries have changed a bit.

 

One of the ways i have found that gives me the information im after is this:

#get a selection
current_selection = neu_ui.get_selections()
# store selection to disk or database, along with part name & version

 

 

# pull part selection from disk or database
neu_ui.set_selections(res)
# export to stl

 

Segements & SegId's.

The commands above work really well, but where they fall short is when you version a file, the Segment & segIds change, example below:

 

 

# v1 of fusion file
[{'occurrence': {'source': {'segId': 49, 'entityId': 3, 'rootId': ''},
                 'path': [{'segId': 49, 'entityId': 33366, 'rootId': ''},
                          {'segId': 49, 'entityId': 33096, 'rootId': ''}]},
  'entity': {'segId': 49, 'entityId': 27100, 'rootId': ''}, 'type': 'Ns::BREP::BodySelection'}]

# v5 of the fusion file
[{'occurrence': {'source': {'segId': 9, 'entityId': 3, 'rootId': ''},
                 'path': [{'segId': 9, 'entityId': 33366, 'rootId': ''},
                          {'segId': 9, 'entityId': 33096, 'rootId': ''}]},
  'entity': {'segId': 9, 'entityId': 27100, 'rootId': ''}, 'type': 'Ns::BREP::BodySelection'}]

 

 

I realized i can get the file's segment/ SegID with :

neu_server.get_current_segment()

 

In most of my cases, its only the segId that changes between versions of the file, it would be easy enough to write a method to update this between versions.

 

The question i have is this the best way right now to do this kind of information tracking?

Are there other methods or workflows that allow us to store this kind of information and use it between versions. 

 

Cheers
kym

 

 

Thanks in advance,

Kym

 

0 Likes
Accepted solutions (2)
723 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor
Accepted solution

I would avoid using text commands whenever possible. They're not intended for use outside of Autodesk, and as a result, no testing or effort is made to ensure they continue to work in the same way from release to release. However, sometimes there isn't an option to use anything else, and most of the time, it does continue to work, but it's a risk.

 

I believe there is a relatively simple solution to what you want by using the API. Before I describe it, I have a question. What if the model has been exported more than once and possibly with a different set of bodies each time? Do you need to remember every time the model has been exported and somehow allow the user to choose previous export settings when exporting? Even in this case, I believe there is a solution, but it becomes more complicated for you and the user.

 

The solution is to use just attributes or a combination of attributes and entity tokens. You need an ID for each item that is exported and then a way to save the list of these ID's. That list, and possibly some other input values, defines what you need to be able to export that set again.

 

An entity token is an ID, and you can use that ID to get back the original entity the token was obtained from. The downside to a token is that it's not very friendly when debugging your data. The only way to know what the token represents is to use the Design.findEntityByToken method to bind back to the entity. An alternative is to add an attribute to the entity with the value you assign as the name. This is a bit easier to debug.

 

In either case you have a list of ID's whether they're tokens or names you created that you need to save. For this, I would create a new attribute on the Design object and save the ID's and any other settings as a formatted string, most likely JSON. Using this string, you can get back the settings and find all the entities you need to perform the same export.

 

 

 

 

 

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

kymC6Q8L
Enthusiast
Enthusiast

Hi Brian,

 

Thanks for the reply.

 

Im curious on how the entity tokens work, i did try something similar a few months back, but when i tried to get the entity again via design.findEntityByToken , i would always get a BaseVector back and not the entity i was expecting.

 

As for the rest of the workflow, i currently provide a tool that allows someone to pick the part from our erp's database, im wrapping exportMgr.createSTLExportOptions , based on the selected part, sets the naming and version of the new stl file. in the erp's data base im storing the name of the file , version and user.  

For the most part allot of the parts are exported out of one file with nested assemblies, but in some cases the parts are exported out of there own files. in our erp i can present the information as to what fusion file needs to be accessed, but making that into a workflow is a problem for another time.

 

If you can provide a little more information on how i can use the entity tokens and attributes would be interesting to try out.

 

Thanks

Kym

0 Likes
Message 4 of 5

kandennti
Mentor
Mentor
Accepted solution

Hi @kymC6Q8L .

 

Although not documented, xxxVector is like a List.

 

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui = core.UserInterface.cast(None)
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct

        # EntityToken of BRepBody that you have checked beforehand.
        token = '/v4BAAAARlJLZXkAH4sIAAAAAAAA/zNQsFAwAEJDBSMgBrOMLIAsQwUTkIiZwu2UXGWNDJ+Ume56kYrTPKcAVRgpGJspJKeZGKSlGafpmiVZGOuamBqZ6CamWRromqQZmBkmmpgZWpqkgdUCzWBAA2D7DBTA1oGsNzI2BAALM092iAAAAA=='

        # adsk.core.BaseVector is like a List.
        baseVector = des.findEntityByToken(token)
        print(f'Count:{len(baseVector)}')

        if len(baseVector) < 1:
            return

        body: fusion.BRepBody = baseVector[0]
        print(f'Name:{body.name}')

        print('*** adsk.core.BaseVector Attribute List ***')
        print(dir(baseVector))

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

 

Normally, the maximum number of elements that can be obtained by the findEntityByToken method would be one, so it would be jy sufficient to retrieve only the first element.

0 Likes
Message 5 of 5

kymC6Q8L
Enthusiast
Enthusiast

Fantastic,

 

 

Im going to test the return of des.findEntityByToken(token) , for now ill unpack the result is a adsk.core.BaseVector , in case it gets fixed in the future to return the entity directly. 

 

Thank you both @BrianEkins & @kandennti .

 

Cheers

Kym