How do you know item-id of selected faces?

How do you know item-id of selected faces?

adachitech7
Consultant Consultant
1,562 Views
7 Replies
Message 1 of 8

How do you know item-id of selected faces?

adachitech7
Consultant
Consultant

I'd like to know how do you find item-id of selected face like this.

01.png

 

 

I'm trying to make a script for shell command and make a body like below.

02.png

 

 

Could you please tell me the way to find the item-id of the face?

0 Likes
Accepted solutions (1)
1,563 Views
7 Replies
Replies (7)
Message 2 of 8

ekinsb
Alumni
Alumni

There are several ways of getting access to a specific face.  The easiest way is to have the user select it and there are a few ways to do that.  If the user is expected to have already selected it, like you show in your image, and then you run your script, you can get all of the currently selected entities using the activeSelections property of the UserInterface object.  This will return a Selections object which you can access the selected items using its item method.

 

If you want the user to select the face as part of the script, you can use the selectEntity method of the UserInterface object. Using this you can specify which type of object is available for selection and also provide a prompt that will be displayed. This is probably the best option for the user in a simple script.

 

If you create a command, then you can use the SelectionCommandInput to get selections from the user and you have similar capabilities to the selectEntity but also more control over the selection.

 

Finally, if you don't want the user to be involved there are various ways to query features or the resulting body to find certain faces based on their shape, position, or other attributes you might be looking for.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 8

adachitech7
Consultant
Consultant

HI, Braian-san.

 

Thank you for your reply. I'll try it later.

0 Likes
Message 4 of 8

adachitech7
Consultant
Consultant

Hello,

 

I created a script to find item-id from selected face, but it didn't work.

Could you tell me what is wrong in the script?

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        sels = ui.activeSelections.item
        
        ui.messageBox(sels)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Message 5 of 8

ekinsb
Alumni
Alumni

There are a few problems with the script.

 

  1. The item method of the Selections object requires an argument to tell it which selection in the collection you want.  In your case you probably want the first (and most likely the only) selection.  You would do it with: 
    sel = ui.activeSelections.item(0) 
  2. The object returned is a Selection object which provides the point on the selected entity and the entity.  To get the selected entity you use the entity property.
    selectedEnt = sel.entity
  3. The messageBox method requires a string as input.  If you want to check the type of an object you can get the type as a string by using the objectType property.
    ui.messageBox('The selected entity is a ' + selectedEnt.objectType) 

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 6 of 8

adachitech7
Consultant
Consultant

HI, Brian-san,

 

Thank you for your detailed answer and I really appreciated it.

 

I could get the information of the face which I selected.

01.PNG

 

 

In addition to the information of the face, I'd like to know a specific number of it.

 

There are 18 faces of this model, showing below, and I think it has specific number to identify in the body.

I want to find the number of selected face and put it in my script.

 

02.PNG

 

 

Here's a part of my script.

        # Create a collection of entities for shell
        faces1 = rootComp.bRepBodies.item(0).faces.item(15)
        entities1 = adsk.core.ObjectCollection.create()
        entities1.add(faces1)

In this case, "item(15)" is target face, but it's really troublesome to find the number of 15.

 

Are there any good way to find it?

0 Likes
Message 7 of 8

ekinsb
Alumni
Alumni
Accepted solution

Faces do not currently have a unique ID.  You're referring to the index the face is in within the BRepBody object's faces collection.  A face is at a particular index, but that index shouldn't ever be counted on as being consistent.  For example, the face you have highlighted might be face 6 in the collection but if you make a small change to the body, then it will likely be some other index.  If you just need the index in this session and your not modifying the body in any way then it should be consistent.  But to find out which index it is at you'll need to look through the collection and find that face.  Below is some code that does that.

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try: 
        app = adsk.core.Application.get()
        ui = app.userInterface

        faceSel = ui.selectEntity('Select a face.', 'Faces')
        if faceSel:
            selectedFace = adsk.fusion.BRepFace.cast(faceSel.entity)

            # Find the index of this face within the body.            
            faceIndex = -1
            body = selectedFace.body
            faceCount = 0
            for face in body.faces:
                if face == selectedFace:
                    faceIndex = faceCount
                    break
                
                faceCount += 1

            ui.messageBox('The selected face is index ' + str(faceCount))                
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 8 of 8

adachitech7
Consultant
Consultant

HI,

 

Thank you for your reply.

 

The script you made is perfect for me and it's really useful! I could get the index number of the face.

 

I'll try to use your script as reference.

 

01.PNG

0 Likes